-
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.
[#20] Integrate Realm Kotlin to save survey data in local storage
- Loading branch information
Showing
9 changed files
with
93 additions
and
8 deletions.
There are no files selected for viewing
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
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
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
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
22 changes: 22 additions & 0 deletions
22
...rc/commonMain/kotlin/vn/luongvo/kmm/survey/data/local/datasource/SurveyLocalDataSource.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,22 @@ | ||
package vn.luongvo.kmm.survey.data.local.datasource | ||
|
||
import io.realm.kotlin.Realm | ||
import io.realm.kotlin.UpdatePolicy | ||
import io.realm.kotlin.ext.query | ||
import vn.luongvo.kmm.survey.data.local.model.SurveyRealmObject | ||
|
||
interface SurveyLocalDataSource { | ||
|
||
fun saveSurveys(surveys: List<SurveyRealmObject>) | ||
} | ||
|
||
class SurveyLocalDataSourceImpl(private val realm: Realm) : SurveyLocalDataSource { | ||
|
||
override fun saveSurveys(surveys: List<SurveyRealmObject>) { | ||
realm.writeBlocking { | ||
surveys.forEach { | ||
copyToRealm(it, updatePolicy = UpdatePolicy.ALL) | ||
} | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
shared/src/commonMain/kotlin/vn/luongvo/kmm/survey/data/local/model/SurveyRealmObject.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,36 @@ | ||
package vn.luongvo.kmm.survey.data.local.model | ||
|
||
import io.realm.kotlin.types.RealmObject | ||
import io.realm.kotlin.types.annotations.PrimaryKey | ||
import vn.luongvo.kmm.survey.domain.model.Survey | ||
|
||
class SurveyRealmObject : RealmObject { | ||
|
||
@PrimaryKey | ||
var id: String = "" | ||
var title: String = "" | ||
var description: String = "" | ||
var coverImageUrl: String = "" | ||
|
||
constructor( | ||
id: String, | ||
title: String, | ||
description: String, | ||
coverImageUrl: String | ||
) : this() { | ||
this.id = id | ||
this.title = title | ||
this.description = description | ||
this.coverImageUrl = coverImageUrl | ||
} | ||
|
||
constructor() | ||
} | ||
|
||
fun SurveyRealmObject.toSurvey() = Survey( | ||
id = id, | ||
title = title, | ||
description = description, | ||
coverImageUrl = coverImageUrl, | ||
questions = null | ||
) |
10 changes: 10 additions & 0 deletions
10
shared/src/commonMain/kotlin/vn/luongvo/kmm/survey/data/local/realm/Realm.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,10 @@ | ||
package vn.luongvo.kmm.survey.data.local.realm | ||
|
||
import io.realm.kotlin.Realm | ||
import io.realm.kotlin.RealmConfiguration | ||
import vn.luongvo.kmm.survey.data.local.model.SurveyRealmObject | ||
|
||
val realm: Realm by lazy { | ||
val configuration = RealmConfiguration.create(schema = setOf(SurveyRealmObject::class)) | ||
Realm.open(configuration) | ||
} |
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
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