Skip to content

Commit

Permalink
convert rest queries to graphql
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaroslav Sudnik committed Aug 3, 2020
1 parent 217ef87 commit 5b91a31
Show file tree
Hide file tree
Showing 39 changed files with 175 additions and 675 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ import org.kodein.di.generic.singleton

val appModule = Kodein.Module(name = "appModule") {
bind<StringResource>() with singleton { StringResource(instance()) }
bind<ErrorHandler>() with singleton { ErrorHandler(instance(), instance(), instance(), instance()) }
bind<ErrorHandler>() with singleton { ErrorHandler(instance(), instance(), instance()) }
bind<Router>() with singleton { Router(instance()) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ import org.kodein.di.generic.provider

val repoModule = Kodein.Module(name = "repoModule") {
bind<LoginRepository>() with provider { LoginRepository(instance(), instance(), instance()) }
bind<ProfileRepository>() with provider { ProfileRepository(instance(), instance(), instance(), instance()) }
bind<ProfileRepository>() with provider { ProfileRepository(instance(), instance(), instance()) }
}
8 changes: 8 additions & 0 deletions app/src/main/java/com/flatstack/android/login/LoginMapper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.flatstack.android.login

import com.flatstack.android.graphql.mutation.LoginMutation
import com.flatstack.android.model.entities.Session

object LoginMapper {
fun mapLogin(signin: LoginMutation.Signin?) = Session(accessToken = signin?.accessToken ?: "")
}
35 changes: 18 additions & 17 deletions app/src/main/java/com/flatstack/android/login/LoginRepository.kt
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
package com.flatstack.android.login

import com.flatstack.android.login.entities.LoginRequest
import com.flatstack.android.login.entities.LoginResponse
import com.apollographql.apollo.ApolloClient
import com.apollographql.apollo.api.Response
import com.apollographql.apollo.coroutines.toDeferred
import com.flatstack.android.graphql.mutation.LoginMutation
import com.flatstack.android.model.entities.Session
import com.flatstack.android.model.network.ApiResponse
import com.flatstack.android.model.network.IApi
import com.flatstack.android.model.network.NetworkBoundResource
import com.flatstack.android.model.network.errors.ErrorHandler
import com.flatstack.android.profile.AuthorizationModel
import kotlinx.coroutines.*

class LoginRepository(
private val api: IApi,
private val authorizationModel: AuthorizationModel,
private val errorHandler: ErrorHandler
private val apolloClient: ApolloClient,
private val authorizationModel: AuthorizationModel,
private val errorHandler: ErrorHandler
) : CoroutineScope {
override val coroutineContext = SupervisorJob() + Dispatchers.IO

fun login(
username: String,
password: String
) = object : NetworkBoundResource<Session, LoginResponse>(coroutineContext, errorHandler) {
override suspend fun createCallAsync(): Deferred<ApiResponse<LoginResponse>> =
api.loginAsync(LoginRequest(username, password))
fun login(username: String, password: String) =
object : NetworkBoundResource<Session, LoginMutation.Data>(coroutineContext, errorHandler) {
override suspend fun createCallAsync(): Deferred<Response<LoginMutation.Data>> =
apolloClient.mutate(loginMutation(username, password)).toDeferred()

override suspend fun saveCallResult(item: LoginResponse) =
authorizationModel.setSession(item.session)
override suspend fun saveCallResult(item: LoginMutation.Data?) =
authorizationModel.setSession(LoginMapper.mapLogin(item?.signin))

override suspend fun loadFromDb() = authorizationModel.getSession()
}.asLiveData()
override suspend fun loadFromDb() = authorizationModel.getSession()
}.asLiveData()

fun unAuthorize() {
launch {
Expand All @@ -37,4 +35,7 @@ class LoginRepository(
}

fun onDestroy() = coroutineContext.cancelChildren()

private fun loginMutation(email: String, password: String) =
LoginMutation(email = email, password = password)
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ class LoginViewModel(

override fun onCleared() = loginRepository.onDestroy()

fun login(username: String, password: String) {
login.postValue(LoginId(username, password))
}
fun login(username: String, password: String) = login.postValue(LoginId(username, password))

@VisibleForTesting
fun emptyLoginResource() =
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import androidx.room.RoomDatabase
import com.flatstack.android.model.db.daos.ProfileDao
import com.flatstack.android.model.db.daos.SessionDao
import com.flatstack.android.model.entities.Session
import com.flatstack.android.profile.entities.Book
import com.flatstack.android.profile.entities.Profile

@Database(
entities = [Profile::class, Book::class, Session::class],
entities = [Profile::class, Session::class],
version = 1,
exportSchema = false
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import androidx.room.Insert
import androidx.room.OnConflictStrategy.REPLACE
import androidx.room.Query
import androidx.room.Transaction
import com.flatstack.android.profile.entities.Book
import com.flatstack.android.profile.entities.Profile

@Dao
Expand All @@ -19,30 +18,12 @@ abstract class ProfileDao {
@Query("DELETE FROM profile")
abstract suspend fun clearProfile()

@Insert(onConflict = REPLACE)
abstract suspend fun insertBooks(books: List<Book>)

@Query("SELECT * FROM books")
abstract suspend fun getBooks(): List<Book>

@Query("DELETE FROM books")
abstract suspend fun clearBooks()

@Transaction
open suspend fun insertProfileAndBooks(profile: Profile) {
open suspend fun insertUserProfile(profile: Profile) {
clear()
insertBooks(profile.booksRead)
insertProfile(profile)
}

@Transaction
open suspend fun getProfileWithBooks(): Profile? = getProfile()?.apply {
booksRead = getBooks()
}

@Transaction
open suspend fun clear() {
clearBooks()
clearProfile()
}
open suspend fun clear() = clearProfile()
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package com.flatstack.android.model.entities

import androidx.room.Entity
import androidx.room.PrimaryKey
import com.google.gson.annotations.SerializedName

@Entity(tableName = "session")
data class Session(
val accessToken: String,
@PrimaryKey(autoGenerate = true)
val id: Int,
@SerializedName("access_token")
val accessToken: String
val id: Int = 0
)

This file was deleted.

This file was deleted.

This file was deleted.

21 changes: 0 additions & 21 deletions app/src/main/java/com/flatstack/android/model/network/IApi.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.flatstack.android.model.network

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.apollographql.apollo.api.Response
import com.flatstack.android.model.entities.Resource
import com.flatstack.android.model.network.errors.ErrorHandler
import kotlinx.coroutines.CoroutineScope
Expand All @@ -10,8 +11,8 @@ import kotlinx.coroutines.launch
import kotlin.coroutines.CoroutineContext

abstract class NetworkBoundResource<ResultType, RequestType>(
override val coroutineContext: CoroutineContext,
private val errorHandler: ErrorHandler
override val coroutineContext: CoroutineContext,
private val errorHandler: ErrorHandler
) : CoroutineScope {

private val result = MutableLiveData<Resource<ResultType>>()
Expand All @@ -26,26 +27,26 @@ abstract class NetworkBoundResource<ResultType, RequestType>(
fun fetchFromNetwork() {
launch {
result.postValue(Resource.loading(loadFromDb()))
when (val apiResponse = createCallAsync().await()) {
is ApiSuccessResponse -> {
saveCallResult(processResponse(apiResponse))
result.postValue(Resource.success(loadFromDb()))
}
is ApiErrorResponse -> {
onFetchFailed()
result.postValue(Resource.error(errorHandler.proceed(apiResponse.error), loadFromDb()))
}
val apiResponse = createCallAsync().await()
if (!apiResponse.hasErrors()) {
saveCallResult(processResponse(apiResponse))
result.postValue(Resource.success(loadFromDb()))
} else {
onFetchFailed()
result.postValue(Resource.error(errorHandler.proceed(processErrorResponse(apiResponse)), loadFromDb()))
}
}
}

protected open fun onFetchFailed() {}

protected open fun processResponse(response: ApiSuccessResponse<RequestType>) = response.body
protected open fun processResponse(response: Response<RequestType>) = response.data

protected abstract suspend fun saveCallResult(item: RequestType)
protected open fun processErrorResponse(response: Response<RequestType>) = response.errors?.get(0)

protected abstract suspend fun saveCallResult(item: RequestType?)

protected abstract suspend fun loadFromDb(): ResultType?

protected abstract suspend fun createCallAsync(): Deferred<ApiResponse<RequestType>>
protected abstract suspend fun createCallAsync(): Deferred<Response<RequestType>>
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit 5b91a31

Please sign in to comment.