Skip to content

Commit

Permalink
Merge branch 'dev' into 'main'
Browse files Browse the repository at this point in the history
  • Loading branch information
nsh07 committed Nov 8, 2024
2 parents fb12b93 + 07f4b86 commit d5d06bd
Show file tree
Hide file tree
Showing 18 changed files with 352 additions and 129 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ android {
applicationId = "org.nsh07.wikireader"
minSdk = 26
targetSdk = 35
versionCode = 12
versionName = "1.6.1"
versionCode = 13
versionName = "1.6.2"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down
88 changes: 55 additions & 33 deletions app/src/main/java/org/nsh07/wikireader/AppScreen.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.nsh07.wikireader

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
Expand All @@ -12,9 +11,12 @@ import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.asPaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.statusBars
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.material3.AlertDialogDefaults
Expand Down Expand Up @@ -49,6 +51,7 @@ import org.nsh07.wikireader.ui.scaffoldComponents.AppSearchBar
import org.nsh07.wikireader.ui.viewModel.PreferencesState
import org.nsh07.wikireader.ui.viewModel.UiViewModel

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AppScreen(
viewModel: UiViewModel,
Expand Down Expand Up @@ -100,55 +103,65 @@ fun AppScreen(
animationSpec = tween(200)
) + fadeOut(tween(100))
},
modifier = Modifier.background(MaterialTheme.colorScheme.background)
modifier = modifier.background(MaterialTheme.colorScheme.background)
) {
composable("HomeScreen") {
AnimatedVisibility(showDeleteDialog) {
if (showDeleteDialog)
DeleteHistoryItemDialog(
historyItem,
setShowDeleteDialog
) { viewModel.removeHistoryItem(it) }
}
) {
if (historyItem != "") viewModel.removeHistoryItem(it)
else viewModel.clearHistory()
}
Scaffold(
floatingActionButton = {
AppFab(
focusSearch = { viewModel.focusSearchBar() },
scrollToTop = { coroutineScope.launch { listState.animateScrollToItem(0) } },
index = index,
fabEnter = fabEnter,
fabExit = fabExit
)
},
modifier = Modifier.fillMaxSize()
) { insets ->
Column(modifier = modifier.padding(top = insets.calculateTopPadding())) {
topBar = {
AppSearchBar(
searchBarState = searchBarState,
performSearch = { viewModel.performSearch(it) },
setExpanded = { viewModel.setExpanded(it) },
setQuery = { viewModel.setQuery(it) },
clearHistory = {
setHistoryItem("")
setShowDeleteDialog(true)
},
removeHistoryItem = {
setHistoryItem(it)
setShowDeleteDialog(true)
},
onSettingsClick = {
navController.navigate("Settings")
it(false)
}
)
AppHomeScreen(
homeScreenState = homeScreenState,
listState = listState,
onImageClick = {
if (homeScreenState.photo != null)
navController.navigate("FullScreenImage")
},
insets = insets,
fontSize = preferencesState.fontSize,
modifier = Modifier
.fillMaxSize()
modifier = Modifier.padding(
top = WindowInsets.statusBars.asPaddingValues().calculateTopPadding()
)
)
}
},
floatingActionButton = {
AppFab(
focusSearch = { viewModel.focusSearchBar() },
scrollToTop = { coroutineScope.launch { listState.animateScrollToItem(0) } },
index = index,
fabEnter = fabEnter,
fabExit = fabExit
)
},
modifier = Modifier.fillMaxSize()
) { insets ->
AppHomeScreen(
homeScreenState = homeScreenState,
listState = listState,
preferencesState = preferencesState,
onImageClick = {
if (homeScreenState.photo != null)
navController.navigate("FullScreenImage")
},
insets = insets,
modifier = Modifier
.fillMaxSize()
.padding(top = insets.calculateTopPadding())
)
}
}

