Skip to content

Commit

Permalink
Enable support for (experimental) data object in samples instead of…
Browse files Browse the repository at this point in the history
… empty placeholders
  • Loading branch information
Zhuinden committed Oct 20, 2022
1 parent bc4713d commit 1369acb
Show file tree
Hide file tree
Showing 64 changed files with 143 additions and 111 deletions.
44 changes: 36 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,39 @@ class MainActivity : AppCompatActivity(), SimpleStateChanger.NavigationHandler {
}
```

Where `FirstScreen` looks like this:
Where `FirstScreen` looks like this (assuming you have `data object` enabled):

```groovy
kotlinOptions {
jvmTarget = "1.8"
languageVersion = '1.8' // data objects
}
```

```kotlin
// no args
@Parcelize
data object FirstScreen : DefaultFragmentKey() {
override fun instantiateFragment(): Fragment = FirstFragment()
}
```

If you don't have `data object` support yet, then no-args keys look like this (to ensure stable
hashCode/equals/toString):

``` kotlin
@Parcelize // typically data class
class FirstScreen: DefaultFragmentKey() {
// no args
@Parcelize
data class FirstScreen(private val noArgsPlaceholder: String = ""): DefaultFragmentKey() {
override fun instantiateFragment(): Fragment = FirstFragment()
}

// has args
@Parcelize
data class FirstScreen(
val username: String,
val password: String,
): DefaultFragmentKey() {
override fun instantiateFragment(): Fragment = FirstFragment()
}
```
Expand Down Expand Up @@ -212,10 +240,10 @@ And then:

``` kotlin
@Parcelize // typically data class
class FirstScreen: DefaultFragmentKey(), DefaultServiceProvider.HasServices {
data object FirstScreen: DefaultFragmentKey(), DefaultServiceProvider.HasServices {
override fun instantiateFragment(): Fragment = FirstFragment()

override fun getScopeTag() = javaClass.name
override fun getScopeTag() = toString()

override fun bindServices(serviceBinder: ServiceBinder) {
with(serviceBinder) {
Expand Down Expand Up @@ -257,15 +285,15 @@ Using `Backstack` to navigate allows you to move navigation responsibilities out
class FirstScopedModel(private val backstack: Backstack) {
fun doSomething() {
// ...
backstack.goTo(SecondScreen())
backstack.goTo(SecondScreen)
}
}
```

Another additional benefit is that your navigation history can be unit tested.

``` java
assertThat(backstack.getHistory()).containsExactly(SomeScreen(), OtherScreen())
assertThat(backstack.getHistory()).containsExactly(SomeScreen, OtherScreen)
```

And most importantly, navigation (swapping screens) happens in one place, and you are in direct control of what happens in such a scenario. By writing a `StateChanger`, you can set up "how to display my current navigation state" in any way you want. No more `((MainActivity)getActivity()).setTitleText("blah");` inside Fragment's `onStart()`.
Expand Down Expand Up @@ -303,7 +331,7 @@ For Fragment + Simple-Stack + Compose integration, you can also check [the corre

## License

Copyright 2017-2021 Gabor Varadi
Copyright 2017-2022 Gabor Varadi

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ buildscript {
classpath("me.tatarka:gradle-retrolambda:3.7.0")
classpath("io.realm:realm-gradle-plugin:10.12.0")
classpath("io.realm:realm-transformer:10.12.0")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.20")
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,27 @@ android {

kotlinOptions {
jvmTarget = "1.8"
languageVersion = '1.8' // data objects
}

buildFeatures {
viewBinding = true

compose = true
}

composeOptions {
kotlinCompilerExtensionVersion compose_version
kotlinCompilerExtensionVersion "1.3.2"
}
packagingOptions {
resources {
excludes += ['META-INF/DEPENDENCIES', 'META-INF/LICENSE', 'META-INF/LICENSE.txt', 'META-INF/license.txt', 'META-INF/NOTICE', 'META-INF/NOTICE.txt', 'META-INF/notice.txt', 'META-INF/ASL2.0']
pickFirsts += ['META-INF/core-ktx_release.kotlin_module']
}
}

}


dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class MainActivity : AppCompatActivity(), SimpleStateChanger.NavigationHandler {
this, androidContentFrame, History.of(
when {
authenticationManager.isAuthenticated() -> ProfileKey(authenticationManager.getAuthenticatedUser())
else -> LoginKey()
else -> LoginKey
}
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.zhuinden.simplestackextensionscomposesample.app.FragmentKey
import kotlinx.parcelize.Parcelize

@Parcelize
data class LoginKey(private val noArgsPlaceholder: String = "") : FragmentKey() {
data object LoginKey : FragmentKey() {
@Suppress("RemoveExplicitTypeArguments")
override fun bindServices(serviceBinder: ServiceBinder) {
with(serviceBinder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ package com.zhuinden.simplestackextensionscomposesample.features.login

import com.jakewharton.rxrelay2.BehaviorRelay
import com.zhuinden.rxvalidatebykt.validateBy
import com.zhuinden.simplestack.Backstack
import com.zhuinden.simplestack.Bundleable
import com.zhuinden.simplestack.History
import com.zhuinden.simplestack.ScopedServices
import com.zhuinden.simplestack.StateChange
import com.zhuinden.simplestack.*
import com.zhuinden.simplestackextensionscomposesample.app.AuthenticationManager
import com.zhuinden.simplestackextensionscomposesample.features.profile.ProfileKey
import com.zhuinden.simplestackextensionscomposesample.features.registration.EnterProfileDataKey
Expand Down Expand Up @@ -54,7 +50,7 @@ class LoginViewModel(
}

fun onRegisterClicked() {
backstack.goTo(EnterProfileDataKey())
backstack.goTo(EnterProfileDataKey)
}

override fun toBundle(): StateBundle = StateBundle().apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ProfileViewModel(
) : ScopedServices.Activated {
override fun onServiceActive() {
if (!authenticationManager.isAuthenticated()) {
backstack.setHistory(History.of(LoginKey()), StateChange.REPLACE)
backstack.setHistory(History.of(LoginKey), StateChange.REPLACE)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import com.zhuinden.simplestackextensionscomposesample.app.FragmentKey
import kotlinx.parcelize.Parcelize

@Parcelize
data class CreateLoginCredentialsKey(
private val noArgsPlaceholder: String = "",
) : FragmentKey(), ScopeKey.Child {
data object CreateLoginCredentialsKey : FragmentKey(), ScopeKey.Child {
override fun getParentScopes(): List<String> = listOf(RegistrationViewModel::class.java.name)

override fun instantiateFragment(): Fragment = CreateLoginCredentialsFragment()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import com.zhuinden.simplestackextensionscomposesample.app.FragmentKey
import kotlinx.parcelize.Parcelize

@Parcelize
data class EnterProfileDataKey(
private val noArgsPlaceholder: String = "",
) : FragmentKey(), ScopeKey.Child {
data object EnterProfileDataKey : FragmentKey(), ScopeKey.Child {
override fun getParentScopes(): List<String> = listOf(RegistrationViewModel::class.java.name)

override fun instantiateFragment(): Fragment = EnterProfileDataFragment()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ package com.zhuinden.simplestackextensionscomposesample.features.registration

import com.jakewharton.rxrelay2.BehaviorRelay
import com.zhuinden.rxvalidatebykt.validateBy
import com.zhuinden.simplestack.Backstack
import com.zhuinden.simplestack.Bundleable
import com.zhuinden.simplestack.History
import com.zhuinden.simplestack.ScopedServices
import com.zhuinden.simplestack.StateChange
import com.zhuinden.simplestack.*
import com.zhuinden.simplestackextensionscomposesample.app.AuthenticationManager
import com.zhuinden.simplestackextensionscomposesample.features.profile.ProfileKey
import com.zhuinden.simplestackextensionscomposesample.utils.get
Expand Down Expand Up @@ -65,7 +61,7 @@ class RegistrationViewModel(

fun onEnterProfileNextClicked() {
if (isEnterProfileNextEnabledRelay.get()) {
backstack.goTo(CreateLoginCredentialsKey())
backstack.goTo(CreateLoginCredentialsKey)
}
}

Expand Down
1 change: 1 addition & 0 deletions samples/advanced-samples/extensions-example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ android {

kotlinOptions {
jvmTarget = "1.8"
languageVersion = '1.8' // data objects
}

buildFeatures {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class MainActivity : AppCompatActivity(), SimpleStateChanger.NavigationHandler {
this, binding.step9Root, History.of(
when {
authenticationManager.isAuthenticated() -> ProfileKey(authenticationManager.getAuthenticatedUser())
else -> LoginKey()
else -> LoginKey
}
))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.zhuinden.simplestackextensions.servicesktx.lookup
import kotlinx.parcelize.Parcelize

@Parcelize
data class LoginKey(private val placeholder: String = "") : DefaultFragmentKey(), DefaultServiceProvider.HasServices {
data object LoginKey : DefaultFragmentKey(), DefaultServiceProvider.HasServices {
override fun bindServices(serviceBinder: ServiceBinder) {
with(serviceBinder) {
add(LoginViewModel(lookup(), backstack))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ package com.zhuinden.simplestackextensionsample.features.login

import com.jakewharton.rxrelay2.BehaviorRelay
import com.zhuinden.rxvalidatebykt.validateBy
import com.zhuinden.simplestack.Backstack
import com.zhuinden.simplestack.Bundleable
import com.zhuinden.simplestack.History
import com.zhuinden.simplestack.ScopedServices
import com.zhuinden.simplestack.StateChange
import com.zhuinden.simplestack.*
import com.zhuinden.simplestackextensionsample.app.AuthenticationManager
import com.zhuinden.simplestackextensionsample.features.profile.ProfileKey
import com.zhuinden.simplestackextensionsample.features.registration.EnterProfileDataKey
Expand Down Expand Up @@ -50,7 +46,7 @@ class LoginViewModel(
}

fun onRegisterClicked() {
backstack.goTo(EnterProfileDataKey())
backstack.goTo(EnterProfileDataKey)
}

override fun toBundle(): StateBundle = StateBundle().apply {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.zhuinden.simplestackextensionsample.features.profile

import android.content.Context
import com.zhuinden.simplestack.Backstack
import com.zhuinden.simplestack.History
import com.zhuinden.simplestack.ScopedServices
Expand All @@ -14,7 +13,7 @@ class ProfileViewModel(
) : ScopedServices.Activated {
override fun onServiceActive() {
if (!authenticationManager.isAuthenticated()) {
backstack.setHistory(History.of(LoginKey()), StateChange.REPLACE)
backstack.setHistory(History.of(LoginKey), StateChange.REPLACE)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.zhuinden.simplestackextensions.fragments.DefaultFragmentKey
import kotlinx.parcelize.Parcelize

@Parcelize
data class CreateLoginCredentialsKey(private val placeholder: String = "") : DefaultFragmentKey(), ScopeKey.Child {
data object CreateLoginCredentialsKey : DefaultFragmentKey(), ScopeKey.Child {
override fun instantiateFragment(): Fragment = CreateLoginCredentialsFragment()

override fun getParentScopes(): List<String> = listOf(RegistrationViewModel::class.java.name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.zhuinden.simplestackextensions.fragments.DefaultFragmentKey
import kotlinx.parcelize.Parcelize

@Parcelize
data class EnterProfileDataKey(private val placeholder: String = "") : DefaultFragmentKey(), ScopeKey.Child {
data object EnterProfileDataKey : DefaultFragmentKey(), ScopeKey.Child {
override fun instantiateFragment(): Fragment = EnterProfileDataFragment()

override fun getParentScopes(): List<String> = listOf(RegistrationViewModel::class.java.name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ package com.zhuinden.simplestackextensionsample.features.registration

import com.jakewharton.rxrelay2.BehaviorRelay
import com.zhuinden.rxvalidatebykt.validateBy
import com.zhuinden.simplestack.Backstack
import com.zhuinden.simplestack.Bundleable
import com.zhuinden.simplestack.History
import com.zhuinden.simplestack.ScopedServices
import com.zhuinden.simplestack.StateChange
import com.zhuinden.simplestack.*
import com.zhuinden.simplestackextensionsample.app.AuthenticationManager
import com.zhuinden.simplestackextensionsample.features.profile.ProfileKey
import com.zhuinden.simplestackextensionsample.utils.get
Expand Down Expand Up @@ -63,7 +59,7 @@ class RegistrationViewModel(

fun onEnterProfileNextClicked() {
if (isEnterProfileNextEnabledRelay.get()) {
backstack.goTo(CreateLoginCredentialsKey())
backstack.goTo(CreateLoginCredentialsKey)
}
}

Expand Down
1 change: 1 addition & 0 deletions samples/advanced-samples/mvvm-sample/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ android {

kotlinOptions {
jvmTarget = "1.8"
languageVersion = "1.8" // data objects
}

buildFeatures {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ class MainActivity : AppCompatActivity(), SimpleStateChanger.NavigationHandler {

binding.navView.setNavigationItemSelectedListener { item: MenuItem ->
when (item.itemId) {
R.id.list_navigation_menu_item -> backstack.goTo(TasksKey())
R.id.statistics_navigation_menu_item -> backstack.goTo(StatisticsKey())
R.id.list_navigation_menu_item -> backstack.goTo(TasksKey)
R.id.statistics_navigation_menu_item -> backstack.goTo(StatisticsKey)
else -> {
}
}
Expand All @@ -62,7 +62,7 @@ class MainActivity : AppCompatActivity(), SimpleStateChanger.NavigationHandler {
.setStateChanger(SimpleStateChanger(this))
.setGlobalServices(globalServices)
.setScopedServices(DefaultServiceProvider())
.install(this, binding.contentFrame, History.of(TasksKey()))
.install(this, binding.contentFrame, History.of(TasksKey))
}

override fun onBackPressed() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import kotlinx.parcelize.Parcelize
* Created by Zhuinden on 2017.07.26..
*/
@Parcelize
data class StatisticsKey(private val noArgPlaceHolder: String = "") : BaseKey() {
data object StatisticsKey : BaseKey() {
override fun instantiateFragment(): Fragment = StatisticsFragment()

@Suppress("RemoveExplicitTypeArguments")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ import com.zhuinden.simplestackextensions.servicesktx.lookup
import kotlinx.parcelize.Parcelize

@Parcelize
data class TasksKey(private val noArgsPlaceHolder: String = "") : BaseKey() {
data object TasksKey : BaseKey() {
override fun instantiateFragment(): Fragment = TasksFragment()

@Suppress("RemoveExplicitTypeArguments")
override fun bindServices(serviceBinder: ServiceBinder) {
with(serviceBinder) {
add(TasksViewModel(
lookup<SnackbarTextEmitter>(),
lookup<TasksDataSource>(),
backstack,
getKey()
add(
TasksViewModel(
lookup<SnackbarTextEmitter>(),
lookup<TasksDataSource>(),
backstack,
getKey()
))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ android {
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

kotlinOptions {
jvmTarget = "1.8"
languageVersion = "1.8" // data objects
}


buildTypes {
getByName("release") {
isMinifyEnabled = false
Expand Down
Loading

0 comments on commit 1369acb

Please sign in to comment.