-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathMainActivity.kt
More file actions
471 lines (432 loc) · 17.5 KB
/
Copy pathMainActivity.kt
File metadata and controls
471 lines (432 loc) · 17.5 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
package io.simplelogin.android
import android.content.Intent
import android.content.pm.ActivityInfo
import android.os.Build
import android.os.Bundle
import androidx.activity.SystemBarStyle
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.background
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.widthIn
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.outlined.ContactSupport
import androidx.compose.material.icons.automirrored.outlined.Logout
import androidx.compose.material.icons.outlined.AllInbox
import androidx.compose.material.icons.outlined.Language
import androidx.compose.material.icons.outlined.Person
import androidx.compose.material.icons.outlined.Settings
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.DrawerState
import androidx.compose.material3.DrawerValue
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ModalDrawerSheet
import androidx.compose.material3.ModalNavigationDrawer
import androidx.compose.material3.NavigationDrawerItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Snackbar
import androidx.compose.material3.SnackbarDefaults
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.SnackbarResult
import androidx.compose.material3.Text
import androidx.compose.material3.rememberDrawerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.core.net.toUri
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.AndroidEntryPoint
import dagger.hilt.android.lifecycle.HiltViewModel
import io.simplelogin.android.root.AppRoot
import io.simplelogin.android.root.AppRootViewModel
import io.simplelogin.core.common.ProtonLinkManager
import io.simplelogin.core.common.ProtonLoginManager
import io.simplelogin.core.common.di.LoadingState
import io.simplelogin.core.common.di.LoadingStateFlow
import io.simplelogin.core.common.usecase.ObserveDeviceSettingsUseCase
import io.simplelogin.core.common.usecase.ObserveSessionSettingsUseCase
import io.simplelogin.core.designsystem.clickableRippleDisabled
import io.simplelogin.core.designsystem.snackbar.SnackbarManager
import io.simplelogin.core.designsystem.snackbar.colors
import io.simplelogin.core.designsystem.theme.SimpleLoginTheme
import io.simplelogin.core.designsystem.theme.Spacing
import io.simplelogin.core.model.api.UserInfo
import io.simplelogin.core.model.preferences.DevicePreferences
import io.simplelogin.core.model.preferences.Theme
import io.simplelogin.core.network.BaseUrlProvider
import io.simplelogin.core.ui.UserInfoCard
import io.simplelogin.feature.lock.LockScreen
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import javax.inject.Inject
import io.simplelogin.core.designsystem.R as DesignSystemR
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
private val viewModel: MainViewModel by viewModels()
private val appRootViewModel: AppRootViewModel by viewModels()
@Inject
lateinit var protonLoginManager: ProtonLoginManager
@Inject
lateinit var protonLinkManager: ProtonLinkManager
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
setRecentsScreenshotEnabled(false)
}
viewModel.observe()
setUpSplashScreen()
applyOrientationRestrictions()
enableEdgeToEdge()
setContent {
val devicePreferences by viewModel.devicePreferences.collectAsState()
val darkTheme = when (devicePreferences.theme) {
Theme.LIGHT -> false
Theme.DARK -> true
Theme.MATCH_SYSTEM -> isSystemInDarkTheme()
}
val drawerState = rememberDrawerState(initialValue = DrawerValue.Closed)
val scope = rememberCoroutineScope()
val appRooState by appRootViewModel.stateFlow.collectAsState()
val userInfo by viewModel.userInfoStateFlow.collectAsState()
DisposableEffect(darkTheme) {
enableEdgeToEdge(
statusBarStyle = SystemBarStyle.auto(
android.graphics.Color.TRANSPARENT,
android.graphics.Color.TRANSPARENT,
) { darkTheme },
navigationBarStyle = SystemBarStyle.auto(
android.graphics.Color.TRANSPARENT,
android.graphics.Color.TRANSPARENT,
) { darkTheme }
)
onDispose {}
}
fun closeDrawerAndExecute(task: () -> Unit) {
scope.launch {
drawerState.close()
task()
}
}
fun openAccountSettings() {
closeDrawerAndExecute(appRootViewModel::showAccountSettingsScreen)
}
SimpleLoginTheme(darkTheme = darkTheme, dynamicColor = devicePreferences.dynamicColor) {
Box(modifier = Modifier.fillMaxSize()) {
ModalNavigationDrawer(
drawerState = drawerState,
gesturesEnabled = userInfo != null,
drawerContent = {
Drawer(
appVersion = appRootViewModel.appVersion,
userInfo = userInfo,
onUserInfoClick = {
openAccountSettings()
},
onMailboxesClick = {
closeDrawerAndExecute(appRootViewModel::showMailboxesScreen)
},
onCustomDomainsClick = {
closeDrawerAndExecute(appRootViewModel::showCustomDomainsScreen)
},
onAccountSettingsClick = {
openAccountSettings()
},
onDeviceSettingsClick = {
closeDrawerAndExecute(appRootViewModel::showDeviceSettingsScreen)
},
onContactUsClick = {
closeDrawerAndExecute(::openContactUsPage)
},
onSignOutClick = {
closeDrawerAndExecute(appRootViewModel::showLogOutDialog)
}
)
},
content = {
MainUi(
drawerState = drawerState,
viewModel = viewModel,
appRootViewModel = appRootViewModel
)
}
)
if (appRooState.apiKey != null) {
LockScreen(onLogOut = { appRootViewModel.logOut() })
}
}
}
}
}
/**
* Callback for when the Login with Proton process is done.
* The Login with Proton will redirect the user to
* auth.simplelogin://**/login?apikey=YOUR_API_KEY
*
* (The intent-filter is registered in AndroidManifest.xml)
*/
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
when (intent.data?.path) {
"/login" -> {
val apiKey = intent.data?.getQueryParameter("apikey") ?: return
protonLoginManager.pendingApiKey.tryEmit(apiKey)
}
"/link" -> protonLinkManager.linkedEvents.tryEmit(Unit)
}
}
private fun setUpSplashScreen() {
val splashScreen = installSplashScreen()
splashScreen.setKeepOnScreenCondition { !appRootViewModel.stateFlow.value.isReady }
}
private fun applyOrientationRestrictions() {
val configuration = resources.configuration
val isTablet = configuration.smallestScreenWidthDp >= 600
requestedOrientation = if (isTablet) {
ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
} else {
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
}
}
private fun openContactUsPage() {
val intent = Intent(Intent.ACTION_VIEW, "https://simplelogin.io/contact/".toUri())
startActivity(intent)
}
}
// TODO: Convert to a use case
@HiltViewModel
class MainViewModel @Inject constructor(
@LoadingState private val loadingState: LoadingStateFlow,
private val baseUrlProvider: BaseUrlProvider,
private val observeSessionSettings: ObserveSessionSettingsUseCase,
val snackbarManager: SnackbarManager,
observeDeviceSettings: ObserveDeviceSettingsUseCase
) : ViewModel() {
val showLoadingIndicator = loadingState.asStateFlow()
val devicePreferences = observeDeviceSettings()
.stateIn(
viewModelScope,
SharingStarted.Eagerly,
DevicePreferences.Default
)
val userInfoStateFlow = observeSessionSettings()
.map { it.userInfo }
.stateIn(
viewModelScope,
SharingStarted.Eagerly,
null
)
fun observe() {
viewModelScope.launch {
observeSessionSettings()
.collect {
baseUrlProvider.updateBaseUrl(it.baseUrl)
}
}
}
}
@Composable
private fun MainUi(
drawerState: DrawerState,
viewModel: MainViewModel,
appRootViewModel: AppRootViewModel
) {
val scope = rememberCoroutineScope()
val snackbarHostState = remember { SnackbarHostState() }
val isLoading by viewModel.showLoadingIndicator.collectAsState()
var currentSnackbarJob by remember { mutableStateOf<Job?>(null) }
LaunchedEffect(Unit) {
viewModel.snackbarManager.configuration.collect { configuration ->
currentSnackbarJob?.cancel()
currentSnackbarJob = scope.launch {
val result = snackbarHostState.showSnackbar(visuals = configuration.toVisuals())
when (result) {
SnackbarResult.ActionPerformed -> {
configuration.action?.action?.let { it() }
}
else -> Unit
}
}
}
}
Scaffold(
modifier = Modifier.fillMaxSize(),
snackbarHost = {
SnackbarHost(
hostState = snackbarHostState,
snackbar = { data ->
val colors = data.visuals.colors()
Snackbar(
snackbarData = data,
containerColor = colors.containerColor ?: SnackbarDefaults.color,
contentColor = colors.contentColor ?: SnackbarDefaults.contentColor,
actionColor = colors.actionColor ?: SnackbarDefaults.actionColor,
actionContentColor = colors.actionContentColor
?: SnackbarDefaults.actionContentColor,
dismissActionContentColor = colors.dismissActionContentColor
?: SnackbarDefaults.dismissActionContentColor,
)
}
)
}
) { innerPadding ->
Box(
modifier = Modifier.fillMaxSize()
) {
AppRoot(
modifier = Modifier.fillMaxSize(),
innerPadding = innerPadding,
viewModel = appRootViewModel,
onOpenDrawer = {
scope.launch {
drawerState.open()
}
}
)
AnimatedVisibility(
visible = isLoading,
enter = fadeIn(),
exit = fadeOut()
) {
Box(
modifier = Modifier
.fillMaxSize()
.background(Color.LightGray.copy(alpha = 0.1f))
// Intercept all click events to disable click while loading
.clickableRippleDisabled(onClick = {}),
contentAlignment = Alignment.Center
) {
CircularProgressIndicator()
}
}
}
}
}
@Composable
private fun Drawer(
appVersion: String,
userInfo: UserInfo?,
onUserInfoClick: () -> Unit,
onMailboxesClick: () -> Unit,
onCustomDomainsClick: () -> Unit,
onAccountSettingsClick: () -> Unit,
onDeviceSettingsClick: () -> Unit,
onContactUsClick: () -> Unit,
onSignOutClick: () -> Unit
) {
ModalDrawerSheet(
drawerShape = RectangleShape
) {
userInfo?.let {
UserInfoCard(userInfo = it, onClick = onUserInfoClick)
}
NavigationDrawerItem(
label = { Text(text = stringResource(R.string.mailboxes)) },
icon = { Icon(imageVector = Icons.Outlined.AllInbox, contentDescription = null) },
shape = RectangleShape,
selected = false,
onClick = onMailboxesClick
)
NavigationDrawerItem(
label = { Text(text = stringResource(R.string.custom_domains)) },
icon = { Icon(imageVector = Icons.Outlined.Language, contentDescription = null) },
shape = RectangleShape,
selected = false,
onClick = onCustomDomainsClick
)
HorizontalDivider()
NavigationDrawerItem(
label = { Text(text = stringResource(R.string.account_settings)) },
icon = { Icon(imageVector = Icons.Outlined.Person, contentDescription = null) },
shape = RectangleShape,
selected = false,
onClick = onAccountSettingsClick
)
NavigationDrawerItem(
label = { Text(text = stringResource(R.string.device_settings)) },
icon = { Icon(imageVector = Icons.Outlined.Settings, contentDescription = null) },
shape = RectangleShape,
selected = false,
onClick = onDeviceSettingsClick
)
HorizontalDivider()
NavigationDrawerItem(
label = { Text(text = stringResource(R.string.contact_us)) },
icon = {
Icon(
imageVector = Icons.AutoMirrored.Outlined.ContactSupport,
contentDescription = null
)
},
shape = RectangleShape,
selected = false,
onClick = onContactUsClick
)
HorizontalDivider()
NavigationDrawerItem(
label = { Text(text = stringResource(R.string.sign_out)) },
icon = {
Icon(
imageVector = Icons.AutoMirrored.Outlined.Logout,
contentDescription = null
)
},
shape = RectangleShape,
selected = false,
onClick = onSignOutClick
)
Spacer(modifier = Modifier.weight(1f))
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.Center
) {
Icon(
modifier = Modifier.widthIn(max = 180.dp),
painter = painterResource(DesignSystemR.drawable.ic_logo_powered_by_proton),
tint = MaterialTheme.colorScheme.onSurfaceVariant,
contentDescription = null
)
}
Spacer(modifier = Modifier.height(Spacing.large))
Text(
modifier = Modifier.fillMaxWidth(),
text = appVersion,
color = MaterialTheme.colorScheme.onSurfaceVariant,
textAlign = TextAlign.Center
)
}
}