Skip to content

Commit

Permalink
courses: smoother joining (fixes #5013) (#5015)
Browse files Browse the repository at this point in the history
Co-authored-by: dogi <[email protected]>
  • Loading branch information
Okuro3499 and dogi authored Jan 13, 2025
1 parent 3ac0e07 commit 44beed2
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 30 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 2194
versionName "0.21.94"
versionCode 2195
versionName "0.21.95"
ndkVersion '21.3.6528147'
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.google.gson.JsonObject
import io.realm.Realm
import io.realm.RealmObject
import io.realm.annotations.PrimaryKey
import kotlinx.coroutines.*
import org.ole.planet.myplanet.utilities.NetworkUtils
import java.util.Date
import java.util.UUID
Expand All @@ -23,17 +24,23 @@ open class RealmCourseActivity : RealmObject() {

companion object {
@JvmStatic
fun createActivity(realm: Realm, userModel: RealmUserModel?, course: RealmMyCourse?) {
if (!realm.isInTransaction) {
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
suspend fun createActivity(realm: Realm, userModel: RealmUserModel?, course: RealmMyCourse?) {
withContext(Dispatchers.IO) {
try {
if (!realm.isInTransaction) {
realm.executeTransaction { realmInstance ->
val activity = realmInstance.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
}
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import android.widget.SeekBar.OnSeekBarChangeListener
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.viewpager.widget.ViewPager
import io.realm.Realm
import kotlinx.coroutines.*
import org.ole.planet.myplanet.R
import org.ole.planet.myplanet.databinding.FragmentTakeCourseBinding
import org.ole.planet.myplanet.datamanager.DatabaseService
Expand Down Expand Up @@ -122,24 +124,47 @@ class TakeCourseFragment : Fragment(), ViewPager.OnPageChangeListener, View.OnCl
}

private fun setCourseData() {
if (userModel?.isGuest() != true && currentCourse?.userId?.contains(userModel?.id) != true) {
fragmentTakeCourseBinding.btnRemove.visibility = View.VISIBLE
fragmentTakeCourseBinding.btnRemove.text = getString(R.string.join)
getAlertDialog(requireActivity(), getString(R.string.do_you_want_to_join_this_course), getString(R.string.join_this_course)) { _: DialogInterface?, _: Int -> addRemoveCourse() }
} else {
fragmentTakeCourseBinding.btnRemove.visibility = View.GONE
}
createActivity(mRealm, userModel, currentCourse)
fragmentTakeCourseBinding.courseProgress.max = steps.size
updateStepDisplay(fragmentTakeCourseBinding.viewPager2.currentItem)
val isGuest = userModel?.isGuest() == true
val containsUserId = currentCourse?.userId?.contains(userModel?.id) == true
val stepsSize = steps.size
val currentItem = fragmentTakeCourseBinding.viewPager2.currentItem

lifecycleScope.launch {
withContext(Dispatchers.Main) {
if (!isGuest && !containsUserId) {
fragmentTakeCourseBinding.btnRemove.visibility = View.VISIBLE
fragmentTakeCourseBinding.btnRemove.text = getString(R.string.join)
getAlertDialog(requireActivity(), getString(R.string.do_you_want_to_join_this_course), getString(R.string.join_this_course)) { _: DialogInterface?, _: Int ->
addRemoveCourse()
}
} else {
fragmentTakeCourseBinding.btnRemove.visibility = View.GONE
}
}

if (currentCourse?.userId?.contains(userModel?.id) == true) {
fragmentTakeCourseBinding.nextStep.visibility = View.VISIBLE
fragmentTakeCourseBinding.courseProgress.visibility = View.VISIBLE
} else {
fragmentTakeCourseBinding.nextStep.visibility = View.GONE
fragmentTakeCourseBinding.previousStep.visibility = View.GONE
fragmentTakeCourseBinding.courseProgress.visibility = View.GONE
withContext(Dispatchers.IO) {
try {
Realm.getDefaultInstance().use { backgroundRealm ->
createActivity(backgroundRealm, userModel?.let { backgroundRealm.copyFromRealm(it) }, currentCourse?.let { backgroundRealm.copyFromRealm(it) })
}
} catch (e: Exception) {
e.printStackTrace()
}
}

withContext(Dispatchers.Main) {
fragmentTakeCourseBinding.courseProgress.max = stepsSize
updateStepDisplay(currentItem)

if (containsUserId) {
fragmentTakeCourseBinding.nextStep.visibility = View.VISIBLE
fragmentTakeCourseBinding.courseProgress.visibility = View.VISIBLE
} else {
fragmentTakeCourseBinding.nextStep.visibility = View.GONE
fragmentTakeCourseBinding.previousStep.visibility = View.GONE
fragmentTakeCourseBinding.courseProgress.visibility = View.GONE
}
}
}
}

Expand Down

0 comments on commit 44beed2

Please sign in to comment.