-
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 cloud data source for location (#33)
- Loading branch information
Showing
60 changed files
with
519 additions
and
162 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,5 @@ java { | |
|
||
dependencies { | ||
implementation(libs.kotlinx.coroutines.core) | ||
implementation(libs.retrofit) | ||
} |
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
18 changes: 18 additions & 0 deletions
18
core/data/src/main/java/com/github/yuriisurzhykov/purs/core/data/CloudDataSource.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,18 @@ | ||
package com.github.yuriisurzhykov.purs.core.data | ||
|
||
interface CloudDataSource { | ||
|
||
suspend fun <T> handle(block: suspend () -> T): T | ||
|
||
abstract class Abstract( | ||
private val handleError: HandleError | ||
) : CloudDataSource { | ||
override suspend fun <T> handle(block: suspend () -> T): T { | ||
return try { | ||
block.invoke() | ||
} catch (error: Exception) { | ||
throw handleError.handle(error) | ||
} | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
core/data/src/main/java/com/github/yuriisurzhykov/purs/core/data/HandleError.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,6 @@ | ||
package com.github.yuriisurzhykov.purs.core.data | ||
|
||
interface HandleError { | ||
|
||
fun handle(exception: Exception): Exception | ||
} |
20 changes: 20 additions & 0 deletions
20
core/data/src/main/java/com/github/yuriisurzhykov/purs/core/data/ProvideConverterFactory.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,20 @@ | ||
package com.github.yuriisurzhykov.purs.core.data | ||
|
||
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory | ||
import kotlinx.serialization.json.Json | ||
import okhttp3.MediaType | ||
import okhttp3.MediaType.Companion.toMediaType | ||
import retrofit2.Converter | ||
|
||
interface ProvideConverterFactory { | ||
|
||
fun converterFactory(): Converter.Factory | ||
|
||
abstract class Abstract( | ||
private val mediaType: MediaType = "application/json; charset=UTF8".toMediaType() | ||
) : ProvideConverterFactory { | ||
override fun converterFactory(): Converter.Factory { | ||
return Json.asConverterFactory(mediaType) | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
core/data/src/main/java/com/github/yuriisurzhykov/purs/core/data/ProvideInterceptor.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,19 @@ | ||
package com.github.yuriisurzhykov.purs.core.data | ||
|
||
import okhttp3.logging.HttpLoggingInterceptor | ||
|
||
interface ProvideInterceptor { | ||
|
||
fun interceptor(): HttpLoggingInterceptor | ||
|
||
abstract class Abstract( | ||
private val loggingLevel: HttpLoggingInterceptor.Level | ||
) : ProvideInterceptor { | ||
override fun interceptor() = HttpLoggingInterceptor().apply { | ||
level = loggingLevel | ||
} | ||
} | ||
|
||
class Debug : Abstract(HttpLoggingInterceptor.Level.BODY) | ||
class Release : Abstract(HttpLoggingInterceptor.Level.NONE) | ||
} |
21 changes: 21 additions & 0 deletions
21
...data/src/main/java/com/github/yuriisurzhykov/purs/core/data/ProvideOkHttpClientBuilder.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,21 @@ | ||
package com.github.yuriisurzhykov.purs.core.data | ||
|
||
import okhttp3.OkHttpClient | ||
import java.util.concurrent.TimeUnit | ||
|
||
interface ProvideOkHttpClientBuilder { | ||
|
||
fun provideOkHttpClientBuilder(): OkHttpClient.Builder | ||
|
||
abstract class Abstract( | ||
private val provideInterceptor: ProvideInterceptor, | ||
private val timeoutSeconds: Long = 60L | ||
) : ProvideOkHttpClientBuilder { | ||
override fun provideOkHttpClientBuilder() = OkHttpClient.Builder() | ||
.addInterceptor(provideInterceptor.interceptor()) | ||
.connectTimeout(timeoutSeconds, TimeUnit.SECONDS) | ||
.readTimeout(timeoutSeconds, TimeUnit.SECONDS) | ||
} | ||
|
||
class Base(provideInterceptor: ProvideInterceptor) : Abstract(provideInterceptor) | ||
} |
17 changes: 17 additions & 0 deletions
17
core/data/src/main/java/com/github/yuriisurzhykov/purs/core/data/ProvideRetrofitBuilder.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,17 @@ | ||
package com.github.yuriisurzhykov.purs.core.data | ||
|
||
import retrofit2.Retrofit | ||
|
||
interface ProvideRetrofitBuilder { | ||
|
||
fun provideRetrofitBuilder(): Retrofit.Builder | ||
|
||
abstract class Abstract( | ||
private val provideConverterFactory: ProvideConverterFactory, | ||
private val httpClientBuilder: ProvideOkHttpClientBuilder | ||
) : ProvideRetrofitBuilder { | ||
override fun provideRetrofitBuilder(): Retrofit.Builder = Retrofit.Builder() | ||
.addConverterFactory(provideConverterFactory.converterFactory()) | ||
.client(httpClientBuilder.provideOkHttpClientBuilder().build()) | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
core/src/main/java/com/github/yuriisurzhykov/purs/core/MergeStrategy.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,93 @@ | ||
package com.github.yuriisurzhykov.purs.core | ||
|
||
interface MergeStrategy<E> { | ||
fun merge(right: E, left: E): E | ||
} | ||
|
||
internal class RequestResponseMergeStrategy<T : Any> : MergeStrategy<RequestResult<T>> { | ||
override fun merge( | ||
right: RequestResult<T>, | ||
left: RequestResult<T> | ||
): RequestResult<T> { | ||
return when { | ||
right is RequestResult.InProgress && left is RequestResult.InProgress -> merge( | ||
right, | ||
left | ||
) | ||
|
||
right is RequestResult.Success && left is RequestResult.InProgress -> merge(right, left) | ||
right is RequestResult.InProgress && left is RequestResult.Success -> merge(right, left) | ||
right is RequestResult.Success && left is RequestResult.Success -> merge(right, left) | ||
right is RequestResult.Success && left is RequestResult.Error -> merge(right, left) | ||
right is RequestResult.InProgress && left is RequestResult.Error -> merge(right, left) | ||
right is RequestResult.Error && left is RequestResult.InProgress -> merge(right, left) | ||
right is RequestResult.Error && left is RequestResult.Success -> merge(right, left) | ||
|
||
else -> error("Unimplemented branch right=$right & left=$left") | ||
} | ||
} | ||
|
||
private fun merge( | ||
cache: RequestResult.InProgress<T>, | ||
server: RequestResult.InProgress<T> | ||
): RequestResult<T> { | ||
return when { | ||
server.data != null -> RequestResult.InProgress(server.data) | ||
else -> RequestResult.InProgress(cache.data) | ||
} | ||
} | ||
|
||
@Suppress("UNUSED_PARAMETER") | ||
private fun merge( | ||
cache: RequestResult.Success<T>, | ||
server: RequestResult.InProgress<T> | ||
): RequestResult<T> { | ||
return RequestResult.InProgress(cache.data) | ||
} | ||
|
||
@Suppress("UNUSED_PARAMETER") | ||
private fun merge( | ||
cache: RequestResult.InProgress<T>, | ||
server: RequestResult.Success<T> | ||
): RequestResult<T> { | ||
return RequestResult.InProgress(server.data) | ||
} | ||
|
||
private fun merge( | ||
cache: RequestResult.Success<T>, | ||
server: RequestResult.Error<T> | ||
): RequestResult<T> { | ||
return RequestResult.Error(data = cache.data, error = server.error) | ||
} | ||
|
||
@Suppress("UNUSED_PARAMETER") | ||
private fun merge( | ||
cache: RequestResult.Success<T>, | ||
server: RequestResult.Success<T> | ||
): RequestResult<T> { | ||
return RequestResult.Success(data = server.data) | ||
} | ||
|
||
private fun merge( | ||
cache: RequestResult.InProgress<T>, | ||
server: RequestResult.Error<T> | ||
): RequestResult<T> { | ||
return RequestResult.Error(data = server.data ?: cache.data, error = server.error) | ||
} | ||
|
||
@Suppress("UNUSED_PARAMETER") | ||
private fun merge( | ||
cache: RequestResult.Error<T>, | ||
server: RequestResult.InProgress<T> | ||
): RequestResult<T> { | ||
return server | ||
} | ||
|
||
@Suppress("UNUSED_PARAMETER") | ||
private fun merge( | ||
cache: RequestResult.Error<T>, | ||
server: RequestResult.Success<T> | ||
): RequestResult<T> { | ||
return server | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
core/src/main/java/com/github/yuriisurzhykov/purs/core/RequestResult.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,26 @@ | ||
package com.github.yuriisurzhykov.purs.core | ||
|
||
sealed class RequestResult<out E : Any>(open val data: E? = null) { | ||
data class InProgress<E : Any>(override val data: E? = null) : RequestResult<E>(data) | ||
|
||
data class Success<E : Any>(override val data: E) : RequestResult<E>(data) | ||
|
||
data class Error<E : Any>(override val data: E? = null, val error: Throwable? = null) : | ||
RequestResult<E>(data) | ||
} | ||
|
||
fun <I : Any, O : Any> RequestResult<I>.map(mapper: (I) -> O): RequestResult<O> { | ||
return when (this) { | ||
is RequestResult.Success -> RequestResult.Success(mapper(data)) | ||
is RequestResult.Error -> RequestResult.Error(data?.let(mapper)) | ||
is RequestResult.InProgress -> RequestResult.InProgress(data?.let(mapper)) | ||
} | ||
} | ||
|
||
internal fun <T : Any> Result<T>.toRequestResult(): RequestResult<T> { | ||
return when { | ||
isSuccess -> RequestResult.Success(getOrThrow()) | ||
isFailure -> RequestResult.Error(data = null, error = exceptionOrNull()) | ||
else -> error("Impossible branch") | ||
} | ||
} |
4 changes: 0 additions & 4 deletions
4
data/cloud/src/main/java/com/github/yuriisurzhykov/purs/data/cloud/Cloud.kt
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
24 changes: 0 additions & 24 deletions
24
.../androidTest/java/com/github/yuriisurzhykov/location/selection/ExampleInstrumentedTest.kt
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.