diff --git a/app/src/main/java/com/example/timetracker/data/db/model/Task.kt b/app/src/main/java/com/example/timetracker/data/db/model/Task.kt new file mode 100644 index 0000000..d250f99 --- /dev/null +++ b/app/src/main/java/com/example/timetracker/data/db/model/Task.kt @@ -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 + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/timetracker/data/db/repository/TaskRepository.kt b/app/src/main/java/com/example/timetracker/data/db/repository/TaskRepository.kt new file mode 100644 index 0000000..c68a5ec --- /dev/null +++ b/app/src/main/java/com/example/timetracker/data/db/repository/TaskRepository.kt @@ -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? { + return realm?.where(Task::class.java)?.findAll() + } + + fun getTasksAsync(): RealmResults? { + return realm?.where(Task::class.java)?.findAllAsync() + } + fun removeTask(task: Task) { + task.deleteFromRealm() + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/timetracker/ui/fragment/home/createTask/CreateTaskBottomSheetDialogViewModel.kt b/app/src/main/java/com/example/timetracker/ui/fragment/home/createTask/CreateTaskBottomSheetDialogViewModel.kt index e5bc3f2..818e56f 100644 --- a/app/src/main/java/com/example/timetracker/ui/fragment/home/createTask/CreateTaskBottomSheetDialogViewModel.kt +++ b/app/src/main/java/com/example/timetracker/ui/fragment/home/createTask/CreateTaskBottomSheetDialogViewModel.kt @@ -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("") @@ -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 }