diff --git a/app/src/main/java/io/timelimit/android/data/model/Category.kt b/app/src/main/java/io/timelimit/android/data/model/Category.kt index 96f82ba..84e66b5 100644 --- a/app/src/main/java/io/timelimit/android/data/model/Category.kt +++ b/app/src/main/java/io/timelimit/android/data/model/Category.kt @@ -166,3 +166,15 @@ data class Category( writer.endObject() } } + +object CategoryTimeWarnings { + val durationToBitIndex = mapOf( + 1000L * 60 to 0, // 1 minute + 1000L * 60 * 3 to 1, // 3 minutes + 1000L * 60 * 5 to 2, // 5 minutes + 1000L * 60 * 10 to 3, // 10 minutes + 1000L * 60 * 15 to 4 // 15 minutes + ) + + val durations = durationToBitIndex.keys +} \ No newline at end of file diff --git a/app/src/main/java/io/timelimit/android/ui/manage/category/settings/CategorySettingsFragment.kt b/app/src/main/java/io/timelimit/android/ui/manage/category/settings/CategorySettingsFragment.kt index 7bfce0d..8f8ea00 100644 --- a/app/src/main/java/io/timelimit/android/ui/manage/category/settings/CategorySettingsFragment.kt +++ b/app/src/main/java/io/timelimit/android/ui/manage/category/settings/CategorySettingsFragment.kt @@ -77,6 +77,13 @@ class CategorySettingsFragment : Fragment() { categoryLive = categoryEntry ) + CategoryTimeWarningView.bind( + view = binding.timeWarnings, + auth = auth, + categoryLive = categoryEntry, + lifecycleOwner = this + ) + binding.btnDeleteCategory.setOnClickListener { deleteCategory() } binding.editCategoryTitleGo.setOnClickListener { renameCategory() } diff --git a/app/src/main/java/io/timelimit/android/ui/manage/category/settings/CategoryTimeWarningView.kt b/app/src/main/java/io/timelimit/android/ui/manage/category/settings/CategoryTimeWarningView.kt new file mode 100644 index 0000000..6dba678 --- /dev/null +++ b/app/src/main/java/io/timelimit/android/ui/manage/category/settings/CategoryTimeWarningView.kt @@ -0,0 +1,60 @@ +package io.timelimit.android.ui.manage.category.settings + +import android.widget.CheckBox +import androidx.lifecycle.LifecycleOwner +import androidx.lifecycle.LiveData +import androidx.lifecycle.Observer +import io.timelimit.android.data.model.Category +import io.timelimit.android.data.model.CategoryTimeWarnings +import io.timelimit.android.databinding.CategoryTimeWarningsViewBinding +import io.timelimit.android.sync.actions.UpdateCategoryTimeWarningsAction +import io.timelimit.android.ui.main.ActivityViewModel +import io.timelimit.android.util.TimeTextUtil + +object CategoryTimeWarningView { + fun bind( + view: CategoryTimeWarningsViewBinding, + lifecycleOwner: LifecycleOwner, + categoryLive: LiveData, + auth: ActivityViewModel + ) { + view.linearLayout.removeAllViews() + + val durationToCheckbox = mutableMapOf() + + CategoryTimeWarnings.durations.sorted().forEach { duration -> + CheckBox(view.root.context).let { checkbox -> + checkbox.text = TimeTextUtil.time(duration.toInt(), view.root.context) + + view.linearLayout.addView(checkbox) + durationToCheckbox[duration] = checkbox + } + } + + categoryLive.observe(lifecycleOwner, Observer { category -> + durationToCheckbox.entries.forEach { (duration, checkbox) -> + checkbox.setOnCheckedChangeListener { _, _ -> } + + val flag = (1 shl CategoryTimeWarnings.durationToBitIndex[duration]!!) + val enable = (category?.timeWarnings ?: 0) and flag != 0 + checkbox.isChecked = enable + + checkbox.setOnCheckedChangeListener { _, isChecked -> + if (isChecked != enable && category != null) { + if (auth.tryDispatchParentAction( + UpdateCategoryTimeWarningsAction( + categoryId = category.id, + enable = isChecked, + flags = flag + ) + )) { + // it worked + } else { + checkbox.isChecked = enable + } + } + } + } + }) + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/category_time_warnings_view.xml b/app/src/main/res/layout/category_time_warnings_view.xml new file mode 100644 index 0000000..0d1ffa4 --- /dev/null +++ b/app/src/main/res/layout/category_time_warnings_view.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_category_settings.xml b/app/src/main/res/layout/fragment_category_settings.xml index c20f13b..9e144f9 100644 --- a/app/src/main/res/layout/fragment_category_settings.xml +++ b/app/src/main/res/layout/fragment_category_settings.xml @@ -70,6 +70,9 @@ + + diff --git a/app/src/main/res/values-de/strings-category-time-warnings.xml b/app/src/main/res/values-de/strings-category-time-warnings.xml new file mode 100644 index 0000000..b6783b1 --- /dev/null +++ b/app/src/main/res/values-de/strings-category-time-warnings.xml @@ -0,0 +1,19 @@ + + + + Zeitwarnung + Wenn weniger Zeit als unten ausgewählt übrig ist, dann wird eine Benachrichtigung angezeigt + \ No newline at end of file diff --git a/app/src/main/res/values/strings-category-time-warnings.xml b/app/src/main/res/values/strings-category-time-warnings.xml new file mode 100644 index 0000000..1c15423 --- /dev/null +++ b/app/src/main/res/values/strings-category-time-warnings.xml @@ -0,0 +1,19 @@ + + + + Time warnings + If there is less time remaining than checked below, then a notification will be shown + \ No newline at end of file