Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ allprojects {
excludeGroup("Kotlin/Native")
}
}
maven {
url 'https://oss.jfrog.org/artifactory/oss-snapshot-local'
content {
excludeGroup("Kotlin/Native")
}
}
}
tasks.matching { it instanceof Test }.all {
testLogging {
Expand Down
8 changes: 8 additions & 0 deletions buildSrc/src/main/java/dependencies/Dep.kt
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ object Dep {
val serializationIos = "org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:0.10.0"
}

object Arrow {
val version = "0.9.0-SNAPSHOT"
val typeclasses = "io.arrow-kt:arrow-typeclasses:$version"
val data = "io.arrow-kt:arrow-effects-data:$version"
val extensions = "io.arrow-kt:arrow-effects-extensions:$version"
val ioExtensions = "io.arrow-kt:arrow-effects-io-extensions:$version"
}

object Firebase {
val core = "com.google.firebase:firebase-core:16.0.4"
val fireStore = "com.google.firebase:firebase-firestore:17.1.3"
Expand Down
5 changes: 5 additions & 0 deletions feature/session/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ dependencies {
api Dep.Kotlin.coroutines
implementation Dep.Kotlin.androidCoroutinesDispatcher

implementation Dep.Arrow.extensions
api Dep.Arrow.data
implementation Dep.Arrow.typeclasses
implementation Dep.Arrow.ioExtensions

implementation Dep.AndroidX.appCompat
implementation Dep.AndroidX.fragment
implementation Dep.AndroidX.constraint
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import io.github.droidkaigi.confsched2019.session.ui.widget.DaggerFragment
import io.github.droidkaigi.confsched2019.user.store.UserStore
import io.github.droidkaigi.confsched2019.util.ProgressTimeLatch
import me.tatarka.injectedvmprovider.InjectedViewModelProviders
import timber.log.Timber
import timber.log.debug
import javax.inject.Inject
import javax.inject.Provider

Expand Down Expand Up @@ -87,6 +89,7 @@ class SessionPagesFragment : DaggerFragment() {
sessionPagesActionCreator.load(sessionContentsStore.sessions)
}
sessionContentsStore.loadingState.changed(viewLifecycleOwner) { loadingState ->
Timber.debug { "Fragment:$loadingState" }
progressTimeLatch.loading = loadingState.isLoading
}
sessionPagesStore.sessionScrollAdjusted.observe(viewLifecycleOwner) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
package io.github.droidkaigi.confsched2019.session.ui.actioncreator

import androidx.lifecycle.Lifecycle
import arrow.Kind
import arrow.core.Either
import arrow.core.getOrHandle
import arrow.core.left
import arrow.core.right
import arrow.core.success
import arrow.effects.ForIO
import arrow.effects.IO
import arrow.effects.extensions.io.async.async
import arrow.effects.extensions.io.effect.runAsync
import arrow.effects.extensions.io.fx.fx
import arrow.effects.extensions.io.unsafeRun.runNonBlocking
import arrow.effects.fix
import arrow.unsafe
import io.github.droidkaigi.confsched2019.action.Action
import io.github.droidkaigi.confsched2019.data.repository.SessionRepository
import io.github.droidkaigi.confsched2019.di.PageScope
Expand All @@ -13,6 +27,7 @@ import io.github.droidkaigi.confsched2019.session.R
import io.github.droidkaigi.confsched2019.system.actioncreator.ErrorHandler
import io.github.droidkaigi.confsched2019.util.SessionAlarm
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.InternalCoroutinesApi
import kotlinx.coroutines.launch
import timber.log.Timber
import timber.log.debug
Expand Down Expand Up @@ -66,27 +81,48 @@ class SessionContentsActionCreator @Inject constructor(
}

fun toggleFavorite(session: Session) {
launch {
try {
fx {
!effect {
Copy link
Contributor

@jmatsu jmatsu Feb 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the purpose to use fx? 🤔
AFAIK, fx forces to represent side-effects by using suspend functions through compilation errors.

If you want to just integrate with coroutine, then arrow supports coroutine extension with DefferedK

val defferedK = async {
...
LoadingState.LOADED
}.k().handleErrorWith {
   dispatcher.dispatch(
                        Action.ShowProcessingMessage(
                            Message.of(
                                R.string.session_favorite_connection_error
                            )
                        )
                    )
  LoadingState.INITIALIZED
 }

dispatcher.dispatch(defferedK.unsafeAttemptSync())

maybe be like above.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

@takahirom takahirom Feb 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Probably .k and DeferredK will be removed. 😇

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After diving into arrow fx, I found a cause.

The current fx is for Try monad and it doesn't take care of asynchronous operations.

In FP world, IO monad is what you need to use.

  • Add io.arrow-kt:arrow-effects-io-extensions dependency
  • Import arrow.effects.extensions.io.fx.fx correctly

Then, follow the style below.

fun toggleFavorite(...) {
        unsafe {
            runNonBlocking({
                fx {
                    ....
                }
            }, { // Either monad will be coming
            })
        }
}

dispatcher.dispatchLoadingState(LoadingState.LOADING)
sessionRepository.toggleFavorite(session)
sessionAlarm.toggleRegister(session)
val sessionContents = sessionRepository.sessionContents()
dispatcher.dispatch(Action.SessionContentsLoaded(sessionContents))
dispatcher.dispatchLoadingState(LoadingState.LOADED)
} catch (e: Exception) {
dispatcher.dispatch(
LoadingState.LOADED
}
}.runAndHold { result ->
val state = result.getOrHandle {
dispatcher.launchAndDispatch(
Action.ShowProcessingMessage(
Message.of(
R.string.session_favorite_connection_error
)
)
)
LoadingState.INITIALIZED
}
dispatcher.launchAndDispatchLoadingState(state)
}
}

@UseExperimental(InternalCoroutinesApi::class)
private fun <A> Kind<ForIO, A>.runAndHold(result: (Either<Throwable, A>) -> Unit) {
IO.async().run {
defer(coroutineContext) {
this@runAndHold
}
}.fix().attempt().unsafeRunAsync {
result.invoke(it.getOrHandle { it.left() })
}
}

private suspend fun Dispatcher.dispatchLoadingState(loadingState: LoadingState) {
println("Fragment: Dispatch $loadingState")
dispatch(Action.SessionLoadingStateChanged(loadingState))
}

private fun Dispatcher.launchAndDispatchLoadingState(loadingState: LoadingState) {
println("Fragment: Dispatch $loadingState")
launchAndDispatch(Action.SessionLoadingStateChanged(loadingState))
}
}