Skip to content

Commit

Permalink
Added serialization / deserialization ability for AppScreen
Browse files Browse the repository at this point in the history
  • Loading branch information
Starksoft committed Feb 17, 2021
1 parent 4c674c2 commit 59f5470
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
Expand Up @@ -6,10 +6,11 @@ import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentFactory
import com.github.terrakok.cicerone.Screen
import java.io.Serializable

sealed class AppScreen : Screen
sealed class AppScreen : Screen, Serializable

fun interface Creator<A, R> {
fun interface Creator<A, R>: Serializable {
fun create(argument: A): R
}

Expand All @@ -28,4 +29,4 @@ open class ActivityScreen @JvmOverloads constructor(
override val screenKey: String get() = key ?: super.screenKey
open val startActivityOptions: Bundle? = null
fun createIntent(context: Context) = intentCreator.create(context)
}
}
Expand Up @@ -28,8 +28,11 @@ object Screens {
Intent(it, StartActivity::class.java)
}

fun Main() = ActivityScreen {
Intent(it, MainActivity::class.java)
// Consider to use val and upper-case naming
val MAIN = ActivityScreen {
Intent(it, MainActivity::class.java).apply {
putExtra("sampleScreen", Sample(1))
}
}

fun BottomNavigation() = ActivityScreen {
Expand Down Expand Up @@ -59,4 +62,4 @@ object Screens {
fun SelectPhoto(resultKey: String) = FragmentScreen {
SelectPhotoFragment.getNewInstance(resultKey)
}
}
}
@@ -1,8 +1,8 @@
package com.github.terrakok.cicerone.sample.mvp.start

import com.github.terrakok.cicerone.Router
import com.github.terrakok.cicerone.sample.Screens
import com.github.terrakok.cicerone.sample.Screens.BottomNavigation
import com.github.terrakok.cicerone.sample.Screens.Main
import com.github.terrakok.cicerone.sample.Screens.Profile
import moxy.MvpPresenter

Expand All @@ -12,7 +12,7 @@ import moxy.MvpPresenter
class StartActivityPresenter(private val router: Router) : MvpPresenter<StartActivityView>() {

fun onOrdinaryPressed() {
router.navigateTo(Main())
router.navigateTo(Screens.MAIN)
}

fun onMultiPressed() {
Expand All @@ -27,4 +27,4 @@ class StartActivityPresenter(private val router: Router) : MvpPresenter<StartAct
router.exit()
}

}
}
Expand Up @@ -8,7 +8,9 @@ import com.github.terrakok.cicerone.Command
import com.github.terrakok.cicerone.Navigator
import com.github.terrakok.cicerone.NavigatorHolder
import com.github.terrakok.cicerone.Replace
import com.github.terrakok.cicerone.Router
import com.github.terrakok.cicerone.androidx.AppNavigator
import com.github.terrakok.cicerone.androidx.FragmentScreen
import com.github.terrakok.cicerone.sample.R
import com.github.terrakok.cicerone.sample.SampleApplication
import com.github.terrakok.cicerone.sample.Screens.Sample
Expand All @@ -31,6 +33,9 @@ class MainActivity : MvpAppCompatActivity(), ChainHolder {
@Inject
lateinit var navigatorHolder: NavigatorHolder

@Inject
lateinit var router: Router

private val navigator: Navigator = object : AppNavigator(this, R.id.main_container) {

override fun applyCommands(commands: Array<out Command>) {
Expand All @@ -48,7 +53,9 @@ class MainActivity : MvpAppCompatActivity(), ChainHolder {
screensSchemeTV = findViewById<View>(R.id.screens_scheme) as TextView

if (savedInstanceState == null) {
navigator.applyCommands(arrayOf<Command>(Replace(Sample(1))))
// Just for sample of deserialization
val sampleScreen = intent.getSerializableExtra("sampleScreen") as FragmentScreen
router.replaceScreen(sampleScreen)
} else {
printScreensScheme()
}
Expand Down Expand Up @@ -92,4 +99,4 @@ class MainActivity : MvpAppCompatActivity(), ChainHolder {
}
screensSchemeTV.text = "Chain: $keys"
}
}
}

0 comments on commit 59f5470

Please sign in to comment.