Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Introduce Arrow Fx #821

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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 coroutines = "io.arrow-kt:arrow-effects-kotlinx-coroutines:$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
implementation Dep.Arrow.data
implementation Dep.Arrow.typeclasses
implementation Dep.Arrow.coroutines

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,7 @@
package io.github.droidkaigi.confsched2019.session.ui.actioncreator

import androidx.lifecycle.Lifecycle
import arrow.core.extensions.`try`.monad.fx
import io.github.droidkaigi.confsched2019.action.Action
import io.github.droidkaigi.confsched2019.data.repository.SessionRepository
import io.github.droidkaigi.confsched2019.di.PageScope
Expand Down Expand Up @@ -66,27 +67,39 @@ 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(
Action.ShowProcessingMessage(
Message.of(
R.string.session_favorite_connection_error
)
)
)
}
}
.fold(
ifSuccess = {
dispatcher.launchAndDispatchLoadingState(LoadingState.LOADED)
},
ifFailure = {
dispatcher.launchAndDispatch(
Action.ShowProcessingMessage(
Message.of(
R.string.session_favorite_connection_error
)
)
)
dispatcher.launchAndDispatchLoadingState(LoadingState.INITIALIZED)
}
)
}

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))
}
}