-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: Improve UiLessonScreen state management and player lifecycle
- Removed redundant private state variables (_isPlaying, _playbackDuration, _playbackPosition). - Updated UiLessonScreen state directly within the StateFlow for playback state changes. - Ensured proper player lifecycle management. Key improvements: - Made UiLessonScreen reactive by observing state via StateFlow. - Managed isPlaying and playbackPosition updates via the ViewModel. - Fixed LaunchedEffect and improper delegation usage in the Composable.
- Loading branch information
Mihai-Cristian Condrea
committed
Dec 5, 2024
1 parent
cbe1e2d
commit e7ad933
Showing
14 changed files
with
167 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...src/main/kotlin/com/d4rk/englishwithlidia/plus/ui/components/navigation/NavigationHost.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.d4rk.englishwithlidia.plus.ui.components.navigation | ||
|
||
import android.content.Context | ||
import android.content.Intent | ||
import android.net.Uri | ||
import com.d4rk.englishwithlidia.plus.data.model.ui.screens.home.UiHomeLesson | ||
|
||
fun openLessonDetailActivity(context : Context , lesson : UiHomeLesson) { | ||
println("English with Lidia -> openLessonDetailActivity") | ||
|
||
println("English with Lidia -> openLessonDetailActivity checking deep link path -> ${lesson.lessonDeepLinkPath}") | ||
|
||
Intent(Intent.ACTION_VIEW , Uri.parse(lesson.lessonDeepLinkPath)).let { intent -> | ||
println("English with Lidia -> openLessonDetailActivity intent -> $intent") | ||
intent.resolveActivity(context.packageManager)?.let { | ||
println("English with Lidia -> openLessonDetailActivity start activity -> $it") | ||
context.startActivity(intent) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 21 additions & 1 deletion
22
...n/kotlin/com/d4rk/englishwithlidia/plus/ui/screens/lessons/repository/LessonRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,23 @@ | ||
package com.d4rk.englishwithlidia.plus.ui.screens.lessons.repository | ||
|
||
class LessonRepository {} | ||
import android.app.Application | ||
import com.d4rk.englishwithlidia.plus.data.datastore.DataStore | ||
import com.d4rk.englishwithlidia.plus.data.model.ui.screens.home.UiLessonScreen | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
|
||
class LessonRepository( | ||
dataStore : DataStore , application : Application , | ||
) : LessonRepositoryImplementation(application , dataStore) { | ||
|
||
suspend fun getLessonRepository( | ||
lessonId : String , onSuccess : (UiLessonScreen) -> Unit | ||
) { | ||
withContext(Dispatchers.IO) { | ||
val lesson = getLessonImplementation(lessonId = lessonId) | ||
withContext(Dispatchers.Main) { | ||
onSuccess(lesson) | ||
} | ||
} | ||
} | ||
} |
60 changes: 59 additions & 1 deletion
60
...4rk/englishwithlidia/plus/ui/screens/lessons/repository/LessonRepositoryImplementation.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,61 @@ | ||
package com.d4rk.englishwithlidia.plus.ui.screens.lessons.repository | ||
|
||
class LessonRepositoryImplementation {} | ||
import android.app.Application | ||
import com.d4rk.englishwithlidia.plus.BuildConfig | ||
import com.d4rk.englishwithlidia.plus.constants.api.ApiConstants | ||
import com.d4rk.englishwithlidia.plus.data.core.AppCoreManager | ||
import com.d4rk.englishwithlidia.plus.data.datastore.DataStore | ||
import com.d4rk.englishwithlidia.plus.data.model.api.ApiLessonResponse | ||
import com.d4rk.englishwithlidia.plus.data.model.ui.screens.home.UiLessonContent | ||
import com.d4rk.englishwithlidia.plus.data.model.ui.screens.home.UiLessonScreen | ||
import io.ktor.client.HttpClient | ||
import io.ktor.client.request.get | ||
import io.ktor.client.statement.bodyAsText | ||
import kotlinx.serialization.json.Json | ||
|
||
abstract class LessonRepositoryImplementation( | ||
val application : Application , | ||
val dataStore : DataStore , | ||
) { | ||
|
||
private val client : HttpClient = AppCoreManager.ktorClient | ||
|
||
private val baseUrl = BuildConfig.DEBUG.let { isDebug -> | ||
val environment = if (isDebug) "debug" else "release" | ||
"${ApiConstants.BASE_REPOSITORY_URL}/$environment/ro/lessons" | ||
} | ||
|
||
private val jsonParser = Json { | ||
ignoreUnknownKeys = true | ||
isLenient = true | ||
} | ||
|
||
suspend fun getLessonImplementation(lessonId : String) : UiLessonScreen { | ||
return runCatching { | ||
val url = "$baseUrl/api_get_$lessonId.json" | ||
val response = client.get(url) | ||
val jsonString = response.bodyAsText() | ||
|
||
val lessons = jsonString.takeUnless { it.isBlank() } | ||
?.let { jsonParser.decodeFromString<ApiLessonResponse>(it) } | ||
?.takeIf { it.data.isNotEmpty() }?.data?.map { networkLesson -> | ||
UiLessonScreen(lessonTitle = networkLesson.lessonTitle , | ||
lessonContent = ArrayList(networkLesson.lessonContent.map { networkContent -> | ||
UiLessonContent( | ||
contentId = networkContent.contentId , | ||
contentType = networkContent.contentType , | ||
contentText = networkContent.contentText , | ||
contentAudioUrl = networkContent.contentAudioUrl , | ||
contentImageUrl = networkContent.contentImageUrl | ||
) | ||
})) | ||
}?.also { lessons -> | ||
println("Fetched ${lessons.size} lessons") | ||
} ?: emptyList() | ||
lessons.firstOrNull() ?: UiLessonScreen() | ||
}.getOrElse { exception -> | ||
println("Error: ${exception.message}") | ||
UiLessonScreen() | ||
} | ||
} | ||
} |