Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update realm in build gradle and start models migration (fixes #4773) #4774

Draft
wants to merge 44 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
6724903
update realm in build gradle and start models migration
Okuro3499 Nov 13, 2024
f307e8c
Merge branch 'master' into 4773-migrate-to-realm-kotlin-sdk
Okuro3499 Nov 20, 2024
2db88db
more models migrated to kotlin
Okuro3499 Nov 20, 2024
f95444b
Merge branch 'master' into 4773-migrate-to-realm-kotlin-sdk
Okuro3499 Nov 22, 2024
da5e814
migrate more realm models to the kotlin sdk
Okuro3499 Nov 22, 2024
4accf1a
Update DatabaseService.kt
Okuro3499 Nov 22, 2024
bd69e86
update file class name
Okuro3499 Nov 25, 2024
3288ea7
Merge branch '4773-migrate-to-realm-kotlin-sdk' of https://github.com…
Okuro3499 Nov 25, 2024
dbfe2b7
Merge branch 'master' into 4773-migrate-to-realm-kotlin-sdk
Okuro3499 Nov 25, 2024
94135c3
migrate more file to kotlin realm sdk
Okuro3499 Nov 26, 2024
254dd48
migrate realmMylife
Okuro3499 Dec 3, 2024
f5f95ce
migrate realm rating
Okuro3499 Dec 6, 2024
dcee5de
migrate realm feedback
Okuro3499 Dec 6, 2024
8064d85
migrate realm myhealthpojo
Okuro3499 Dec 6, 2024
ae8c364
migrate realm realmStepExam and realmExamQuestion
Okuro3499 Dec 6, 2024
27389ba
delete commented code
Okuro3499 Dec 6, 2024
3d6fa7f
migrate more classes to kotlin realm
Okuro3499 Dec 9, 2024
443631a
migrate realmMyTeam
Okuro3499 Dec 10, 2024
3f4ff4a
migrate realmMyCourse and course progress
Okuro3499 Dec 10, 2024
bf5931f
migrate realmMyLibrary
Okuro3499 Dec 11, 2024
1a85145
migrate mainapplication
Okuro3499 Dec 11, 2024
28b82d0
cleanup
Okuro3499 Dec 11, 2024
6de988b
migrate baseContainer fragment
Okuro3499 Dec 11, 2024
b8fcf7c
migrate baseNewsFragment
Okuro3499 Dec 11, 2024
b62f96c
migrate baseRecyclerFragment
Okuro3499 Dec 11, 2024
381b3ab
migrate baseRecyclerParentFragment
Okuro3499 Dec 11, 2024
158d240
migrate baseRecyclerFragment
Okuro3499 Dec 13, 2024
588bc99
migrate manager sync
Okuro3499 Dec 13, 2024
74f6229
migrate myDownload service
Okuro3499 Dec 13, 2024
60b1a51
migrate service class to the kotlin realmsdk
Okuro3499 Dec 13, 2024
b4bc457
Merge branch 'master' into 4773-migrate-to-realm-kotlin-sdk
Okuro3499 Dec 13, 2024
4dbe865
resolve realmCourseProgress error
Okuro3499 Dec 17, 2024
7063068
migrate syncManager and task notification worker
Okuro3499 Dec 18, 2024
3f94d5f
migrate transaction syncManager
Okuro3499 Dec 18, 2024
31f96b6
Merge branch 'master' into 4773-migrate-to-realm-kotlin-sdk
Okuro3499 Dec 18, 2024
56c88fb
migrate service to realm kotlin sdk
Okuro3499 Dec 20, 2024
65d7834
migrate uploadManager to realm kotlin sdk
Okuro3499 Dec 20, 2024
9f11501
migrate uploadToShelfService to realm kotlin sdk
Okuro3499 Dec 20, 2024
0afd532
migrate UserProfileDbHandler to realm kotlin sdk
Okuro3499 Dec 20, 2024
5512adb
migrate settingsActivity
Okuro3499 Dec 23, 2024
ad0920f
migrate chatDetailFragment
Okuro3499 Dec 23, 2024
15af9d0
migrate chatHistoryListAdapter
Okuro3499 Dec 23, 2024
7629826
migrate chatHistoryListFragment
Okuro3499 Dec 23, 2024
6155cb2
migrate AddLinkFragment
Okuro3499 Dec 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
migrate service class to the kotlin realmsdk
Okuro3499 committed Dec 13, 2024
commit 60b1a5152509e4a49c75b422f53d548991c9e36c
141 changes: 72 additions & 69 deletions app/src/main/java/org/ole/planet/myplanet/datamanager/Service.kt
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ import android.net.Uri
import android.text.TextUtils
import com.google.gson.Gson
import com.google.gson.JsonObject
import io.realm.Realm
import io.realm.kotlin.Realm
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
@@ -190,7 +190,9 @@ class Service(private val context: Context) {
override fun onResponse(call: Call<JsonObject>, response: Response<JsonObject>) {
if (response.body() != null && response.body()!!.has("id")) {
uploadToShelf(obj)
saveUserToDb(realm, response.body()!!.get("id").asString, obj, callback)
serviceScope.launch {
saveUserToDb(realm, response.body()!!.get("id").asString, obj, callback)
}
} else {
callback.onSuccess("Unable to create user")
}
@@ -211,21 +213,27 @@ class Service(private val context: Context) {

override fun notAvailable() {
val settings = MainApplication.context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
if (isUserExists(realm, obj["name"].asString)) {
callback.onSuccess("User already exists")
return
}
realm.beginTransaction()
val model = populateUsersTable(obj, realm, settings)
val keyString = generateKey()
val iv = generateIv()
if (model != null) {
model.key = keyString
model.iv = iv
serviceScope.launch {
if (isUserExists(realm, obj["name"].asString)) {
callback.onSuccess("User already exists")
return@launch
}

val model = populateUsersTable(obj, realm, settings)
realm.write {
val keyString = generateKey()
val iv = generateIv()
if (model != null) {
findLatest(model)?.apply {
key = keyString
this.iv = iv
}
}
}

Utilities.toast(MainApplication.context, "Not connected to planet, created user offline.")
callback.onSuccess("Not connected to planet, created user offline.")
}
realm.commitTransaction()
Utilities.toast(MainApplication.context, "Not connected to planet, created user offline.")
callback.onSuccess("Not connected to planet, created user offline.")
}
})
}
@@ -238,86 +246,81 @@ class Service(private val context: Context) {
})
}

