Skip to content

Commit

Permalink
prepare new trace repository
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed May 6, 2024
1 parent 9a478a6 commit 075c60a
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import dev.datlag.aniflow.model.safeFirstOrNull
import dev.datlag.aniflow.other.UserHelper
import dev.datlag.aniflow.settings.Settings
import dev.datlag.aniflow.trace.Trace
import dev.datlag.aniflow.trace.TraceStateMachine
import dev.datlag.aniflow.trace.TraceRepository
import dev.datlag.tooling.async.suspendCatching
import io.github.aakira.napier.Napier
import kotlinx.coroutines.flow.map
Expand Down Expand Up @@ -113,12 +113,6 @@ data object NetworkModule {
baseUrl("https://api.trace.moe/")
}.create<Trace>()
}
bindProvider<TraceStateMachine> {
TraceStateMachine(
trace = instance(),
crashlytics = nullableFirebaseInstance()?.crashlytics
)
}
bindSingleton<TrendingRepository> {
val appSettings = instance<Settings.PlatformAppSettings>()

Expand Down Expand Up @@ -169,5 +163,10 @@ data object NetworkModule {
fallbackClient = instance(Constants.AniList.FALLBACK_APOLLO_CLIENT)
)
}
bindSingleton<TraceRepository> {
TraceRepository(
trace = instance(),
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import dev.datlag.aniflow.anilist.TrendingRepository
import dev.datlag.aniflow.anilist.model.Medium
import dev.datlag.aniflow.anilist.state.CollectionState
import dev.datlag.aniflow.anilist.type.MediaType
import dev.datlag.aniflow.trace.TraceRepository
import dev.datlag.aniflow.ui.navigation.Component
import kotlinx.coroutines.flow.Flow

Expand All @@ -16,9 +17,13 @@ interface HomeComponent : Component {
val popularNow: Flow<CollectionState>
val popularNext: Flow<CollectionState>

val traceState: Flow<TraceRepository.State>

fun viewProfile()
fun viewAnime()
fun viewManga()

fun details(medium: Medium)
fun trace(byteArray: ByteArray)
fun clearTrace()
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.datlag.aniflow.ui.navigation.screen.home

import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.LinearOutSlowInEasing
import androidx.compose.animation.core.tween
Expand All @@ -10,6 +11,8 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.CameraEnhance
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
Expand All @@ -31,6 +34,8 @@ import dev.datlag.aniflow.anilist.type.MediaType
import dev.datlag.aniflow.common.LocalPadding
import dev.datlag.aniflow.common.isScrollingUp
import dev.datlag.aniflow.other.StateSaver
import dev.datlag.aniflow.other.rememberImagePickerState
import dev.datlag.aniflow.trace.TraceRepository
import dev.datlag.aniflow.ui.navigation.screen.component.CollapsingToolbar
import dev.datlag.aniflow.ui.navigation.screen.component.HidingNavigationBar
import dev.datlag.aniflow.ui.navigation.screen.home.component.AllLoadingView
Expand All @@ -46,6 +51,9 @@ fun HomeScreen(component: HomeComponent) {
state = appBarState
)
val listState = rememberLazyListState()
val imagePicker = rememberImagePickerState {
it?.let(component::trace)
}

Scaffold(
modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
Expand All @@ -60,7 +68,23 @@ fun HomeScreen(component: HomeComponent) {
)
},
floatingActionButton = {
val traceState by component.traceState.collectAsStateWithLifecycle(TraceRepository.State.None)

ExtendedFloatingActionButton(
onClick = {
imagePicker.launch()
},
expanded = listState.isScrollingUp(),
icon = {
Icon(
imageVector = Icons.Filled.CameraEnhance,
contentDescription = null
)
},
text = {
Text(text = "Scan")
}
)
},
bottomBar = {
HidingNavigationBar(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import dev.datlag.aniflow.common.onRender
import dev.datlag.aniflow.model.coroutines.Executor
import dev.datlag.aniflow.other.StateSaver
import dev.datlag.aniflow.settings.Settings
import dev.datlag.aniflow.trace.TraceRepository
import dev.datlag.tooling.decompose.ioScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.SharingStarted
Expand Down Expand Up @@ -79,6 +80,13 @@ class HomeScreenComponent(
initialValue = CollectionState.None
)

private val traceRepository by instance<TraceRepository>()
override val traceState: Flow<TraceRepository.State> = traceRepository.response

init {
traceRepository.clear()
}

@Composable
override fun render() {
val haze = remember { HazeState() }
Expand Down Expand Up @@ -117,4 +125,12 @@ class HomeScreenComponent(
override fun details(medium: Medium) {
onMediumDetails(medium)
}

override fun trace(byteArray: ByteArray) {
traceRepository.search(byteArray)
}

override fun clearTrace() {
traceRepository.clear()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package dev.datlag.aniflow.trace

import dev.datlag.aniflow.model.CatchResult
import dev.datlag.aniflow.trace.model.SearchResponse
import kotlinx.coroutines.flow.*
import kotlinx.serialization.Serializable

class TraceRepository(
private val trace: Trace
) {

private val byteArray = MutableStateFlow<ByteArray?>(null)
val response: Flow<State> = byteArray.transform {
return@transform if (it == null || it.isEmpty()) {
emit(State.None)
} else {
emit(
State.fromResponse(
CatchResult.repeat(2) {
trace.search(it)
}.asNullableSuccess()
)
)
}
}

fun clear() = byteArray.update { null }
fun search(array: ByteArray) = byteArray.update { array }

@Serializable
sealed interface State {
@Serializable
data object None : State

@Serializable
data class Success(
val response: SearchResponse,
) : State

@Serializable
data object Error : State

companion object {
fun fromResponse(response: SearchResponse?): State {
return if (response == null || response.isError) {
Error
} else {
Success(response)
}
}
}
}
}

This file was deleted.

0 comments on commit 075c60a

Please sign in to comment.