Skip to content

Commit

Permalink
Added the new API
Browse files Browse the repository at this point in the history
  • Loading branch information
D4rK7355608 committed Jul 15, 2024
1 parent 66c087c commit 9b6c9a7
Show file tree
Hide file tree
Showing 16 changed files with 172 additions and 185 deletions.
1 change: 1 addition & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
alias(libs.plugins.androidApplication)
alias(libs.plugins.jetbrainsKotlinAndroid)
alias(libs.plugins.jetbrainsKotlinParcelize)
alias(libs.plugins.kotlin.serialization)
alias(libs.plugins.googlePlayServices)
alias(libs.plugins.googleOssServices)
alias(libs.plugins.googleFirebase)
Expand Down Expand Up @@ -147,8 +148,14 @@ dependencies {
// Kotlin
implementation(libs.kotlinx.coroutines.android)

implementation("io.ktor:ktor-client-core:2.3.4")
implementation("io.ktor:ktor-client-cio:2.3.4")
implementation("io.ktor:ktor-client-content-negotiation:2.3.4")
implementation("io.ktor:ktor-serialization-kotlinx-json:2.3.4")

// Other
implementation(libs.lottie.compose)
implementation("io.coil-kt:coil-compose:2.6.0")
implementation(libs.glide)

// Test
Expand Down
Binary file added app/release/app-release.aab
Binary file not shown.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.d4rk.englishwithlidia.plus.data.model.api

import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonNames

@Serializable
data class ApiResponse(
val data: List<NetworkLesson>
)

@Serializable
data class NetworkLesson @OptIn(ExperimentalSerializationApi::class) constructor(
val id: Int,
val title: String,
val banner: String,
@JsonNames("lessonDetails.title") val lessonDetailsTitle: String,
@JsonNames("lessonDetails.audioUrl") val lessonDetailsAudioUrl: String,
@JsonNames("lessonDetails.lessonIntro") val lessonDetailsLessonIntro: String,
@JsonNames("lessonDetails.lessonSummary") val lessonDetailsLessonSummary: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package com.d4rk.englishwithlidia.plus.data.model.ui.lessons

import android.os.Parcelable
import androidx.annotation.RawRes
import androidx.annotation.StringRes
import kotlinx.parcelize.Parcelize

@Parcelize
data class UiLessonDetails(
val title: String,
@RawRes val audioResId: Int,
val audioUrl: String,
val lessonIntro: String,
val lessonSummary: String
) : Parcelable
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ import kotlinx.parcelize.Parcelize
data class UiLessonsAsset(
val id: Int,
val title: String,
val banner: Int,
val banner: String,
val lessonDetails: UiLessonDetails
) : Parcelable

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Card
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
Expand All @@ -35,6 +36,8 @@ import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import coil.compose.AsyncImage
import coil.request.ImageRequest
import com.airbnb.lottie.compose.LottieAnimation
import com.airbnb.lottie.compose.LottieCompositionSpec
import com.airbnb.lottie.compose.LottieConstants
Expand All @@ -52,9 +55,10 @@ import com.d4rk.englishwithlidia.plus.utils.drawable.homeBanner
fun HomeComposable() {
val context = LocalContext.current
val dataStore = DataStore.getInstance(context)
val repository = LessonRepository(context)
val repository = LessonRepository()
val viewModel: HomeViewModel = viewModel(factory = HomeViewModelFactory(repository))
val lessons by viewModel.lessons.collectAsState()
val isLoading by viewModel.isLoading.collectAsState()
LazyColumn(
modifier = Modifier
.fillMaxSize()
Expand Down Expand Up @@ -112,13 +116,27 @@ fun HomeComposable() {
}
Spacer(modifier = Modifier.height(16.dp))
}
items(lessons) { lesson ->
LessonCard(title = lesson.title, imageResource = lesson.banner, onClick = {
val intent = Intent(context, LessonsActivity::class.java).apply {
putExtra("lessonDetails", lesson)
if (isLoading) {
item {
Box(
modifier = Modifier
.fillMaxWidth()
.wrapContentHeight()
.padding(16.dp),
contentAlignment = Alignment.Center
) {
CircularProgressIndicator()
}
context.startActivity(intent)
})
}
} else {
items(lessons) { lesson ->
LessonCard(title = lesson.title, imageResource = lesson.banner, onClick = {
val intent = Intent(context, LessonsActivity::class.java).apply {
putExtra("lessonDetails", lesson)
}
context.startActivity(intent)
})
}
}
item {
BannerAdsComposable(modifier = Modifier.fillMaxWidth(), dataStore = dataStore)
Expand All @@ -142,7 +160,8 @@ fun HomeComposable() {
}

@Composable
fun LessonCard(title: String, imageResource: Int, onClick: () -> Unit) {
fun LessonCard(title: String, imageResource: String, onClick: () -> Unit) {
val context = LocalContext.current
Column(
modifier = Modifier
.fillMaxWidth()
Expand All @@ -155,11 +174,14 @@ fun LessonCard(title: String, imageResource: Int, onClick: () -> Unit) {
.clickable(onClick = onClick)
.aspectRatio(2.06f / 1f),
) {
Image(
painter = painterResource(id = imageResource),
AsyncImage(
model = ImageRequest.Builder(context)
.data(imageResource)
.crossfade(true)
.build(),
contentDescription = null,
modifier = Modifier.fillMaxSize(),
contentScale = ContentScale.Crop
contentScale = ContentScale.Crop,
)
}
Spacer(modifier = Modifier.height(8.dp))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@ import com.d4rk.englishwithlidia.plus.data.model.ui.lessons.UiLessonsAsset
import com.d4rk.englishwithlidia.plus.ui.home.repository.LessonRepository
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch

class HomeViewModel(private val repository: LessonRepository) : ViewModel() {
private val _lessons = MutableStateFlow<List<UiLessonsAsset>>(emptyList())
val lessons: StateFlow<List<UiLessonsAsset>> = _lessons

private val _isLoading = MutableStateFlow(false)
val isLoading: StateFlow<Boolean> = _isLoading.asStateFlow()

init {
viewModelScope.launch {
_isLoading.value = true
_lessons.value = repository.getLessonData()
_isLoading.value = false
}
}
}
Original file line number Diff line number Diff line change
@@ -1,36 +1,42 @@
package com.d4rk.englishwithlidia.plus.ui.home.repository

import android.content.Context
import com.d4rk.englishwithlidia.plus.R
import com.d4rk.englishwithlidia.plus.data.model.api.ApiResponse
import com.d4rk.englishwithlidia.plus.data.model.ui.lessons.UiLessonDetails
import com.d4rk.englishwithlidia.plus.data.model.ui.lessons.UiLessonsAsset
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.engine.cio.CIO
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.client.request.get
import io.ktor.serialization.kotlinx.json.json

class LessonRepository(private val context: Context) {
class LessonRepository {
private val client = HttpClient(CIO) {
install(ContentNegotiation) {
json()
}
}

fun getLessonData(): List<UiLessonsAsset> {
return listOf(
UiLessonsAsset(
id = 1,
title = context.getString(R.string.lesson_1_title),
banner = R.drawable.im_lesson1,
lessonDetails = UiLessonDetails(
title = context.getString(R.string.lesson_1_title),
audioResId = R.raw.lesson1,
lessonIntro = context.getString(R.string.introduction_first_lesson),
lessonSummary = context.getString(R.string.summary_first_lesson)
)
),
UiLessonsAsset(
id = 2,
title = context.getString(R.string.lesson_2_title),
banner = R.drawable.im_lesson2,
lessonDetails = UiLessonDetails(
title = context.getString(R.string.lesson_2_title),
audioResId = R.raw.lesson2,
lessonIntro = context.getString(R.string.introduction_second_lesson),
lessonSummary = context.getString(R.string.summary_second_lesson)
suspend fun getLessonData(): List<UiLessonsAsset> {
val url =
"https://script.googleusercontent.com/macros/echo?user_content_key=IqfXaaMa5_xVh9PXgCkTSSvJ5oXI2E6ovHb-xZ-9oeDNzYVvgbAQub1KyRTJ8iBLSdRQS9gDeA6vYWII3idMI7fMbNijCwsvm5_BxDlH2jW0nuo2oDemN9CCS2h10ox_1xSncGQajx_ryfhECjZEnJ73NK6pJWSpPRfbuzxijC-yIhItdrlA4B3P1J--f1LjXLtmxSn7AD8ey3pHscewS7Oo7Ec-OFT-mp7LnhIc8L9DWUmcQiBQTNz9Jw9Md8uu&lib=MSUzLTgsn65WMd0y5_jDbxNmCwTyR-I_G"
return try {
val response: ApiResponse = client.get(url).body()
response.data.map { networkLesson ->
UiLessonsAsset(
id = networkLesson.id,
title = networkLesson.title,
banner = networkLesson.banner,
lessonDetails = UiLessonDetails(
title = networkLesson.lessonDetailsTitle,
audioUrl = networkLesson.lessonDetailsAudioUrl,
lessonIntro = networkLesson.lessonDetailsLessonIntro,
lessonSummary = networkLesson.lessonDetailsLessonSummary
)
)
),
)
}
} catch (e: Exception) {
emptyList()
}
}
}
Loading

0 comments on commit 9b6c9a7

Please sign in to comment.