Skip to content

Commit

Permalink
Fix initial image loading & Fix current screen check
Browse files Browse the repository at this point in the history
  • Loading branch information
Pepijn98 committed May 30, 2022
1 parent 43866b3 commit 1709ee2
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 26 deletions.
24 changes: 15 additions & 9 deletions app/src/main/java/dev/vdbroek/nekos/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,23 @@ class MainActivity : ComponentActivity() {
UserState.username = prefs[USERNAME]
}

App.isReady = prefs[READY] ?: true

val (tagsResponse) = Nekos.getTags()
if (tagsResponse != null) {
App.tags.addAll(tagsResponse.tags)
}

val (response, exception) = Nekos.getImages()
when {
response != null -> {
HomeScreenState.images.addAll(if (App.uncensored) response.images else response.images.filter { !it.tags.contains(App.buggedTag) })
isSplashActive = false
if (App.isReady) {
val (response, exception) = Nekos.getImages()
when {
response != null -> {
HomeScreenState.images.addAll(if (App.uncensored) response.images else response.images.filter { !it.tags.contains(App.buggedTag) })
isSplashActive = false
App.initialLoad = false
}
exception != null -> finish()
}
exception != null -> finish()
}
}

Expand All @@ -106,16 +111,17 @@ class MainActivity : ComponentActivity() {

val navigation = rememberNavController()
val entry by navigation.currentBackStackEntryAsState()
val screen by remember { derivedStateOf { entry?.destination?.route ?: "" } }
val screen = remember { derivedStateOf { entry?.destination?.route ?: "" } }

CompositionLocalProvider(
LocalActivity provides this,
LocalNavigation provides navigation,
LocalScreen provides screen
) {

NekosTheme {
when (LocalScreen.current) {
val current by LocalScreen.current

when (current) {
Screens.Login.route,
Screens.Register.route -> {
window.statusBarColor = MaterialTheme.colorScheme.primary.toArgb()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.vdbroek.nekos.components

import android.util.Log
import androidx.compose.foundation.clickable
import androidx.compose.foundation.gestures.ScrollableState
import androidx.compose.foundation.layout.*
Expand Down Expand Up @@ -165,7 +166,10 @@ fun InfiniteListHandler(
.distinctUntilChanged()
.collect {
// Only load more when true
if (it) onLoadMore()
if (!App.initialLoad && App.isReady && it) {
Log.e("LOAD_MORE", "$it")
onLoadMore()
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ fun NekosAppBar(
) {
val navigation = LocalNavigation.current
val context = LocalContext.current
val current by LocalScreen.current

val coroutine = rememberCoroutineScope()
val toolbarScaffoldState = rememberCollapsingToolbarScaffoldState()
Expand All @@ -50,7 +51,7 @@ fun NekosAppBar(
.height(100.dp)
.parallax(ratio = 0.2f)
)
when (LocalScreen.current) {
when (current) {
Screens.Home.route -> {
Box(
modifier = Modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
Expand All @@ -27,11 +28,12 @@ private val noNavBar = listOf(
@Composable
fun NekosAppContent() {
val navController = LocalNavigation.current
val current by LocalScreen.current

Scaffold(
contentColor = MaterialTheme.colorScheme.background,
bottomBar = {
if (!noNavBar.contains(LocalScreen.current)) {
if (!noNavBar.contains(current)) {
NekosNavBar()
}
},
Expand Down
8 changes: 3 additions & 5 deletions app/src/main/java/dev/vdbroek/nekos/components/NekosNavBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ import androidx.compose.material3.Icon
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.*
import androidx.navigation.compose.currentBackStackEntryAsState
import dev.vdbroek.nekos.api.UserState
import dev.vdbroek.nekos.ui.Screens
import dev.vdbroek.nekos.utils.App
Expand All @@ -31,7 +29,7 @@ import me.onebone.toolbar.ExperimentalToolbarApi
@Composable
fun NekosNavBar() {
val navController = LocalNavigation.current
val screen = LocalScreen.current
val screen by LocalScreen.current

val coroutine = rememberCoroutineScope()

Expand Down
21 changes: 14 additions & 7 deletions app/src/main/java/dev/vdbroek/nekos/ui/screens/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,20 @@ fun Settings(
val contentOptions = listOf("everything", "no_nsfw", "only_nsfw")
var selectedContentOption by remember { mutableStateOf(App.nsfw) }

Column(
modifier = Modifier
val modifier = if (App.uncensored) {
Modifier
.fillMaxSize()
.scrollable(
state = toolbarState,
orientation = Orientation.Vertical
)
} else {
Modifier
.fillMaxSize()
}

Column(
modifier = modifier
) {
Text(
modifier = Modifier.padding(start = 24.dp),
Expand Down Expand Up @@ -350,9 +357,9 @@ suspend fun changeTheme(
}
}

dataStore.edit { preferences ->
preferences[MANUAL] = ThemeState.manual
preferences[IS_DARK] = ThemeState.isDark
dataStore.edit { prefs ->
prefs[MANUAL] = ThemeState.manual
prefs[IS_DARK] = ThemeState.isDark
}
}

Expand All @@ -369,8 +376,8 @@ suspend fun changeLayout(
}
}

dataStore.edit { preferences ->
preferences[STAGGERED] = ThemeState.staggered
dataStore.edit { prefs ->
prefs[STAGGERED] = ThemeState.staggered
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/dev/vdbroek/nekos/utils/Globals.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ val IS_DARK = booleanPreferencesKey("is_dark")
val MANUAL = booleanPreferencesKey("manual")
val STAGGERED = booleanPreferencesKey("staggered")
val NSFW = stringPreferencesKey("nsfw")

val READY = booleanPreferencesKey("ready")
val IS_LOGGED_IN = booleanPreferencesKey("is_logged_in")
val TOKEN = stringPreferencesKey("token")
val USERNAME = stringPreferencesKey("username")
5 changes: 4 additions & 1 deletion app/src/main/java/dev/vdbroek/nekos/utils/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ val LocalNavigation = staticCompositionLocalOf<NavHostController> {
error("CompositionLocal LocalNavigation not present")
}

val LocalScreen = staticCompositionLocalOf<String> {
val LocalScreen = staticCompositionLocalOf<State<String>> {
error("CompositionLocal LocalScreen not present")
}

Expand Down Expand Up @@ -173,6 +173,9 @@ object App {
*/
const val uncensored = BuildConfig.BUILD_TYPE == "uncensored"

var isReady by mutableStateOf(false)
var initialLoad by mutableStateOf(true)

const val defaultNsfw = "no_nsfw"
var nsfw by mutableStateOf(defaultNsfw)
var permissionGranted by mutableStateOf(false)
Expand Down

0 comments on commit 1709ee2

Please sign in to comment.