Skip to content
This repository was archived by the owner on Aug 22, 2024. It is now read-only.

Commit 025437d

Browse files
authored
Merge pull request #81 from amardeshbd/cleanup-activity
[FIXED] Using navigator pattern to clean up the activity.
2 parents e805e73 + af70c0c commit 025437d

File tree

11 files changed

+248
-48
lines changed

11 files changed

+248
-48
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) 2019 Hossain Khan
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.hossainkhan.android.demo.browse
18+
19+
import android.content.Context
20+
import android.content.Intent
21+
import com.hossainkhan.android.demo.layoutpreview.LayoutPreviewBaseActivity
22+
import javax.inject.Inject
23+
24+
class DefaultLayoutBrowseNavigator @Inject constructor(private val context: Context) : LayoutBrowseNavigator {
25+
override fun loadLayoutPreview(layoutResId: Int) {
26+
val startIntent = LayoutPreviewBaseActivity.createStartIntent(context, layoutResId)
27+
// FIX IT: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
28+
startIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
29+
context.startActivity(startIntent)
30+
}
31+
32+
override fun <T> loadLayoutPreview(clazz: Class<T>, layoutResId: Int) {
33+
val startIntent = LayoutPreviewBaseActivity.createStartIntent(context, clazz, layoutResId)
34+
// FIX IT: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
35+
startIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
36+
context.startActivity(startIntent)
37+
}
38+
}

app/src/main/java/com/hossainkhan/android/demo/browse/LayoutBrowseActivity.kt

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,8 @@ import androidx.lifecycle.ViewModelProviders
2222
import androidx.recyclerview.widget.GridLayoutManager
2323
import androidx.recyclerview.widget.RecyclerView
2424
import com.hossainkhan.android.demo.R
25-
import com.hossainkhan.android.demo.layoutpreview.LayoutChainStyleActivity
26-
import com.hossainkhan.android.demo.layoutpreview.LayoutGuidelineBarrierActivity
27-
import com.hossainkhan.android.demo.layoutpreview.LayoutGuidelineGroupActivity
28-
import com.hossainkhan.android.demo.layoutpreview.LayoutPreviewBaseActivity
29-
import com.hossainkhan.android.demo.layoutpreview.LayoutVisibilityGoneActivity
3025
import com.hossainkhan.android.demo.viewmodel.LayoutPreviewViewModelFactory
3126
import dagger.android.AndroidInjection
32-
import timber.log.Timber
3327
import javax.inject.Inject
3428

3529
/**
@@ -61,7 +55,7 @@ class LayoutBrowseActivity : AppCompatActivity() {
6155
viewAdapter = LayoutBrowseAdapter(
6256
viewModel = viewModel,
6357
lifecycleOwner = this,
64-
itemSelectedListener = this::onLayoutItemSelected)
58+
itemSelectedListener = viewModel::onLayoutItemSelected)
6559

6660
recyclerView = findViewById<RecyclerView>(R.id.recycler_view).apply {
6761
// use this setting to improve performance if you know that changes
@@ -75,35 +69,4 @@ class LayoutBrowseActivity : AppCompatActivity() {
7569
adapter = viewAdapter
7670
}
7771
}
78-
79-
private fun onLayoutItemSelected(layoutResId: Int) {
80-
Timber.i("Selected layout id: %s", layoutResId)
81-
82-
/*
83-
* Where applicable, loads specific activity with interactive feature for user to try out feature.
84-
*/
85-
when (layoutResId) {
86-
R.layout.preview_visibility_gone -> {
87-
startActivity(LayoutPreviewBaseActivity.createStartIntent(this,
88-
LayoutVisibilityGoneActivity::class.java, R.layout.preview_visibility_gone))
89-
}
90-
R.layout.preview_chain_style_main -> {
91-
startActivity(LayoutPreviewBaseActivity.createStartIntent(this,
92-
LayoutChainStyleActivity::class.java, R.layout.preview_chain_style_main))
93-
}
94-
R.layout.preview_virtual_helper_barrier -> {
95-
startActivity(LayoutPreviewBaseActivity.createStartIntent(this,
96-
LayoutGuidelineBarrierActivity::class.java, R.layout.preview_virtual_helper_barrier))
97-
}
98-
R.layout.preview_virtual_helper_group -> {
99-
startActivity(LayoutPreviewBaseActivity.createStartIntent(this,
100-
LayoutGuidelineGroupActivity::class.java, R.layout.preview_virtual_helper_group))
101-
}
102-
else -> {
103-
// By default it loads the preview activity with the layout requested.
104-
startActivity(LayoutPreviewBaseActivity.createStartIntent(this, layoutResId))
105-
}
106-
}
107-
108-
}
10972
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (c) 2019 Hossain Khan
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.hossainkhan.android.demo.browse
18+
19+
import androidx.annotation.LayoutRes
20+
21+
/**
22+
* Interface to navigate for Layout Browse view.
23+
*/
24+
interface LayoutBrowseNavigator {
25+
fun loadLayoutPreview(@LayoutRes layoutResId: Int)
26+
fun <T> loadLayoutPreview(clazz: Class<T>, @LayoutRes layoutResId: Int)
27+
}

