Skip to content

Instantly share code, notes, and snippets.

@manijshrestha
Created June 3, 2017 05:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manijshrestha/a71676fc23640b3cb27f49cc5d2168a5 to your computer and use it in GitHub Desktop.
Save manijshrestha/a71676fc23640b3cb27f49cc5d2168a5 to your computer and use it in GitHub Desktop.
package com.manijshrestha.todolist.ui
import com.manijshrestha.todolist.data.Task
import com.manijshrestha.todolist.data.TaskDao
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.schedulers.Schedulers
import javax.inject.Inject
class ToDoPresenter @Inject constructor(val taskDao: TaskDao) {
val compositeDisposable = CompositeDisposable()
var tasks = ArrayList<Task>()
var presentation: ToDoPresentation? = null
fun onCreate(toDoPresentation: ToDoPresentation) {
presentation = toDoPresentation
loadTasks()
}
fun onDestroy() {
compositeDisposable.dispose()
presentation = null
}
fun loadTasks() {
compositeDisposable.add(taskDao.getAllTasks()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
tasks.clear()
tasks.addAll(it)
(tasks.size - 1).takeIf { it >= 0 }?.let {
presentation?.taskAddedAt(it)
presentation?.scrollTo(it)
}
}))
presentation?.showTasks(tasks)
}
fun addNewTask(taskDescription: String) {
val newTask = Task(description = taskDescription)
compositeDisposable.add(Observable.fromCallable { taskDao.insertTask(newTask) }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment