-
Notifications
You must be signed in to change notification settings - Fork 0
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
5e2f6be
commit 8faabf3
Showing
11 changed files
with
127 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
app/src/main/kotlin/id/shaderboi/koffie/ui/main/home/view_model/StoreFrontEvent.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,5 @@ | ||
package id.shaderboi.koffie.ui.main.home.view_model | ||
|
||
sealed class StoreFrontEvent { | ||
object Load: StoreFrontEvent() | ||
} |
38 changes: 38 additions & 0 deletions
38
app/src/main/kotlin/id/shaderboi/koffie/ui/main/home/view_model/StoreFrontViewModel.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,38 @@ | ||
package id.shaderboi.koffie.ui.main.home.view_model | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import id.shaderboi.koffie.core.domain.model.StoreFront | ||
import id.shaderboi.koffie.core.domain.repository.VendorRepository | ||
import id.shaderboi.koffie.core.util.Resource | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlinx.coroutines.flow.asSharedFlow | ||
import kotlinx.coroutines.flow.collectLatest | ||
import kotlinx.coroutines.launch | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class StoreFrontViewModel @Inject constructor( | ||
private val vendorRepository: VendorRepository | ||
) : ViewModel() { | ||
private val _storeFront = MutableSharedFlow<Resource<StoreFront>>() | ||
val storeFront = _storeFront.asSharedFlow() | ||
|
||
init { | ||
onEvent(StoreFrontEvent.Load) | ||
} | ||
|
||
fun onEvent(event: StoreFrontEvent) { | ||
when (event) { | ||
StoreFrontEvent.Load -> loadStoreFront() | ||
} | ||
} | ||
|
||
private fun loadStoreFront() = viewModelScope.launch { | ||
_storeFront.emit(Resource.Loading()) | ||
vendorRepository.loadStoreFront().collectLatest { resource -> | ||
_storeFront.emit(resource) | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
core/src/main/kotlin/id/shaderboi/koffie/core/data/data_source/network/KoffieAPIService.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,4 +1,10 @@ | ||
package id.shaderboi.koffie.core.data.data_source.network | ||
|
||
import id.shaderboi.koffie.core.domain.model.StoreFront | ||
import kotlinx.coroutines.flow.Flow | ||
import retrofit2.http.GET | ||
|
||
interface KoffieAPIService { | ||
@GET("/api/v1/blabla") | ||
fun getStoreFront(): Flow<StoreFront> | ||
} |
18 changes: 18 additions & 0 deletions
18
core/src/main/kotlin/id/shaderboi/koffie/core/data/repository/VendorRepositoryImpl.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,18 @@ | ||
package id.shaderboi.koffie.core.data.repository | ||
|
||
import id.shaderboi.koffie.core.data.data_source.network.KoffieAPIService | ||
import id.shaderboi.koffie.core.domain.model.StoreFront | ||
import id.shaderboi.koffie.core.domain.repository.VendorRepository | ||
import id.shaderboi.koffie.core.util.Resource | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.catch | ||
import kotlinx.coroutines.flow.map | ||
|
||
class VendorRepositoryImpl( | ||
private val koffieAPIService: KoffieAPIService | ||
) : VendorRepository { | ||
override suspend fun loadStoreFront(): Flow<Resource<StoreFront>> = | ||
koffieAPIService.getStoreFront() | ||
.map { value -> Resource.Loaded(value) } | ||
.catch { cause -> Resource.Error<StoreFront>(cause) } | ||
} |
13 changes: 13 additions & 0 deletions
13
core/src/main/kotlin/id/shaderboi/koffie/core/domain/model/CategoryProducts.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,13 @@ | ||
package id.shaderboi.koffie.core.domain.model | ||
|
||
import com.squareup.moshi.Json | ||
import com.squareup.moshi.JsonClass | ||
|
||
@JsonClass(generateAdapter = true) | ||
data class CategoryProducts( | ||
@Json(name = "category_name") | ||
val categoryName: String, | ||
@Json(name = "category_id") | ||
val categoryId: Int, | ||
val products: List<Product> | ||
) |
18 changes: 18 additions & 0 deletions
18
core/src/main/kotlin/id/shaderboi/koffie/core/domain/model/Product.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,18 @@ | ||
package id.shaderboi.koffie.core.domain.model | ||
|
||
import com.squareup.moshi.Json | ||
import com.squareup.moshi.JsonClass | ||
|
||
@JsonClass(generateAdapter = true) | ||
data class Product( | ||
val itemId: Int, | ||
val name: String, | ||
val description: String, | ||
val price: Int, | ||
@Json(name = "image_url") | ||
val imageUrl: String, | ||
@Json(name = "undiscounted_price") | ||
val undiscountedPrice: Int?, | ||
@Json(name = "created_at") | ||
val createdAt: String | ||
) |
8 changes: 8 additions & 0 deletions
8
core/src/main/kotlin/id/shaderboi/koffie/core/domain/model/StoreFront.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,8 @@ | ||
package id.shaderboi.koffie.core.domain.model | ||
|
||
import com.squareup.moshi.JsonClass | ||
|
||
@JsonClass(generateAdapter = true) | ||
data class StoreFront( | ||
val categories: List<CategoryProducts> | ||
) |
9 changes: 9 additions & 0 deletions
9
core/src/main/kotlin/id/shaderboi/koffie/core/domain/repository/VendorRepository.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 id.shaderboi.koffie.core.domain.repository | ||
|
||
import id.shaderboi.koffie.core.domain.model.StoreFront | ||
import id.shaderboi.koffie.core.util.Resource | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
interface VendorRepository { | ||
suspend fun loadStoreFront(): Flow<Resource<StoreFront>> | ||
} |
7 changes: 7 additions & 0 deletions
7
core/src/main/kotlin/id/shaderboi/koffie/core/util/Resource.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,7 @@ | ||
package id.shaderboi.koffie.core.util | ||
|
||
sealed class Resource<T> { | ||
class Loading<T> : Resource<T>() | ||
data class Loaded<T>(val data: T) : Resource<T>() | ||
data class Error<T>(val error: Throwable) : Resource<T>() | ||
} |