-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create repository for locaiton data sources
Signed-off-by: Yurii Surzhykov <yuriisurzhykov@gmail.com>
- Loading branch information
1 parent
e06b9c2
commit fcc3366
Showing
6 changed files
with
103 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...tory/src/main/java/com/github/yuriisurzhykov/purs/data/repository/LocationCloudMappers.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.github.yuriisurzhykov.purs.data.repository | ||
|
||
import com.github.yuriisurzhykov.purs.core.Mapper | ||
import com.github.yuriisurzhykov.purs.data.cache.GetLocationId | ||
import com.github.yuriisurzhykov.purs.data.cache.model.LocationCache | ||
import com.github.yuriisurzhykov.purs.data.cache.model.LocationWithWorkingHours | ||
import com.github.yuriisurzhykov.purs.data.cache.model.WorkingHourCache | ||
import com.github.yuriisurzhykov.purs.data.cloud.model.LocationCloud | ||
import com.github.yuriisurzhykov.purs.data.cloud.model.WorkingHourCloud | ||
import javax.inject.Inject | ||
|
||
interface LocationCloudToCacheMapper : Mapper<LocationCloud, LocationWithWorkingHours> { | ||
|
||
class Base @Inject constructor( | ||
private val cloudMapper: LocationCloudMapper, | ||
private val workingHoursCloudMapper: LocationWorkingHoursCloudMapper | ||
) : LocationCloudToCacheMapper { | ||
override fun map(source: LocationCloud): LocationWithWorkingHours { | ||
val sourceLocation: LocationCache = source.map(cloudMapper) | ||
val workingHours: List<WorkingHourCache> = source.map(workingHoursCloudMapper) | ||
return LocationWithWorkingHours(sourceLocation, workingHours) | ||
} | ||
} | ||
} | ||
|
||
class LocationCloudMapper @Inject constructor( | ||
private val getLocationId: GetLocationId | ||
) : LocationCloud.Mapper<LocationCache> { | ||
override fun map(locationName: String, workingHours: List<WorkingHourCloud>) = | ||
LocationCache(getLocationId.locationId(), locationName) | ||
} | ||
|
||
class LocationWorkingHoursCloudMapper @Inject constructor( | ||
private val mapper: WorkingHoursCloudMapper | ||
) : LocationCloud.Mapper<List<WorkingHourCache>> { | ||
override fun map( | ||
locationName: String, | ||
workingHours: List<WorkingHourCloud> | ||
): List<WorkingHourCache> = | ||
workingHours.map { workingHoursCloud -> workingHoursCloud.map(mapper) } | ||
} | ||
|
||
class WorkingHoursCloudMapper @Inject constructor() : WorkingHourCloud.Mapper<WorkingHourCache> { | ||
override fun map(dayName: String, startTime: String, endTime: String): WorkingHourCache { | ||
return WorkingHourCache(0, startTime, endTime, dayName, 0) | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...sitory/src/main/java/com/github/yuriisurzhykov/purs/data/repository/LocationRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.github.yuriisurzhykov.purs.data.repository | ||
|
||
import com.github.yuriisurzhykov.purs.core.MergeStrategy | ||
import com.github.yuriisurzhykov.purs.core.RequestResult | ||
import com.github.yuriisurzhykov.purs.core.map | ||
import com.github.yuriisurzhykov.purs.data.cache.LocationCacheDataSource | ||
import com.github.yuriisurzhykov.purs.data.cache.model.LocationWithWorkingHours | ||
import com.github.yuriisurzhykov.purs.data.cloud.LocationCloudDataSource | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.combine | ||
import kotlinx.coroutines.flow.flowOf | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.flow.merge | ||
import kotlinx.coroutines.flow.onEach | ||
import javax.inject.Inject | ||
|
||
interface LocationRepository { | ||
|
||
suspend fun fetchLocationDetails(): Flow<RequestResult<LocationWithWorkingHours>> | ||
|
||
class Base @Inject constructor( | ||
private val cloudDataSource: LocationCloudDataSource, | ||
private val cacheDataSource: LocationCacheDataSource, | ||
private val sourceMergeStrategy: MergeStrategy<RequestResult<LocationWithWorkingHours>>, | ||
private val mapper: LocationCloudToCacheMapper | ||
) : LocationRepository { | ||
override suspend fun fetchLocationDetails(): Flow<RequestResult<LocationWithWorkingHours>> { | ||
// Fetch cached data if any | ||
val cachedResponse = cacheDataSource.fetchLocation() | ||
// Start loading data from cloud | ||
val cloudResponse = cloudDataSource.fetchLocation() | ||
.map { response -> // Map cloud data to cache data | ||
response.map { it.let { locationCloud -> mapper.map(locationCloud) } } | ||
}.onEach { response -> // Persist cloud data to cache | ||
response.map { cacheDataSource.persistLocation(it) } | ||
} | ||
|
||
// Produce initial state that is loading | ||
val initial = | ||
flowOf<RequestResult<LocationWithWorkingHours>>(RequestResult.InProgress()) | ||
|
||
// Combine two data sources together with the strategy based on cloud and cache results | ||
val combinedSources = cachedResponse.combine(cloudResponse) { cache, cloud -> | ||
sourceMergeStrategy.merge(cache, cloud) | ||
} | ||
return merge(initial, combinedSources) | ||
} | ||
} | ||
} |