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
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 2 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlinx-serialization'
apply plugin: 'dagger.hilt.android.plugin'
apply plugin: 'realm-android'

android {
compileSdk 35
defaultConfig {
Expand Down Expand Up @@ -233,9 +233,7 @@ dependencies {
implementation(platform("org.jetbrains.kotlin:kotlin-bom:2.1.0"))

implementation 'com.github.chrisbanes:PhotoView:2.3.0'
}
realm {
syncEnabled = true
implementation "io.realm.kotlin:library-base:1.11.0"
}
kapt{
correctErrorTypes true
Expand Down
71 changes: 34 additions & 37 deletions app/src/main/java/org/ole/planet/myplanet/MainApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,15 @@ import androidx.preference.PreferenceManager
import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.PeriodicWorkRequest
import androidx.work.WorkManager
import io.realm.Realm
import io.realm.kotlin.Realm
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import org.ole.planet.myplanet.MainApplication
import org.ole.planet.myplanet.base.BaseResourceFragment.Companion.backgroundDownload
import org.ole.planet.myplanet.base.BaseResourceFragment.Companion.getAllLibraryList
import org.ole.planet.myplanet.callback.TeamPageListener
Expand Down Expand Up @@ -75,29 +73,27 @@ class MainApplication : Application(), Application.ActivityLifecycleCallbacks {
val applicationScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
lateinit var defaultPref: SharedPreferences

fun createLog(type: String) {
runBlocking {
withContext(Dispatchers.IO) {
val realm = Realm.getDefaultInstance()
try {
realm.executeTransaction { r ->
val log = r.createObject(RealmApkLog::class.java, "${UUID.randomUUID()}")
suspend fun createLog(type: String) {
withContext(Dispatchers.IO) {
try {
mRealm.write {
val log = RealmApkLog().apply {
id = UUID.randomUUID().toString()
val model = UserProfileDbHandler(context).userModel
if (model != null) {
log.parentCode = model.parentCode
log.createdOn = model.planetCode
log.userId = model.id
parentCode = model.parentCode
createdOn = model.planetCode
userId = model.id
}
log.time = "${Date().time}"
log.page = ""
log.version = getVersionName(context)
log.type = type
time = Date().time.toString()
page = ""
version = getVersionName(context)
this.type = type
}
} catch (e: Exception) {
e.printStackTrace()
} finally {
realm.close()
copyToRealm(log)
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
Expand Down Expand Up @@ -153,24 +149,22 @@ class MainApplication : Application(), Application.ActivityLifecycleCallbacks {
e.printStackTrace()
applicationScope.launch(Dispatchers.IO) {
try {
val realm = Realm.getDefaultInstance()
try {
realm.executeTransaction { r ->
val log = r.createObject(RealmApkLog::class.java, "${UUID.randomUUID()}")
mRealm.write {
val log = RealmApkLog().apply {
id = UUID.randomUUID().toString()
val model = UserProfileDbHandler(context).userModel
if (model != null) {
log.parentCode = model.parentCode
log.createdOn = model.planetCode
log.userId = model.id
parentCode = model.parentCode
createdOn = model.planetCode
userId = model.id
}
log.time = "${Date().time}"
log.page = ""
log.version = getVersionName(context)
log.type = RealmApkLog.ERROR_TYPE_CRASH
log.setError(e)
time = Date().time.toString()
page = ""
version = getVersionName(context)
type = RealmApkLog.ERROR_TYPE_CRASH
setError(e)
}
} finally {
realm.close()
copyToRealm(log)
}
} catch (ex: Exception) {
ex.printStackTrace()
Expand All @@ -195,7 +189,7 @@ class MainApplication : Application(), Application.ActivityLifecycleCallbacks {
startListenNetworkState()

preferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE)
service = DatabaseService(context)
service = DatabaseService()
mRealm = service.realmInstance
defaultPref = PreferenceManager.getDefaultSharedPreferences(this)

Expand All @@ -213,7 +207,9 @@ class MainApplication : Application(), Application.ActivityLifecycleCallbacks {
scheduleTaskNotificationWork()

Thread.setDefaultUncaughtExceptionHandler { _: Thread?, e: Throwable ->
handleUncaughtException(e)
applicationScope.launch {
handleUncaughtException(e)
}
}
registerActivityLifecycleCallbacks(this)
onAppStarted()
Expand Down Expand Up @@ -334,6 +330,7 @@ class MainApplication : Application(), Application.ActivityLifecycleCallbacks {
override fun onTerminate() {
super.onTerminate()
onAppClosed()
mRealm.close()
applicationScope.cancel()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.view.ContextThemeWrapper
import androidx.appcompat.widget.AppCompatRatingBar
import androidx.core.content.FileProvider
import androidx.lifecycle.lifecycleScope
import com.google.gson.JsonObject
import kotlinx.coroutines.launch
import org.ole.planet.myplanet.MainApplication
import org.ole.planet.myplanet.R
import org.ole.planet.myplanet.base.PermissionActivity.Companion.hasInstallPermission
Expand Down Expand Up @@ -106,35 +107,33 @@ abstract class BaseContainerFragment : BaseResourceFragment() {
}
private fun openIntent(items: RealmMyLibrary, typeClass: Class<*>?) {
val fileOpenIntent = Intent(activity, typeClass)
if (items.resourceLocalAddress?.contains("ole/audio") == true || items.resourceLocalAddress?.contains("ole/video") == true) {
if (items.resourceLocalAddress.contains("ole/audio") == true || items.resourceLocalAddress.contains("ole/video") == true) {
fileOpenIntent.putExtra("TOUCHED_FILE", items.resourceLocalAddress)
fileOpenIntent.putExtra("RESOURCE_TITLE", items.title)
} else {
fileOpenIntent.putExtra("TOUCHED_FILE", items.id + "/" + items.resourceLocalAddress)
fileOpenIntent.putExtra("TOUCHED_FILE", "${items.id}/${items.resourceLocalAddress}")
fileOpenIntent.putExtra("RESOURCE_TITLE", items.title)
}
startActivity(fileOpenIntent)
}
private fun openPdf(item: RealmMyLibrary) {
val fileOpenIntent = Intent(activity, PDFReaderActivity::class.java)
fileOpenIntent.putExtra("TOUCHED_FILE", item.id + "/" + item.resourceLocalAddress)
fileOpenIntent.putExtra("TOUCHED_FILE", "${item.id}/${item.resourceLocalAddress}")
fileOpenIntent.putExtra("resourceId", item.id)
startActivity(fileOpenIntent)
}

fun openResource(items: RealmMyLibrary) {
val matchingItems = mRealm.where(RealmMyLibrary::class.java)
.equalTo("resourceLocalAddress", items.resourceLocalAddress)
.findAll()
val matchingItems = MainApplication.mRealm.query<RealmMyLibrary>(RealmMyLibrary::class, "resourceLocalAddress == $0", items.resourceLocalAddress).find()
val anyOffline = matchingItems.any { it.isResourceOffline() }
if (anyOffline) {
val offlineItem = matchingItems.first { it.isResourceOffline()}
val offlineItem = matchingItems.first { it.isResourceOffline() }
openFileType(offlineItem, "offline")
} else {
if (items.isResourceOffline()) {
openFileType(items, "offline")
} else if (FileUtils.getFileExtension(items.resourceLocalAddress) == "mp4") {
openFileType(items, "online")
openFileType(items, "online")
} else {
val arrayList = ArrayList<String>()
arrayList.add(Utilities.getUrl(items))
Expand All @@ -145,38 +144,41 @@ abstract class BaseContainerFragment : BaseResourceFragment() {
}

private fun checkFileExtension(items: RealmMyLibrary) {
val filenameArray = items.resourceLocalAddress?.split("\\.".toRegex())?.toTypedArray()
val extension = filenameArray?.get(filenameArray.size - 1)
val filenameArray = items.resourceLocalAddress.split("\\.".toRegex()).toTypedArray()
val extension = filenameArray[filenameArray.size - 1]
val mimetype = Utilities.getMimeType(items.resourceLocalAddress)

if (mimetype != null) {
if (mimetype.contains("image")) {
openIntent(items, ImageViewerActivity::class.java)
} else if (mimetype.contains("pdf")) {
openPdf(items)
} else if (mimetype.contains("audio")) {
openIntent(items, AudioPlayerActivity::class.java)
} else {
checkMoreFileExtensions(extension, items)
lifecycleScope.launch {
if (mimetype != null) {
if (mimetype.contains("image")) {
openIntent(items, ImageViewerActivity::class.java)
} else if (mimetype.contains("pdf")) {
openPdf(items)
} else if (mimetype.contains("audio")) {
openIntent(items, AudioPlayerActivity::class.java)
} else {
checkMoreFileExtensions(extension, items)
}
}
}
}

private fun checkMoreFileExtensions(extension: String?, items: RealmMyLibrary) {
when (extension) {
"txt" -> {
openIntent(items, TextFileViewerActivity::class.java)
}
"md" -> {
openIntent(items, MarkdownViewerActivity::class.java)
}
"csv" -> {
openIntent(items, CSVViewerActivity::class.java)
}
"apk" -> {
installApk(items)
lifecycleScope.launch {
when (extension) {
"txt" -> {
openIntent(items, TextFileViewerActivity::class.java)
}
"md" -> {
openIntent(items, MarkdownViewerActivity::class.java)
}
"csv" -> {
openIntent(items, CSVViewerActivity::class.java)
}
"apk" -> {
installApk(items)
}
}
else -> Toast.makeText(activity, getString(R.string.this_file_type_is_currently_unsupported), Toast.LENGTH_LONG).show()
}
}

Expand All @@ -188,14 +190,14 @@ abstract class BaseContainerFragment : BaseResourceFragment() {
throw RuntimeException("Failed to create directory: " + directory.absolutePath)
}
}
val apkFile = items.resourceLocalAddress?.let { File(directory, it) }
if (apkFile != null) {
val apkFile = File(directory, items.resourceLocalAddress)
if (true) {
if (!apkFile.exists()) {
Utilities.toast(activity,"APK file not found")
return
}
}
val uri = apkFile?.let {
val uri = apkFile.let {
FileProvider.getUriForFile(MainApplication.context, "${MainApplication.context.packageName}.fileprovider", it)
}
val intent = Intent(Intent.ACTION_VIEW).apply {
Expand Down Expand Up @@ -241,7 +243,7 @@ abstract class BaseContainerFragment : BaseResourceFragment() {
bundle.putString("videoURL", "" + Utilities.getUrl(items))
bundle.putString("Auth", "" + auth)
} else if (videoType == "offline") {
if (items.resourceRemoteAddress == null && items.resourceLocalAddress != null) {
if (false) {
bundle.putString("videoURL", items.resourceLocalAddress)
} else {
bundle.putString("videoURL", "" + Uri.fromFile(File("" + FileUtils.getSDPathFromUrl(items.resourceRemoteAddress))))
Expand Down
Loading
Loading