Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. const val FROM_DB = "FROM_DB"
  2. const val FROM_API = "FROM_API"
  3.  
  4. class MainActivity : AppCompatActivity(), CoroutineScope {
  5.  
  6. //region Coroutines initialization
  7. private val job = Job()
  8. override val coroutineContext: CoroutineContext = Dispatchers.Main + job
  9. //endregion
  10.  
  11. //region variable declaration
  12. private val todos = ArrayList<Todo>()
  13. private var adapter: TodoAdapter? = null
  14. //endregion
  15.  
  16. //region livedata variables
  17. private val progressLiveData = MutableLiveData<Boolean>()
  18. private val todoListLiveData = MutableLiveData<Result.ResultWrapper<List<Todo>, String>>()
  19. //endregion
  20.  
  21.  
  22. override fun onCreate(savedInstanceState: Bundle?) {
  23. super.onCreate(savedInstanceState)
  24. setContentView(R.layout.activity_main)
  25. initAdapter()
  26. initRealm()
  27. setListeners()
  28. getLocalTodos()
  29. getRemoteTodos()
  30. }
  31.  
  32.  
  33. /**
  34. * Adapter is initialized here. Called once from [onCreate()]
  35. */
  36. private fun initAdapter() {
  37. adapter = TodoAdapter(this, todos)
  38. rvTodos.layoutManager = LinearLayoutManager(this)
  39. rvTodos.adapter = adapter
  40. }
  41.  
  42. /**
  43. * Realm instance is initialized here. Called once from [onCreate()]
  44. */
  45. private fun initRealm() {
  46. Realm.init(this)
  47. val config = RealmConfiguration
  48. .Builder()
  49. .name("simplified_caching.realm")
  50. .schemaVersion(1)
  51. .deleteRealmIfMigrationNeeded()
  52. .build()
  53. Realm.setDefaultConfiguration(config)
  54. }
  55.  
  56. /**
  57. * LiveData observers are set here
  58. * [todoListLiveData] observes for changes to [todo] which is the data source for our [adapter]
  59. * [progressLiveData] observes a boolean which is responsible for flipping our [vfMain]
  60. */
  61. private fun setListeners() {
  62. progressLiveData.observe(this, Observer { isLoading ->
  63. vfMain.displayedChild = if(isLoading) 0 else 1
  64. })
  65.  
  66. todoListLiveData.observe(this, Observer { result ->
  67. val isFromDb = result.message == FROM_DB
  68. if(!isFromDb) {
  69. //presist data if changes observered are not from DB
  70. result.data.saveAll()
  71. }
  72. //for any change to [todos] clear list, add new data to list, notifiy adapter
  73. clearAndAllToList(result.data)
  74. adapter?.notifyDataSetChanged()
  75. })
  76. }
  77.  
  78. private fun clearAndAllToList(todosToAdd: List<Todo>?) {
  79. todos.clear()
  80. todosToAdd?.let { todos.addAll(it) }
  81. }
  82.  
  83. /**
  84. * Get cached data from Realm
  85. */
  86. private fun getLocalTodos() {
  87. progressLiveData.value = false
  88. val todosFromDb = Todo().queryAll()
  89. todoListLiveData.value = Result.ResultWrapper(todosFromDb, FROM_DB)
  90. }
  91.  
  92. /**
  93. * Get remote data from API using Retrofit
  94. */
  95. private fun getRemoteTodos() {
  96. launch {
  97. if(todos.isEmpty()) {
  98. progressLiveData.value = true
  99. }
  100. val todosFromApi = ApiController.retrofit.create<ApiInterface>().getTodos().await()
  101. progressLiveData.value = false
  102. todoListLiveData.value = Result.ResultWrapper(todosFromApi, FROM_API)
  103. }
  104. }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement