|
| 1 | +package com.smarttoolfactory.domain.usecase |
| 2 | + |
| 3 | +import com.smarttoolfactory.data.constant.ORDER_BY_NONE |
| 4 | +import com.smarttoolfactory.data.model.local.PropertyEntity |
| 5 | +import com.smarttoolfactory.data.repository.PropertyRepositoryCoroutines |
| 6 | +import com.smarttoolfactory.domain.dispatcher.UseCaseDispatchers |
| 7 | +import com.smarttoolfactory.domain.error.EmptyDataException |
| 8 | +import com.smarttoolfactory.domain.mapper.PropertyEntityToItemListMapper |
| 9 | +import com.smarttoolfactory.domain.model.PropertyItem |
| 10 | +import javax.inject.Inject |
| 11 | +import kotlinx.coroutines.flow.Flow |
| 12 | +import kotlinx.coroutines.flow.catch |
| 13 | +import kotlinx.coroutines.flow.emitAll |
| 14 | +import kotlinx.coroutines.flow.flow |
| 15 | +import kotlinx.coroutines.flow.flowOf |
| 16 | +import kotlinx.coroutines.flow.flowOn |
| 17 | +import kotlinx.coroutines.flow.map |
| 18 | + |
| 19 | +/** |
| 20 | + * UseCase for getting UI Item list with offline first or offline last approach. |
| 21 | + * |
| 22 | + * * *Offline-first* first source to look for data is local data source, or database, |
| 23 | + * if database is empty or if caching is used it's expiry date is over, looks for remote source |
| 24 | + * for data. If both of the sources are empty, either return empty list or error to notify UI. |
| 25 | + * |
| 26 | + * This approach is good for when user is offline or have no internet connection, additional logic |
| 27 | + * can be added to check if user is offline to not deleted cached data. |
| 28 | + * |
| 29 | + * * *Offline-last* always checks remote data source for data and applies to database or offline |
| 30 | + * source as last resort. Offline-last is used especially when user refreshes data with a UI |
| 31 | + * element to get the latest data or new data is always first preference. |
| 32 | + */ |
| 33 | +class GetPropertiesUseCasePaged @Inject constructor( |
| 34 | + private val repository: PropertyRepositoryCoroutines, |
| 35 | + private val mapper: PropertyEntityToItemListMapper, |
| 36 | + private val dispatcherProvider: UseCaseDispatchers |
| 37 | +) { |
| 38 | + |
| 39 | + /** |
| 40 | + * Function to retrieve data from repository with offline-last which checks |
| 41 | + * REMOTE data source first. |
| 42 | + * |
| 43 | + * * Check out Remote Source first |
| 44 | + * * If empty data or null returned throw empty set exception |
| 45 | + * * If error occurred while fetching data from remote: Try to fetch data from db |
| 46 | + * * If data is fetched from remote source: delete old data, save new data and return new data |
| 47 | + * * If both network and db don't have any data throw empty set exception |
| 48 | + * |
| 49 | + */ |
| 50 | + fun getPagedOfflineLast(page: Int, orderBy: String): Flow<List<PropertyItem>> { |
| 51 | + return flow { emit(repository.fetchEntitiesFromRemote(orderBy)) } |
| 52 | + .map { |
| 53 | + if (it.isNullOrEmpty()) { |
| 54 | + throw EmptyDataException("No Data is available in Remote source!") |
| 55 | + } else { |
| 56 | + repository.run { |
| 57 | + |
| 58 | + if (page == 1) { |
| 59 | + deletePropertyEntities() |
| 60 | + } |
| 61 | + |
| 62 | + // 🔥 Add an insert order since we are not using Room's ORDER BY |
| 63 | + it.forEachIndexed { index, propertyEntity -> |
| 64 | + propertyEntity.insertOrder = index |
| 65 | + } |
| 66 | + savePropertyEntities(it) |
| 67 | + |
| 68 | + getPropertyEntitiesFromLocal() |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + .flowOn(dispatcherProvider.ioDispatcher) |
| 73 | + // This is where remote exception or least likely db exceptions are caught |
| 74 | + .catch { throwable -> |
| 75 | + emitAll(flowOf(repository.getPropertyEntitiesFromLocal())) |
| 76 | + } |
| 77 | + .map { |
| 78 | + toPropertyListOrError(it) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private fun toPropertyListOrError(entityList: List<PropertyEntity>): List<PropertyItem> { |
| 83 | + return if (!entityList.isNullOrEmpty()) { |
| 84 | + mapper.map(entityList) |
| 85 | + } else { |
| 86 | + throw EmptyDataException("Empty data mapping error!") |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + fun getCurrentSortKey(defaultKey: String = ORDER_BY_NONE): Flow<String> { |
| 91 | + return flow { emit(repository.getSortOrderKey()) } |
| 92 | + .catch { |
| 93 | + emitAll(flowOf(defaultKey)) |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments