Skip to content
This repository has been archived by the owner on May 10, 2022. It is now read-only.

Commit

Permalink
Added feature to set a customBundle for a viewmodel
Browse files Browse the repository at this point in the history
To avoid any interactions with the resource bundle, there is now a new custom bundle object that can be set via a builder and will be parsed to the viewmodel.
  • Loading branch information
AlexanderEggers committed Dec 9, 2019
1 parent a70ac9b commit 70d5f55
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 8 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ buildscript {
powermock_version = '2.0.2'
support_lib_lifecycle_testing_version = '2.1.0'

archtree_library_version = "1.0.0-beta4"
archtree_library_version = "1.0.0-beta5"
}

repositories {
Expand Down
13 changes: 13 additions & 0 deletions builder/src/main/java/archtree/ArchTreeBuilder.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package archtree

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.annotation.DrawableRes
import androidx.annotation.IdRes
import androidx.annotation.LayoutRes
Expand Down Expand Up @@ -32,6 +33,8 @@ abstract class ArchTreeBuilder<ViewModel : BaseViewModel, out Builder> {
var lifecycleOwnerBindingKey: Int = -1
private set

var customBundle: Bundle? = null
private set
var componentLayer: ComponentLayer<ViewModel>? = null
private set

Expand Down Expand Up @@ -129,6 +132,16 @@ abstract class ArchTreeBuilder<ViewModel : BaseViewModel, out Builder> {
return this as Builder
}

/**
* This method defines a custom bundle that will be used when initialising the viewmodel.
*
* @param bundle Bundle object that will be used for the viewmodel.
*/
open fun setCustomBundle(bundle: Bundle?): Builder {
this.customBundle = bundle
return this as Builder
}

/**
* This method defines a menu for the component.
*
Expand Down
3 changes: 3 additions & 0 deletions builder/src/main/java/archtree/ArchTreeResource.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package archtree

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
Expand All @@ -20,6 +21,8 @@ abstract class ArchTreeResource<ViewModel : BaseViewModel> constructor(builder:
var binding: ViewDataBinding? = null
private set

val customBundle: Bundle? = builder.customBundle

val layoutId: Int = builder.layoutId
val bindingKey: Int = builder.bindingKey

Expand Down
4 changes: 2 additions & 2 deletions builder/src/main/java/archtree/activity/ActivityResource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ constructor(builder: ActivityBuilder<ViewModel>) : ArchTreeResource<ViewModel>(b
else Log.d(ActivityResource::class.java.name, "ViewModel is not attached to layout.")

val resourceBundle = activity.intent.extras
if (viewModelInitMode == ViewModelInitMode.FORCE_INIT) viewModel?.init(true, resourceBundle, savedInstanceBundle)
else if (viewModelInitMode == ViewModelInitMode.NON_FORCE_INIT) viewModel?.init(false, resourceBundle, savedInstanceBundle)
if (viewModelInitMode == ViewModelInitMode.FORCE_INIT) viewModel?.init(true, resourceBundle, customBundle, savedInstanceBundle)
else if (viewModelInitMode == ViewModelInitMode.NON_FORCE_INIT) viewModel?.init(false, resourceBundle, customBundle, savedInstanceBundle)
}
}
}
4 changes: 4 additions & 0 deletions builder/src/main/java/archtree/activity/ArchTreeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -318,4 +318,8 @@ abstract class ArchTreeActivity<ViewModel : BaseViewModel> : AppCompatActivity()
open fun getBinding(): ViewDataBinding? {
return activityResource?.binding
}

open fun getCustomBundle(): Bundle? {
return activityResource?.customBundle
}
}
4 changes: 4 additions & 0 deletions builder/src/main/java/archtree/fragment/ArchTreeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,8 @@ abstract class ArchTreeFragment<ViewModel : BaseViewModel> : Fragment(), Injecta
open fun getBinding(): ViewDataBinding? {
return fragmentResource?.binding
}

open fun getCustomBundle(): Bundle? {
return fragmentResource?.customBundle
}
}
4 changes: 2 additions & 2 deletions builder/src/main/java/archtree/fragment/FragmentResource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ constructor(builder: FragmentBuilder<ViewModel>) : ArchTreeResource<ViewModel>(b
else Log.d(FragmentResource::class.java.name, "ViewModel is not attached to layout.")

val resourceBundle = fragment.arguments
if (viewModelInitMode == ViewModelInitMode.FORCE_INIT) viewModel?.init(true, resourceBundle, savedInstanceBundle)
else if (viewModelInitMode == ViewModelInitMode.NON_FORCE_INIT) viewModel?.init(false, resourceBundle, savedInstanceBundle)
if (viewModelInitMode == ViewModelInitMode.FORCE_INIT) viewModel?.init(true, resourceBundle, customBundle, savedInstanceBundle)
else if (viewModelInitMode == ViewModelInitMode.NON_FORCE_INIT) viewModel?.init(false, resourceBundle, customBundle, savedInstanceBundle)
}
}
}
7 changes: 4 additions & 3 deletions viewmodel/src/main/java/archtree/viewmodel/BaseViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ abstract class BaseViewModel : ViewModel() {
private set

@JvmOverloads
fun init(forceInit: Boolean = false, resourceBundle: Bundle? = null, savedInstanceBundle: Bundle? = null) {
fun init(forceInit: Boolean = false, resourceBundle: Bundle? = null, customBundle: Bundle? = null, savedInstanceBundle: Bundle? = null) {
if (!isInitialised || forceInit) {
isInitialised = true
onInit(resourceBundle, savedInstanceBundle)
onInit(resourceBundle, customBundle, savedInstanceBundle)
}
}

Expand All @@ -28,12 +28,13 @@ abstract class BaseViewModel : ViewModel() {
* within e.g. an activity.
*
* @param resourceBundle Bundle object that is provided by the attached activity or fragment
* @param customBundle Custom bundle object
* @param savedInstanceBundle Bundle object that is provided by the state of the attached
* activity or fragment
*
* @since 1.0.0
*/
protected open fun onInit(resourceBundle: Bundle?, savedInstanceBundle: Bundle?) {
protected open fun onInit(resourceBundle: Bundle?, customBundle: Bundle?, savedInstanceBundle: Bundle?) {
//do nothing by default
}

Expand Down

0 comments on commit 70d5f55

Please sign in to comment.