From 240e61df0e0bf8b01ada55134b644a62b871509a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Borcz?= Date: Sat, 10 Nov 2018 23:29:18 +0100 Subject: [PATCH] Add preference for the expandable grades (#178) --- .../repositories/PreferencesRepository.kt | 12 ++++---- .../wulkanowy/services/job/ServiceHelper.kt | 4 +-- .../wulkanowy/services/job/SyncWorker.kt | 2 +- .../modules/attendance/AttendancePresenter.kt | 2 +- .../grade/details/GradeDetailsFragment.kt | 6 ++-- .../grade/details/GradeDetailsHeader.kt | 30 ++++++++++++------- .../grade/details/GradeDetailsPresenter.kt | 3 +- .../ui/modules/settings/SettingsPresenter.kt | 2 +- app/src/main/res/values-pl/strings.xml | 1 + app/src/main/res/values/preferences_keys.xml | 3 +- app/src/main/res/values/strings.xml | 1 + app/src/main/res/xml/scheme_preferences.xml | 5 ++++ 12 files changed, 45 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/PreferencesRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/PreferencesRepository.kt index 2efea046c..430a6cac4 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/PreferencesRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/PreferencesRepository.kt @@ -11,13 +11,15 @@ class PreferencesRepository @Inject constructor( private val sharedPref: SharedPreferences, val context: Context ) { - val startMenuIndex: Int get() = sharedPref.getString(context.getString(R.string.pref_key_start_menu), "0")?.toInt() ?: 0 - val showPresent: Boolean + val isShowPresent: Boolean get() = sharedPref.getBoolean(context.getString(R.string.pref_key_attendance_present), true) + val isGradeExpandable: Boolean + get() = !sharedPref.getBoolean(context.getString(R.string.pref_key_expand_grade), false) + val currentThemeKey: String = context.getString(R.string.pref_key_theme) val currentTheme: Int get() = sharedPref.getString(currentThemeKey, "1")?.toInt() ?: 1 @@ -26,7 +28,7 @@ class PreferencesRepository @Inject constructor( get() = sharedPref.getString(context.getString(R.string.pref_key_grade_modifier), "0.0")?.toDouble() ?: 0.0 val serviceEnablesKey: String = context.getString(R.string.pref_key_services_enable) - val serviceEnabled: Boolean + val isServiceEnabled: Boolean get() = sharedPref.getBoolean(serviceEnablesKey, true) val servicesIntervalKey: String = context.getString(R.string.pref_key_services_interval) @@ -34,9 +36,9 @@ class PreferencesRepository @Inject constructor( get() = sharedPref.getString(servicesIntervalKey, "60")?.toInt() ?: 60 val servicesOnlyWifiKey: String = context.getString(R.string.pref_key_services_wifi_only) - val servicesOnlyWifi: Boolean + val isServicesOnlyWifi: Boolean get() = sharedPref.getBoolean(servicesOnlyWifiKey, true) - val notificationsEnable: Boolean + val isNotificationsEnable: Boolean get() = sharedPref.getBoolean(context.getString(R.string.pref_key_notifications_enable), true) } diff --git a/app/src/main/java/io/github/wulkanowy/services/job/ServiceHelper.kt b/app/src/main/java/io/github/wulkanowy/services/job/ServiceHelper.kt index ab88edd86..d81b4b6d4 100644 --- a/app/src/main/java/io/github/wulkanowy/services/job/ServiceHelper.kt +++ b/app/src/main/java/io/github/wulkanowy/services/job/ServiceHelper.kt @@ -24,7 +24,7 @@ class ServiceHelper @Inject constructor( } fun startFullSyncService(replaceCurrent: Boolean = false) { - if (LocalDate.now().isHolidays || !prefRepository.serviceEnabled) { + if (LocalDate.now().isHolidays || !prefRepository.isServiceEnabled) { Timber.d("Services disabled or it's holidays") return } @@ -41,7 +41,7 @@ class ServiceHelper @Inject constructor( (prefRepository.servicesInterval + 10) * 60 ) ) - .setConstraints(if (prefRepository.servicesOnlyWifi) ON_UNMETERED_NETWORK else ON_ANY_NETWORK) + .setConstraints(if (prefRepository.isServicesOnlyWifi) ON_UNMETERED_NETWORK else ON_ANY_NETWORK) .setReplaceCurrent(replaceCurrent) .setRetryStrategy(DEFAULT_EXPONENTIAL) .build() diff --git a/app/src/main/java/io/github/wulkanowy/services/job/SyncWorker.kt b/app/src/main/java/io/github/wulkanowy/services/job/SyncWorker.kt index e9f446ac1..4e91f5f01 100644 --- a/app/src/main/java/io/github/wulkanowy/services/job/SyncWorker.kt +++ b/app/src/main/java/io/github/wulkanowy/services/job/SyncWorker.kt @@ -80,7 +80,7 @@ class SyncWorker : SimpleJobService() { .subscribe({}, { error = it })) return if (null === error) { - if (prefRepository.notificationsEnable) sendNotifications() + if (prefRepository.isNotificationsEnable) sendNotifications() Timber.d("Synchronization successful") RESULT_SUCCESS } else { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendancePresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendancePresenter.kt index 7df50d9e9..e328c3829 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendancePresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendancePresenter.kt @@ -71,7 +71,7 @@ class AttendancePresenter @Inject constructor( .map { it.single { semester -> semester.current } } .flatMap { attendanceRepository.getAttendance(it, date, date, forceRefresh) } .map { list -> - if (prefRepository.showPresent) list + if (prefRepository.isShowPresent) list else list.filter { !it.presence } } .map { items -> items.map { AttendanceItem(it) } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsFragment.kt index b10d3dec7..5ecbcc64b 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsFragment.kt @@ -3,7 +3,9 @@ package io.github.wulkanowy.ui.modules.grade.details import android.os.Bundle import android.view.LayoutInflater import android.view.View -import android.view.View.* +import android.view.View.GONE +import android.view.View.INVISIBLE +import android.view.View.VISIBLE import android.view.ViewGroup import eu.davidea.flexibleadapter.FlexibleAdapter import eu.davidea.flexibleadapter.common.SmoothScrollLinearLayoutManager @@ -60,8 +62,6 @@ class GradeDetailsFragment : BaseFragment(), GradeDetailsView, GradeView.GradeCh setOnItemClickListener { presenter.onGradeItemSelected(getItem(it)) } } - gradeDetailsAdapter.getItemCountOfTypes() - gradeDetailsRecycler.run { layoutManager = SmoothScrollLinearLayoutManager(context) adapter = gradeDetailsAdapter diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsHeader.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsHeader.kt index 3b4ee8fba..8d8cac8f3 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsHeader.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsHeader.kt @@ -12,11 +12,16 @@ import kotlinx.android.extensions.LayoutContainer import kotlinx.android.synthetic.main.header_grade_details.* class GradeDetailsHeader( - private val subject: String, - private val number: String, - private val average: String, - var newGrades: Int) - : AbstractExpandableItem() { + private val subject: String, + private val number: String, + private val average: String, + var newGrades: Int, + private val isExpandable: Boolean +) : AbstractExpandableItem() { + + init { + isExpanded = !isExpandable + } override fun getLayoutRes() = R.layout.header_grade_details @@ -24,16 +29,16 @@ class GradeDetailsHeader( return ViewHolder(view, adapter) } - override fun bindViewHolder(adapter: FlexibleAdapter>?, holder: ViewHolder, - position: Int, payloads: MutableList?) { + override fun bindViewHolder(adapter: FlexibleAdapter>?, holder: ViewHolder, position: Int, payloads: MutableList?) { holder.run { gradeHeaderSubject.text = subject gradeHeaderAverage.text = average gradeHeaderNumber.text = number gradeHeaderPredicted.visibility = GONE gradeHeaderFinal.visibility = GONE - gradeHeaderNote.visibility = if (newGrades > 0) VISIBLE else GONE + + isViewExpandable = isExpandable } } @@ -57,15 +62,18 @@ class GradeDetailsHeader( return result } + class ViewHolder(view: View?, adapter: FlexibleAdapter>?) : + ExpandableViewHolder(view, adapter), LayoutContainer { - class ViewHolder(view: View?, adapter: FlexibleAdapter>?) : ExpandableViewHolder(view, adapter), - LayoutContainer { + var isViewExpandable = true init { contentView.setOnClickListener(this) } - override fun shouldNotifyParentOnClick() = true + override fun isViewCollapsibleOnClick() = isViewExpandable + + override fun isViewExpandableOnClick() = isViewExpandable override val containerView: View get() = contentView diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsPresenter.kt index a24e395f3..74f442968 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsPresenter.kt @@ -102,7 +102,8 @@ class GradeDetailsPresenter @Inject constructor( subject = it.key, average = formatAverage(average), number = view?.getGradeNumberString(it.value.size).orEmpty(), - newGrades = it.value.filter { grade -> !grade.isRead }.size + newGrades = it.value.filter { grade -> !grade.isRead }.size, + isExpandable = preferencesRepository.isGradeExpandable ).apply { subItems = it.value.map { item -> GradeDetailsItem( diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/settings/SettingsPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/settings/SettingsPresenter.kt index 25e48f87c..2b01f9bfd 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/settings/SettingsPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/settings/SettingsPresenter.kt @@ -26,7 +26,7 @@ class SettingsPresenter @Inject constructor( fun onSharedPreferenceChanged(key: String) { when (key) { preferencesRepository.serviceEnablesKey -> { - if (preferencesRepository.serviceEnabled) serviceHelper.startFullSyncService() + if (preferencesRepository.isServiceEnabled) serviceHelper.startFullSyncService() else serviceHelper.stopFullSyncService() } preferencesRepository.servicesIntervalKey, diff --git a/app/src/main/res/values-pl/strings.xml b/app/src/main/res/values-pl/strings.xml index ca9d3707f..07d0d614f 100644 --- a/app/src/main/res/values-pl/strings.xml +++ b/app/src/main/res/values-pl/strings.xml @@ -135,6 +135,7 @@ Pokazuj obecność we frekwencji Ciemny motyw (Beta) Wartość plusa i minusa + Rozwiń oceny Powiadomienia Pokazuj powiadomienia diff --git a/app/src/main/res/values/preferences_keys.xml b/app/src/main/res/values/preferences_keys.xml index cc829354c..cd0a9a658 100644 --- a/app/src/main/res/values/preferences_keys.xml +++ b/app/src/main/res/values/preferences_keys.xml @@ -3,9 +3,10 @@ start_menu attendance_present theme + grade_modifier + expand_grade services_enable services_interval services_disable_wifi_only notifications_enable - grade_modifier diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1f68f54da..2d7b52cdf 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -128,6 +128,7 @@ Show presence in attendance Dark theme (Beta) Value of the plus and minus + Expand grades Notifications Show notifications diff --git a/app/src/main/res/xml/scheme_preferences.xml b/app/src/main/res/xml/scheme_preferences.xml index 617171552..27addc836 100644 --- a/app/src/main/res/xml/scheme_preferences.xml +++ b/app/src/main/res/xml/scheme_preferences.xml @@ -25,6 +25,11 @@ android:key="@string/pref_key_attendance_present" android:title="@string/pref_view_present" app:iconSpaceReserved="false" /> +