Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Commit

Permalink
For #3633 - Creates generic store provider (#4060)
Browse files Browse the repository at this point in the history
  • Loading branch information
boek authored Jul 15, 2019
1 parent e70ab6b commit ec099a6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,25 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import androidx.lifecycle.get
import mozilla.components.lib.state.Action
import mozilla.components.lib.state.State
import mozilla.components.lib.state.Store

/**
* Generic ViewModel to wrap a State object for state restoration
*/
@Suppress("UNCHECKED_CAST")
class StateViewModel<T : State>(initialState: T) : ViewModel() {
var state: T = initialState
private set(value) { field = value }

fun update(state: T) { this.state = state }

class StoreProvider<S : State, A : Action, T : Store<S, A>>(val store: T) : ViewModel() {
companion object {
fun <S : State> get(fragment: Fragment, initialState: S): StateViewModel<S> {
fun <S : State, A : Action, T : Store<S, A>> get(fragment: Fragment, initialStore: T): T {
val factory = object : ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
return StateViewModel(initialState) as T
override fun <VM : ViewModel?> create(modelClass: Class<VM>): VM {
return StoreProvider(initialStore) as VM
}
}

return ViewModelProviders.of(fragment, factory).get()
val viewModel: StoreProvider<S, A, T> = ViewModelProviders.of(fragment, factory).get()
return viewModel.store
}
}
}
17 changes: 6 additions & 11 deletions app/src/main/java/org/mozilla/fenix/search/SearchFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import org.mozilla.fenix.BrowserDirection
import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.R
import org.mozilla.fenix.ThemeManager
import org.mozilla.fenix.components.StateViewModel
import org.mozilla.fenix.components.StoreProvider
import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.ext.getSpannable
import org.mozilla.fenix.ext.requireComponents
Expand Down Expand Up @@ -72,27 +72,22 @@ class SearchFragment : Fragment(), BackHandler {
val view = inflater.inflate(R.layout.fragment_search, container, false)
val url = session?.url ?: ""

val viewModel = StateViewModel.get(
searchStore = StoreProvider.get(
this,
SearchState(
Store(
SearchState(
query = url,
showShortcutEnginePicker = false,
searchEngineSource = SearchEngineSource.Default(
requireComponents.search.searchEngineManager.getDefaultSearchEngine(requireContext())
),
showSuggestions = Settings.getInstance(requireContext()).showSearchSuggestions,
showVisitedSitesBookmarks = Settings.getInstance(requireContext()).shouldShowVisitedSitesBookmarks,
session = session
session = session),
::searchStateReducer
)
)

searchStore = Store(
viewModel.state,
::searchStateReducer
)

searchStore.observe(this) { viewModel.update(it) }

searchInteractor = SearchInteractor(
activity as HomeActivity,
findNavController(),
Expand Down

0 comments on commit ec099a6

Please sign in to comment.