Basic Android Room Database Example with Android ReactComponentKit.
// Load all words from the DBobject LoadWordsAction: Action
// Insert an word into the DBdata classInsertWordAction(valword:Word): Action
// Delete an word in the DBdata classDeleteWordAction(valword:Word): Action// Load all words from the DBfun MainViewModel.loadWords(state:MainViewState): MainViewState {
val words =WordDB.getInstance(getApplication())
.wordDao()
.getAlphabetizedWords()
return state.copy(words = words)
}
// Insert an word into the DBfun MainViewModel.insertWord(state:MainViewState, action:InsertWordAction): MainViewState {
WordDB.getInstance(getApplication())
.wordDao()
.insert(action.word)
return state
}
// Delete an word in the DBfun MainViewModel.deleteWord(state:MainViewState, action:DeleteWordAction): MainViewState {
WordDB.getInstance(getApplication())
.wordDao()
.delete(action.word)
return state
}// Make ItemModels from word list for the recycler viewfun MainViewModel.makeItemModels(state:MainViewState): MainViewState {
val itemModels = state.words.map { WordModel(it) }
return state.copy(itemModels = itemModels)
}data classMainViewState(
valwords:List<Word> = emptyList(),
valitemModels:List<WordModel> = emptyList()
): State() {
overridefuncopyState(): MainViewState {
return copy()
}
}classMainViewModel(application:Application): RCKViewModel<MainViewState>(application) {
val itemModels =Output<List<WordModel>>(emptyList())
overridefunsetupStore() {
initStore { store ->
store.initialState(MainViewState())
store.flow<LoadWordsAction>(
{ state, _ -> loadWords(state) },
{ state, _ -> makeItemModels(state) }
)
store.flow<InsertWordAction>(
::insertWord,
{ state, _ -> loadWords(state) },
{ state, _ -> makeItemModels(state) }
)
store.flow<DeleteWordAction>(
::deleteWord,
{ state, _ -> loadWords(state) },
{ state, _ -> makeItemModels(state) }
)
}
}
operatorfunget(index:Int): Word= withState { state ->
state.words[index]
}
overridefunon(newState:MainViewState) {
itemModels.accept(newState.itemModels)
}
overridefunon(error:Error) {
Log.e("MainViewModel", error.toString())
}
}classMainActivity : AppCompatActivity() {
privatelateinitvar viewModel:MainViewModelprivateval disposeBag:AutoDisposeBag by lazy {
AutoDisposeBag(this)
}
privateval layoutManager:LinearLayoutManager by lazy {
LinearLayoutManager(this, RecyclerView.VERTICAL, false)
}
privateval adapter:RecyclerViewAdapter by lazy {
RecyclerViewAdapter(token = viewModel.token, useDiff =true)
}
privateval itemTouchHelper:ItemTouchHelper by lazy {
ItemTouchHelper(SwipeToDeleteCallback {
val word = viewModel[it]
viewModel.dispatch(DeleteWordAction(word))
})
}
overridefunonCreate(savedInstanceState:Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
viewModel =ViewModelProviders.of(this).get(MainViewModel::class.java)
adapter.register(WordComponent::class)
recyclerview.layoutManager = layoutManager
recyclerview.adapter = adapter
itemTouchHelper.attachToRecyclerView(recyclerview)
loadWords()
handleClickEvents()
handleViewModelOutputs()
}
privatefunloadWords() {
viewModel.dispatch(LoadWordsAction)
}
privatefunhandleClickEvents() {
fab.onClick {
val word =Word(WordUtils.randomWord)
viewModel.dispatch(InsertWordAction(word))
}
}
privatefunhandleViewModelOutputs() {
viewModel
.itemModels
.asObservable()
.subscribe {
adapter.set(it)
}
.disposedBy(disposeBag)
}
}The MIT License (MIT)
Copyright (c) 2019 Sungcheol Kim, https://github.com/ReactComponentKit/HelloRoom
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
