Add IBookRepository
This commit is contained in:
parent
6a0710572a
commit
b89bbf69a9
10 changed files with 145 additions and 5 deletions
|
|
@ -7,6 +7,9 @@ interface IInitialisation {
|
|||
@Composable
|
||||
fun LoadApplication(content: @Composable (State) -> Unit)
|
||||
|
||||
@Composable
|
||||
fun LoadBook(content: @Composable (State) -> Unit)
|
||||
|
||||
sealed class State {
|
||||
object Initial : State()
|
||||
object Loading : State()
|
||||
|
|
@ -16,5 +19,7 @@ interface IInitialisation {
|
|||
class Mock(private val state: State = State.Loading) : IInitialisation {
|
||||
@Composable
|
||||
override fun LoadApplication(content: (State) -> Unit) = content(state)
|
||||
@Composable
|
||||
override fun LoadBook(content: (State) -> Unit) = content(state)
|
||||
}
|
||||
}
|
||||
|
|
@ -4,7 +4,9 @@ import androidx.compose.runtime.*
|
|||
import androidx.lifecycle.ViewModel
|
||||
import com.pixelized.biblib.network.client.IBibLibClient
|
||||
import com.pixelized.biblib.network.data.query.AuthLoginQuery
|
||||
import com.pixelized.biblib.network.factory.BookFactory
|
||||
import com.pixelized.biblib.repository.apiCache.IAPICacheRepository
|
||||
import com.pixelized.biblib.repository.book.IBookRepository
|
||||
import com.pixelized.biblib.repository.credential.ICredentialRepository
|
||||
import com.pixelized.biblib.repository.googleSignIn.IGoogleSingInRepository
|
||||
import com.pixelized.biblib.ui.viewmodel.initialisation.IInitialisation.State.*
|
||||
|
|
@ -13,6 +15,7 @@ import kotlinx.coroutines.delay
|
|||
|
||||
class InitialisationViewModel : ViewModel(), IInitialisation {
|
||||
private val credentialRepository: ICredentialRepository by inject()
|
||||
private val bookRepository: IBookRepository by inject()
|
||||
private val googleSignIn: IGoogleSingInRepository by inject()
|
||||
private val client: IBibLibClient by inject()
|
||||
private val apiCache: IAPICacheRepository by inject()
|
||||
|
|
@ -35,6 +38,17 @@ class InitialisationViewModel : ViewModel(), IInitialisation {
|
|||
content(state.value)
|
||||
}
|
||||
|
||||
@Composable
|
||||
override fun LoadBook(content: (IInitialisation.State) -> Unit) {
|
||||
val state: MutableState<IInitialisation.State> = remember { mutableStateOf(Initial) }
|
||||
LaunchedEffect(key1 = "LoadBook") {
|
||||
state.value = Loading
|
||||
loadNewBooks() && loadAllBooks()
|
||||
state.value = Finished(needLogin = false)
|
||||
}
|
||||
content(state.value)
|
||||
}
|
||||
|
||||
private suspend fun loginWithGoogle(): Boolean {
|
||||
val googleToken = googleSignIn.lastGoogleToken
|
||||
return if (googleToken != null) {
|
||||
|
|
@ -89,7 +103,14 @@ class InitialisationViewModel : ViewModel(), IInitialisation {
|
|||
}
|
||||
|
||||
private suspend fun loadAllBooks(): Boolean {
|
||||
apiCache.list = client.service.list()
|
||||
client.service.list().let { response ->
|
||||
// TODO: useless isn't it ?
|
||||
apiCache.list = response
|
||||
|
||||
val factory = BookFactory()
|
||||
val books = response.data?.map { dto -> factory.fromListResponseToBook(dto, false) }
|
||||
books?.let { data -> bookRepository.update(data) }
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue