Skip to content

Commit

Permalink
Merge branch 'master' into 4647-the-main-thread-is-blocked
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuljeet1998 authored Oct 16, 2024
2 parents ba16ea1 + 4554aa4 commit 3e32f43
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 62 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
applicationId "org.ole.planet.myplanet"
minSdkVersion 26
targetSdkVersion 34
versionCode 2030
versionName "0.20.30"
versionCode 2032
versionName "0.20.32"
ndkVersion '21.3.6528147'
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
Expand Down
90 changes: 55 additions & 35 deletions app/src/main/java/org/ole/planet/myplanet/MainApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import java.net.URL
import java.util.Date
import java.util.UUID
import java.util.concurrent.TimeUnit
import kotlinx.coroutines.runBlocking

class MainApplication : Application(), Application.ActivityLifecycleCallbacks {
companion object {
Expand Down Expand Up @@ -76,23 +77,30 @@ class MainApplication : Application(), Application.ActivityLifecycleCallbacks {
lateinit var defaultPref: SharedPreferences

fun createLog(type: String) {
service = DatabaseService(context)
val mRealm = service.realmInstance
if (!mRealm.isInTransaction) {
mRealm.beginTransaction()
}
val log = mRealm.createObject(RealmApkLog::class.java, "${UUID.randomUUID()}")
val model = UserProfileDbHandler(context).userModel
if (model != null) {
log.parentCode = model.parentCode
log.createdOn = model.planetCode
log.userId = model.id
runBlocking {
withContext(Dispatchers.IO) {
val realm = Realm.getDefaultInstance()
try {
realm.executeTransaction { r ->
val log = r.createObject(RealmApkLog::class.java, "${UUID.randomUUID()}")
val model = UserProfileDbHandler(context).userModel
if (model != null) {
log.parentCode = model.parentCode
log.createdOn = model.planetCode
log.userId = model.id
}
log.time = "${Date().time}"
log.page = ""
log.version = getVersionName(context)
log.type = type
}
} catch (e: Exception) {
e.printStackTrace()
} finally {
realm.close()
}
}
}
log.time = "${Date().time}"
log.page = ""
log.version = getVersionName(context)
log.type = type
mRealm.commitTransaction()
}

private fun applyThemeMode(themeMode: String?) {
Expand Down Expand Up @@ -129,6 +137,7 @@ class MainApplication : Application(), Application.ActivityLifecycleCallbacks {
responseCode in 200..299

} catch (e: Exception) {
e.printStackTrace()
false
}
}
Expand Down Expand Up @@ -266,38 +275,49 @@ class MainApplication : Application(), Application.ActivityLifecycleCallbacks {
if (isFirstLaunch) {
isFirstLaunch = false
} else {
val fromForeground = "foreground"
createLog(fromForeground)
applicationScope.launch {
createLog("foreground")
}
}
}

private fun onAppBackgrounded() {}

private fun onAppStarted() {
val newStart = "new login"
createLog(newStart)
applicationScope.launch {
createLog("new login")
}
}

private fun onAppClosed() {}

private fun handleUncaughtException(e: Throwable) {
e.printStackTrace()
if (!mRealm.isInTransaction) {
mRealm.beginTransaction()
}
val log = mRealm.createObject(RealmApkLog::class.java, "${UUID.randomUUID()}")
val model = UserProfileDbHandler(this).userModel
if (model != null) {
log.parentCode = model.parentCode
log.createdOn = model.planetCode
log.userId = model.id
runBlocking {
launch(Dispatchers.IO) {
try {
val realm = Realm.getDefaultInstance()
realm.executeTransaction { r ->
val log = r.createObject(RealmApkLog::class.java, "${UUID.randomUUID()}")
val model = UserProfileDbHandler(this@MainApplication).userModel
if (model != null) {
log.parentCode = model.parentCode
log.createdOn = model.planetCode
log.userId = model.id
}
log.time = "${Date().time}"
log.page = ""
log.version = getVersionName(this@MainApplication)
log.type = RealmApkLog.ERROR_TYPE_CRASH
log.setError(e)
}
realm.close()
} catch (ex: Exception) {
ex.printStackTrace()
}
}
}
log.time = "${Date().time}"
log.page = ""
log.version = getVersionName(this)
log.type = RealmApkLog.ERROR_TYPE_CRASH
log.setError(e)
mRealm.commitTransaction()

val homeIntent = Intent(Intent.ACTION_MAIN)
homeIntent.addCategory(Intent.CATEGORY_HOME)
homeIntent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import io.realm.Realm
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.ResponseBody
import org.ole.planet.myplanet.model.Download
import org.ole.planet.myplanet.model.RealmMyLibrary
Expand Down Expand Up @@ -207,14 +208,16 @@ class MyDownloadService : Service() {
private fun changeOfflineStatus() {
CoroutineScope(Dispatchers.IO).launch {
val currentFileName = getFileNameFromUrl(urls[currentIndex])
mRealm.executeTransaction { realm ->
realm.where(RealmMyLibrary::class.java)
.equalTo("resourceLocalAddress", currentFileName)
.findAll()
?.forEach {
it.resourceOffline = true
it.downloadedRev = it._rev
}
withContext(Dispatchers.Main) { // Switch to the main thread
mRealm.executeTransaction { realm ->
realm.where(RealmMyLibrary::class.java)
.equalTo("resourceLocalAddress", currentFileName)
.findAll()
?.forEach {
it.resourceOffline = true
it.downloadedRev = it._rev
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,17 @@ open class RealmCourseActivity : RealmObject() {
@JvmStatic
fun createActivity(realm: Realm, userModel: RealmUserModel?, course: RealmMyCourse?) {
if (!realm.isInTransaction) {
realm.beginTransaction()
realm.executeTransaction {
val activity = it.createObject(RealmCourseActivity::class.java, UUID.randomUUID().toString())
activity.type = "visit"
activity.title = course?.courseTitle
activity.courseId = course?.courseId
activity.time = Date().time
activity.parentCode = userModel?.parentCode
activity.createdOn = userModel?.planetCode
activity.user = userModel?.name
}
}
val activity = realm.createObject(RealmCourseActivity::class.java, UUID.randomUUID().toString())
activity.type = "visit"
activity.title = course?.courseTitle
activity.courseId = course?.courseId
activity.time = Date().time
activity.parentCode = userModel?.parentCode
activity.createdOn = userModel?.planetCode
activity.createdOn = userModel?.planetCode
activity.user = userModel?.name
realm.commitTransaction()
}

@JvmStatic
Expand All @@ -64,4 +63,4 @@ open class RealmCourseActivity : RealmObject() {
return ob
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class TakeCourseFragment : Fragment(), ViewPager.OnPageChangeListener, View.OnCl
val i = getCurrentProgress(steps, mRealm, userModel?.id, courseId)
if (i < steps.size) fragmentTakeCourseBinding.courseProgress.secondaryProgress = i + 1
fragmentTakeCourseBinding.courseProgress.progress = i
fragmentTakeCourseBinding.tvStep.text = String.format(Locale.getDefault(),getString(R.string.step) + " %d/%d", position, steps.size)
fragmentTakeCourseBinding.tvStep.text = String.format(Locale.getDefault(),"${getString(R.string.step)} %d/%d", position, steps.size)
}

private fun changeNextButtonState(position: Int) {
Expand All @@ -157,7 +157,7 @@ class TakeCourseFragment : Fragment(), ViewPager.OnPageChangeListener, View.OnCl
override fun onPageScrollStateChanged(state: Int) {}

private fun onClickNext() {
fragmentTakeCourseBinding.tvStep.text = String.format(Locale.getDefault(),getString(R.string.step)+ " %d/%d", fragmentTakeCourseBinding.viewPager2.currentItem, currentCourse?.courseSteps?.size)
fragmentTakeCourseBinding.tvStep.text = String.format(Locale.getDefault(),"${getString(R.string.step)} %d/%d", fragmentTakeCourseBinding.viewPager2.currentItem, currentCourse?.courseSteps?.size)
if (fragmentTakeCourseBinding.viewPager2.currentItem == currentCourse?.courseSteps?.size) {
fragmentTakeCourseBinding.nextStep.setTextColor(ContextCompat.getColor(requireContext(), R.color.md_grey_500))
fragmentTakeCourseBinding.nextStep.visibility = View.GONE
Expand All @@ -166,7 +166,7 @@ class TakeCourseFragment : Fragment(), ViewPager.OnPageChangeListener, View.OnCl
}

private fun onClickPrevious() {
fragmentTakeCourseBinding.tvStep.text = String.format(Locale.getDefault(),getString(R.string.step) + " %d/%d", fragmentTakeCourseBinding.viewPager2.currentItem - 1, currentCourse?.courseSteps?.size)
fragmentTakeCourseBinding.tvStep.text = String.format(Locale.getDefault(),"${getString(R.string.step)} %d/%d", fragmentTakeCourseBinding.viewPager2.currentItem - 1, currentCourse?.courseSteps?.size)
if (fragmentTakeCourseBinding.viewPager2.currentItem - 1 == 0) {
fragmentTakeCourseBinding.previousStep.visibility = View.GONE
fragmentTakeCourseBinding.nextStep.visibility = View.VISIBLE
Expand Down Expand Up @@ -217,7 +217,6 @@ class TakeCourseFragment : Fragment(), ViewPager.OnPageChangeListener, View.OnCl
val realm = DatabaseService(requireActivity()).realmInstance
val user = UserProfileDbHandler(requireActivity()).userModel
val courseProgressMap = RealmCourseProgress.getCourseProgress(realm, user?.id)
// Extract the current progress for the specific courseId
val courseProgress = courseProgressMap[courseId]?.asJsonObject?.get("current")?.asInt
return courseProgress ?: 0
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.coroutines.launch
import kotlinx.serialization.json.Json
import okhttp3.ResponseBody
import org.ole.planet.myplanet.BuildConfig
import org.ole.planet.myplanet.MainApplication.Companion.applicationScope
import org.ole.planet.myplanet.MainApplication.Companion.context
import org.ole.planet.myplanet.MainApplication.Companion.createLog
import org.ole.planet.myplanet.R
Expand Down Expand Up @@ -349,7 +351,9 @@ abstract class SyncActivity : ProcessUserDataActivity(), SyncListener, CheckVers
syncIconDrawable.stop()
syncIconDrawable.selectDrawable(0)
syncIcon.invalidateDrawable(syncIconDrawable)
createLog("synced successfully")
applicationScope.launch {
createLog("synced successfully")
}
showSnack(findViewById(android.R.id.content), getString(R.string.sync_completed))
downloadAdditionalResources()
if (defaultPref.getBoolean("beta_auto_download", false)) {
Expand Down

0 comments on commit 3e32f43

Please sign in to comment.