app/src/main/java/com/hossainkhan/android/demo/browse/LayoutBrowseViewModel.kt

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,57 @@ package com.hossainkhan.android.demo.browse
1919
import androidx.lifecycle.LiveData
2020
import androidx.lifecycle.MutableLiveData
2121
import androidx.lifecycle.ViewModel
22+
import com.hossainkhan.android.demo.R
2223
import com.hossainkhan.android.demo.data.AppDataStore
2324
import com.hossainkhan.android.demo.data.LayoutInformation
25+
import com.hossainkhan.android.demo.layoutpreview.LayoutChainStyleActivity
26+
import com.hossainkhan.android.demo.layoutpreview.LayoutGuidelineBarrierActivity
27+
import com.hossainkhan.android.demo.layoutpreview.LayoutGuidelineGroupActivity
28+
import com.hossainkhan.android.demo.layoutpreview.LayoutVisibilityGoneActivity
2429
import timber.log.Timber
2530

2631
class LayoutBrowseViewModel(
27-
appDataStore: AppDataStore) : ViewModel() {
32+
appDataStore: AppDataStore,
33+
private val browseNavigator: LayoutBrowseNavigator) : ViewModel() {
2834

2935
private val layoutInfoListLiveData = MutableLiveData<List<LayoutInformation>>()
3036

3137
val layoutInfos: LiveData<List<LayoutInformation>>
3238
get() = layoutInfoListLiveData
3339

3440
init {
35-
Timber.d("Got data: ${appDataStore.isFirstTime()}")
41+
Timber.d("Is first time user: ${appDataStore.isFirstTime()}")
3642
appDataStore.updateFirstTimeUser(false)
3743

3844

3945
layoutInfoListLiveData.value = appDataStore.layoutStore.supportedLayoutInfos
4046
}
47+
48+
49+
fun onLayoutItemSelected(layoutResId: Int) {
50+
Timber.i("Selected layout id: %s", layoutResId)
51+
52+
/*
53+
* Where applicable, loads specific activity with interactive feature for user to try out feature.
54+
*/
55+
when (layoutResId) {
56+
R.layout.preview_visibility_gone -> {
57+
browseNavigator.loadLayoutPreview(LayoutVisibilityGoneActivity::class.java, layoutResId)
58+
}
59+
R.layout.preview_chain_style_main -> {
60+
browseNavigator.loadLayoutPreview(LayoutChainStyleActivity::class.java, layoutResId)
61+
}
62+
R.layout.preview_virtual_helper_barrier -> {
63+
browseNavigator.loadLayoutPreview(LayoutGuidelineBarrierActivity::class.java, layoutResId)
64+
}
65+
R.layout.preview_virtual_helper_group -> {
66+
browseNavigator.loadLayoutPreview(LayoutGuidelineGroupActivity::class.java, layoutResId)
67+
}
68+
else -> {
69+
// By default it loads the preview activity with the layout requested.
70+
browseNavigator.loadLayoutPreview(layoutResId)
71+
}
72+
}
73+
74+
}
4175
}

app/src/main/java/com/hossainkhan/android/demo/dagger/ActivityBindingModule.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.hossainkhan.android.demo.dagger
1818

