/
HomeScreen.kt
232 lines (225 loc) · 9.56 KB
/
HomeScreen.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package com.desarrollodroide.pagekeeper.ui.home
import androidx.activity.compose.BackHandler
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.ui.Modifier
import org.koin.androidx.compose.get
import androidx.compose.runtime.*
import androidx.compose.material3.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.FilterList
import androidx.compose.material.icons.filled.Search
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.TopAppBarScrollBehavior
import androidx.compose.ui.Alignment
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.desarrollodroide.data.helpers.SHIORI_ANDROID_CLIENT_GITHUB_URL
import com.desarrollodroide.pagekeeper.navigation.NavItem
import com.desarrollodroide.pagekeeper.ui.feed.FeedScreen
import com.desarrollodroide.pagekeeper.ui.feed.FeedViewModel
import com.desarrollodroide.pagekeeper.ui.settings.PrivacyPolicyScreen
import com.desarrollodroide.pagekeeper.ui.settings.SettingsScreen
import com.desarrollodroide.pagekeeper.ui.settings.TermsOfUseScreen
import java.io.File
import com.desarrollodroide.pagekeeper.R
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun HomeScreen(
feedViewModel: FeedViewModel,
goToLogin: () -> Unit,
onFinish: () -> Unit,
openUrlInBrowser: (String) -> Unit,
shareEpubFile: (File) -> Unit,
) {
val navController = rememberNavController()
val (isCategoriesVisible, setCategoriesVisible) = remember { mutableStateOf(feedViewModel.isCategoriesVisible()) }
val isSearchBarVisible = remember { mutableStateOf(false) }
val (showTopBar, setShowTopBar) = remember { mutableStateOf(true) }
val hasCategories = feedViewModel.uniqueCategories.value.isNotEmpty()
val hasBookmarks = feedViewModel.bookmarksUiState.collectAsState().value.data?.isNotEmpty() == true
val clickSearch: ( () -> Unit) = {}
BackHandler {
onFinish()
}
NavHost(
navController = navController,
startDestination = NavItem.HomeNavItem.route
) {
composable(NavItem.HomeNavItem.route) {
val scrollBehavior = TopAppBarDefaults.enterAlwaysScrollBehavior()
Scaffold(
containerColor = MaterialTheme.colorScheme.background,
modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
topBar = {
AnimatedVisibility (showTopBar) {
TopBar(
toggleCategoryVisibility = {
setCategoriesVisible(!isCategoriesVisible)
feedViewModel.saveCategoriesVisibilityState(!isCategoriesVisible)
},
toggleSearchBarVisibility = { isSearchBarVisible.value = !isSearchBarVisible.value },
onSettingsClick = { navController.navigate(NavItem.SettingsNavItem.route) },
//isSearchActive = isSearchBarVisible,
isFilterActive = isCategoriesVisible,
scrollBehavior = scrollBehavior,
hasCategories = hasCategories,
hasBookmarks = hasBookmarks,
onSearchClicked = clickSearch
)
}
}
) { paddingValues ->
Box(
modifier = Modifier
.padding(paddingValues)
) {
FeedScreen(
feedViewModel = feedViewModel,
isCategoriesVisible = isCategoriesVisible,
goToLogin = goToLogin,
openUrlInBrowser = openUrlInBrowser,
shareEpubFile = shareEpubFile,
isSearchBarVisible = isSearchBarVisible,
setShowTopBar = setShowTopBar,
)
}
}
}
composable(NavItem.SettingsNavItem.route) {
SettingsScreen(
settingsViewModel = get(),
goToLogin = goToLogin,
onNavigateToPrivacyPolicy = {
navController.navigate(NavItem.PrivacyPolicyNavItem.route)
},
onNavigateToTermsOfUse = {
navController.navigate(NavItem.TermsOfUseNavItem.route)
},
onBack = {
navController.navigateUp()
},
onNavigateToSourceCode = {
openUrlInBrowser.invoke(SHIORI_ANDROID_CLIENT_GITHUB_URL)
}
)
}
composable(NavItem.TermsOfUseNavItem.route) {
TermsOfUseScreen(
onBack = {
navController.navigateUp()
}
)
}
composable(NavItem.PrivacyPolicyNavItem.route) {
PrivacyPolicyScreen(
onBack = {
navController.navigateUp()
}
)
}
}
}
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun TopBar(
toggleCategoryVisibility: () -> Unit,
toggleSearchBarVisibility: () -> Unit,
onSettingsClick: () -> Unit,
onSearchClicked: () -> Unit,
// isSearchActive: Boolean,
isFilterActive: Boolean,
scrollBehavior: TopAppBarScrollBehavior,
hasCategories: Boolean,
hasBookmarks: Boolean,
) {
TopAppBar(
scrollBehavior = scrollBehavior,
title = {
Box(modifier = Modifier.fillMaxWidth()) {
Text(
color = MaterialTheme.colorScheme.primary,
text = "PageKeeper",
modifier = Modifier
.align(Alignment.CenterStart)
.padding(bottom = 4.dp),
style = MaterialTheme.typography.titleLarge.copy(fontWeight = FontWeight.Bold, fontSize = 28.sp)
)
}
},
navigationIcon = {
Image(
painter = painterResource(id = R.drawable.logo_pagekeeper),
contentDescription = "Menu",
modifier = Modifier
.width(45.dp)
.padding(8.dp)
)
},
actions = {
if (hasBookmarks) {
IconButton(onClick = { toggleSearchBarVisibility() }) {
Icon(
imageVector = Icons.Filled.Search,
contentDescription = "Search",
tint = MaterialTheme.colorScheme.secondary,
// modifier = Modifier
// .background(
// if (isSearchActive) MaterialTheme.colorScheme.secondaryContainer else Color.Transparent,
// shape = CircleShape
// )
// .padding(6.dp)
)
}
}
if (hasCategories){
IconButton(onClick = { toggleCategoryVisibility() }) {
Icon(
imageVector = Icons.Filled.FilterList,
contentDescription = "Filter",
tint = if (isFilterActive) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.secondary,
modifier = Modifier
.background(
if (isFilterActive) MaterialTheme.colorScheme.secondaryContainer else Color.Transparent,
shape = CircleShape
)
.padding(6.dp)
)
}
}
IconButton(onClick = onSettingsClick ) {
Icon(
imageVector = Icons.Filled.Settings,
contentDescription = "Settings",
tint = MaterialTheme.colorScheme.secondary
)
}
},
colors = TopAppBarDefaults.smallTopAppBarColors(
containerColor = MaterialTheme.colorScheme.background, // Sets the background color of the TopAppBar
titleContentColor = MaterialTheme.colorScheme.primary, // Optional: Set the title color if needed
navigationIconContentColor = MaterialTheme.colorScheme.primary, // Optional: Set the navigation icon color if needed
actionIconContentColor = MaterialTheme.colorScheme.primary // Optional: Set the action icons color if needed
)
)
}