Skip to content

Commit

Permalink
Cleaning up
Browse files Browse the repository at this point in the history
Trying to use the listdetailscaffold with settings, but it is just not working like it was previously. Need to look more into it
  • Loading branch information
jakepurple13 committed Dec 20, 2023
1 parent 8202719 commit e8c0648
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,12 @@ abstract class BaseMainActivity : AppCompatActivity() {
historyClick = { navController.navigate(Screen.HistoryScreen.route) { launchSingleTop = true } },
globalSearchClick = { navController.navigate(Screen.GlobalSearchScreen.route) { launchSingleTop = true } },
listClick = { navController.navigate(Screen.CustomListScreen.route) { launchSingleTop = true } },
debugMenuClick = { navController.navigate(Screen.DebugScreen.route) { launchSingleTop = true } }
debugMenuClick = { navController.navigate(Screen.DebugScreen.route) { launchSingleTop = true } },
extensionClick = { navController.navigate(Screen.ExtensionListScreen.route) { launchSingleTop = true } },
notificationSettingsClick = { navController.navigate(Screen.NotificationsSettings.route) },
generalClick = { navController.navigate(Screen.GeneralSettings.route) },
otherClick = { navController.navigate(Screen.OtherSettings.route) },
moreInfoClick = { navController.navigate(Screen.MoreInfoSettings.route) }
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

package com.programmersbox.uiviews.lists

import android.content.Context
import android.content.Intent
import android.graphics.drawable.Drawable
import android.net.Uri
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.animation.AnimatedVisibility
Expand Down Expand Up @@ -83,6 +85,7 @@ import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import coil.compose.rememberAsyncImagePainter
import coil.request.ImageRequest
import com.programmersbox.favoritesdatabase.CustomList
import com.programmersbox.favoritesdatabase.CustomListInfo
import com.programmersbox.favoritesdatabase.ListDao
import com.programmersbox.favoritesdatabase.toDbModel
Expand Down Expand Up @@ -124,20 +127,30 @@ import org.koin.compose.koinInject
@OptIn(ExperimentalMaterial3Api::class, ExperimentalFoundationApi::class)
@Composable
fun OtakuCustomListScreen(
viewModel: OtakuListViewModel,
customItem: CustomList?,
writeToFile: (Uri, Context) -> Unit,
deleteAll: suspend () -> Unit,
rename: suspend (String) -> Unit,
listBySource: Map<String, List<CustomListInfo>>,
removeItems: suspend (List<CustomListInfo>) -> Result<Boolean>,
items: List<Map.Entry<String, List<CustomListInfo>>>,
searchItems: List<CustomListInfo>,
searchQuery: String,
setQuery: (String) -> Unit,
searchBarActive: Boolean,
onSearchBarActiveChange: (Boolean) -> Unit,
navigateBack: () -> Unit,
isHorizontal: Boolean = false,
) {
val context = LocalContext.current
val scope = rememberCoroutineScope()
val customItem = viewModel.customItem
val snackbarHostState = remember { SnackbarHostState() }

val logoDrawable = koinInject<AppLogo>()

val pickDocumentLauncher = rememberLauncherForActivityResult(
ActivityResultContracts.CreateDocument("application/json")
) { document -> document?.let { viewModel.writeToFile(it, context) } }
) { document -> document?.let { writeToFile(it, context) } }

val shareItem = rememberLauncherForActivityResult(
ActivityResultContracts.StartActivityForResult()
Expand Down Expand Up @@ -166,7 +179,7 @@ fun OtakuCustomListScreen(
TextButton(
onClick = {
scope.launch {
withContext(Dispatchers.IO) { viewModel.deleteAll() }
withContext(Dispatchers.IO) { deleteAll() }
deleteList = false
navigateBack()
}
Expand Down Expand Up @@ -202,7 +215,7 @@ fun OtakuCustomListScreen(
TextButton(
onClick = {
scope.launch {
viewModel.rename(name)
rename(name)
showAdd = false
}
},
Expand All @@ -226,8 +239,8 @@ fun OtakuCustomListScreen(

if (showDeleteModal) {
DeleteItemsModal(
list = viewModel.listBySource,
onRemove = viewModel::removeItems,
list = listBySource,
onRemove = removeItems,
onDismiss = { showDeleteModal = false },
drawable = logoDrawable.logo
)
Expand Down Expand Up @@ -272,20 +285,20 @@ fun OtakuCustomListScreen(
snackbarHost = { SnackbarHost(snackbarHostState) },
topBar = {
DynamicSearchBar(
query = viewModel.searchQuery,
onQueryChange = viewModel::setQuery,
query = searchQuery,
onQueryChange = setQuery,
isDocked = isHorizontal,
onSearch = { viewModel.searchBarActive = false },
active = viewModel.searchBarActive,
onActiveChange = { viewModel.searchBarActive = it },
onSearch = { onSearchBarActiveChange(false) },
active = searchBarActive,
onActiveChange = { onSearchBarActiveChange(it) },
placeholder = { Text(stringResource(id = R.string.search) + " " + customItem?.item?.name.orEmpty()) },
leadingIcon = {
IconButton(onClick = navigateBack) { Icon(Icons.AutoMirrored.Filled.ArrowBack, null) }
},
colors = SearchBarDefaults.colors(
containerColor = animateColorAsState(
MaterialTheme.colorScheme.surface.copy(
alpha = if (viewModel.searchBarActive) 1f else 0f
alpha = if (searchBarActive) 1f else 0f
),
label = ""
).value,
Expand All @@ -294,8 +307,8 @@ fun OtakuCustomListScreen(
Row(
verticalAlignment = Alignment.CenterVertically
) {
AnimatedVisibility(viewModel.searchQuery.isNotEmpty()) {
IconButton(onClick = { viewModel.setQuery("") }) {
AnimatedVisibility(searchQuery.isNotEmpty()) {
IconButton(onClick = { setQuery("") }) {
Icon(Icons.Default.Cancel, null)
}
}
Expand Down Expand Up @@ -348,7 +361,7 @@ fun OtakuCustomListScreen(
)
}

AnimatedVisibility(!viewModel.searchBarActive) {
AnimatedVisibility(!searchBarActive) {
IconButton(
onClick = {
shareItem.launchCatching(
Expand All @@ -367,7 +380,7 @@ fun OtakuCustomListScreen(
}
) { Icon(Icons.Default.Share, null) }
}
AnimatedVisibility(!viewModel.searchBarActive) {
AnimatedVisibility(!searchBarActive) {
IconButton(onClick = { showMenu = true }) { Icon(Icons.Default.MoreVert, null) }
}
}
Expand All @@ -381,14 +394,14 @@ fun OtakuCustomListScreen(
.padding(16.dp)
.fillMaxWidth(),
) {
itemsIndexed(items = viewModel.searchItems) { index, item ->
itemsIndexed(items = searchItems) { index, item ->
ListItem(
headlineContent = { Text(item.title) },
leadingContent = { Icon(Icons.Filled.Search, contentDescription = null) },
modifier = Modifier
.clickable {
viewModel.setQuery(item.title)
viewModel.searchBarActive = false
setQuery(item.title)
onSearchBarActiveChange(false)
}
.animateItemPlacement()
)
Expand All @@ -409,7 +422,7 @@ fun OtakuCustomListScreen(
modifier = Modifier.padding(vertical = 4.dp)
) {
items(
items = viewModel.items,
items = items,
key = { it.key },
contentType = { it }
) { item ->
Expand Down Expand Up @@ -684,10 +697,22 @@ private fun DeleteItemsModal(
private fun CustomListScreenPreview() {
PreviewTheme {
val listDao: ListDao = LocalCustomListDao.current
val vm: OtakuListViewModel = viewModel { OtakuListViewModel(listDao) }
val viewModel: OtakuListViewModel = viewModel { OtakuListViewModel(listDao) }
OtakuCustomListScreen(
viewModel = vm,
navigateBack = {}
customItem = null,
writeToFile = viewModel::writeToFile,
navigateBack = {},
isHorizontal = false,
deleteAll = viewModel::deleteAll,
rename = viewModel::rename,
listBySource = viewModel.listBySource,
removeItems = viewModel::removeItems,
items = viewModel.items,
searchItems = viewModel.searchItems,
searchQuery = viewModel.searchQuery,
setQuery = viewModel::setQuery,
searchBarActive = viewModel.searchBarActive,
onSearchBarActiveChange = { viewModel.searchBarActive = it }
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ fun OtakuListScreen(
viewModel: OtakuListViewModel = viewModel { OtakuListViewModel(listDao) },
isHorizontal: Boolean = false,
) {
//TODO: Figure out why this is suddenly not working
val showListDetail by LocalSettingsHandling.current
.showListDetail
.collectAsStateWithLifecycle(true)
Expand All @@ -58,7 +59,7 @@ fun OtakuListScreen(
)

val details: @Composable ThreePaneScaffoldScope.() -> Unit = {
AnimatedPane(modifier = Modifier) {
AnimatedPane(modifier = Modifier.fillMaxSize()) {
AnimatedContent(
targetState = viewModel.customItem,
label = "",
Expand All @@ -68,12 +69,23 @@ fun OtakuListScreen(
) { targetState ->
if (targetState != null) {
OtakuCustomListScreen(
viewModel = viewModel,
customItem = targetState,
writeToFile = viewModel::writeToFile,
isHorizontal = isHorizontal,
deleteAll = viewModel::deleteAll,
rename = viewModel::rename,
listBySource = viewModel.listBySource,
removeItems = viewModel::removeItems,
items = viewModel.items,
searchItems = viewModel.searchItems,
searchQuery = viewModel.searchQuery,
setQuery = viewModel::setQuery,
searchBarActive = viewModel.searchBarActive,
onSearchBarActiveChange = { viewModel.searchBarActive = it },
navigateBack = {
viewModel.customItem = null
state.navigateBack()
},
isHorizontal = isHorizontal
)
BackHandler {
viewModel.customItem = null
Expand All @@ -91,8 +103,10 @@ fun OtakuListScreen(
listPane = {
AnimatedPane(modifier = Modifier.fillMaxSize()) {
OtakuListView(
viewModel = viewModel,
customItem = viewModel.customItem,
customLists = viewModel.customLists,
navigateDetail = {
viewModel.customItem = it
if (showListDetail)
state.navigateTo(ListDetailPaneScaffoldRole.Detail)
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import com.programmersbox.favoritesdatabase.CustomList
import com.programmersbox.uiviews.R
import com.programmersbox.uiviews.utils.BackButton
import com.programmersbox.uiviews.utils.InsetSmallTopAppBar
Expand All @@ -55,8 +56,9 @@ import kotlinx.coroutines.launch
@OptIn(ExperimentalMaterial3Api::class, ExperimentalFoundationApi::class)
@Composable
fun OtakuListView(
viewModel: OtakuListViewModel,
navigateDetail: () -> Unit,
customLists: List<CustomList>,
customItem: CustomList? = null,
navigateDetail: (CustomList) -> Unit,
) {
val navController = LocalNavController.current
val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior(rememberTopAppBarState())
Expand Down Expand Up @@ -122,16 +124,15 @@ fun OtakuListView(
verticalArrangement = Arrangement.spacedBy(4.dp),
modifier = Modifier.fillMaxSize()
) {
items(viewModel.customLists) {
items(customLists) {
ElevatedCard(
onClick = {
viewModel.customItem = it
navigateDetail()
navigateDetail(it)
},
modifier = Modifier
.animateItemPlacement()
.padding(horizontal = 4.dp)
.thenIf(viewModel.customItem == it) {
.thenIf(customItem == it) {
border(
2.dp,
color = MaterialTheme.colorScheme.primary,
Expand Down Expand Up @@ -164,7 +165,8 @@ fun OtakuListView(
private fun ListScreenPreview() {
PreviewTheme {
OtakuListView(
viewModel = OtakuListViewModel(LocalCustomListDao.current),
customLists = emptyList(),
customItem = null,
navigateDetail = {}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import kotlinx.coroutines.runBlocking
@ExperimentalComposeUiApi
@Composable
fun GeneralSettings(
customSettings: (@Composable () -> Unit)? = null,
customSettings: @Composable () -> Unit = {},
) {
SettingsScaffold(stringResource(R.string.general_menu_title)) {
val scope = rememberCoroutineScope()
Expand Down Expand Up @@ -142,7 +142,7 @@ fun GeneralSettings(
}
)

customSettings?.invoke()
customSettings()
}
}

Expand All @@ -151,6 +151,6 @@ fun GeneralSettings(
@Composable
private fun GeneralSettingsPreview() {
PreviewTheme {
GeneralSettings(null)
GeneralSettings()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import kotlinx.coroutines.runBlocking
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun PlaySettings(
customSettings: (@Composable () -> Unit)? = null,
customSettings: @Composable () -> Unit = {},
) {
SettingsScaffold(stringResource(R.string.playSettings)) {
val scope = rememberCoroutineScope()
Expand All @@ -43,14 +43,14 @@ fun PlaySettings(
onValueChangedFinished = { scope.launch { settingsHandling.setBatteryPercentage(sliderValue.toInt()) } }
)

customSettings?.invoke()
customSettings()
}
}

@LightAndDarkPreviews
@Composable
private fun PlaySettingsPreview() {
PreviewTheme {
PlaySettings(null)
PlaySettings()
}
}
Loading

0 comments on commit e8c0648

Please sign in to comment.