@@ -29,6 +29,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
2929import kotlinx.coroutines.flow.channelFlow
3030import kotlinx.coroutines.flow.collectLatest
3131import kotlinx.coroutines.flow.combine
32+ import kotlinx.coroutines.flow.first
3233import kotlinx.coroutines.flow.flatMapLatest
3334import kotlinx.coroutines.flow.map
3435import 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