mirror of
https://codeberg.org/timelimit/timelimit-android.git
synced 2025-10-05 19:42:20 +02:00
Add UI to manage time warnings
This commit is contained in:
parent
cd458e411b
commit
cd72aef0ef
7 changed files with 156 additions and 0 deletions
|
@ -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
|
||||
}
|
|
@ -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() }
|
||||
|
||||
|
|
|
@ -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<Category?>,
|
||||
auth: ActivityViewModel
|
||||
) {
|
||||
view.linearLayout.removeAllViews()
|
||||
|
||||
val durationToCheckbox = mutableMapOf<Long, CheckBox>()
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
36
app/src/main/res/layout/category_time_warnings_view.xml
Normal file
36
app/src/main/res/layout/category_time_warnings_view.xml
Normal file
|
@ -0,0 +1,36 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<layout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
<androidx.cardview.widget.CardView
|
||||
app:cardUseCompatPadding="true"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
<LinearLayout
|
||||
android:orientation="vertical"
|
||||
android:padding="8dp"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<TextView
|
||||
android:text="@string/time_warnings_title"
|
||||
android:textAppearance="?android:textAppearanceLarge"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
<TextView
|
||||
android:text="@string/time_warning_desc"
|
||||
android:textAppearance="?android:textAppearanceMedium"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="vertical"
|
||||
android:id="@+id/linear_layout"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
<!-- checkboxes will be added hear -->
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
</androidx.cardview.widget.CardView>
|
||||
</layout>
|
|
@ -70,6 +70,9 @@
|
|||
</LinearLayout>
|
||||
</androidx.cardview.widget.CardView>
|
||||
|
||||
<include android:id="@+id/time_warnings"
|
||||
layout="@layout/category_time_warnings_view" />
|
||||
|
||||
<include android:id="@+id/category_for_unassigned_apps"
|
||||
layout="@layout/manage_category_for_unassigned_apps" />
|
||||
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
TimeLimit Copyright <C> 2019 Jonas Lochmann
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation version 3 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
-->
|
||||
<resources>
|
||||
<string name="time_warnings_title">Zeitwarnung</string>
|
||||
<string name="time_warning_desc">Wenn weniger Zeit als unten ausgewählt übrig ist, dann wird eine Benachrichtigung angezeigt</string>
|
||||
</resources>
|
19
app/src/main/res/values/strings-category-time-warnings.xml
Normal file
19
app/src/main/res/values/strings-category-time-warnings.xml
Normal file
|
@ -0,0 +1,19 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
TimeLimit Copyright <C> 2019 Jonas Lochmann
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation version 3 of the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
-->
|
||||
<resources>
|
||||
<string name="time_warnings_title">Time warnings</string>
|
||||
<string name="time_warning_desc">If there is less time remaining than checked below, then a notification will be shown</string>
|
||||
</resources>
|
Loading…
Add table
Add a link
Reference in a new issue