Browse Source

Convert FavoritesViewModel to kotlin and optimise code

renovate/org.robolectric-robolectric-4.x
Ammar Githam 4 years ago
parent
commit
77965f9634
  1. 2
      app/build.gradle
  2. 38
      app/src/main/java/awais/instagrabber/fragments/FavoritesFragment.kt
  3. 55
      app/src/main/java/awais/instagrabber/viewmodels/FavoritesViewModel.kt

2
app/build.gradle

@ -182,6 +182,8 @@ dependencies {
def core_version = "1.6.0-beta01" def core_version = "1.6.0-beta01"
implementation "androidx.core:core:$core_version" implementation "androidx.core:core:$core_version"
implementation "androidx.fragment:fragment-ktx:1.3.4"
// Room // Room
def room_version = "2.3.0" def room_version = "2.3.0"
implementation "androidx.room:room-runtime:$room_version" implementation "androidx.room:room-runtime:$room_version"

38
app/src/main/java/awais/instagrabber/fragments/FavoritesFragment.kt

@ -7,17 +7,14 @@ import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.NavHostFragment import androidx.navigation.fragment.NavHostFragment
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import awais.instagrabber.R import awais.instagrabber.R
import awais.instagrabber.adapters.FavoritesAdapter import awais.instagrabber.adapters.FavoritesAdapter
import awais.instagrabber.databinding.FragmentFavoritesBinding import awais.instagrabber.databinding.FragmentFavoritesBinding
import awais.instagrabber.db.datasources.FavoriteDataSource
import awais.instagrabber.db.entities.Favorite import awais.instagrabber.db.entities.Favorite
import awais.instagrabber.db.repositories.FavoriteRepository
import awais.instagrabber.db.repositories.RepositoryCallback
import awais.instagrabber.models.enums.FavoriteType import awais.instagrabber.models.enums.FavoriteType
import awais.instagrabber.utils.extensions.TAG import awais.instagrabber.utils.extensions.TAG
import awais.instagrabber.viewmodels.FavoritesViewModel import awais.instagrabber.viewmodels.FavoritesViewModel
@ -28,16 +25,9 @@ class FavoritesFragment : Fragment() {
private lateinit var binding: FragmentFavoritesBinding private lateinit var binding: FragmentFavoritesBinding
private lateinit var root: RecyclerView private lateinit var root: RecyclerView
private lateinit var favoritesViewModel: FavoritesViewModel
private lateinit var favoriteRepository: FavoriteRepository
private lateinit var adapter: FavoritesAdapter private lateinit var adapter: FavoritesAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val context = context ?: return
favoriteRepository = FavoriteRepository.getInstance(FavoriteDataSource.getInstance(context))
favoritesViewModel = ViewModelProvider(this).get(FavoritesViewModel::class.java)
}
private val favoritesViewModel: FavoritesViewModel by viewModels()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
if (this::root.isInitialized) { if (this::root.isInitialized) {
@ -61,13 +51,6 @@ class FavoritesFragment : Fragment() {
if (!this::adapter.isInitialized) return if (!this::adapter.isInitialized) return
// refresh list every time in onViewStateRestored since it is cheaper than implementing pull down to refresh // refresh list every time in onViewStateRestored since it is cheaper than implementing pull down to refresh
favoritesViewModel.list.observe(viewLifecycleOwner, { list: List<Favorite?>? -> adapter.submitList(list) }) favoritesViewModel.list.observe(viewLifecycleOwner, { list: List<Favorite?>? -> adapter.submitList(list) })
favoriteRepository.getAllFavorites(object : RepositoryCallback<List<Favorite>> {
override fun onSuccess(favorites: List<Favorite>) {
favoritesViewModel.list.postValue(favorites)
}
override fun onDataNotAvailable() {}
})
} }
private fun init() { private fun init() {
@ -109,22 +92,7 @@ class FavoritesFragment : Fragment() {
val context = context ?: return@FavoritesAdapter false val context = context ?: return@FavoritesAdapter false
MaterialAlertDialogBuilder(context) MaterialAlertDialogBuilder(context)
.setMessage(getString(R.string.quick_access_confirm_delete, model.query)) .setMessage(getString(R.string.quick_access_confirm_delete, model.query))
.setPositiveButton(R.string.yes) { d: DialogInterface, _: Int ->
favoriteRepository.deleteFavorite(model.query, model.type, object : RepositoryCallback<Void> {
override fun onSuccess(result: Void) {
d.dismiss()
favoriteRepository.getAllFavorites(object : RepositoryCallback<List<Favorite>> {
override fun onSuccess(result: List<Favorite>) {
favoritesViewModel.list.postValue(result)
}
override fun onDataNotAvailable() {}
})
}
override fun onDataNotAvailable() {}
})
}
.setPositiveButton(R.string.yes) { d: DialogInterface, _: Int -> favoritesViewModel.delete(model) { d.dismiss() } }
.setNegativeButton(R.string.no, null) .setNegativeButton(R.string.no, null)
.show() .show()
true true

55
app/src/main/java/awais/instagrabber/viewmodels/FavoritesViewModel.kt

@ -1,19 +1,48 @@
package awais.instagrabber.viewmodels;
package awais.instagrabber.viewmodels
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import awais.instagrabber.db.datasources.FavoriteDataSource
import awais.instagrabber.db.entities.Favorite
import awais.instagrabber.db.repositories.FavoriteRepository
import awais.instagrabber.db.repositories.RepositoryCallback
import java.util.List;
class FavoritesViewModel(application: Application) : AndroidViewModel(application) {
private val _list = MutableLiveData<List<Favorite>>()
val list: LiveData<List<Favorite>> = _list
import awais.instagrabber.db.entities.Favorite;
private val favoriteRepository: FavoriteRepository = FavoriteRepository.getInstance(FavoriteDataSource.getInstance(application))
public class FavoritesViewModel extends ViewModel {
private MutableLiveData<List<Favorite>> list;
init {
fetch()
}
fun fetch() {
favoriteRepository.getAllFavorites(object : RepositoryCallback<List<Favorite>> {
override fun onSuccess(favorites: List<Favorite>?) {
_list.postValue(favorites ?: emptyList())
}
override fun onDataNotAvailable() {}
})
}
fun delete(favorite: Favorite, onSuccess: () -> Unit) {
favoriteRepository.deleteFavorite(favorite.query, favorite.type, object : RepositoryCallback<Void> {
override fun onSuccess(result: Void?) {
onSuccess()
favoriteRepository.getAllFavorites(object : RepositoryCallback<List<Favorite>> {
override fun onSuccess(result: List<Favorite>?) {
_list.postValue(result ?: emptyList())
}
override fun onDataNotAvailable() {}
})
}
public MutableLiveData<List<Favorite>> getList() {
if (list == null) {
list = new MutableLiveData<>();
}
return list;
override fun onDataNotAvailable() {}
})
} }
}
}
Loading…
Cancel
Save