Skip to content

eoqkrskfk94/android_mvvm_architecture

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

image

License API

Android MVVM Architecture Template

Developing an android app is easy but developing an app with reusable components are the hardest thing, Some tools are helpful to write a code with prebuild tools and configuration.

This guideline will help set for setting MVVM architecture with different plugins.



[v1.0] MVVM Architecture template using DI KOIN


Main View (MainAcitivty)

Simple texView & Button sample using ViewModel, liveData and DataBinding


liveData countText in MainViewModel is incremented by 1 whenever INCREASE NUMBER is clicked.

//MainViewModel.class
fun clickIncreaseButton() {
  _countText.value = "click count : ${++count}"
}

Then countText is observed and will be notifed of updates whenever the value is changed.

viewModel.countText.observe(this, Observer { number ->
  binding.textviewNumber.text = number
})






Pokemon List View (ListActivity)

Simple RecyclerView Sample using Retrofit, Koin, ViewModel. (PokeApi is used in this sample)


pokemon list is retreived using Retrofit and saved to liveData

//ListViewModel.class

private val _pokemonList = MutableLiveData<List<PokemonEntity>>()
val pokemonList: LiveData<List<PokemonEntity>> get() = _pokemonList

init {
  viewModelScope.launch {
  _pokemonList.postValue(getPokemonListUseCase.invoke())
  }
}


fun addFavoritePokemon(pokemonEntity: PokemonEntity) = viewModelScope.launch {
  setLocalPokemonUseCase(pokemonEntity)
  }
}



then the list is observed and will be notifed of updates whenever the the list is retreived.

//ListAcitivty.class

viewModel.pokemonList.observe(this, Observer {
  binding.progressBarLoading.isVisible = false
  adapter.setData(it)
  })

//button to favorite activity
binding.floatingBtnFavorite.setOnClickListener {
  startActivity(Intent(this, FavoriteActivity::class.java))
  }
}



Pokemon Favorite List View (FavoriteActivity)

Simple RecyclerView Sample using Room, Koin, ViewModel.


RecyclerView, ViewModel, liveData structure is similar to Pokemon List View. However, for Data, Room is used instead of Retrofit in order to save Favorites.


when a pokemon is clicked in ListActivity, clicked pokemon is add to favorite using Room database. In favorite Activity (through floating button), added pokemon can be deleted from favorite.














[v1.1] MVVM Architecture template + Jetpack Paging3

RecyclerView without paging RecyclerView with paging

Paging3 helps load and display pages of data from a larger dataset from local storage or over network. This will help configure RecyclerView adapters that automatically request data as the user scrolls toward the end of the loaded data.

paging3 needs PagingRepository, PagingSource, and PagingAdapter






[v2.0] MVVM Architecture template using DI Hilt

Used Hilt instead of Koin for Dependancy Injection




License

MIT License

Copyright (c) 2021 Daniel Kim

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.