Skip to content

Commit

Permalink
#6 added task realm object and repository
Browse files Browse the repository at this point in the history
  • Loading branch information
nikol412 committed Mar 3, 2021
1 parent 838b2ed commit ee61cde
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
43 changes: 43 additions & 0 deletions app/src/main/java/com/example/timetracker/data/db/model/Task.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.example.timetracker.data.db.model

import io.realm.RealmObject
import io.realm.annotations.PrimaryKey

open class Task : RealmObject {

@PrimaryKey
private var id = 0

var title: String = ""
var date: String = ""

var description: String = ""

constructor(): super()

constructor(title: String, date: String, description: String) {
this.title = title
this.date = date
this.description = description
}

override fun equals(other: Any?): Boolean {

if(this === other) return true
if (other !is Task) return false

if(title != other.title) return false
if(date != other.date) return false
if(description != other.description) return false

return true
}

override fun hashCode(): Int {
var result = title.hashCode()
result = 31 * result + date.hashCode()
result = 31 * result + description.hashCode()

return result
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.example.timetracker.data.db.repository

import com.example.timetracker.data.db.IRepository
import com.example.timetracker.data.db.model.Task
import io.realm.Realm
import io.realm.RealmResults
import javax.inject.Inject

class TaskRepository @Inject constructor() : IRepository {
var realm: Realm? = Realm.getDefaultInstance()

override fun close() {
realm?.close()
}

fun createTask(task: Task) {
realm?.executeTransaction { realm ->
realm.copyToRealmOrUpdate(task)
}
}

fun getTasks(): RealmResults<Task>? {
return realm?.where(Task::class.java)?.findAll()
}

fun getTasksAsync(): RealmResults<Task>? {
return realm?.where(Task::class.java)?.findAllAsync()
}
fun removeTask(task: Task) {
task.deleteFromRealm()
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.example.timetracker.ui.fragment.home.createTask

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.example.timetracker.App
import com.example.timetracker.common.live.SingleLiveEvent
import com.example.timetracker.data.db.model.Task
import com.example.timetracker.data.db.repository.TaskRepository
import com.example.timetracker.ui.base.BaseViewModel
import javax.inject.Inject

class CreateTaskBottomSheetDialogViewModel: BaseViewModel() {
class CreateTaskBottomSheetDialogViewModel : BaseViewModel() {

@Inject
lateinit var taskRepository: TaskRepository

val taskName = MutableLiveData("")
val taskDescription = MutableLiveData("")
Expand All @@ -19,6 +24,8 @@ class CreateTaskBottomSheetDialogViewModel: BaseViewModel() {
}

fun onCreateTaskClick() {
App.appComponent?.inject(this)
taskRepository.createTask(Task(taskName.value!!, taskDate.value!!, taskDescription.value!!))
event.value = CreateTaskEvent.ADD_TASK
}

Expand Down

0 comments on commit ee61cde

Please sign in to comment.