private fun saveUserToDb(realm: Realm, id: String, obj: JsonObject, callback: CreateUserCallback) {
private suspend fun saveUserToDb(realm: Realm, id: String, obj: JsonObject, callback: CreateUserCallback) {
val settings = MainApplication.context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
realm.executeTransactionAsync({ realm1: Realm? ->
try {
try {
withContext(Dispatchers.IO) {
val res = retrofitInterface?.getJsonObject(Utilities.header, Utilities.getUrl() + "/_users/" + id)?.execute()
if (res?.body() != null) {
val model = populateUsersTable(res.body(), realm1, settings)
if (model != null) {
UploadToShelfService(MainApplication.context).saveKeyIv(retrofitInterface, model, obj)
val model = populateUsersTable(res.body(), realm, settings)
realm.write {
if (model != null) {
UploadToShelfService(MainApplication.context).saveKeyIv(retrofitInterface, model, obj)
}
}
}
} catch (e: IOException) {
e.printStackTrace()
}
}, {
callback.onSuccess("User created successfully")
isNetworkConnectedFlow.onEach { isConnected ->
if (isConnected) {
val serverUrl = settings.getString("serverURL", "")
if (!serverUrl.isNullOrEmpty()) {
serviceScope.launch {
val canReachServer = withContext(Dispatchers.IO) {
isServerReachable(serverUrl)
}
if (canReachServer) {
if (context is ProcessUserDataActivity) {
context.runOnUiThread {
context.startUpload("becomeMember")
}
val canReachServer = withContext(Dispatchers.IO) {
isServerReachable(serverUrl)
}
if (canReachServer) {
if (context is ProcessUserDataActivity) {
withContext(Dispatchers.Main) {
context.startUpload("becomeMember")
}
TransactionSyncManager.syncDb(realm, "tablet_users")
}
TransactionSyncManager.syncDb(realm, "tablet_users")
}
}
}
}.launchIn(serviceScope)
}) { error: Throwable ->
} catch (error: Exception) {
error.printStackTrace()
callback.onSuccess("Unable to save user please sync")
}
}

fun syncPlanetServers(realm: Realm, callback: SuccessListener) {
retrofitInterface?.getJsonObject("", "https://planet.earth.ole.org/db/communityregistrationrequests/_all_docs?include_docs=true")?.enqueue(object : Callback<JsonObject?> {
override fun onResponse(call: Call<JsonObject?>, response: Response<JsonObject?>) {
if (response.body() != null) {
val arr = JsonUtils.getJsonArray("rows", response.body())
if (!realm.isClosed) {
realm.executeTransactionAsync({ realm1: Realm ->
realm1.delete(RealmCommunity::class.java)
for (j in arr) {
var jsonDoc = j.asJsonObject
jsonDoc = JsonUtils.getJsonObject("doc", jsonDoc)
val id = JsonUtils.getString("_id", jsonDoc)
val community = realm1.createObject(RealmCommunity::class.java, id)
if (JsonUtils.getString("name", jsonDoc) == "learning") {
community.weight = 0
}
community.localDomain = JsonUtils.getString("localDomain", jsonDoc)
community.name = JsonUtils.getString("name", jsonDoc)
community.parentDomain = JsonUtils.getString("parentDomain", jsonDoc)
community.registrationRequest = JsonUtils.getString("registrationRequest", jsonDoc)
suspend fun syncPlanetServers(realm: Realm, callback: SuccessListener) {
try {
val response = withContext(Dispatchers.IO) {
retrofitInterface?.getJsonObject("", "https://planet.earth.ole.org/db/communityregistrationrequests/_all_docs?include_docs=true")?.execute()
}

if (response?.body() != null) {
val arr = JsonUtils.getJsonArray("rows", response.body())
realm.write {
// Delete existing communities
query<RealmCommunity>(RealmCommunity::class).find().forEach { delete(it) }
// Add new communities
arr.forEach { j ->
var jsonDoc = j.asJsonObject
jsonDoc = JsonUtils.getJsonObject("doc", jsonDoc)
val id = JsonUtils.getString("_id", jsonDoc)

val community = RealmCommunity().apply {
this.id = id
if (JsonUtils.getString("name", jsonDoc) == "learning") {
weight = 0
}
}, {
realm.close()
callback.onSuccess("Server sync successfully")
}) { error: Throwable ->
realm.close()
error.printStackTrace()
callback.onSuccess("Unable to connect to planet earth")
localDomain = JsonUtils.getString("localDomain", jsonDoc)
name = JsonUtils.getString("name", jsonDoc)
parentDomain = JsonUtils.getString("parentDomain", jsonDoc)
registrationRequest = JsonUtils.getString("registrationRequest", jsonDoc)
}
copyToRealm(community)
}
}
callback.onSuccess("Server sync successfully")
}

override fun onFailure(call: Call<JsonObject?>, t: Throwable) {
realm.close()
callback.onSuccess("Unable to connect to planet earth")
}
})
} catch (error: Exception) {
error.printStackTrace()
callback.onSuccess("Unable to connect to planet earth")
}
}

fun getMinApk(listener: ConfigurationIdListener?, url: String, pin: String, activity: SyncActivity) {