@@ -11,9 +11,9 @@ import ly.david.musicsearch.data.database.dao.CollectionEntityDao
1111import ly.david.musicsearch.data.musicbrainz.api.CollectionApi
1212import ly.david.musicsearch.data.repository.internal.paging.BrowseEntityRemoteMediator
1313import ly.david.musicsearch.shared.domain.browse.BrowseRemoteMetadataRepository
14- import ly.david.musicsearch.shared.domain.collection.CollectionFeedback
1514import ly.david.musicsearch.shared.domain.collection.CollectionRepository
1615import ly.david.musicsearch.shared.domain.collection.CollectionSortOption
16+ import ly.david.musicsearch.shared.domain.collection.EditACollectionFeedback
1717import ly.david.musicsearch.shared.domain.error.Action
1818import ly.david.musicsearch.shared.domain.error.ActionableResult
1919import ly.david.musicsearch.shared.domain.error.ErrorResolution
@@ -31,6 +31,7 @@ class CollectionRepositoryImpl(
3131 private val collectionEntityDao : CollectionEntityDao ,
3232 private val browseRemoteMetadataDao : BrowseRemoteMetadataDao ,
3333 private val browseEntityCountRepository : BrowseRemoteMetadataRepository ,
34+ private val clock : Clock ,
3435) : CollectionRepository {
3536
3637 @OptIn(ExperimentalPagingApi ::class )
@@ -126,14 +127,14 @@ class CollectionRepositoryImpl(
126127 override fun markDeletedFromCollection (
127128 collection : CollectionListItemModel ,
128129 collectableIds : List <String >,
129- ): Flow <Feedback <CollectionFeedback >> = flow {
130+ ): Flow <Feedback <EditACollectionFeedback >> = flow {
130131 collectionEntityDao.markDeletedFromCollection(
131132 collectionId = collection.id,
132133 collectableIds = collectableIds,
133134 )
134135 emit(
135136 Feedback .Actionable (
136- data = CollectionFeedback .Deleting (
137+ data = EditACollectionFeedback .Deleting (
137138 count = collectableIds.size,
138139 collectionName = collection.name,
139140 ),
@@ -148,11 +149,11 @@ class CollectionRepositoryImpl(
148149
149150 override suspend fun deleteFromCollection (
150151 collection : CollectionListItemModel ,
151- ): Flow <Feedback <CollectionFeedback >> = flow {
152- emit(Feedback .Loading (CollectionFeedback .Loading ))
153-
152+ ): Flow <Feedback <EditACollectionFeedback >> = flow {
154153 val idsMarkedForDeletion = collectionEntityDao.getIdsMarkedForDeletionFromCollection(collection.id)
154+
155155 if (collection.isRemote) {
156+ emit(Feedback .Loading (EditACollectionFeedback .SyncingWithMusicBrainz ))
156157 try {
157158 // TODO: handle deleting more than 400 items at a time
158159 // https://musicbrainz.org/doc/MusicBrainz_API#collections
@@ -164,7 +165,7 @@ class CollectionRepositoryImpl(
164165 } catch (ex: HandledException ) {
165166 emit(
166167 Feedback .Error (
167- data = CollectionFeedback . Failed (
168+ data = EditACollectionFeedback . FailedToDelete (
168169 collectionName = collection.name,
169170 errorMessage = ex.userMessage,
170171 ),
@@ -179,7 +180,7 @@ class CollectionRepositoryImpl(
179180 collectionEntityDao.deleteFromCollection(collectionId = collection.id)
180181 emit(
181182 Feedback .Success (
182- data = CollectionFeedback .Deleted (
183+ data = EditACollectionFeedback .Deleted (
183184 count = idsMarkedForDeletion.size,
184185 collectionName = collection.name,
185186 ),
@@ -191,11 +192,26 @@ class CollectionRepositoryImpl(
191192 collectionId : String ,
192193 entityType : MusicBrainzEntityType ,
193194 entityIds : List <String >,
194- ): ActionableResult {
195- val collection = collectionDao.getCollection(collectionId) ? : return ActionableResult (" Does not exist" )
195+ ): Flow <Feedback <EditACollectionFeedback >> = flow {
196+ val collection = collectionDao.getCollection(collectionId)
197+ if (collection == null ) {
198+ emit(
199+ Feedback .Error (
200+ data = EditACollectionFeedback .DoesNotExist ,
201+ errorResolution = ErrorResolution .None ,
202+ time = clock.now(),
203+ ),
204+ )
205+ return @flow
206+ }
196207
197- var result = ActionableResult ()
198208 if (collection.isRemote) {
209+ emit(
210+ Feedback .Loading (
211+ data = EditACollectionFeedback .SyncingWithMusicBrainz ,
212+ time = clock.now(),
213+ ),
214+ )
199215 try {
200216 // TODO: support adding more than 16KB worth of items at a time
201217 collectionApi.addToCollection(
@@ -204,31 +220,65 @@ class CollectionRepositoryImpl(
204220 mbids = entityIds,
205221 )
206222 } catch (ex: HandledException ) {
207- val userFacingError = " Failed to add to ${collection.name} . ${ex.userMessage} "
208- return ActionableResult (
209- message = userFacingError,
210- action = Action .Login .takeIf { ex.errorResolution == ErrorResolution .Login },
211- errorResolution = ex.errorResolution,
223+ emit(
224+ Feedback .Error (
225+ data = EditACollectionFeedback .FailedToAdd (
226+ collectionName = collection.name,
227+ errorMessage = ex.userMessage,
228+ ),
229+ action = Action .Login .takeIf { ex.errorResolution == ErrorResolution .Login },
230+ errorResolution = ex.errorResolution,
231+ time = clock.now(),
232+ ),
212233 )
234+ return @flow
213235 }
214236 }
215237
216- collectionEntityDao.withTransaction {
217- val insertions = collectionEntityDao.addAllToCollection(
218- collectionId = collectionId,
219- entityIds = entityIds.toList(),
238+ val feedback: EditACollectionFeedback = insertAndGetResult(
239+ collectionId = collectionId,
240+ entityIds = entityIds,
241+ collection = collection,
242+ )
243+ emit(
244+ Feedback .Success (
245+ data = feedback,
246+ time = clock.now(),
247+ ),
248+ )
249+ }
250+
251+ private fun insertAndGetResult (
252+ collectionId : String ,
253+ entityIds : List <String >,
254+ collection : CollectionListItemModel ,
255+ ): EditACollectionFeedback {
256+ val insertions = collectionEntityDao.addAllToCollection(
257+ collectionId = collectionId,
258+ entityIds = entityIds,
259+ ).toInt()
260+
261+ val data: EditACollectionFeedback = when {
262+ insertions == 0 -> EditACollectionFeedback .AlreadyIn (
263+ collectionName = collection.name,
220264 )
221265
222- result = ActionableResult (
223- message = when {
224- insertions == 0L -> " Already in ${collection.name} ."
225- entityIds.size == 1 -> " Added to ${collection.name} ."
226- else -> " Added ${insertions.toInt()} to ${collection.name} ."
227- },
266+ entityIds.size == 1 -> EditACollectionFeedback .AddedOne (
267+ collectionName = collection.name,
228268 )
229- }
230269
231- return result
270+ insertions == entityIds.size -> EditACollectionFeedback .AddedMultiple (
271+ newInsertions = insertions,
272+ collectionName = collection.name,
273+ )
274+
275+ else -> EditACollectionFeedback .AddedMultipleSomeAlreadyAdded (
276+ newInsertions = insertions,
277+ collectionName = collection.name,
278+ countAlreadyAdded = entityIds.size - insertions,
279+ )
280+ }
281+ return data
232282 }
233283
234284 override fun markDeletedCollections (
0 commit comments