Expand All @@ -167,6 +180,8 @@ fun AppScreen(
onBack = { navController.navigateUp() },
onThemeChanged = { viewModel.setTheme(it) },
onFontSizeChangeFinished = { viewModel.saveFontSize(it) },
onExpandedSectionsChanged = { viewModel.saveExpandedSections(it) },
onDataSaverChanged = { viewModel.saveDataSaver(it) }
)
}
}
Expand All @@ -182,6 +197,13 @@ fun DeleteHistoryItemDialog(
BasicAlertDialog(
onDismissRequest = { setShowDeleteDialog(false) }
) {
val titleText =
if (item != "") "Delete this search from your history?"
else "Delete your search history?"
val descText =
if (item != "") "\"$item\" will be permanently deleted from your search history."
else "Your search history will be permanently deleted."

Surface(
modifier = Modifier
.wrapContentWidth()
Expand All @@ -191,12 +213,12 @@ fun DeleteHistoryItemDialog(
) {
Column(modifier = Modifier.padding(24.dp)) {
Text(
text = "Delete this search from your history?",
text = titleText,
style = MaterialTheme.typography.headlineSmall
)
Spacer(modifier = Modifier.padding(16.dp))
Text(
text = "\"$item\" will be permanently deleted from your search history.",
text = descText,
style = MaterialTheme.typography.bodyMedium
)
Spacer(modifier = Modifier.height(24.dp))
Expand All @@ -205,8 +227,8 @@ fun DeleteHistoryItemDialog(
Text(text = "Cancel")
}
TextButton(onClick = {
removeHistoryItem(item)
setShowDeleteDialog(false)
removeHistoryItem(item)
}) {
Text(text = "Delete")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.nsh07.wikireader.data
import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.intPreferencesKey
import androidx.datastore.preferences.core.stringPreferencesKey
Expand All @@ -13,10 +14,12 @@ import kotlinx.coroutines.flow.first
interface PreferencesRepository {
suspend fun saveStringPreference(key: String, value: String): String
suspend fun saveIntPreference(key: String, value: Int): Int
suspend fun saveBooleanPreference(key: String, value: Boolean): Boolean
suspend fun saveHistory(history: Set<String>)

suspend fun readStringPreference(key: String): String?
suspend fun readIntPreference(key: String): Int?
suspend fun readBooleanPreference(key: String): Boolean?
suspend fun readHistory(): Set<String>?
}

Expand Down Expand Up @@ -49,6 +52,14 @@ class AppPreferencesRepository(
return value
}

override suspend fun saveBooleanPreference(key: String, value: Boolean): Boolean {
val dataStoreKey = booleanPreferencesKey(key)
context.dataStore.edit { preferences ->
preferences[dataStoreKey] = value
}
return value
}

override suspend fun saveHistory(history: Set<String>) {
val dataStoreKey = stringSetPreferencesKey("history")
context.dataStore.edit { preferences ->
Expand All @@ -73,6 +84,11 @@ class AppPreferencesRepository(
return context.dataStore.data.first()[dataStoreKey]
}

override suspend fun readBooleanPreference(key: String): Boolean? {
val dataStoreKey = booleanPreferencesKey(key)
return context.dataStore.data.first()[dataStoreKey]
}

override suspend fun readHistory(): Set<String>? {
val dataStoreKey = stringSetPreferencesKey("history")
return context.dataStore.data.first()[dataStoreKey]
Expand Down
27 changes: 17 additions & 10 deletions app/src/main/java/org/nsh07/wikireader/ui/AppHomeScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import org.nsh07.wikireader.R
import org.nsh07.wikireader.ui.viewModel.HomeScreenState
import org.nsh07.wikireader.ui.viewModel.PreferencesState

/**
* The app home screen composable.
Expand All @@ -41,13 +42,15 @@ import org.nsh07.wikireader.ui.viewModel.HomeScreenState
fun AppHomeScreen(
homeScreenState: HomeScreenState,
listState: LazyListState,
preferencesState: PreferencesState,
onImageClick: () -> Unit,
fontSize: Int,
insets: PaddingValues,
modifier: Modifier = Modifier
) {
val photo = homeScreenState.photo
val photoDesc = homeScreenState.photoDesc
val fontSize = preferencesState.fontSize

var s = homeScreenState.extract.size
if (s > 1) s -= 2
else s = 0
Expand All @@ -56,7 +59,7 @@ fun AppHomeScreen(
if (homeScreenState.title != "") {
LazyColumn( // The article
state = listState,
modifier = modifier.fillMaxSize()
modifier = Modifier.fillMaxSize()
) {
item { // Title
Text(
Expand All @@ -71,7 +74,8 @@ fun AppHomeScreen(
ImageCard(
photo = photo,
photoDesc = photoDesc,
onClick = onImageClick
showPhoto = !preferencesState.dataSaver,
onClick = onImageClick,
)
}
}
Expand All @@ -87,14 +91,17 @@ fun AppHomeScreen(
}
}

items(count = s) { i -> // Expandable sections logic
for (i in 1..s) {
if (i % 2 == 1) // Elements at odd indices are titles
SelectionContainer {
ExpandableSection(
title = homeScreenState.extract[i],
body = homeScreenState.extract[i + 1],
fontSize = fontSize
)
item { // Expandable sections logic
SelectionContainer {
ExpandableSection(
title = homeScreenState.extract[i],
body = homeScreenState.extract[i + 1],
fontSize = fontSize,
expanded = preferencesState.expandedSections
)
}
}
}

Expand Down
16 changes: 11 additions & 5 deletions app/src/main/java/org/nsh07/wikireader/ui/ExpandableSection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import androidx.compose.animation.shrinkVertically
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.KeyboardArrowDown
import androidx.compose.material.icons.outlined.KeyboardArrowUp
Expand All @@ -33,8 +32,14 @@ import org.nsh07.wikireader.R
import org.nsh07.wikireader.ui.theme.WikiReaderTheme

@Composable
fun ExpandableSection(title: String, body: String, fontSize: Int, modifier: Modifier = Modifier) {
var expanded by rememberSaveable { mutableStateOf(false) }
fun ExpandableSection(
title: String,
body: String,
fontSize: Int,
expanded: Boolean,
modifier: Modifier = Modifier
) {
var expanded by rememberSaveable { mutableStateOf(expanded) }

Card(
colors = CardDefaults.cardColors(
Expand All @@ -46,7 +51,7 @@ fun ExpandableSection(title: String, body: String, fontSize: Int, modifier: Modi
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier
.clip(RoundedCornerShape(12.dp))
.clip(MaterialTheme.shapes.large)
.clickable(onClick = { expanded = !expanded })
) {
if (expanded) Icon(
Expand Down Expand Up @@ -93,7 +98,8 @@ fun ExpandableSectionPreview() {
ExpandableSection(
title = "Title",
body = "Lorem\nIpsum\nBig\nHonkin\nBody\nText",
fontSize = 16
fontSize = 16,
expanded = false
)
}
}
3 changes: 2 additions & 1 deletion app/src/main/java/org/nsh07/wikireader/ui/ImageCard.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import org.nsh07.wikireader.data.WikiPhotoDesc
fun ImageCard(
photo: WikiPhoto?,
photoDesc: WikiPhotoDesc,
showPhoto: Boolean,
onClick: () -> Unit,
modifier: Modifier = Modifier
) {
Expand All @@ -53,7 +54,7 @@ fun ImageCard(
modifier = Modifier
.animateContentSize(tween(easing = FastOutSlowInEasing))
) {
if (photo != null) {
if (photo != null && showPhoto) {
PageImage(
photo = photo,
photoDesc = photoDesc,
Expand Down
Loading

0 comments on commit d5d06bd

Please sign in to comment.