Skip to content

Commit

Permalink
Created new setting for managing the time in between activity transit…
Browse files Browse the repository at this point in the history
…ions notifications
  • Loading branch information
Javinator9889 committed Jun 30, 2020
1 parent 3625997 commit 272d327
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 6 deletions.
Expand Up @@ -27,6 +27,7 @@ import com.javinator9889.handwashingreminder.R
import com.javinator9889.handwashingreminder.activities.base.LayoutVisibilityChange
import com.javinator9889.handwashingreminder.data.SettingsLoader
import com.javinator9889.handwashingreminder.gms.vendor.BillingService
import timber.log.Timber
import java.lang.ref.WeakReference

class SettingsView : PreferenceFragmentCompat(),
Expand Down Expand Up @@ -61,7 +62,10 @@ class SettingsView : PreferenceFragmentCompat(),
override fun onPreferenceChange(
preference: Preference?,
newValue: Any?
): Boolean = loader.onPreferenceChange(preference, newValue)
): Boolean {
Timber.d("Preference $preference changed - ${preference?.key}")
return loader.onPreferenceChange(preference, newValue)
}

override fun onVisibilityChanged(visibility: Int) {
if (visibility == View.VISIBLE)
Expand Down
Expand Up @@ -27,6 +27,7 @@ import androidx.annotation.StringRes
import androidx.emoji.text.EmojiCompat
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import androidx.preference.EditTextPreference
import androidx.preference.ListPreference
import androidx.preference.Preference
import androidx.preference.SwitchPreference
Expand Down Expand Up @@ -293,6 +294,24 @@ class SettingsLoader(
),
dispatcher = Dispatchers.Main
).also { deferreds.add(it) }
setupPreferenceAsync(
Preferences.ACTIVITY_MINIMUM_TIME,
Ionicons.Icon.ion_ios_stopwatch_outline,
onInitialized = { it, _ ->
runCatching {
it as EditTextPreference
val timeText = Integer.parseInt(it.text)
val minutes = resources.getQuantityString(
R.plurals.minutes,
timeText,
timeText
)
it.summary =
getString(R.string.minimum_time_summ, minutes)
}
},
onChangeListener = this@with
).also { deferreds.add(it) }
deferreds.awaitAll()
arePreferencesInitialized.set(true)
}
Expand Down Expand Up @@ -454,25 +473,28 @@ class SettingsLoader(
preference: Preference?,
newValue: Any?
): Boolean =
when (preference) {
view.firebaseAnalyticsPreference.get() -> {
when (preference?.key) {
Preferences.ANALYTICS_ENABLED -> {
val enabled = newValue as Boolean
Timber.d("Analytics collection is $enabled")
with(FirebaseAnalytics.getInstance(view.requireContext())) {
setAnalyticsCollectionEnabled(enabled)
if (!enabled)
setCurrentScreen(view.requireActivity(), null, null)
}
true
}
view.firebasePerformancePreference.get() -> {
Preferences.PERFORMANCE_ENABLED -> {
val enabled = newValue as Boolean
Timber.d("Performance is $enabled")
with(FirebasePerformance.getInstance()) {
isPerformanceCollectionEnabled = enabled
}
true
}
view.adsPreference.get() -> {
Preferences.ADS_ENABLED -> {
val enabled = newValue as Boolean
Timber.d("Ads are enabled $enabled")
var ret = false
val adEnabler = AdsEnabler(HandwashingApplication.instance)
if (enabled) {
Expand Down Expand Up @@ -512,7 +534,7 @@ class SettingsLoader(
}
ret
}
view.donationsPreference.get() -> {
Preferences.DONATIONS -> {
Timber.d("Purchase clicked - $newValue")
val purchaseId = newValue as String
if (isConnected())
Expand All @@ -531,6 +553,19 @@ class SettingsLoader(
}
false
}
Preferences.ACTIVITY_MINIMUM_TIME -> runCatching {
preference as EditTextPreference
Timber.d("Changing activity interval - $newValue")
val timeText = Integer.parseInt(newValue as String)
val minutes = view.resources.getQuantityString(
R.plurals.minutes,
timeText,
timeText
)
preference.summary =
view.getString(R.string.minimum_time_summ, minutes)
true
}.getOrElse { Timber.w(it); false }
else -> true
}
}
Expand Up @@ -42,6 +42,7 @@ object Preferences {
)
const val DONATIONS = "donations"
const val INITIAL_TUTORIAL_DONE = "app:tutorial:is_done"
const val ACTIVITY_MINIMUM_TIME = "activity:gms:minimumInterval"
}

object TimeConfig {
Expand Down
31 changes: 31 additions & 0 deletions app/src/main/res/layout/preference_edit_text.xml
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="wrap_content">

<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:hint="@string/minimum_time_hint"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<com.google.android.material.textfield.TextInputEditText
android:id="@android:id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/raleway_medium"
android:inputType="number"
android:maxLines="1"
android:selectAllOnFocus="true" />
</com.google.android.material.textfield.TextInputLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
7 changes: 7 additions & 0 deletions app/src/main/res/values-es/strings.xml
Expand Up @@ -278,4 +278,11 @@
<string name="add_another">¡Añade otra más! &#128588;</string>
<string name="reduce_count">Bueno, quizás una menos &#128517;</string>
<string name="diseases_info">Información de enfermedades</string>
<string name="minimum_time_hint">Tiempo mínimo en minutos…</string>
<string name="minimum_time">Tiempo mínimo entre notificaciones</string>
<string name="minimum_time_summ">Recibirás notificaciones con un tiempo mínimo de %1$s entre ellas</string>
<plurals name="minutes">
<item quantity="one">%d minuto</item>
<item quantity="other">%d minutos</item>
</plurals>
</resources>
7 changes: 7 additions & 0 deletions app/src/main/res/values/strings.xml
Expand Up @@ -306,4 +306,11 @@
<string name="add_another">Add another! &#128588;</string>
<string name="reduce_count">Well, maybe one less &#128517;</string>
<string name="diseases_info">Diseases information</string>
<string name="minimum_time_hint">Minimum time in minutes…</string>
<string name="minimum_time">Minimum time in between notifications</string>
<string name="minimum_time_summ">You will receive notifications with at least %1$s in between them</string>
<plurals name="minutes">
<item quantity="one">%d minute</item>
<item quantity="other">%d minutes</item>
</plurals>
</resources>
9 changes: 9 additions & 0 deletions app/src/main/res/xml/preferences.xml
Expand Up @@ -57,6 +57,15 @@
android:key="activity:gms:tracking"
android:title="@string/activity_recognition"
app:summary="For enabling the activity recognition, you need both Google Play Services and permissions" />

<EditTextPreference
android:defaultValue="15"
android:key="activity:gms:minimumInterval"
android:selectAllOnFocus="true"
android:singleLine="true"
android:dependency="activity:gms:tracking"
android:dialogLayout="@layout/preference_edit_text"
android:title="@string/minimum_time" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/data_sharing_category">

Expand Down

0 comments on commit 272d327

Please sign in to comment.