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

[FIXED] Using navigator pattern to clean up the activity. #81

Merged
merged 1 commit into from
Apr 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2019 Hossain Khan
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hossainkhan.android.demo.browse

import android.content.Context
import android.content.Intent
import com.hossainkhan.android.demo.layoutpreview.LayoutPreviewBaseActivity
import javax.inject.Inject

class DefaultLayoutBrowseNavigator @Inject constructor(private val context: Context) : LayoutBrowseNavigator {
override fun loadLayoutPreview(layoutResId: Int) {
val startIntent = LayoutPreviewBaseActivity.createStartIntent(context, layoutResId)
// 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?
startIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
context.startActivity(startIntent)
}

override fun <T> loadLayoutPreview(clazz: Class<T>, layoutResId: Int) {
val startIntent = LayoutPreviewBaseActivity.createStartIntent(context, clazz, layoutResId)
// 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?
startIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
context.startActivity(startIntent)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,8 @@ import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.hossainkhan.android.demo.R
import com.hossainkhan.android.demo.layoutpreview.LayoutChainStyleActivity
import com.hossainkhan.android.demo.layoutpreview.LayoutGuidelineBarrierActivity
import com.hossainkhan.android.demo.layoutpreview.LayoutGuidelineGroupActivity
import com.hossainkhan.android.demo.layoutpreview.LayoutPreviewBaseActivity
import com.hossainkhan.android.demo.layoutpreview.LayoutVisibilityGoneActivity
import com.hossainkhan.android.demo.viewmodel.LayoutPreviewViewModelFactory
import dagger.android.AndroidInjection
import timber.log.Timber
import javax.inject.Inject

/**
Expand Down Expand Up @@ -61,7 +55,7 @@ class LayoutBrowseActivity : AppCompatActivity() {
viewAdapter = LayoutBrowseAdapter(
viewModel = viewModel,
lifecycleOwner = this,
itemSelectedListener = this::onLayoutItemSelected)
itemSelectedListener = viewModel::onLayoutItemSelected)

recyclerView = findViewById<RecyclerView>(R.id.recycler_view).apply {
// use this setting to improve performance if you know that changes
Expand All @@ -75,35 +69,4 @@ class LayoutBrowseActivity : AppCompatActivity() {
adapter = viewAdapter
}
}

private fun onLayoutItemSelected(layoutResId: Int) {
Timber.i("Selected layout id: %s", layoutResId)

/*
* Where applicable, loads specific activity with interactive feature for user to try out feature.
*/
when (layoutResId) {
R.layout.preview_visibility_gone -> {
startActivity(LayoutPreviewBaseActivity.createStartIntent(this,
LayoutVisibilityGoneActivity::class.java, R.layout.preview_visibility_gone))
}
R.layout.preview_chain_style_main -> {
startActivity(LayoutPreviewBaseActivity.createStartIntent(this,
LayoutChainStyleActivity::class.java, R.layout.preview_chain_style_main))
}
R.layout.preview_virtual_helper_barrier -> {
startActivity(LayoutPreviewBaseActivity.createStartIntent(this,
LayoutGuidelineBarrierActivity::class.java, R.layout.preview_virtual_helper_barrier))
}
R.layout.preview_virtual_helper_group -> {
startActivity(LayoutPreviewBaseActivity.createStartIntent(this,
LayoutGuidelineGroupActivity::class.java, R.layout.preview_virtual_helper_group))
}
else -> {
// By default it loads the preview activity with the layout requested.
startActivity(LayoutPreviewBaseActivity.createStartIntent(this, layoutResId))
}
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2019 Hossain Khan
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hossainkhan.android.demo.browse

import androidx.annotation.LayoutRes

/**
* Interface to navigate for Layout Browse view.
*/
interface LayoutBrowseNavigator {
fun loadLayoutPreview(@LayoutRes layoutResId: Int)
fun <T> loadLayoutPreview(clazz: Class<T>, @LayoutRes layoutResId: Int)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,57 @@ package com.hossainkhan.android.demo.browse
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.hossainkhan.android.demo.R
import com.hossainkhan.android.demo.data.AppDataStore
import com.hossainkhan.android.demo.data.LayoutInformation
import com.hossainkhan.android.demo.layoutpreview.LayoutChainStyleActivity
import com.hossainkhan.android.demo.layoutpreview.LayoutGuidelineBarrierActivity
import com.hossainkhan.android.demo.layoutpreview.LayoutGuidelineGroupActivity
import com.hossainkhan.android.demo.layoutpreview.LayoutVisibilityGoneActivity
import timber.log.Timber

class LayoutBrowseViewModel(
appDataStore: AppDataStore) : ViewModel() {
appDataStore: AppDataStore,
private val browseNavigator: LayoutBrowseNavigator) : ViewModel() {

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

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

init {
Timber.d("Got data: ${appDataStore.isFirstTime()}")
Timber.d("Is first time user: ${appDataStore.isFirstTime()}")
appDataStore.updateFirstTimeUser(false)


layoutInfoListLiveData.value = appDataStore.layoutStore.supportedLayoutInfos
}


fun onLayoutItemSelected(layoutResId: Int) {
Timber.i("Selected layout id: %s", layoutResId)

/*
* Where applicable, loads specific activity with interactive feature for user to try out feature.
*/
when (layoutResId) {
R.layout.preview_visibility_gone -> {
browseNavigator.loadLayoutPreview(LayoutVisibilityGoneActivity::class.java, layoutResId)
}
R.layout.preview_chain_style_main -> {
browseNavigator.loadLayoutPreview(LayoutChainStyleActivity::class.java, layoutResId)
}
R.layout.preview_virtual_helper_barrier -> {
browseNavigator.loadLayoutPreview(LayoutGuidelineBarrierActivity::class.java, layoutResId)
}
R.layout.preview_virtual_helper_group -> {
browseNavigator.loadLayoutPreview(LayoutGuidelineGroupActivity::class.java, layoutResId)
}
else -> {
// By default it loads the preview activity with the layout requested.
browseNavigator.loadLayoutPreview(layoutResId)
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.hossainkhan.android.demo.dagger

import com.hossainkhan.android.demo.browse.LayoutBrowseActivity
import com.hossainkhan.android.demo.layoutpreview.LayoutChainStyleActivity
import com.hossainkhan.android.demo.layoutpreview.LayoutGuidelineBarrierActivity
import com.hossainkhan.android.demo.layoutpreview.LayoutGuidelineGroupActivity
Expand Down Expand Up @@ -48,9 +49,10 @@ abstract class ActivityBindingModule {
* into the subcomponent. If the subcomponent needs scopes, apply the scope annotations to
* the method as well.
*/

@ActivityScope
@ContributesAndroidInjector
abstract fun layoutPositioningActivity(): LayoutPreviewBaseActivity
abstract fun layoutPreviewBaseActivity(): LayoutPreviewBaseActivity

@ActivityScope
@ContributesAndroidInjector
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ import javax.inject.Singleton
ApplicationModule::class,
ActivityBindingModule::class,
DataStoreModule::class,
MainActivityModule::class))
NavigatorModule::class,
LayoutBrowseActivityModule::class))
interface DemoApplicationComponent {
fun inject(app: DemoApplication)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ import dagger.android.ActivityKey
import dagger.android.AndroidInjector
import dagger.multibindings.IntoMap

@Module(subcomponents = arrayOf(MainActivitySubcomponent::class))
abstract class MainActivityModule {
@Module(subcomponents = [LayoutBrowseSubcomponent::class])
abstract class LayoutBrowseActivityModule {

@Binds
@IntoMap
@ActivityKey(LayoutBrowseActivity::class)
abstract fun bindMainActivityInjectorFactory(
builder: MainActivitySubcomponent.Builder): AndroidInjector.Factory<out Activity>
builder: LayoutBrowseSubcomponent.Builder): AndroidInjector.Factory<out Activity>

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import dagger.Subcomponent
import dagger.android.AndroidInjector

@Subcomponent
interface MainActivitySubcomponent : AndroidInjector<LayoutBrowseActivity> {
interface LayoutBrowseSubcomponent : AndroidInjector<LayoutBrowseActivity> {
@Subcomponent.Builder
abstract class Builder : AndroidInjector.Builder<LayoutBrowseActivity>()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2019 Hossain Khan
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hossainkhan.android.demo.dagger

import android.content.Context
import com.hossainkhan.android.demo.browse.DefaultLayoutBrowseNavigator
import com.hossainkhan.android.demo.browse.LayoutBrowseNavigator
import dagger.Module
import dagger.Provides

@Module
class NavigatorModule {

@Provides
fun provideLayoutBrowseNavigator(context: Context): LayoutBrowseNavigator {
return DefaultLayoutBrowseNavigator(context)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.hossainkhan.android.demo.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.hossainkhan.android.demo.browse.LayoutBrowseNavigator
import com.hossainkhan.android.demo.browse.LayoutBrowseViewModel
import com.hossainkhan.android.demo.data.AppDataStore
import com.hossainkhan.android.demo.layoutpreview.LayoutInfoViewModel
Expand All @@ -27,7 +28,8 @@ import javax.inject.Inject
* The [ViewModelProvider.Factory] that provides all the ViewModels for the activities and fragments.
*/
class LayoutPreviewViewModelFactory @Inject constructor(
private val dataStore: AppDataStore
private val dataStore: AppDataStore,
private val browseNavigator: LayoutBrowseNavigator

) : ViewModelProvider.Factory {
@Suppress("UNCHECKED_CAST")
Expand All @@ -37,7 +39,7 @@ class LayoutPreviewViewModelFactory @Inject constructor(
LayoutInfoViewModel(dataStore) as T
}
modelClass.isAssignableFrom(LayoutBrowseViewModel::class.java) -> {
LayoutBrowseViewModel(dataStore) as T
LayoutBrowseViewModel(dataStore,browseNavigator) as T
}
else -> throw IllegalArgumentException("Unknown ViewModel class")
}
Expand Down
Loading