Sort categories in the category screen

This commit is contained in:
Jonas Lochmann 2020-02-03 01:00:00 +01:00
parent 9fda41a006
commit 412966df26
No known key found for this signature in database
GPG key ID: 8B8C9AEE10FA5B36
3 changed files with 90 additions and 55 deletions

View file

@ -1,5 +1,5 @@
/*
* TimeLimit Copyright <C> 2019 Jonas Lochmann
* TimeLimit Copyright <C> 2019 - 2020 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
@ -126,6 +126,7 @@ class Adapter: RecyclerView.Adapter<ViewHolder>() {
}
binding.usedForAppsWithoutCategory = item.usedForNotAssignedApps
binding.parentCategoryTitle = item.parentCategoryTitle
binding.isChildCategory = item.parentCategoryTitle != null
binding.card.setOnClickListener { handlers?.onCategoryClicked(item.category) }

View file

@ -1,5 +1,5 @@
/*
* TimeLimit Copyright <C> 2019 Jonas Lochmann
* TimeLimit Copyright <C> 2019 - 2020 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
@ -20,6 +20,7 @@ import android.util.SparseLongArray
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import io.timelimit.android.data.extensions.mapToTimezone
import io.timelimit.android.data.model.Category
import io.timelimit.android.data.model.HintsToShow
import io.timelimit.android.date.DateInTimezone
import io.timelimit.android.date.getMinuteOfWeek
@ -29,6 +30,7 @@ import io.timelimit.android.livedata.map
import io.timelimit.android.livedata.switchMap
import io.timelimit.android.logic.DefaultAppLogic
import io.timelimit.android.logic.RemainingTime
import java.util.*
class ManageChildCategoriesModel(application: Application): AndroidViewModel(application) {
private val logic = DefaultAppLogic.with(application)
@ -76,8 +78,25 @@ class ManageChildCategoriesModel(application: Application): AndroidViewModel(app
)
}
private val sortedCategories = categories.map { categories ->
val categoryById = categories.associateBy { it.id }
val sortedCategories = mutableListOf<Category>()
val childCategories = categories.filter { categoryById.containsKey(it.parentCategoryId) }.groupBy { it.parentCategoryId }
categories.filterNot { categoryById.containsKey(it.parentCategoryId) }.sortedBy { it.title.toLowerCase(Locale.getDefault()) }.forEach { category ->
sortedCategories.add(category)
childCategories[category.id]?.sortedBy { it.title.toLowerCase(Locale.getDefault()) }?.let { items ->
sortedCategories.addAll(items)
}
}
sortedCategories.toList()
}
private val categoryItems = categoryForUnassignedAppsLive.switchMap { categoryForUnassignedApps ->
categories.switchMap { categories ->
sortedCategories.switchMap { categories ->
timeLimitRules.switchMap { timeLimitRules ->
childDate.switchMap { childDate ->
usedTimeItemsForWeek.switchMap { usedTimeItemsForWeek ->

View file

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
TimeLimit Copyright <C> 2019 Jonas Lochmann
TimeLimit Copyright <C> 2019 - 2020 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.
@ -42,69 +42,84 @@
name="parentCategoryTitle"
type="String" />
<variable
name="isChildCategory"
type="boolean" />
<import type="android.text.TextUtils" />
<import type="android.view.View" />
</data>
<androidx.cardview.widget.CardView
android:foreground="?selectableItemBackground"
android:id="@+id/card"
android:paddingStart="8dp"
android:paddingEnd="8dp"
app:cardUseCompatPadding="true"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:padding="8dp"
android:orientation="vertical"
android:layout_height="wrap_content"
android:orientation="horizontal">
<View
android:visibility="@{isChildCategory ? View.VISIBLE : View.GONE}"
android:layout_width="32dp"
android:layout_height="wrap_content" />
<androidx.cardview.widget.CardView
android:foreground="?selectableItemBackground"
android:id="@+id/card"
android:paddingStart="8dp"
android:paddingEnd="8dp"
app:cardUseCompatPadding="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textAppearance="?android:textAppearanceLarge"
android:text="@{title}"
tools:text="Erlaubte Apps"
<LinearLayout
android:padding="8dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
android:layout_height="wrap_content">
<TextView
android:textAppearance="?android:textAppearanceMedium"
android:visibility="@{usedForAppsWithoutCategory ? View.VISIBLE : View.GONE}"
android:text="@string/manage_child_category_for_unassigned_apps"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:textAppearance="?android:textAppearanceLarge"
android:text="@{title}"
tools:text="Erlaubte Apps"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:textAppearance="?android:textAppearanceMedium"
android:visibility="@{TextUtils.isEmpty(parentCategoryTitle) ? View.GONE : View.VISIBLE}"
tools:text="@string/manage_child_category_is_child"
android:text="@{@string/manage_child_category_is_child(parentCategoryTitle)}"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:textAppearance="?android:textAppearanceMedium"
android:visibility="@{usedForAppsWithoutCategory ? View.VISIBLE : View.GONE}"
android:text="@string/manage_child_category_for_unassigned_apps"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:visibility="@{TextUtils.isEmpty(usedTimeToday) ? View.GONE : View.VISIBLE}"
android:text="@{usedTimeToday}"
tools:text="2 Minuten benutzt"
android:textAppearance="?android:textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:textAppearance="?android:textAppearanceMedium"
android:visibility="@{TextUtils.isEmpty(parentCategoryTitle) ? View.GONE : View.VISIBLE}"
tools:text="@string/manage_child_category_is_child"
android:text="@{@string/manage_child_category_is_child(parentCategoryTitle)}"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:visibility="@{TextUtils.isEmpty(remainingTimeToday) ? View.GONE : View.VISIBLE}"
android:text="@{remainingTimeToday}"
tools:text="5 Minuten verbleibend"
android:textAppearance="?android:textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:visibility="@{TextUtils.isEmpty(usedTimeToday) ? View.GONE : View.VISIBLE}"
android:text="@{usedTimeToday}"
tools:text="2 Minuten benutzt"
android:textAppearance="?android:textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:visibility="@{TextUtils.isEmpty(remainingTimeToday) ? View.VISIBLE : View.GONE}"
android:textAppearance="?android:textAppearanceMedium"
android:text="@string/manage_child_category_no_time_limits"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:visibility="@{TextUtils.isEmpty(remainingTimeToday) ? View.GONE : View.VISIBLE}"
android:text="@{remainingTimeToday}"
tools:text="5 Minuten verbleibend"
android:textAppearance="?android:textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<TextView
android:visibility="@{TextUtils.isEmpty(remainingTimeToday) ? View.VISIBLE : View.GONE}"
android:textAppearance="?android:textAppearanceMedium"
android:text="@string/manage_child_category_no_time_limits"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</layout>