Skip to content
This repository has been archived by the owner on May 6, 2024. It is now read-only.

Commit

Permalink
Fixed for detail item view (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
VovaStelmashchuk committed Jun 19, 2023
1 parent 9aa7d57 commit 78f741c
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 25 deletions.
6 changes: 2 additions & 4 deletions iosApp/iosApp/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import shared

struct ComposeView: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> UIViewController {
Main_iosKt.MainViewController()
let controller = Main_iosKt.MainViewController()
return controller
}

func updateUIViewController(_ uiViewController: UIViewController, context: Context) {}
Expand All @@ -16,6 +17,3 @@ struct ContentView: View {
.ignoresSafeArea(.keyboard) // Compose has own keyboard handler
}
}



Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
package org.mixdrinks.data

import de.jensklingenberg.ktorfit.http.GET
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import org.mixdrinks.dto.SnapshotDto

internal interface MixDrinksService {

@GET("/snapshot")
suspend fun getSnapshot(): SnapshotDto

@GET("/version")
suspend fun getVersion(): Version

}

@Serializable
data class Version(
@SerialName("version_code")
val versionCode: Int,
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ package org.mixdrinks.data

import com.russhwolf.settings.Settings
import com.russhwolf.settings.set
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import org.mixdrinks.app.utils.undomain.LazySuspend
import org.mixdrinks.dto.SnapshotDto


Expand All @@ -14,30 +19,44 @@ internal class SnapshotRepository(
private val json: Json,
) {

private val snapshot = LazySuspend {
val cacheValue = settings.getStringOrNull(SNAPSHOT_KEY)
if (cacheValue != null) {
try {
json.decodeFromString(cacheValue)
} catch (_: Exception) {
settings.remove(SNAPSHOT_KEY)
val snapshot = mixDrinksService.getSnapshot()
settings[SNAPSHOT_KEY] = json.encodeToString(snapshot)
snapshot
private val flowSnapshot: MutableStateFlow<SnapshotDto?> = MutableStateFlow(null)

init {
CoroutineScope(Dispatchers.Main).launch {
settings.getStringOrNull(SNAPSHOT_KEY)?.let { cachedSnapshotStr ->
try {
flowSnapshot.emit(json.decodeFromString(cachedSnapshotStr))
} catch (_: Exception) {
updateSnapshot()
}
}

val currentVersion = settings.getInt(SNAPSHOT_VERSION_KEY, -1)
val newVersion = mixDrinksService.getVersion().versionCode
if (currentVersion != newVersion) {
updateSnapshot()
settings[SNAPSHOT_VERSION_KEY] = newVersion
}
} else {
}
}

private suspend fun updateSnapshot() {
try {
println("Update snapshot")
val snapshot = mixDrinksService.getSnapshot()
settings[SNAPSHOT_KEY] = json.encodeToString(snapshot)
snapshot
flowSnapshot.emit(snapshot)
} catch (_: Exception) {

}
}

suspend fun get(): SnapshotDto {
return snapshot()
return flowSnapshot.filterNotNull().first()
}

companion object {
private const val SNAPSHOT_KEY = "SNAPSHOT_KEY"
private const val SNAPSHOW_VERSION_KEY = "SNAPSHOW_VERSION_KEY"
private const val SNAPSHOT_VERSION_KEY = "SNAPSHOT_VERSION_KEY"
}
}
4 changes: 2 additions & 2 deletions shared/src/commonMain/kotlin/org/mixdrinks/ui/RootContent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import com.arkivanov.decompose.extensions.compose.jetbrains.stack.animation.stac
import org.mixdrinks.ui.details.DetailView
import org.mixdrinks.ui.filters.main.FilterView
import org.mixdrinks.ui.filters.search.SearchItemView
import org.mixdrinks.ui.goods.AccessoriesView
import org.mixdrinks.ui.goods.ItemDetailsView
import org.mixdrinks.ui.list.main.MutableCocktailList

@Composable
Expand Down Expand Up @@ -53,7 +53,7 @@ internal fun RootContent(component: RootComponent, deepLink: String?) {
content = {
when (val child = it.instance) {
is RootComponent.Child.List -> MutableCocktailList(child.component)
is RootComponent.Child.Item -> AccessoriesView(child.component)
is RootComponent.Child.Item -> ItemDetailsView(child.component)
is RootComponent.Child.Details -> DetailView(child.component)
is RootComponent.Child.Filters -> FilterView(child.component)
is RootComponent.Child.ItemSearch -> SearchItemView(child.component)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import org.mixdrinks.ui.list.cocktailListInserter
import org.mixdrinks.ui.widgets.undomain.ContentHolder

@Composable
internal fun AccessoriesView(component: ItemDetailComponent) {
internal fun ItemDetailsView(component: ItemDetailComponent) {
Box(
modifier = Modifier
.fillMaxSize()
Expand All @@ -41,14 +41,14 @@ internal fun AccessoriesView(component: ItemDetailComponent) {
ContentHolder(
stateflow = component.state
) {
GoodsViewContent(it, component)
ItemViewContent(it, component)
}
}
}

@OptIn(ExperimentalResourceApi::class, ExperimentalFoundationApi::class)
@Composable
internal fun GoodsViewContent(
internal fun ItemViewContent(
good: DetailGoodsUiModel,
component: ItemDetailComponent
) {
Expand All @@ -74,7 +74,7 @@ internal fun GoodsViewContent(
.size(32.dp)
.padding(start = 12.dp),
painter = painterResource("ic_arrow_back.xml"),
contentDescription = "Test"
contentDescription = "Назад"
)
}
Text(
Expand All @@ -89,6 +89,13 @@ internal fun GoodsViewContent(
}
}
goodsViewScrollContent(Modifier.padding(horizontal = 8.dp), good)
item {
Text(
modifier = Modifier.padding(8.dp),
style = MixDrinksTextStyles.H2,
text = "Коктейлі з ${good.name}",
)
}
cocktailListInserter(cocktails, predefineComponent::onCocktailClick)
}
}
Expand Down

0 comments on commit 78f741c

Please sign in to comment.