mirror of
https://github.com/KokaKiwi/BarInsta
synced 2024-11-12 17:57:29 +00:00
Convert FavoritesViewModel to kotlin and optimise code
This commit is contained in:
parent
39687136dc
commit
77965f9634
@ -182,6 +182,8 @@ dependencies {
|
||||
def core_version = "1.6.0-beta01"
|
||||
implementation "androidx.core:core:$core_version"
|
||||
|
||||
implementation "androidx.fragment:fragment-ktx:1.3.4"
|
||||
|
||||
// Room
|
||||
def room_version = "2.3.0"
|
||||
implementation "androidx.room:room-runtime:$room_version"
|
||||
|
@ -7,17 +7,14 @@ import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.fragment.app.viewModels
|
||||
import androidx.navigation.fragment.NavHostFragment
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import awais.instagrabber.R
|
||||
import awais.instagrabber.adapters.FavoritesAdapter
|
||||
import awais.instagrabber.databinding.FragmentFavoritesBinding
|
||||
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 awais.instagrabber.models.enums.FavoriteType
|
||||
import awais.instagrabber.utils.extensions.TAG
|
||||
import awais.instagrabber.viewmodels.FavoritesViewModel
|
||||
@ -28,16 +25,9 @@ class FavoritesFragment : Fragment() {
|
||||
|
||||
private lateinit var binding: FragmentFavoritesBinding
|
||||
private lateinit var root: RecyclerView
|
||||
private lateinit var favoritesViewModel: FavoritesViewModel
|
||||
private lateinit var favoriteRepository: FavoriteRepository
|
||||
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 {
|
||||
if (this::root.isInitialized) {
|
||||
@ -61,13 +51,6 @@ class FavoritesFragment : Fragment() {
|
||||
if (!this::adapter.isInitialized) return
|
||||
// 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) })
|
||||
favoriteRepository.getAllFavorites(object : RepositoryCallback<List<Favorite>> {
|
||||
override fun onSuccess(favorites: List<Favorite>) {
|
||||
favoritesViewModel.list.postValue(favorites)
|
||||
}
|
||||
|
||||
override fun onDataNotAvailable() {}
|
||||
})
|
||||
}
|
||||
|
||||
private fun init() {
|
||||
@ -109,22 +92,7 @@ class FavoritesFragment : Fragment() {
|
||||
val context = context ?: return@FavoritesAdapter false
|
||||
MaterialAlertDialogBuilder(context)
|
||||
.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)
|
||||
.show()
|
||||
true
|
||||
|
@ -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;
|
||||
|
||||
public MutableLiveData<List<Favorite>> getList() {
|
||||
if (list == null) {
|
||||
list = new MutableLiveData<>();
|
||||
}
|
||||
return 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() {}
|
||||
})
|
||||
}
|
||||
|
||||
override fun onDataNotAvailable() {}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user