Skip to content

Commit

Permalink
Going back to Result (as KMMResult is no longer needed in iOS)
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine Robiez committed Mar 20, 2024
1 parent f128435 commit 4fa0a7a
Show file tree
Hide file tree
Showing 23 changed files with 37 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class AgendaLayoutViewModel(
val state: StateFlow<AgendaLayoutState> = combine(
roomsRepository.getRooms()
.map { rooms ->
rooms/*.recover { emptyList() }*/
rooms.recover { emptyList() }
.getOrThrow()
},
sessionFilters,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package fr.paug.androidmakers.ui.components.agenda

import at.asitplus.KmmResult
import com.androidmakers.ui.common.LceViewModel
import fr.androidmakers.domain.interactor.GetAgendaUseCase
import fr.androidmakers.domain.interactor.GetFavoriteSessionsUseCase
Expand All @@ -16,7 +15,7 @@ class AgendaPagerViewModel(
private val setSessionBookmarkUseCase: SetSessionBookmarkUseCase,
private val getFavoriteSessionsUseCase: GetFavoriteSessionsUseCase
) : LceViewModel<Agenda>() {
override fun produce(): Flow<KmmResult<Agenda>> {
override fun produce(): Flow<Result<Agenda>> {
return getAgendaUseCase()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class SessionDetailViewModel(

private suspend fun getSpeakers(session: Session): List<Speaker> {
val allSpeakers = speakersRepository.getSpeakers().firstOrNull()
//?.recover { emptyList() }
?.recover { emptyList() }
?.getOrThrow()
?: return emptyList()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package fr.androidmakers.store.graphql

import at.asitplus.KmmResult
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.map

internal fun <T> Flow<T>.toResultFlow(): Flow<KmmResult<T>> = this.map {
KmmResult.success(it)
internal fun <T> Flow<T>.toResultFlow(): Flow<Result<T>> = this.map {
Result.success(it)
}.catch {
emit(KmmResult.failure<T>(it))
emit(Result.failure(it))
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package fr.androidmakers.store.graphql

import at.asitplus.KmmResult
import com.apollographql.apollo3.ApolloClient
import com.apollographql.apollo3.cache.normalized.FetchPolicy
import com.apollographql.apollo3.cache.normalized.fetchPolicy
Expand All @@ -12,7 +11,7 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map

class PartnersGraphQLRepository(private val apolloClient: ApolloClient): PartnersRepository {
override fun getPartners(): Flow<KmmResult<List<PartnerGroup>>> {
override fun getPartners(): Flow<Result<List<PartnerGroup>>> {
return apolloClient.query(GetPartnerGroupsQuery())
.fetchPolicy(FetchPolicy.CacheAndNetwork)
.watch().map {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package fr.androidmakers.store.graphql

import at.asitplus.KmmResult
import com.apollographql.apollo3.ApolloClient
import com.apollographql.apollo3.cache.normalized.FetchPolicy
import com.apollographql.apollo3.cache.normalized.fetchPolicy
Expand All @@ -11,13 +10,13 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map

class RoomsGraphQLRepository(private val apolloClient: ApolloClient): RoomsRepository {
override fun getRoom(id: String): Flow<KmmResult<Room>> {
override fun getRoom(id: String): Flow<Result<Room>> {
return getRooms().map {
it.map { it.singleOrNull { it.id == id } ?: error("Not Room") }
}
}

override fun getRooms(): Flow<KmmResult<List<Room>>> {
override fun getRooms(): Flow<Result<List<Room>>> {
return apolloClient.query(GetRoomsQuery())
.fetchPolicy(FetchPolicy.NetworkFirst)
.watch()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package fr.androidmakers.store.graphql

import at.asitplus.KmmResult
import com.apollographql.apollo3.ApolloClient
import com.apollographql.apollo3.api.Mutation
import com.apollographql.apollo3.cache.normalized.FetchPolicy
Expand Down Expand Up @@ -51,7 +50,7 @@ class SessionsGraphQLRepository(private val apolloClient: ApolloClient): Session
}
}

override fun getSession(id: String): Flow<KmmResult<Session>> {
override fun getSession(id: String): Flow<Result<Session>> {
return apolloClient.query(GetSessionQuery(id))
.fetchPolicy(FetchPolicy.CacheAndNetwork)
.watch().map {
Expand All @@ -60,7 +59,7 @@ class SessionsGraphQLRepository(private val apolloClient: ApolloClient): Session
.toResultFlow()
}

override fun getBookmarks(uid: String): Flow<KmmResult<Set<String>>> {
override fun getBookmarks(uid: String): Flow<Result<Set<String>>> {
return apolloClient.query(BookmarksQuery())
.fetchPolicy(FetchPolicy.NetworkOnly)
.refetchPolicy(FetchPolicy.CacheOnly)
Expand Down Expand Up @@ -91,7 +90,7 @@ class SessionsGraphQLRepository(private val apolloClient: ApolloClient): Session
return response.data != null
}

override fun getSessions(): Flow<KmmResult<List<Session>>> {
override fun getSessions(): Flow<Result<List<Session>>> {
return apolloClient.query(GetSessionsQuery())
.fetchPolicy(FetchPolicy.CacheAndNetwork)
.watch()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package fr.androidmakers.store.graphql

import at.asitplus.KmmResult
import com.apollographql.apollo3.ApolloClient
import com.apollographql.apollo3.cache.normalized.FetchPolicy
import com.apollographql.apollo3.cache.normalized.fetchPolicy
Expand All @@ -12,13 +11,13 @@ import kotlinx.coroutines.flow.map

class SpeakersGraphQLRepository(private val apolloClient: ApolloClient): SpeakersRepository {

override fun getSpeakers(): Flow<KmmResult<List<Speaker>>> {
override fun getSpeakers(): Flow<Result<List<Speaker>>> {
return apolloClient.query(GetSpeakersQuery()).watch().map {
it.dataAssertNoErrors.speakers.map { it.speakerDetails.toSpeaker() }
}.toResultFlow()
}

override fun getSpeaker(id: String): Flow<KmmResult<Speaker>> {
override fun getSpeaker(id: String): Flow<Result<Speaker>> {
return apolloClient.query(GetSpeakersQuery())
.fetchPolicy(FetchPolicy.CacheAndNetwork)
.watch().map {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package fr.androidmakers.store.graphql

import at.asitplus.KmmResult
import com.apollographql.apollo3.ApolloClient
import com.apollographql.apollo3.cache.normalized.FetchPolicy
import com.apollographql.apollo3.cache.normalized.fetchPolicy
Expand All @@ -11,7 +10,7 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map

class VenueGraphQLRepository(private val apolloClient: ApolloClient): VenueRepository {
override fun getVenue(id: String): Flow<KmmResult<Venue>> {
override fun getVenue(id: String): Flow<Result<Venue>> {
return apolloClient.query(GetVenueQuery(id))
.fetchPolicy(FetchPolicy.CacheAndNetwork)
.watch().map {
Expand Down
4 changes: 0 additions & 4 deletions shared/domain/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ kotlin {
commonMain.dependencies {
implementation(libs.kotlinx.coroutines.core)
api(libs.kotlinx.datetime)

// Temporary dependency to handle the fact that swift doesnt support Kotlin Result
// It will be removed when we will merge the viewmodels or go for Compose MP
api("at.asitplus:kmmresult:1.5.4")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package fr.androidmakers.domain.interactor

import at.asitplus.KmmResult
import fr.androidmakers.domain.model.Agenda
import fr.androidmakers.domain.repo.RoomsRepository
import fr.androidmakers.domain.repo.SessionsRepository
Expand All @@ -13,7 +12,7 @@ class GetAgendaUseCase(
private val speakersRepository: SpeakersRepository,
private val roomsRepository: RoomsRepository,
) {
operator fun invoke(): Flow<KmmResult<Agenda>> {
operator fun invoke(): Flow<Result<Agenda>> {
return combine(
sessionsRepository.getSessions(),
roomsRepository.getRooms(),
Expand All @@ -22,18 +21,18 @@ class GetAgendaUseCase(

sessions.exceptionOrNull()?.let {
it.printStackTrace()
return@combine KmmResult.failure(it)
return@combine Result.failure(it)
}
rooms.exceptionOrNull()?.let {
it.printStackTrace()
return@combine KmmResult.failure(it)
return@combine Result.failure(it)
}
speakers.exceptionOrNull()?.let {
it.printStackTrace()
return@combine KmmResult.failure(it)
return@combine Result.failure(it)
}

KmmResult.success(
Result.success(
Agenda(
sessions = sessions.getOrThrow().associateBy { it.id },
rooms = rooms.getOrThrow().associateBy { it.id },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package fr.androidmakers.domain.repo

import at.asitplus.KmmResult
import fr.androidmakers.domain.model.PartnerGroup
import kotlinx.coroutines.flow.Flow

interface PartnersRepository {
fun getPartners(): Flow<KmmResult<List<PartnerGroup>>>
fun getPartners(): Flow<Result<List<PartnerGroup>>>
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package fr.androidmakers.domain.repo

import at.asitplus.KmmResult
import fr.androidmakers.domain.model.Room
import kotlinx.coroutines.flow.Flow

interface RoomsRepository {

fun getRoom(id: String): Flow<KmmResult<Room>>
fun getRoom(id: String): Flow<Result<Room>>

fun getRooms(): Flow<KmmResult<List<Room>>>
fun getRooms(): Flow<Result<List<Room>>>
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package fr.androidmakers.domain.repo

import at.asitplus.KmmResult
import fr.androidmakers.domain.model.Session
import kotlinx.coroutines.flow.Flow

interface SessionsRepository {
fun getSession(id: String): Flow<KmmResult<Session>>
fun getSession(id: String): Flow<Result<Session>>

fun getSessions(): Flow<KmmResult<List<Session>>>
fun getSessions(): Flow<Result<List<Session>>>

fun getBookmarks(userId: String): Flow<KmmResult<Set<String>>>
fun getBookmarks(userId: String): Flow<Result<Set<String>>>

suspend fun setBookmark(userId: String, sessionId: String, value: Boolean)
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package fr.androidmakers.domain.repo

import at.asitplus.KmmResult
import fr.androidmakers.domain.model.Speaker
import kotlinx.coroutines.flow.Flow

interface SpeakersRepository {

fun getSpeaker(id: String): Flow<KmmResult<Speaker>>
fun getSpeaker(id: String): Flow<Result<Speaker>>

fun getSpeakers(): Flow<KmmResult<List<Speaker>>>
fun getSpeakers(): Flow<Result<List<Speaker>>>
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package fr.androidmakers.domain.repo

import at.asitplus.KmmResult
import fr.androidmakers.domain.model.Venue
import kotlinx.coroutines.flow.Flow

interface VenueRepository {
fun getVenue(id: String): Flow<KmmResult<Venue>>
fun getVenue(id: String): Flow<Result<Venue>>
}
3 changes: 0 additions & 3 deletions shared/src/commonMain/kotlin/TestCla.kt

This file was deleted.

5 changes: 5 additions & 0 deletions shared/src/commonMain/kotlin/TestClass.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class TestClass {
// TODO find a solution to remove this class
// It is necessary to build the iOS Framework
// Maybe by configuring src sets on iOS?
}
1 change: 0 additions & 1 deletion shared/ui/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ kotlin {
implementation(compose.ui)
implementation(compose.components.resources)
implementation(compose.components.uiToolingPreview)
api("at.asitplus:kmmresult:1.5.4")
implementation(project(":shared:domain"))
implementation(project(":shared:di"))
api("io.github.qdsfdhvh:image-loader:1.7.8")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.androidmakers.ui.common

import at.asitplus.KmmResult
import com.androidmakers.ui.model.Lce
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.Flow
Expand All @@ -14,7 +13,7 @@ import moe.tlaster.precompose.viewmodel.viewModelScope


abstract class LceViewModel<T> : ViewModel() {
abstract fun produce(): Flow<KmmResult<T>>
abstract fun produce(): Flow<Result<T>>

private val _mutableSharedState = MutableStateFlow<Lce<T>>(Lce.Loading)
val values = _mutableSharedState.asStateFlow()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.androidmakers.ui.model

import at.asitplus.KmmResult

sealed interface Lce<out T> {
object Loading : Lce<Nothing>
class Content<T>(val content: T) : Lce<T>
object Error : Lce<Nothing>
}

fun <T> KmmResult<T>.toLce(): Lce<T> = if (isSuccess) {
fun <T> Result<T>.toLce(): Lce<T> = if (isSuccess) {
Lce.Content(getOrThrow())
} else {
Lce.Error
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.androidmakers.ui.sponsors

import at.asitplus.KmmResult
import com.androidmakers.ui.common.LceViewModel
import fr.androidmakers.domain.interactor.GetPartnersUseCase
import fr.androidmakers.domain.model.PartnerGroup
Expand All @@ -9,7 +8,7 @@ import kotlinx.coroutines.flow.Flow
class SponsorsViewModel(
private val getPartnersUseCase: GetPartnersUseCase
) : LceViewModel<List<PartnerGroup>>() {
override fun produce(): Flow<KmmResult<List<PartnerGroup>>> {
override fun produce(): Flow<Result<List<PartnerGroup>>> {
return getPartnersUseCase()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package com.androidmakers.ui.sponsors

import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.window.ComposeUIViewController
import at.asitplus.KmmResult
import com.androidmakers.ui.model.Lce
import com.androidmakers.ui.theme.AndroidMakersTheme
import fr.androidmakers.di.DepContainer
import platform.UIKit.UIViewController
Expand Down

0 comments on commit 4fa0a7a

Please sign in to comment.