1818
1919package org.comixedproject.variant.android.view
2020
21+ import androidx.compose.foundation.layout.fillMaxSize
2122import androidx.compose.foundation.layout.padding
23+ import androidx.compose.material3.Icon
2224import androidx.compose.material3.Scaffold
25+ import androidx.compose.material3.Text
26+ import androidx.compose.material3.adaptive.navigationsuite.NavigationSuiteScaffold
2327import androidx.compose.runtime.Composable
24- import androidx.compose.runtime.collectAsState
2528import androidx.compose.runtime.getValue
2629import androidx.compose.runtime.mutableStateOf
2730import androidx.compose.runtime.remember
28- import androidx.compose.runtime.rememberCoroutineScope
2931import androidx.compose.runtime.setValue
3032import androidx.compose.ui.Modifier
33+ import androidx.compose.ui.res.painterResource
34+ import androidx.compose.ui.res.stringResource
3135import androidx.compose.ui.tooling.preview.Preview
32- import kotlinx.coroutines.Dispatchers
33- import kotlinx.coroutines.launch
36+ import org.comixedproject.variant.android.COMIC_BOOK_LIST
3437import org.comixedproject.variant.android.VariantTheme
3538import org.comixedproject.variant.android.view.comics.ComicBookView
3639import org.comixedproject.variant.android.view.reading.ReadingView
3740import org.comixedproject.variant.android.view.server.ServerView
3841import org.comixedproject.variant.android.view.settings.SettingsView
42+ import org.comixedproject.variant.model.library.ComicBook
3943import org.comixedproject.variant.platform.Log
40- import org.comixedproject.variant.viewmodel.VariantViewModel
41- import org.koin.androidx.compose.koinViewModel
44+ import org.comixedproject.variant.viewmodel.BrowsingState
4245
4346private const val TAG = " HomeView"
4447
4548@Composable
46- fun HomeView () {
47- val variantViewModel: VariantViewModel = koinViewModel()
49+ fun HomeView (
50+ comicBook : ComicBook ? ,
51+ comicBookList : List <ComicBook >,
52+ browsingState : BrowsingState ,
53+ loading : Boolean ,
54+ selectionMode : Boolean ,
55+ selectionList : List <String >,
56+ address : String , username : String , password : String ,
57+ onLoadDirectory : (String , Boolean ) -> Unit ,
58+ onDownloadFile : (String , String ) -> Unit ,
59+ onReadComicBook : (ComicBook ? ) -> Unit ,
60+ onSetSelectionMode : (Boolean ) -> Unit ,
61+ onUpdateSelection : (ComicBook ) -> Unit ,
62+ onDeleteSelections : () -> Unit ,
63+ onSaveSettings : (String , String , String ) -> Unit
64+ ) {
4865 var currentDestination by remember { mutableStateOf(AppDestination .COMICS ) }
49- val coroutineScope = rememberCoroutineScope()
50- val comicBookList by variantViewModel.comicBookList.collectAsState()
51- val selectionMode by variantViewModel.selectionMode.collectAsState()
52- val selectionList by variantViewModel.selectionList.collectAsState()
53- val comicBook by variantViewModel.comicBook.collectAsState()
5466
5567 Scaffold (
56- topBar = {
57- VariantTopAppBar (
58- onBrowseComics = { currentDestination = AppDestination .COMICS },
59- onBrowseServer = {
60- coroutineScope.launch(Dispatchers .IO ) {
61- variantViewModel.loadDirectory(
62- variantViewModel.browsingState.value.currentPath,
63- false
64- )
65- }
66- currentDestination = AppDestination .BROWSE
67- },
68- onUpdateSettings = { currentDestination = AppDestination .SETTINGS })
69- },
68+ topBar = { VariantTopAppBar () },
7069 content = { padding ->
71- when (currentDestination) {
72- AppDestination .COMICS ->
73- if (comicBook != null ) {
74- ReadingView (
75- comicBook!! ,
76- modifier = Modifier .padding(padding),
77- onStopReading = { variantViewModel.readComicBook(null ) }
78- )
79- } else {
80- ComicBookView (
81- comicBookList,
82- selectionMode,
83- selectionList,
84- onSetSelectionMode = {
85- Log .info(TAG , " Setting selection mode: ${it} " )
86- variantViewModel.setSelectMode(it)
70+ NavigationSuiteScaffold (
71+ navigationSuiteItems = {
72+ AppDestination .entries.forEach {
73+ item(
74+ icon = {
75+ Icon (
76+ painterResource(it.icon),
77+ contentDescription = stringResource(it.label)
78+ )
8779 },
88- onComicBookClicked = { comicBook ->
89- if (selectionMode) {
90- Log .info(
91- TAG ,
92- " Toggling comic book selection: ${comicBook.path} "
93- )
94- variantViewModel.updateSelectionList(comicBook.path)
95- } else {
96- Log .info(TAG , " Reading comic book: ${comicBook.filename} " )
97- variantViewModel.readComicBook(comicBook)
80+ label = { Text (stringResource(it.label)) },
81+ selected = it == currentDestination,
82+ onClick = {
83+ if (it == AppDestination .BROWSE
84+ ) {
85+ onLoadDirectory(browsingState.currentPath, false )
9886 }
99- },
100- onDeleteComics = {
101- coroutineScope.launch(Dispatchers .IO ) {
102- variantViewModel.deleteSelections()
103- }
104- },
105- modifier = Modifier .padding(padding)
87+ currentDestination = it
88+ }
10689 )
10790 }
91+ },
92+ modifier = Modifier .padding(padding)
93+ ) {
94+ when (currentDestination) {
95+ AppDestination .COMICS ->
96+ if (comicBook != null ) {
97+ ReadingView (
98+ comicBook,
99+ onStopReading = { onReadComicBook(null ) },
100+ modifier = Modifier
101+ .fillMaxSize()
102+ )
103+ } else {
104+ ComicBookView (
105+ comicBookList,
106+ selectionMode,
107+ selectionList,
108+ onSetSelectionMode = {
109+ Log .info(TAG , " Setting selection mode: ${it} " )
110+ onSetSelectionMode(it)
111+ },
112+ onComicBookClicked = { comicBook ->
113+ if (selectionMode) {
114+ Log .info(
115+ TAG ,
116+ " Toggling comic book selection: ${comicBook.path} "
117+ )
118+ onUpdateSelection(comicBook)
119+ } else {
120+ Log .info(TAG , " Reading comic book: ${comicBook.filename} " )
121+ onReadComicBook(comicBook)
122+ }
123+ },
124+ onDeleteComics = { onDeleteSelections() },
125+ modifier = Modifier
126+ .fillMaxSize()
127+ )
128+ }
129+
130+ AppDestination .BROWSE -> ServerView (
131+ browsingState, comicBookList, loading,
132+ onLoadDirectory = { path, reload -> onLoadDirectory(path, reload) },
133+ onDownloadFile = { path, filename -> onDownloadFile(path, filename) },
134+ modifier = Modifier
135+ .fillMaxSize()
136+ )
108137
109- AppDestination .BROWSE -> ServerView (modifier = Modifier .padding(padding))
110- AppDestination .SETTINGS -> SettingsView (onCloseSettings = {
111- currentDestination = AppDestination .COMICS
112- }, modifier = Modifier .padding(padding))
138+ AppDestination .SETTINGS -> SettingsView (
139+ address, username, password,
140+ onSaveSettings = { address, username, password ->
141+ Log .info(
142+ TAG ,
143+ " Updating server settings: address=${address} username=${username} password=${
144+ password.first()
145+ } *****"
146+ )
147+ onSaveSettings(address, username, password)
148+ currentDestination = AppDestination .COMICS
149+ },
150+ modifier = Modifier
151+ .fillMaxSize()
152+ )
153+ }
113154 }
114- }
115- )
155+ })
116156}
117157
118158@Composable
119159@Preview
120160fun HomeViewPreview () {
121- VariantTheme { HomeView () }
161+ VariantTheme {
162+ HomeView (
163+ COMIC_BOOK_LIST .get(0 ),
164+ COMIC_BOOK_LIST ,
165+ BrowsingState (" " , " " , " " , listOf (), listOf ()),
166+ false ,
167+ false ,
168+ listOf (),
169+ " http://www.comixedproject.org:7171" , " reader@comixedproject.org" , " my!password" ,
170+ onLoadDirectory = { _, _ -> },
171+ onDownloadFile = { _, _ -> },
172+ onReadComicBook = { _ -> },
173+ onSetSelectionMode = { _ -> },
174+ onUpdateSelection = { _ -> },
175+ onDeleteSelections = { }, onSaveSettings = { _, _, _ -> }
176+ )
177+ }
122178}
0 commit comments