Skip to content

Commit 540bf6b

Browse files
committed
feat: order groups by when they most recently opened
1 parent 7f0d1df commit 540bf6b

9 files changed

Lines changed: 456 additions & 7 deletions

File tree

app/schemas/io.github.sds100.keymapper.data.db.AppDatabase/17.json

Lines changed: 408 additions & 0 deletions
Large diffs are not rendered by default.

app/src/main/java/io/github/sds100/keymapper/backup/BackupManager.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -462,17 +462,21 @@ class BackupManagerImpl(
462462
.toSet()
463463
.also { groupUids.addAll(it) }
464464

465+
val currentTime = System.currentTimeMillis()
466+
465467
for (group in backupContent.groups) {
466-
var movedGroup = group
468+
// Set the last opened date to now so that the imported group
469+
// shows as the most recent.
470+
var modifiedGroup = group.copy(lastOpenedDate = currentTime)
467471

468472
// If the group's parent wasn't backed up or doesn't exist
469473
// then set it the parent to the root group
470474
if (!groupUids.contains(group.parentUid)) {
471-
movedGroup = movedGroup.copy(parentUid = null)
475+
modifiedGroup = modifiedGroup.copy(parentUid = null)
472476
}
473477

474478
RepositoryUtils.saveUniqueName(
475-
movedGroup,
479+
modifiedGroup,
476480
saveBlock = { groupRepository.insert(it) },
477481
renameBlock = { entity, suffix ->
478482
entity.copy(name = "${entity.name} $suffix")

app/src/main/java/io/github/sds100/keymapper/data/db/AppDatabase.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import io.github.sds100.keymapper.data.migration.Migration5To6
3838
import io.github.sds100.keymapper.data.migration.Migration6To7
3939
import io.github.sds100.keymapper.data.migration.Migration8To9
4040
import io.github.sds100.keymapper.data.migration.Migration9To10
41+
import io.github.sds100.keymapper.data.migration.fingerprintmaps.AutoMigration16To17
4142

4243
/**
4344
* Created by sds100 on 24/01/2020.
@@ -51,6 +52,8 @@ import io.github.sds100.keymapper.data.migration.Migration9To10
5152
AutoMigration(from = 14, to = 15, spec = AutoMigration14To15::class),
5253
// This deletes the folder name column from key maps
5354
AutoMigration(from = 15, to = 16, spec = AutoMigration15To16::class),
55+
// This adds last opened timestamp to groups
56+
AutoMigration(from = 16, to = 17, spec = AutoMigration16To17::class),
5457
],
5558
)
5659
@TypeConverters(
@@ -62,7 +65,7 @@ import io.github.sds100.keymapper.data.migration.Migration9To10
6265
abstract class AppDatabase : RoomDatabase() {
6366
companion object {
6467
const val DATABASE_NAME = "key_map_database"
65-
const val DATABASE_VERSION = 16
68+
const val DATABASE_VERSION = 17
6669

6770
val MIGRATION_1_2 = object : Migration(1, 2) {
6871

app/src/main/java/io/github/sds100/keymapper/data/db/dao/GroupDao.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ interface GroupDao {
2020
const val KEY_CONSTRAINTS = "constraints"
2121
const val KEY_CONSTRAINT_MODE = "constraint_mode"
2222
const val KEY_PARENT_UID = "parent_uid"
23+
const val KEY_LAST_OPENED_DATE = "last_opened_date"
2324
}
2425

2526
@Query("SELECT * FROM $TABLE_NAME")
@@ -54,4 +55,7 @@ interface GroupDao {
5455

5556
@Query("DELETE FROM $TABLE_NAME WHERE $KEY_UID IN (:uid)")
5657
suspend fun deleteByUid(vararg uid: String)
58+
59+
@Query("UPDATE $TABLE_NAME SET $KEY_LAST_OPENED_DATE = (:timestamp) WHERE $KEY_UID IS (:groupUid)")
60+
suspend fun setLastOpenedDate(groupUid: String, timestamp: Long)
5761
}

app/src/main/java/io/github/sds100/keymapper/data/entities/GroupEntity.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import androidx.room.Index
88
import androidx.room.PrimaryKey
99
import com.github.salomonbrys.kotson.byArray
1010
import com.github.salomonbrys.kotson.byInt
11+
import com.github.salomonbrys.kotson.byNullableLong
1112
import com.github.salomonbrys.kotson.byNullableString
1213
import com.github.salomonbrys.kotson.byString
1314
import com.github.salomonbrys.kotson.jsonDeserializer
@@ -51,6 +52,10 @@ data class GroupEntity(
5152
@SerializedName(NAME_PARENT_UID)
5253
val parentUid: String?,
5354

55+
@ColumnInfo(name = GroupDao.KEY_LAST_OPENED_DATE)
56+
@SerializedName(NAME_LAST_OPENED_DATE)
57+
val lastOpenedDate: Long?,
58+
5459
) : Parcelable {
5560
companion object {
5661
// DON'T CHANGE THESE. Used for JSON serialization and parsing.
@@ -59,6 +64,7 @@ data class GroupEntity(
5964
const val NAME_CONSTRAINTS = "constraints"
6065
const val NAME_CONSTRAINT_MODE = "constraint_mode"
6166
const val NAME_PARENT_UID = "parent_uid"
67+
const val NAME_LAST_OPENED_DATE = "last_opened_date"
6268

6369
val DESERIALIZER = jsonDeserializer {
6470
val uid by it.json.byString(NAME_UID)
@@ -69,8 +75,9 @@ data class GroupEntity(
6975

7076
val constraintMode by it.json.byInt(NAME_CONSTRAINT_MODE)
7177
val parentUid by it.json.byNullableString(NAME_PARENT_UID)
78+
val lastOpenedDate by it.json.byNullableLong(NAME_LAST_OPENED_DATE)
7279

73-
GroupEntity(uid, name, constraintList, constraintMode, parentUid)
80+
GroupEntity(uid, name, constraintList, constraintMode, parentUid, lastOpenedDate)
7481
}
7582
}
7683
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package io.github.sds100.keymapper.data.migration.fingerprintmaps
2+
3+
import androidx.room.migration.AutoMigrationSpec
4+
5+
class AutoMigration16To17 : AutoMigrationSpec

app/src/main/java/io/github/sds100/keymapper/data/repositories/GroupRepository.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ interface GroupRepository {
2727
suspend fun insert(groupEntity: GroupEntity)
2828
suspend fun update(groupEntity: GroupEntity)
2929
fun delete(uid: String)
30+
suspend fun setLastOpenedDate(groupUid: String, timestamp: Long)
3031
}
3132

3233
class RoomGroupRepository(
@@ -81,4 +82,10 @@ class RoomGroupRepository(
8182
}
8283
}
8384
}
85+
86+
override suspend fun setLastOpenedDate(groupUid: String, timestamp: Long) {
87+
withContext(dispatchers.io()) {
88+
dao.setLastOpenedDate(groupUid, timestamp)
89+
}
90+
}
8491
}

app/src/main/java/io/github/sds100/keymapper/groups/Group.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ data class Group(
1010
val name: String,
1111
val constraintState: ConstraintState,
1212
val parentUid: String?,
13+
val lastOpenedDate: Long,
1314
)
1415

1516
object GroupEntityMapper {
@@ -24,6 +25,7 @@ object GroupEntityMapper {
2425
name = entity.name,
2526
constraintState = ConstraintState(constraintList, constraintMode),
2627
parentUid = entity.parentUid,
28+
lastOpenedDate = entity.lastOpenedDate ?: System.currentTimeMillis(),
2729
)
2830
}
2931

@@ -36,6 +38,7 @@ object GroupEntityMapper {
3638
},
3739
constraintMode = ConstraintModeEntityMapper.toEntity(group.constraintState.mode),
3840
parentUid = group.parentUid,
41+
lastOpenedDate = group.lastOpenedDate,
3942
)
4043
}
4144
}

app/src/main/java/io/github/sds100/keymapper/mappings/keymaps/ListKeyMapsUseCase.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ class ListKeyMapsUseCaseImpl(
5555
private val group: Flow<GroupWithSubGroups> = groupUid.flatMapLatest { groupUid ->
5656
if (groupUid == null) {
5757
groupRepository.getGroupsByParent(null).map { subGroupEntities ->
58-
val subGroups = subGroupEntities.map(GroupEntityMapper::fromEntity)
58+
val subGroups = subGroupEntities
59+
.map(GroupEntityMapper::fromEntity)
60+
.sortedByDescending { it.lastOpenedDate }
5961
GroupWithSubGroups(group = null, subGroups = subGroups)
6062
}
6163
} else {
@@ -105,7 +107,11 @@ class ListKeyMapsUseCaseImpl(
105107

106108
override suspend fun newGroup() {
107109
val defaultName = resourceProvider.getString(R.string.default_group_name)
108-
val group = GroupEntity(parentUid = groupUid.value, name = defaultName)
110+
val group = GroupEntity(
111+
parentUid = groupUid.value,
112+
name = defaultName,
113+
lastOpenedDate = System.currentTimeMillis(),
114+
)
109115

110116
ensureUniqueName(group) {
111117
groupRepository.insert(it)
@@ -177,6 +183,8 @@ class ListKeyMapsUseCaseImpl(
177183
list.plus(group.uid)
178184
}
179185
}
186+
187+
groupRepository.setLastOpenedDate(group.uid, System.currentTimeMillis())
180188
}
181189
}
182190

0 commit comments

Comments
 (0)