Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. private fun getUserCats() {
  2. vm.getCats().observe(this, Observer {
  3. if(it!=null) {
  4. rc_cats.visibility= View.VISIBLE
  5. pb.visibility=View.GONE
  6. catAdapter.reloadData(it)
  7.  
  8. }
  9. })
  10. }
  11.  
  12. class CategoryViewModel(private val model:CategoryModel): ViewModel() {
  13. private lateinit var catsLiveData:MutableLiveData<MutableList<Cat>>
  14.  
  15. fun getCats():MutableLiveData<MutableList<Cat>>{
  16. if(!::catsLiveData.isInitialized){
  17. catsLiveData=model.getCats()
  18. }
  19. return catsLiveData;
  20. }
  21.  
  22. fun addCat(catName:String){
  23. model.addCat(catName)
  24. }
  25.  
  26. class CategoryModel(
  27. private val netManager: NetManager,
  28. private val sharedPrefManager: SharedPrefManager) {
  29.  
  30. private lateinit var categoryDao: CategoryDao
  31. private lateinit var dbConnection: DbConnection
  32. private lateinit var lastUpdate: LastUpdate
  33.  
  34. fun getCats(): MutableLiveData<MutableList<Cat>> {
  35. dbConnection = DbConnection.getInstance(MyApp.INSTANCE)!!
  36. categoryDao = dbConnection.CategoryDao()
  37. lastUpdate = LastUpdate(MyApp.INSTANCE)
  38.  
  39. if (netManager.isConnected!!) {
  40. return getCatsOnline();
  41. } else {
  42. return getCatsOffline();
  43. }
  44. }
  45.  
  46. fun addCat(catName: String) {
  47. val Category = ApiConnection.client.create(Category::class.java)
  48. Category.newCategory(catName, sharedPrefManager.getUid())
  49. .subscribeOn(Schedulers.io())
  50. .observeOn(AndroidSchedulers.mainThread())
  51. .subscribe(
  52. { success ->
  53. getCatsOnline()
  54. }, { error ->
  55. Log.v("this", "ErrorNewCat " + error.localizedMessage)
  56. }
  57. )
  58. }
  59.  
  60. private fun getCatsOnline(): MutableLiveData<MutableList<Cat>> {
  61. Log.v("this", "online ");
  62. var list: MutableLiveData<MutableList<Cat>> = MutableLiveData()
  63. list = getCatsOffline()
  64.  
  65. val getCats = ApiConnection.client.create(Category::class.java)
  66. getCats.getCats(sharedPrefManager.getUid(), lastUpdate.getLastCatDate())
  67. .subscribeOn(Schedulers.io())
  68. .observeOn(AndroidSchedulers.mainThread())
  69. .subscribe(
  70. { success ->
  71. list += success.cats
  72. lastUpdate.setLastCatDate()
  73.  
  74. Observable.just(DbConnection)
  75. .subscribeOn(Schedulers.io())
  76. .subscribe({ db ->
  77. categoryDao.insert(success.cats)
  78. })
  79.  
  80. }, { error ->
  81. Log.v("this", "ErrorGetCats " + error.localizedMessage);
  82. }
  83. )
  84.  
  85. return list;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement