-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#6 added task realm object and repository
- Loading branch information
Showing
3 changed files
with
84 additions
and
2 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
app/src/main/java/com/example/timetracker/data/db/model/Task.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
app/src/main/java/com/example/timetracker/data/db/repository/TaskRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters