Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private fun getUserCats() {
- vm.getCats().observe(this, Observer {
- if(it!=null) {
- rc_cats.visibility= View.VISIBLE
- pb.visibility=View.GONE
- catAdapter.reloadData(it)
- }
- })
- }
- class CategoryViewModel(private val model:CategoryModel): ViewModel() {
- private lateinit var catsLiveData:MutableLiveData<MutableList<Cat>>
- fun getCats():MutableLiveData<MutableList<Cat>>{
- if(!::catsLiveData.isInitialized){
- catsLiveData=model.getCats()
- }
- return catsLiveData;
- }
- fun addCat(catName:String){
- model.addCat(catName)
- }
- class CategoryModel(
- private val netManager: NetManager,
- private val sharedPrefManager: SharedPrefManager) {
- private lateinit var categoryDao: CategoryDao
- private lateinit var dbConnection: DbConnection
- private lateinit var lastUpdate: LastUpdate
- fun getCats(): MutableLiveData<MutableList<Cat>> {
- dbConnection = DbConnection.getInstance(MyApp.INSTANCE)!!
- categoryDao = dbConnection.CategoryDao()
- lastUpdate = LastUpdate(MyApp.INSTANCE)
- if (netManager.isConnected!!) {
- return getCatsOnline();
- } else {
- return getCatsOffline();
- }
- }
- fun addCat(catName: String) {
- val Category = ApiConnection.client.create(Category::class.java)
- Category.newCategory(catName, sharedPrefManager.getUid())
- .subscribeOn(Schedulers.io())
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe(
- { success ->
- getCatsOnline()
- }, { error ->
- Log.v("this", "ErrorNewCat " + error.localizedMessage)
- }
- )
- }
- private fun getCatsOnline(): MutableLiveData<MutableList<Cat>> {
- Log.v("this", "online ");
- var list: MutableLiveData<MutableList<Cat>> = MutableLiveData()
- list = getCatsOffline()
- val getCats = ApiConnection.client.create(Category::class.java)
- getCats.getCats(sharedPrefManager.getUid(), lastUpdate.getLastCatDate())
- .subscribeOn(Schedulers.io())
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe(
- { success ->
- list += success.cats
- lastUpdate.setLastCatDate()
- Observable.just(DbConnection)
- .subscribeOn(Schedulers.io())
- .subscribe({ db ->
- categoryDao.insert(success.cats)
- })
- }, { error ->
- Log.v("this", "ErrorGetCats " + error.localizedMessage);
- }
- )
- return list;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement