Permalink
Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign up| package com.vferreirati.noteskotlinmvvm.repository | |
| import android.content.Context | |
| import android.os.AsyncTask | |
| import com.vferreirati.noteskotlinmvvm.data.Note | |
| import com.vferreirati.noteskotlinmvvm.data.NoteDao | |
| import com.vferreirati.noteskotlinmvvm.data.NoteDatabase | |
| class NotesRepository(context: Context) { | |
| private val noteDao: NoteDao? = NoteDatabase.getInstance(context)?.noteDao() | |
| val allNotes = noteDao?.getAll() | |
| fun insert(note: Note) { | |
| InsertTask(noteDao).execute(note) | |
| } | |
| fun deleteAll() { | |
| DeleteAllTask(noteDao).execute() | |
| } | |
| fun delete(note: Note) { | |
| DeleteOneTask(noteDao).execute(note) | |
| } | |
| class InsertTask(private val noteDao: NoteDao?) : AsyncTask<Note, Unit, Unit>() { | |
| override fun doInBackground(vararg params: Note) { | |
| noteDao?.insert(params[0]) | |
| } | |
| } | |
| class DeleteAllTask(private val noteDao: NoteDao?) : AsyncTask<Unit, Unit, Unit>() { | |
| override fun doInBackground(vararg params: Unit?) { | |
| noteDao?.deleteAll() | |
| } | |
| } | |
| class DeleteOneTask(private val noteDao: NoteDao?) : AsyncTask<Note, Unit, Unit>() { | |
| override fun doInBackground(vararg params: Note) { | |
| noteDao?.delete(params[0]) | |
| } | |
| } | |
| } |