19+
import com.hossainkhan.android.demo.browse.LayoutBrowseActivity
1920
import com.hossainkhan.android.demo.layoutpreview.LayoutChainStyleActivity
2021
import com.hossainkhan.android.demo.layoutpreview.LayoutGuidelineBarrierActivity
2122
import com.hossainkhan.android.demo.layoutpreview.LayoutGuidelineGroupActivity
@@ -48,9 +49,10 @@ abstract class ActivityBindingModule {
4849
* into the subcomponent. If the subcomponent needs scopes, apply the scope annotations to
4950
* the method as well.
5051
*/
52+
5153
@ActivityScope
5254
@ContributesAndroidInjector
53-
abstract fun layoutPositioningActivity(): LayoutPreviewBaseActivity
55+
abstract fun layoutPreviewBaseActivity(): LayoutPreviewBaseActivity
5456

5557
@ActivityScope
5658
@ContributesAndroidInjector

app/src/main/java/com/hossainkhan/android/demo/dagger/DemoApplicationComponent.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ import javax.inject.Singleton
2727
ApplicationModule::class,
2828
ActivityBindingModule::class,
2929
DataStoreModule::class,
30-
MainActivityModule::class))
30+
NavigatorModule::class,
31+
LayoutBrowseActivityModule::class))
3132
interface DemoApplicationComponent {
3233
fun inject(app: DemoApplication)
3334

app/src/main/java/com/hossainkhan/android/demo/dagger/MainActivityModule.kt renamed to app/src/main/java/com/hossainkhan/android/demo/dagger/LayoutBrowseActivityModule.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ import dagger.android.ActivityKey
2424
import dagger.android.AndroidInjector
2525
import dagger.multibindings.IntoMap
2626

27-
@Module(subcomponents = arrayOf(MainActivitySubcomponent::class))
28-
abstract class MainActivityModule {
27+
@Module(subcomponents = [LayoutBrowseSubcomponent::class])
28+
abstract class LayoutBrowseActivityModule {
2929

3030
@Binds
3131
@IntoMap
3232
@ActivityKey(LayoutBrowseActivity::class)
3333
abstract fun bindMainActivityInjectorFactory(
34-
builder: MainActivitySubcomponent.Builder): AndroidInjector.Factory<out Activity>
34+
builder: LayoutBrowseSubcomponent.Builder): AndroidInjector.Factory<out Activity>
3535

3636
}

app/src/main/java/com/hossainkhan/android/demo/dagger/MainActivitySubcomponent.kt renamed to app/src/main/java/com/hossainkhan/android/demo/dagger/LayoutBrowseSubcomponent.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import dagger.Subcomponent
2121
import dagger.android.AndroidInjector
2222

2323
@Subcomponent
24-
interface MainActivitySubcomponent : AndroidInjector<LayoutBrowseActivity> {
24+
interface LayoutBrowseSubcomponent : AndroidInjector<LayoutBrowseActivity> {
2525
@Subcomponent.Builder
2626
abstract class Builder : AndroidInjector.Builder<LayoutBrowseActivity>()
2727
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (c) 2019 Hossain Khan
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.hossainkhan.android.demo.dagger
18+
19+
import android.content.Context
20+
import com.hossainkhan.android.demo.browse.DefaultLayoutBrowseNavigator
21+
import com.hossainkhan.android.demo.browse.LayoutBrowseNavigator
22+
import dagger.Module
23+
import dagger.Provides
24+
25+
@Module
26+
class NavigatorModule {
27+
28+
@Provides
29+
fun provideLayoutBrowseNavigator(context: Context): LayoutBrowseNavigator {
30+
return DefaultLayoutBrowseNavigator(context)
31+
}
32+
}

app/src/main/java/com/hossainkhan/android/demo/viewmodel/LayoutPreviewViewModelFactory.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package com.hossainkhan.android.demo.viewmodel
1818

1919
import androidx.lifecycle.ViewModel
2020
import androidx.lifecycle.ViewModelProvider
21+
import com.hossainkhan.android.demo.browse.LayoutBrowseNavigator
2122
import com.hossainkhan.android.demo.browse.LayoutBrowseViewModel
2223
import com.hossainkhan.android.demo.data.AppDataStore
2324
import com.hossainkhan.android.demo.layoutpreview.LayoutInfoViewModel
@@ -27,7 +28,8 @@ import javax.inject.Inject
2728
* The [ViewModelProvider.Factory] that provides all the ViewModels for the activities and fragments.
2829
*/
2930
class LayoutPreviewViewModelFactory @Inject constructor(
30-
private val dataStore: AppDataStore
31+
private val dataStore: AppDataStore,
32+
private val browseNavigator: LayoutBrowseNavigator
3133

3234
) : ViewModelProvider.Factory {
3335
@Suppress("UNCHECKED_CAST")
@@ -37,7 +39,7 @@ class LayoutPreviewViewModelFactory @Inject constructor(
3739
LayoutInfoViewModel(dataStore) as T
3840
}
3941
modelClass.isAssignableFrom(LayoutBrowseViewModel::class.java) -> {
40-
LayoutBrowseViewModel(dataStore) as T
42+
LayoutBrowseViewModel(dataStore,browseNavigator) as T
4143
}
4244
else -> throw IllegalArgumentException("Unknown ViewModel class")
4345
}

0 commit comments

Comments
 (0)