Skip to content

Commit 3e98a9b

Browse files
committed
migrate to accompanist navi for bottom sheets
1 parent 1cabb48 commit 3e98a9b

File tree

29 files changed

+422
-85
lines changed

29 files changed

+422
-85
lines changed

app/build.gradle

+8-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ android {
66
javaCompileOptions {
77
annotationProcessorOptions {
88
arguments += [
9-
"room.schemaLocation":"$projectDir/schemas".toString(),
10-
"room.incremental":"true"]
9+
"room.schemaLocation": "$projectDir/schemas".toString(),
10+
"room.incremental" : "true"]
1111
}
1212
}
1313
}
@@ -62,11 +62,16 @@ dependencies {
6262

6363
//search
6464
implementation project(path: ':search:searchui')
65+
implementation project(path: ':search:searchdata')
6566

6667
//search result
6768
implementation project(path: ':searchresult:searchresultui')
6869
implementation project(path: ':searchresult:searchresultdestination')
6970

71+
//search filter
72+
implementation project(path: ':search:searchfilter:searchfilterui')
73+
implementation project(path: ':search:searchfilter:searchfilterdestination')
74+
7075
//favorites
7176
implementation project(path: ':favoritebook:favoritebookui')
7277

@@ -114,6 +119,7 @@ dependencies {
114119
implementation "com.github.FunkyMuse.KAHelpers:coroutines:$KAHelpers"
115120
implementation "com.github.FunkyMuse:Composed:$composed"
116121
implementation "androidx.core:core-ktx:$coreKTX"
122+
implementation "androidx.compose.runtime:runtime-livedata:$compose_version"
117123

118124
//crashy
119125
implementation "com.github.FunkyMuse:Crashy:$crashy"

app/src/main/java/com/funkymuse/aurora/MainActivity.kt

+27-6
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ import androidx.compose.animation.core.tween
99
import androidx.compose.animation.fadeIn
1010
import androidx.compose.animation.fadeOut
1111
import androidx.compose.foundation.layout.padding
12-
import androidx.compose.material.MaterialTheme
13-
import androidx.compose.material.Scaffold
14-
import androidx.compose.material.Surface
12+
import androidx.compose.material.*
1513
import androidx.compose.runtime.*
1614
import androidx.compose.ui.Modifier
1715
import androidx.core.view.WindowCompat
@@ -43,6 +41,8 @@ import com.funkymuse.style.theme.AuroraTheme
4341
import com.google.accompanist.insets.ProvideWindowInsets
4442
import com.google.accompanist.navigation.animation.AnimatedNavHost
4543
import com.google.accompanist.navigation.animation.rememberAnimatedNavController
44+
import com.google.accompanist.navigation.material.BottomSheetNavigator
45+
import com.google.accompanist.navigation.material.ExperimentalMaterialNavigationApi
4646
import com.google.accompanist.navigation.material.ModalBottomSheetLayout
4747
import com.google.accompanist.navigation.material.rememberBottomSheetNavigator
4848
import dagger.hilt.android.AndroidEntryPoint
@@ -89,22 +89,31 @@ class MainActivity : ComponentActivity(), AssistedHiltInjectables {
8989

9090
@OptIn(
9191
ExperimentalAnimationApi::class,
92-
com.google.accompanist.navigation.material.ExperimentalMaterialNavigationApi::class
92+
ExperimentalMaterialNavigationApi::class,
93+
ExperimentalMaterialApi::class
9394
)
9495
@Composable
9596
fun AuroraScaffold(auroraNavigator: AuroraNavigator) {
96-
val bottomSheetNavigator = rememberBottomSheetNavigator()
97+
val sheetState = rememberModalBottomSheetState(ModalBottomSheetValue.Hidden, SwipeableDefaults.AnimationSpec)
98+
val bottomSheetNavigator = rememberBottomSheetNavigatorFix(sheetState)
9799
val navController = rememberAnimatedNavController()
98100
navController.navigatorProvider += bottomSheetNavigator
99101

100102
LaunchedEffect(navController) {
101103
auroraNavigator.destinations.collect {
102104
when (val event = it) {
103-
is NavigatorEvent.NavigateUp -> navController.navigateUp()
105+
is NavigatorEvent.NavigateUp -> {
106+
sheetState.hide()
107+
navController.navigateUp()
108+
}
104109
is NavigatorEvent.Directions -> navController.navigate(
105110
event.destination,
106111
event.builder
107112
)
113+
NavigatorEvent.PopBackStack -> {
114+
sheetState.hide()
115+
navController.popBackStack()
116+
}
108117
}
109118
}
110119
}
@@ -174,3 +183,15 @@ fun ResetUserDonationsInfo() {
174183
}
175184

176185

186+
@OptIn(
187+
ExperimentalMaterialNavigationApi::class,
188+
ExperimentalMaterialApi::class
189+
)
190+
@Composable
191+
fun rememberBottomSheetNavigatorFix(
192+
sheetState: ModalBottomSheetState
193+
): BottomSheetNavigator {
194+
return remember(sheetState) {
195+
BottomSheetNavigator(sheetState = sheetState)
196+
}
197+
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.funkymuse.aurora.navigation
22

3+
import android.util.Log
34
import androidx.compose.animation.ExperimentalAnimationApi
45
import androidx.compose.material.ExperimentalMaterialApi
56
import androidx.compose.runtime.Composable
7+
import androidx.compose.runtime.getValue
8+
import androidx.compose.runtime.livedata.observeAsState
69
import androidx.navigation.NavGraphBuilder
710
import androidx.navigation.NavHostController
811
import com.google.accompanist.navigation.animation.composable
@@ -15,23 +18,27 @@ import com.funkymuse.aurora.settingsui.Settings
1518
/**
1619
* Created by funkymuse on 8/15/21 to long live and prosper !
1720
*/
21+
import com.funkymuse.searchfilterdestination.SearchFilterDestination
1822

1923

2024
@OptIn(ExperimentalMaterialApi::class)
21-
private val destinationsBottomNav: Map<BottomNavigationEntry, @Composable () -> Unit> = mapOf(
22-
SearchRoute to { Search() },
23-
FavoritesRoute to { Favorites() },
24-
LatestBooksRoute to { LatestBooks() },
25-
SettingsRoute to { Settings() },
26-
)
25+
private val destinationsBottomNav: Map<BottomNavigationEntry, @Composable (NavHostController) -> Unit> =
26+
mapOf(
27+
SearchRoute to {
28+
Search()
29+
},
30+
FavoritesRoute to { Favorites() },
31+
LatestBooksRoute to { LatestBooks() },
32+
SettingsRoute to { Settings() },
33+
)
2734

2835

2936
@OptIn(ExperimentalAnimationApi::class)
3037
fun NavGraphBuilder.addBottomNavigationDestinations(navController: NavHostController) {
3138
destinationsBottomNav.forEach { entry ->
3239
val destination = entry.key
3340
composable(destination.route) {
34-
entry.value()
41+
entry.value(navController)
3542
}
3643
}
3744
}

app/src/main/java/com/funkymuse/aurora/navigation/BottomSheetDestinations.kt

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@ package com.funkymuse.aurora.navigation
22

33

44
import androidx.compose.runtime.Composable
5+
import androidx.hilt.navigation.compose.hiltViewModel
56
import androidx.navigation.NavBackStackEntry
67
import androidx.navigation.NavGraphBuilder
78
import androidx.navigation.NavHostController
89
import com.funkymuse.aurora.navigator.NavigationDestination
10+
import com.funkymuse.composed.navigation.rememberParentEntry
11+
import com.funkymuse.searchfilterdestination.SearchFilterDestination
12+
import com.funkymuse.searchfilterui.SearchFilterUI
913
import com.google.accompanist.navigation.material.ExperimentalMaterialNavigationApi
1014
import com.google.accompanist.navigation.material.bottomSheet
1115

1216

1317
private val bottomSheetDestinations: Map<NavigationDestination, @Composable (NavBackStackEntry, NavHostController) -> Unit>
1418
get() = mapOf(
15-
19+
SearchFilterDestination to { _, controller -> SearchFilterUI(hiltViewModel(controller.rememberParentEntry())) }
1620
)
1721

1822
@OptIn(ExperimentalMaterialNavigationApi::class)

build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ buildscript {
1010
retrofit = "2.9.0"
1111
accompanist = "0.24.1-alpha"
1212
lifecycle = '2.4.0'
13-
composed = '0.0.16'
13+
composed = '0.0.17'
1414
paging_compose = '1.0.0-alpha14'
1515
paging_runtime = '3.1.0'
1616
navigation = '2.5.0-alpha01'
@@ -25,7 +25,7 @@ buildscript {
2525
hilt_work = "1.0.0"
2626

2727
//compilation
28-
compileVersion = 31
28+
compileVersion = 32
2929
minVersion = 24
3030

3131
//updatable

navigator/src/main/java/com/funkymuse/aurora/navigator/AuroraNavigator.kt

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import kotlinx.coroutines.flow.Flow
99
interface AuroraNavigator {
1010

1111
fun navigateUp(): Boolean
12+
fun popBackStack()
1213
fun navigate(route: String, builder: NavOptionsBuilder.() -> Unit = { launchSingleTop = true }): Boolean
1314
val destinations: Flow<NavigatorEvent>
1415
}

navigator/src/main/java/com/funkymuse/aurora/navigator/AuroraNavigatorImpl.kt

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ internal class AuroraNavigatorImpl @Inject constructor() : AuroraNavigator {
1616
override val destinations = navigationEvents.receiveAsFlow()
1717

1818
override fun navigateUp(): Boolean = navigationEvents.trySend(NavigatorEvent.NavigateUp).isSuccess
19+
override fun popBackStack() {
20+
navigationEvents.trySend(NavigatorEvent.PopBackStack)
21+
}
22+
1923
override fun navigate(route: String, builder: NavOptionsBuilder.() -> Unit): Boolean = navigationEvents.trySend(NavigatorEvent.Directions(route, builder)).isSuccess
2024

2125
}

navigator/src/main/java/com/funkymuse/aurora/navigator/NavigatorEvent.kt

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import androidx.navigation.NavOptionsBuilder
77
*/
88
sealed class NavigatorEvent {
99
object NavigateUp : NavigatorEvent()
10-
class Directions(val destination: String,
11-
val builder: NavOptionsBuilder.() -> Unit) : NavigatorEvent()
10+
class Directions(
11+
val destination: String,
12+
val builder: NavOptionsBuilder.() -> Unit
13+
) : NavigatorEvent()
14+
15+
object PopBackStack : NavigatorEvent()
1216
}

search/searchdata/build.gradle

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ plugins {
33
}
44

55
applyHiltDeps(this)
6+
applyComposeUIDeps(this)
67
dependencies {
78
implementation project(path: ':resources:strings')
8-
99
implementation project(path: ':serverconstants')
10+
implementation project(path: ':search:searchfilter:searchfilterdestination')
11+
implementation project(path: ':navigator')
1012

1113
}

search/searchdata/src/main/java/com/funkymuse/aurora/searchdata/SearchViewModel.kt

+45-1
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,61 @@
11
package com.funkymuse.aurora.searchdata
22

33
import android.app.Application
4+
import android.util.Log
5+
import androidx.compose.runtime.getValue
6+
import androidx.compose.runtime.mutableStateOf
7+
import androidx.compose.runtime.setValue
48
import androidx.lifecycle.AndroidViewModel
9+
import androidx.lifecycle.SavedStateHandle
10+
import com.funkymuse.searchfilterdestination.SearchFilterDestination
11+
import com.funkymuse.searchfilterdestination.SearchFilterDestination.SEARCH_IN_FIELDS_CHECKED_POSITION
12+
import com.funkymuse.searchfilterdestination.SearchFilterDestination.SEARCH_WITH_MASK_WORD
513
import dagger.hilt.android.lifecycle.HiltViewModel
614
import javax.inject.Inject
715

816
/**
917
* Created by funkymuse, date 4/14/21
1018
*/
1119
@HiltViewModel
12-
class SearchViewModel @Inject constructor(application: Application) :
20+
class SearchViewModel @Inject constructor(
21+
private val savedStateHandle: SavedStateHandle,
22+
application: Application
23+
) :
1324
AndroidViewModel(application) {
1425

26+
var argumentSearchInFieldsCheckedPosition =
27+
SearchFilterDestination.searchInFieldsCheckedPosition(
28+
savedStateHandle
29+
)
30+
get() = savedStateHandle[SEARCH_IN_FIELDS_CHECKED_POSITION]
31+
?: SearchFilterDestination.searchInFieldsCheckedPosition(
32+
savedStateHandle
33+
)
34+
set(value) {
35+
searchInFieldsCheckedPosition = value
36+
savedStateHandle[SEARCH_IN_FIELDS_CHECKED_POSITION] = value
37+
field = value
38+
}
39+
40+
var argumentSearchWithMaskWord: Boolean = SearchFilterDestination.searchWithMaskWord(
41+
savedStateHandle
42+
)
43+
get() = savedStateHandle[SEARCH_WITH_MASK_WORD]
44+
?: SearchFilterDestination.searchWithMaskWord(
45+
savedStateHandle
46+
)
47+
set(value) {
48+
searchWithMaskWord = value
49+
savedStateHandle[SEARCH_WITH_MASK_WORD] = searchWithMaskWord
50+
field = value
51+
}
52+
53+
var searchInFieldsCheckedPosition by mutableStateOf(
54+
argumentSearchInFieldsCheckedPosition
55+
)
56+
var searchWithMaskWord by mutableStateOf(
57+
argumentSearchWithMaskWord
58+
)
1559

1660
val searchInFieldEntries
1761
get() = listOf(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
applyComposeUIDeps(this)
2+
3+
dependencies {
4+
implementation project(path: ':navigator')
5+
implementation "com.github.FunkyMuse.Composed:navigation:$composed"
6+
}

search/searchfilter/searchfilterdestination/consumer-rules.pro

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.funkymuse.searchfilterdestination
2+
3+
import androidx.test.platform.app.InstrumentationRegistry
4+
import androidx.test.ext.junit.runners.AndroidJUnit4
5+
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
9+
import org.junit.Assert.*
10+
11+
/**
12+
* Instrumented test, which will execute on an Android device.
13+
*
14+
* See [testing documentation](http://d.android.com/tools/testing).
15+
*/
16+
@RunWith(AndroidJUnit4::class)
17+
class ExampleInstrumentedTest {
18+
@Test
19+
fun useAppContext() {
20+
// Context of the app under test.
21+
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22+
assertEquals("com.funkymuse.searchfilterdestination.test", appContext.packageName)
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.funkymuse.searchfilterdestination">
4+
5+
</manifest>

0 commit comments

Comments
 (0)