Show the category names in the usage history

This commit is contained in:
Jonas Lochmann 2020-10-26 01:00:00 +01:00
parent 871d482a40
commit c3759f6651
No known key found for this signature in database
GPG key ID: 8B8C9AEE10FA5B36
4 changed files with 13 additions and 4 deletions

View file

@ -64,10 +64,10 @@ abstract class UsedTimeDao {
abstract fun getAllUsedTimeItemsSync(): List<UsedTimeItem>
// breaking it into multiple lines causes issues during compilation ...
@Query("SELECT 2 AS type, start_time_of_day AS startMinuteOfDay, end_time_of_day AS endMinuteOfDay, used_time AS duration, day_of_epoch AS day, NULL AS lastUsage, NULL AS maxSessionDuration, NULL AS pauseDuration FROM used_time WHERE category_id = :categoryId UNION ALL SELECT 1 AS type, start_minute_of_day AS startMinuteOfDay, end_minute_of_day AS endMinuteOfDay, last_session_duration AS duration, NULL AS day, last_usage AS lastUsage, max_session_duration AS maxSessionDuration, session_pause_duration AS pauseDuration FROM session_duration WHERE category_id = :categoryId ORDER BY type, day DESC, lastUsage DESC, startMinuteOfDay, endMinuteOfDay")
@Query("SELECT 2 AS type, start_time_of_day AS startMinuteOfDay, end_time_of_day AS endMinuteOfDay, used_time AS duration, day_of_epoch AS day, NULL AS lastUsage, NULL AS maxSessionDuration, NULL AS pauseDuration, category.id AS categoryId, category.title AS categoryTitle FROM used_time JOIN category ON (used_time.category_id = category.id) WHERE category.id = :categoryId UNION ALL SELECT 1 AS type, start_minute_of_day AS startMinuteOfDay, end_minute_of_day AS endMinuteOfDay, last_session_duration AS duration, NULL AS day, last_usage AS lastUsage, max_session_duration AS maxSessionDuration, session_pause_duration AS pauseDuration, category.id AS categoryId, category.title AS categoryTitle FROM session_duration JOIN category ON (session_duration.category_id = category.id) WHERE category.id = :categoryId ORDER BY type, day DESC, lastUsage DESC, startMinuteOfDay, endMinuteOfDay, categoryId")
abstract fun getUsedTimeListItemsByCategoryId(categoryId: String): DataSource.Factory<Int, UsedTimeListItem>
// breaking it into multiple lines causes issues during compilation ...
@Query("SELECT 2 AS type, start_time_of_day AS startMinuteOfDay, end_time_of_day AS endMinuteOfDay, used_time AS duration, day_of_epoch AS day, NULL AS lastUsage, NULL AS maxSessionDuration, NULL AS pauseDuration FROM used_time WHERE category_id IN (SELECT category_id FROM category WHERE child_id = :userId) UNION ALL SELECT 1 AS type, start_minute_of_day AS startMinuteOfDay, end_minute_of_day AS endMinuteOfDay, last_session_duration AS duration, NULL AS day, last_usage AS lastUsage, max_session_duration AS maxSessionDuration, session_pause_duration AS pauseDuration FROM session_duration WHERE category_id IN (SELECT category_id FROM category WHERE child_id = :userId) ORDER BY type, day DESC, lastUsage DESC, startMinuteOfDay, endMinuteOfDay")
@Query("SELECT 2 AS type, start_time_of_day AS startMinuteOfDay, end_time_of_day AS endMinuteOfDay, used_time AS duration, day_of_epoch AS day, NULL AS lastUsage, NULL AS maxSessionDuration, NULL AS pauseDuration, category.id AS categoryId, category.title AS categoryTitle FROM used_time JOIN category ON (used_time.category_id = category.id) WHERE category.child_id = :userId UNION ALL SELECT 1 AS type, start_minute_of_day AS startMinuteOfDay, end_minute_of_day AS endMinuteOfDay, last_session_duration AS duration, NULL AS day, last_usage AS lastUsage, max_session_duration AS maxSessionDuration, session_pause_duration AS pauseDuration, category.id AS categoryId, category.title AS categoryTitle FROM session_duration JOIN category ON (session_duration.category_id = category.id) WHERE category.child_id = :userId ORDER BY type, day DESC, lastUsage DESC, startMinuteOfDay, endMinuteOfDay, categoryId")
abstract fun getUsedTimeListItemsByUserId(userId: String): DataSource.Factory<Int, UsedTimeListItem>
}

View file

@ -17,6 +17,8 @@
package io.timelimit.android.data.model
data class UsedTimeListItem(
val categoryId: String,
val categoryTitle: String,
val startMinuteOfDay: Int,
val endMinuteOfDay: Int,
val duration: Long,

View file

@ -30,6 +30,7 @@ import io.timelimit.android.util.TimeTextUtil
import org.threeten.bp.LocalDate
import org.threeten.bp.ZoneOffset
import java.util.*
import kotlin.properties.Delegates
class UsageHistoryAdapter: PagedListAdapter<UsedTimeListItem, UsageHistoryViewHolder>(diffCallback) {
companion object {
@ -41,6 +42,8 @@ class UsageHistoryAdapter: PagedListAdapter<UsedTimeListItem, UsageHistoryViewHo
}
}
var showCategoryTitle: Boolean by Delegates.observable(false) { _, _, _ -> notifyDataSetChanged() }
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = UsageHistoryViewHolder(
FragmentUsageHistoryItemBinding.inflate(
LayoutInflater.from(parent.context),
@ -59,17 +62,19 @@ class UsageHistoryAdapter: PagedListAdapter<UsedTimeListItem, UsageHistoryViewHo
else
context.getString(R.string.usage_history_time_area, MinuteOfDay.format(item.startMinuteOfDay), MinuteOfDay.format(item.endMinuteOfDay))
val dateStringPrefix = if (showCategoryTitle) item?.categoryTitle + " - " else ""
if (item?.day != null) {
val dateObject = LocalDate.ofEpochDay(item.day)
val dateString = DateFormat.getDateFormat(context).apply {
timeZone = TimeZone.getTimeZone("UTC")
}.format(Date(dateObject.atStartOfDay().toEpochSecond(ZoneOffset.UTC) * 1000L))
binding.date = dateString
binding.date = dateStringPrefix + dateString
binding.timeArea = timeAreaString
binding.usedTime = TimeTextUtil.used(item.duration.toInt(), context)
} else if (item?.lastUsage != null && item.maxSessionDuration != null && item.pauseDuration != null) {
binding.date = context.getString(
binding.date = dateStringPrefix + context.getString(
R.string.usage_history_item_session_duration_limit,
TimeTextUtil.time(item.maxSessionDuration.toInt(), context),
TimeTextUtil.time(item.pauseDuration.toInt(), context)

View file

@ -47,6 +47,8 @@ class UsageHistoryFragment : Fragment() {
val userId = requireArguments().getString(USER_ID)!!
val categoryId = requireArguments().getString(CATEGORY_ID)
adapter.showCategoryTitle = categoryId == null
LivePagedListBuilder(
categoryId?.let { database.usedTimes().getUsedTimeListItemsByCategoryId(it) }
?: database.usedTimes().getUsedTimeListItemsByUserId(userId),