Skip to content

Commit 842bfc7

Browse files
committed
Remove usage of lookupService("appContext") from extension sample
1 parent dbc3ff1 commit 842bfc7

11 files changed

Lines changed: 55 additions & 34 deletions

File tree

samples/advanced-samples/extensions-example/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package="com.zhuinden.simplestackextensionsample">
44

55
<application
6+
android:name=".app.CustomApplication"
67
android:allowBackup="true"
78
android:icon="@mipmap/ic_launcher"
89
android:label="@string/app_name"

samples/advanced-samples/extensions-example/src/main/java/com/zhuinden/simplestackextensionsample/app/AuthenticationManager.kt

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
11
package com.zhuinden.simplestackextensionsample.app
22

3-
import android.content.Context
4-
import android.preference.PreferenceManager
3+
import android.content.SharedPreferences
54

6-
object AuthenticationManager {
7-
@Suppress("DEPRECATION") // w/e androidx-chan
8-
fun isAuthenticated(appContext: Context): Boolean {
9-
val sharedPref = PreferenceManager.getDefaultSharedPreferences(appContext)
10-
return sharedPref.getBoolean("isRegistered", false)
5+
class AuthenticationManager(
6+
private val sharedPreferences: SharedPreferences
7+
) {
8+
fun isAuthenticated(): Boolean {
9+
return sharedPreferences.getBoolean("isRegistered", false)
1110
}
1211

13-
@Suppress("DEPRECATION") // w/e androidx-chan
14-
fun saveRegistration(appContext: Context) {
15-
val sharedPref = PreferenceManager.getDefaultSharedPreferences(appContext)
16-
sharedPref.edit().putBoolean("isRegistered", true).apply()
12+
fun saveRegistration() {
13+
sharedPreferences.edit().putBoolean("isRegistered", true).apply()
1714
}
1815

19-
@Suppress("DEPRECATION") // w/e androidx-chan
20-
fun clearRegistration(appContext: Context) {
21-
val sharedPref = PreferenceManager.getDefaultSharedPreferences(appContext)
22-
sharedPref.edit().remove("isRegistered").apply()
16+
fun clearRegistration() {
17+
sharedPreferences.edit().remove("isRegistered").apply()
2318
}
2419

2520
var authToken: String = "" // why would this be in the viewModel?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.zhuinden.simplestackextensionsample.app
2+
3+
import android.app.Application
4+
import android.preference.PreferenceManager
5+
6+
class CustomApplication: Application() {
7+
lateinit var authenticationManager: AuthenticationManager
8+
private set
9+
10+
override fun onCreate() {
11+
super.onCreate()
12+
13+
@Suppress("DEPRECATION")
14+
authenticationManager = AuthenticationManager(PreferenceManager.getDefaultSharedPreferences(this))
15+
}
16+
}

samples/advanced-samples/extensions-example/src/main/java/com/zhuinden/simplestackextensionsample/app/MainActivity.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,37 @@ import com.zhuinden.simplestack.SimpleStateChanger
99
import com.zhuinden.simplestack.StateChange
1010
import com.zhuinden.simplestack.navigator.Navigator
1111
import com.zhuinden.simplestackextensions.fragments.DefaultFragmentStateChanger
12+
import com.zhuinden.simplestackextensions.servicesktx.add
1213
import com.zhuinden.simplestackextensionsample.R
1314
import com.zhuinden.simplestackextensionsample.features.login.LoginKey
1415
import com.zhuinden.simplestackextensionsample.features.profile.ProfileKey
1516
import kotlinx.android.synthetic.main.main_activity.*
1617

1718
class MainActivity : AppCompatActivity(), SimpleStateChanger.NavigationHandler {
1819
private lateinit var fragmentStateChanger: DefaultFragmentStateChanger
19-
private lateinit var appContext: Context
20+
private lateinit var authenticationManager: AuthenticationManager
2021

2122
override fun onCreate(savedInstanceState: Bundle?) {
2223
super.onCreate(savedInstanceState)
2324
setContentView(R.layout.main_activity)
2425

2526
fragmentStateChanger = DefaultFragmentStateChanger(supportFragmentManager, R.id.step9Root)
26-
appContext = applicationContext
27+
28+
val app = application as CustomApplication
29+
authenticationManager = app.authenticationManager
2730

2831
Navigator.configure()
2932
.setStateChanger(SimpleStateChanger(this))
3033
.setScopedServices(ServiceProvider())
3134
.setGlobalServices(
3235
GlobalServices.builder()
33-
.addService("appContext", appContext)
36+
.add(authenticationManager)
3437
.build()
3538
)
3639
.install(
3740
this, step9Root, History.of(
3841
when {
39-
AuthenticationManager.isAuthenticated(appContext) -> ProfileKey()
42+
authenticationManager.isAuthenticated() -> ProfileKey()
4043
else -> LoginKey()
4144
}
4245
)
@@ -55,7 +58,7 @@ class MainActivity : AppCompatActivity(), SimpleStateChanger.NavigationHandler {
5558

5659
override fun onDestroy() {
5760
if (isFinishing) {
58-
AuthenticationManager.clearRegistration(appContext) // just for sample repeat sake
61+
authenticationManager.clearRegistration() // just for sample repeat sake
5962
}
6063

6164
super.onDestroy()

samples/advanced-samples/extensions-example/src/main/java/com/zhuinden/simplestackextensionsample/app/ServiceProvider.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.zhuinden.simplestackextensionsample.app
33
import com.zhuinden.simplestack.ServiceBinder
44
import com.zhuinden.simplestackextensions.services.DefaultServiceProvider
55
import com.zhuinden.simplestackextensions.servicesktx.add
6+
import com.zhuinden.simplestackextensions.servicesktx.lookup
67
import com.zhuinden.simplestackextensionsample.features.registration.RegistrationViewModel
78

89
class ServiceProvider : DefaultServiceProvider() {
@@ -13,12 +14,9 @@ class ServiceProvider : DefaultServiceProvider() {
1314

1415
with(serviceBinder) {
1516
when (scope) { // explicit shared services
16-
"registration" -> add(
17-
RegistrationViewModel(
18-
lookupService("appContext"),
19-
backstack
20-
)
21-
)
17+
"registration" -> {
18+
add(RegistrationViewModel(lookup(), backstack))
19+
}
2220
else -> {
2321
}
2422
}

samples/advanced-samples/extensions-example/src/main/java/com/zhuinden/simplestackextensionsample/features/login/LoginKey.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import kotlinx.android.parcel.Parcelize
1212
data class LoginKey(private val placeholder: String = "") : DefaultFragmentKey(), DefaultServiceProvider.HasServices {
1313
override fun bindServices(serviceBinder: ServiceBinder) {
1414
with(serviceBinder) {
15-
add(LoginViewModel(lookup("appContext"), backstack))
15+
add(LoginViewModel(lookup(), backstack))
1616
}
1717
}
1818

samples/advanced-samples/extensions-example/src/main/java/com/zhuinden/simplestackextensionsample/features/login/LoginViewModel.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import io.reactivex.rxkotlin.addTo
1515
import io.reactivex.rxkotlin.subscribeBy
1616

1717
class LoginViewModel(
18-
private val appContext: Context,
18+
private val authenticationManager: AuthenticationManager,
1919
private val backstack: Backstack
2020
) : Bundleable, ScopedServices.Registered {
2121
private val compositeDisposable = CompositeDisposable()
@@ -38,7 +38,7 @@ class LoginViewModel(
3838

3939
fun onLoginClicked() {
4040
if (isLoginEnabled.get()) {
41-
AuthenticationManager.saveRegistration(appContext)
41+
authenticationManager.saveRegistration()
4242
backstack.setHistory(History.of(ProfileKey()), StateChange.FORWARD)
4343
}
4444
}

samples/advanced-samples/extensions-example/src/main/java/com/zhuinden/simplestackextensionsample/features/profile/ProfileKey.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import kotlinx.android.parcel.Parcelize
1212
data class ProfileKey(private val placeholder: String = "") : DefaultFragmentKey(), DefaultServiceProvider.HasServices {
1313
override fun bindServices(serviceBinder: ServiceBinder) {
1414
with(serviceBinder) {
15-
add(ProfileViewModel(lookup("appContext"), backstack))
15+
add(ProfileViewModel(lookup(), backstack))
1616
}
1717
}
1818

samples/advanced-samples/extensions-example/src/main/java/com/zhuinden/simplestackextensionsample/features/profile/ProfileViewModel.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import com.zhuinden.simplestackextensionsample.app.AuthenticationManager
99
import com.zhuinden.simplestackextensionsample.features.login.LoginKey
1010

1111
class ProfileViewModel(
12-
private val appContext: Context,
12+
private val authenticationManager: AuthenticationManager,
1313
private val backstack: Backstack
1414
) : ScopedServices.Activated {
1515
override fun onServiceActive() {
16-
if (!AuthenticationManager.isAuthenticated(appContext)) {
16+
if (!authenticationManager.isAuthenticated()) {
1717
backstack.setHistory(History.of(LoginKey()), StateChange.REPLACE)
1818
}
1919
}

samples/advanced-samples/extensions-example/src/main/java/com/zhuinden/simplestackextensionsample/features/registration/RegistrationViewModel.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import io.reactivex.rxkotlin.addTo
1414
import io.reactivex.rxkotlin.subscribeBy
1515

1616
class RegistrationViewModel(
17-
private val appContext: Context,
17+
private val authenticationManager: AuthenticationManager,
1818
private val backstack: Backstack
1919
) : Bundleable, ScopedServices.Registered, ScopedServices.HandlesBack {
2020
enum class RegistrationState { // this is actually kinda superfluous/unnecessary but ok
@@ -55,7 +55,7 @@ class RegistrationViewModel(
5555
fun onRegisterAndLoginClicked() {
5656
if (isRegisterAndLoginEnabled.get()) {
5757
currentState = RegistrationState.REGISTRATION_COMPLETED
58-
AuthenticationManager.saveRegistration(appContext)
58+
authenticationManager.saveRegistration()
5959
backstack.setHistory(History.of(ProfileKey()), StateChange.FORWARD)
6060
}
6161
}

0 commit comments

Comments
 (0)