Skip to content

Commit

Permalink
fix: Fixes according to QA feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
PavloNetrebchuk committed Jun 24, 2024
1 parent 27069f7 commit f1642ba
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 14 deletions.
9 changes: 5 additions & 4 deletions core/src/main/java/org/openedx/core/data/api/CourseApi.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.openedx.core.data.api

import okhttp3.MultipartBody
import org.openedx.core.data.model.AnnouncementModel
import org.openedx.core.data.model.BlocksCompletionBody
import org.openedx.core.data.model.CourseComponentStatus
Expand All @@ -10,11 +11,11 @@ import org.openedx.core.data.model.CourseStructureModel
import org.openedx.core.data.model.HandoutsModel
import org.openedx.core.data.model.ResetCourseDates
import retrofit2.http.Body
import retrofit2.http.FieldMap
import retrofit2.http.FormUrlEncoded
import retrofit2.http.GET
import retrofit2.http.Header
import retrofit2.http.Multipart
import retrofit2.http.POST
import retrofit2.http.Part
import retrofit2.http.Path
import retrofit2.http.Query

Expand Down Expand Up @@ -79,11 +80,11 @@ interface CourseApi {
@Query("requested_fields") fields: List<String> = emptyList()
): CourseEnrollments

@FormUrlEncoded
@Multipart
@POST("/courses/{course_id}/xblock/{block_id}/handler/xmodule_handler/problem_check")
suspend fun submitOfflineXBlockProgress(
@Path("course_id") courseId: String,
@Path("block_id") blockId: String,
@FieldMap progress: Map<String, String>
@Part progress: List<MultipartBody.Part>
)
}
2 changes: 1 addition & 1 deletion core/src/main/java/org/openedx/core/extension/LongExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fun Long.toFileSize(round: Int = 2, space: Boolean = true): String {
val units = arrayOf("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
val digitGroups = (log10(this.toDouble()) / log10(1024.0)).toInt()
val size = this / 1024.0.pow(digitGroups.toDouble())
val formatString = if (size % 1 < 0.05) "%.0f" else "%.${round}f"
val formatString = if (size % 1 < 0.05 || size % 1 >= 0.95) "%.0f" else "%.${round}f"
return String.format(formatString, size) + if (space) " " else "" + units[digitGroups]
} catch (e: Exception) {
println(e.toString())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.openedx.course.data.repository

import kotlinx.coroutines.flow.map
import okhttp3.MultipartBody
import org.openedx.core.ApiConstants
import org.openedx.core.data.api.CourseApi
import org.openedx.core.data.model.BlocksCompletionBody
Expand All @@ -13,6 +14,8 @@ import org.openedx.core.exception.NoCachedDataException
import org.openedx.core.module.db.DownloadDao
import org.openedx.core.system.connection.NetworkConnection
import org.openedx.course.data.storage.CourseDao
import java.net.URLDecoder
import java.nio.charset.StandardCharsets

class CourseRepository(
private val api: CourseApi,
Expand Down Expand Up @@ -120,11 +123,14 @@ class CourseRepository(

private suspend fun submitOfflineXBlockProgress(blockId: String, courseId: String, jsonProgressData: String?) {
if (!jsonProgressData.isNullOrEmpty()) {
val progressMap = jsonProgressData
.split("&")
.map { it.split("=") }
.associate { it[0] to it[1] }
api.submitOfflineXBlockProgress(courseId, blockId, progressMap)
val parts = mutableListOf<MultipartBody.Part>()
val decodedQuery = URLDecoder.decode(jsonProgressData, StandardCharsets.UTF_8.name())
val keyValuePairs = decodedQuery.split("&")
for (pair in keyValuePairs) {
val (key, value) = pair.split("=")
parts.add(MultipartBody.Part.createFormData(key, value))
}
api.submitOfflineXBlockProgress(courseId, blockId, parts)
downloadDao.removeOfflineXBlockProgress(listOf(blockId))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,10 @@ class CourseOutlineViewModel(
val outdatedBlockIds = xBlocks
.filter { block ->
val savedBlock = savedDownloadModelsMap[block.id]
savedBlock != null && block.offlineDownload?.lastModified != savedBlock.lastModified
val isOutdated =
savedBlock != null && block.offlineDownload?.lastModified != savedBlock.lastModified
val isNotDownloaded = savedBlock == null
isOutdated || isNotDownloaded
}
.map { it.id }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class HtmlUnitFragment : Fragment() {
},
saveXBlockProgress = { jsonProgress ->
viewModel.saveXBlockProgress(jsonProgress)
}
},
)
} else {
ConnectionErrorView(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.openedx.course.presentation.unit.html

import android.content.res.AssetManager
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
Expand Down Expand Up @@ -42,7 +43,6 @@ class HtmlUnitViewModel(

init {
tryToSyncProgress()
getXBlockProgress(blockId)
}

fun setWebPageLoaded(assets: AssetManager) {
Expand All @@ -56,6 +56,7 @@ class HtmlUnitViewModel(
assets.readAsText("js_injection/survey_css.js")?.let { jsList.add(it) }

_injectJSList.value = jsList
getXBlockProgress()
}

fun notifyCompletionSet() {
Expand Down Expand Up @@ -84,10 +85,11 @@ class HtmlUnitViewModel(
}
}

private fun getXBlockProgress(blockId: String) {
private fun getXBlockProgress() {
viewModelScope.launch {
if (!isOnline) {
val xBlockProgress = courseInteractor.getXBlockProgress(blockId)
delay(500)
_uiState.update { it.copy(jsonProgress = xBlockProgress?.jsonProgress?.toJson()) }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.app.NotificationManager
import android.content.Context
import android.content.pm.ServiceInfo
import android.os.Build
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat
import androidx.work.CoroutineWorker
Expand Down Expand Up @@ -33,6 +34,7 @@ class OfflineProgressSyncWorker(
tryToSyncProgress()
Result.success()
} catch (e: Exception) {
Log.e(WORKER_TAG, "$e")
Firebase.crashlytics.log("$e")
Result.failure()
}
Expand Down

0 comments on commit f1642ba

Please sign in to comment.