-
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.
- Loading branch information
1 parent
3053e80
commit 81de373
Showing
14 changed files
with
241 additions
and
37 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
5 changes: 5 additions & 0 deletions
5
androidApp/src/main/java/com/nameisjayant/kmmproject/android/di/AppModule.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,11 +1,16 @@ | ||
package com.nameisjayant.kmmproject.android.di | ||
|
||
import com.nameisjayant.kmmproject.android.feature.ui.viewmodel.CatViewModel | ||
import com.nameisjayant.kmmproject.android.feature.ui.viewmodel.PostViewModel | ||
import com.nameisjayant.kmmproject.data.repository.CatRepository | ||
import org.koin.androidx.viewmodel.dsl.viewModel | ||
import org.koin.dsl.module | ||
|
||
val appModule = module { | ||
viewModel { | ||
PostViewModel(get()) | ||
} | ||
viewModel { | ||
CatViewModel(get()) | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
...oidApp/src/main/java/com/nameisjayant/kmmproject/android/feature/cat/screens/CatScreen.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,95 @@ | ||
package com.nameisjayant.kmmproject.android.feature.cat.screens | ||
|
||
import androidx.compose.foundation.layout.* | ||
import androidx.compose.foundation.lazy.LazyColumn | ||
import androidx.compose.foundation.lazy.items | ||
import androidx.compose.foundation.shape.CircleShape | ||
import androidx.compose.foundation.shape.RoundedCornerShape | ||
import androidx.compose.material.Card | ||
import androidx.compose.material.CircularProgressIndicator | ||
import androidx.compose.material.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Alignment.Companion.CenterVertically | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.draw.clip | ||
import androidx.compose.ui.layout.ContentScale | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.res.painterResource | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.unit.dp | ||
import coil.compose.AsyncImage | ||
import coil.request.ImageRequest | ||
import com.nameisjayant.kmmproject.android.R | ||
import com.nameisjayant.kmmproject.android.feature.ui.viewmodel.CatViewModel | ||
import com.nameisjayant.kmmproject.data.model.Cat | ||
|
||
|
||
@Composable | ||
fun CatScreen( | ||
viewModel: CatViewModel | ||
) { | ||
|
||
val res = viewModel.catResponse.value | ||
|
||
if (res.isLoading) | ||
Box( | ||
modifier = Modifier.fillMaxSize(), | ||
contentAlignment = Alignment.Center | ||
) { | ||
CircularProgressIndicator() | ||
} | ||
if (res.data.isNotEmpty()) | ||
LazyColumn { | ||
items( | ||
res.data, | ||
key = { it.id!! } | ||
) { | ||
CatEachRow(cat = it) | ||
} | ||
} | ||
if (res.error.isNotEmpty()) | ||
Box( | ||
modifier = Modifier.fillMaxSize(), | ||
contentAlignment = Alignment.Center | ||
) { | ||
Text(text = res.error) | ||
} | ||
|
||
} | ||
|
||
@Composable | ||
fun CatEachRow( | ||
cat: Cat | ||
) { | ||
|
||
Card( | ||
modifier = Modifier.fillMaxWidth(), | ||
) { | ||
Row( | ||
modifier = Modifier.padding(15.dp) | ||
) { | ||
AsyncImage( | ||
model = ImageRequest.Builder(LocalContext.current) | ||
.data(cat.url) | ||
.crossfade(true) | ||
.build(), | ||
contentDescription = "", | ||
contentScale = ContentScale.Crop, | ||
placeholder = painterResource(id = R.drawable.img), | ||
modifier = Modifier | ||
.clip(CircleShape) | ||
.size(100.dp) | ||
) | ||
Spacer(modifier = Modifier.width(20.dp)) | ||
Column( | ||
modifier = Modifier.align(CenterVertically) | ||
) { | ||
Text(text = "Width: ${cat.width}") | ||
Spacer(modifier = Modifier.height(15.dp)) | ||
Text(text = "Height: ${cat.height}") | ||
} | ||
} | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
...pp/src/main/java/com/nameisjayant/kmmproject/android/feature/ui/viewmodel/CatViewModel.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,49 @@ | ||
package com.nameisjayant.kmmproject.android.feature.ui.viewmodel | ||
|
||
import androidx.compose.runtime.MutableState | ||
import androidx.compose.runtime.State | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.nameisjayant.kmmproject.data.model.Cat | ||
import com.nameisjayant.kmmproject.data.model.Post | ||
import com.nameisjayant.kmmproject.data.repository.CatRepository | ||
import kotlinx.coroutines.flow.catch | ||
import kotlinx.coroutines.flow.onStart | ||
import kotlinx.coroutines.launch | ||
|
||
class CatViewModel constructor( | ||
private val repository: CatRepository | ||
) : ViewModel() { | ||
|
||
private val _catResponse: MutableState<CatState> = mutableStateOf(CatState()) | ||
val catResponse: State<CatState> = _catResponse | ||
|
||
|
||
init { | ||
viewModelScope.launch { | ||
repository.getCats() | ||
.onStart { | ||
_catResponse.value = CatState( | ||
isLoading = true | ||
) | ||
}.catch { | ||
_catResponse.value = CatState( | ||
error = it.message ?: "Something went wrong!" | ||
) | ||
}.collect { | ||
_catResponse.value = CatState( | ||
data = it | ||
) | ||
} | ||
} | ||
} | ||
|
||
|
||
} | ||
|
||
data class CatState( | ||
val data: List<Cat> = emptyList(), | ||
val error: String = "", | ||
val isLoading: Boolean = false | ||
) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,4 +21,4 @@ abstract class BaseRepository { | |
emit(ApiState.Failure(e)) | ||
}.flowOn(Dispatchers.Default) | ||
|
||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
shared/src/commonMain/kotlin/com/nameisjayant/kmmproject/data/model/Cat.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,9 @@ | ||
package com.nameisjayant.kmmproject.data.model | ||
|
||
@kotlinx.serialization.Serializable | ||
data class Cat( | ||
val id: String? = null, | ||
val url: String? = null, | ||
val width: Int? = null, | ||
val height: Int? = null | ||
) |
16 changes: 12 additions & 4 deletions
16
shared/src/commonMain/kotlin/com/nameisjayant/kmmproject/data/network/ApiService.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,19 +1,27 @@ | ||
package com.nameisjayant.kmmproject.data.network | ||
|
||
import com.nameisjayant.kmmproject.data.model.Cat | ||
import com.nameisjayant.kmmproject.data.model.Post | ||
import io.ktor.client.* | ||
import io.ktor.client.call.* | ||
import io.ktor.client.request.* | ||
|
||
class ApiService( | ||
private val client:HttpClient, | ||
private val baseUrl: String = "https://jsonplaceholder.typicode.com" | ||
) { | ||
private val client: HttpClient, | ||
|
||
suspend fun getPosts():List<Post>{ | ||
) { | ||
private val baseUrl: String = "https://jsonplaceholder.typicode.com" | ||
private val catUrl = "https://api.thecatapi.com/v1/images/search?limit=20" | ||
suspend fun getPosts(): List<Post> { | ||
return client.get { | ||
url("$baseUrl/posts") | ||
}.body() | ||
} | ||
|
||
suspend fun getCats(): List<Cat> { | ||
return client.get { | ||
url(catUrl) | ||
}.body() | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
shared/src/commonMain/kotlin/com/nameisjayant/kmmproject/data/repository/CatRepository.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,16 @@ | ||
package com.nameisjayant.kmmproject.data.repository | ||
|
||
import com.nameisjayant.kmmproject.data.model.Cat | ||
import com.nameisjayant.kmmproject.data.network.ApiService | ||
import kotlinx.coroutines.flow.flow | ||
|
||
|
||
class CatRepository constructor( | ||
private val apiService: ApiService | ||
) { | ||
|
||
suspend fun getCats() = flow<List<Cat>> { | ||
emit(apiService.getCats()) | ||
} | ||
|
||
} |
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
Oops, something went wrong.