Skip to content

Commit f7d07e3

Browse files
committed
allow groups with the same name if they have different parents
1 parent 9f8c0e6 commit f7d07e3

4 files changed

Lines changed: 47 additions & 27 deletions

File tree

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ class BackupManagerImpl(
460460
if (backupContent.groups != null) {
461461
val groupUids = backupContent.groups.map { it.uid }.toMutableSet()
462462

463-
groupRepository.getAllGroups().first()
463+
val existingGroupUids = groupRepository.getAllGroups().first()
464464
.map { it.uid }
465465
.toSet()
466466
.also { groupUids.addAll(it) }
@@ -478,13 +478,26 @@ class BackupManagerImpl(
478478
modifiedGroup = modifiedGroup.copy(parentUid = null)
479479
}
480480

481-
RepositoryUtils.saveUniqueName(
481+
val siblings =
482+
groupRepository.getGroupsByParent(modifiedGroup.parentUid).first()
483+
484+
modifiedGroup = RepositoryUtils.saveUniqueName(
482485
modifiedGroup,
483-
saveBlock = { groupRepository.insert(it) },
486+
saveBlock = { renamedGroup ->
487+
if (siblings.any { sibling -> sibling.name == renamedGroup.name }) {
488+
throw IllegalStateException("Non unique group name")
489+
}
490+
},
484491
renameBlock = { entity, suffix ->
485492
entity.copy(name = "${entity.name} $suffix")
486493
},
487494
)
495+
496+
if (existingGroupUids.contains(modifiedGroup.uid)) {
497+
groupRepository.update(modifiedGroup)
498+
} else {
499+
groupRepository.insert(modifiedGroup)
500+
}
488501
}
489502
}
490503

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package io.github.sds100.keymapper.data.repositories
22

3-
import android.database.sqlite.SQLiteConstraintException
4-
53
object RepositoryUtils {
64
suspend fun <T> saveUniqueName(
75
entity: T,
@@ -17,7 +15,7 @@ object RepositoryUtils {
1715
try {
1816
saveBlock(group)
1917
break
20-
} catch (_: SQLiteConstraintException) {
18+
} catch (_: Exception) {
2119
// If the name already exists try creating it with a new name.
2220
group = renameBlock(entity, "(${count + 1})")
2321
count++

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,13 +761,15 @@ class KeyMapListViewModel(
761761

762762
fun onGroupClick(uid: String?) {
763763
coroutineScope.launch {
764+
isEditingGroupName.update { false }
764765
isNewGroup = false
765766
listKeyMaps.openGroup(uid)
766767
}
767768
}
768769

769770
fun onDeleteGroupClick() {
770771
coroutineScope.launch {
772+
isEditingGroupName.update { false }
771773
listKeyMaps.deleteGroup()
772774
}
773775
}

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

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
2929
import kotlinx.coroutines.flow.channelFlow
3030
import kotlinx.coroutines.flow.collectLatest
3131
import kotlinx.coroutines.flow.combine
32+
import kotlinx.coroutines.flow.first
3233
import kotlinx.coroutines.flow.flatMapLatest
3334
import kotlinx.coroutines.flow.map
3435
import kotlinx.coroutines.flow.onEach
@@ -49,6 +50,10 @@ class ListKeyMapsUseCaseImpl(
4950
) : ListKeyMapsUseCase,
5051
DisplayKeyMapUseCase by displayKeyMapUseCase {
5152
private val groupUid = MutableStateFlow<String?>(null)
53+
54+
/**
55+
* These are the parents, grandparents etc of the current group.
56+
*/
5257
private val parentGroupUids = MutableStateFlow<List<String>>(emptyList())
5358

5459
@OptIn(ExperimentalCoroutinesApi::class)
@@ -73,16 +78,14 @@ class ListKeyMapsUseCaseImpl(
7378

7479
@OptIn(ExperimentalCoroutinesApi::class)
7580
private val parentGroups: Flow<List<Group>> =
76-
parentGroupUids
77-
.flatMapLatest { uids ->
78-
groupRepository.getGroups(*uids.toTypedArray())
79-
.map { groups ->
80-
// The repository returns the objects unordered so order them by the
81-
// original UID list again.
82-
val mapped = groups.associateBy { it.uid }
83-
uids.map { GroupEntityMapper.fromEntity(mapped[it]!!) }
84-
}
81+
parentGroupUids.flatMapLatest { parentUids ->
82+
groupRepository.getGroups(*parentUids.toTypedArray()).map { groups ->
83+
// The repository returns the objects unordered so order them by the
84+
// original UID list again.
85+
val mapped = groups.associateBy { it.uid }
86+
parentUids.map { GroupEntityMapper.fromEntity(mapped[it]!!) }
8587
}
88+
}
8689

8790
@OptIn(ExperimentalCoroutinesApi::class)
8891
override val keyMapGroup: Flow<KeyMapGroup> = channelFlow {
@@ -107,15 +110,14 @@ class ListKeyMapsUseCaseImpl(
107110

108111
override suspend fun newGroup() {
109112
val defaultName = resourceProvider.getString(R.string.default_group_name)
110-
val group = GroupEntity(
113+
var group = GroupEntity(
111114
parentUid = groupUid.value,
112115
name = defaultName,
113116
lastOpenedDate = System.currentTimeMillis(),
114117
)
115118

116-
ensureUniqueName(group) {
117-
groupRepository.insert(it)
118-
}
119+
group = ensureUniqueName(group)
120+
groupRepository.insert(group)
119121

120122
groupUid.update { group.uid }
121123
parentGroupUids.update { it.plus(group.uid) }
@@ -143,23 +145,28 @@ class ListKeyMapsUseCaseImpl(
143145

144146
entity = entity.copy(name = name.trim())
145147

146-
try {
147-
groupRepository.update(entity)
148-
} catch (_: SQLiteConstraintException) {
148+
val siblings = groupRepository.getGroupsByParent(entity.parentUid).first()
149+
150+
if (siblings.any { it.name == entity.name }) {
149151
return false
150152
}
153+
154+
groupRepository.update(entity)
151155
}
152156

153157
return true
154158
}
155159

156-
private suspend fun ensureUniqueName(
157-
group: GroupEntity,
158-
block: suspend (entity: GroupEntity) -> Unit,
159-
): GroupEntity {
160+
private suspend fun ensureUniqueName(group: GroupEntity): GroupEntity {
161+
val siblings = groupRepository.getGroupsByParent(group.parentUid).first()
162+
160163
return RepositoryUtils.saveUniqueName(
161164
entity = group,
162-
saveBlock = block,
165+
saveBlock = { renamedGroup ->
166+
if (siblings.any { sibling -> sibling.name == renamedGroup.name }) {
167+
throw IllegalStateException("Non unique group name")
168+
}
169+
},
163170
renameBlock = { entity, suffix ->
164171
entity.copy(name = "${entity.name} $suffix")
165172
},

0 commit comments

Comments
 (0)