Skip to content

Commit aecd810

Browse files
handle add task when rotated
1 parent 456acec commit aecd810

File tree

6 files changed

+41
-15
lines changed

6 files changed

+41
-15
lines changed

app/build.gradle

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,6 @@ dependencies {
8080
kapt "com.google.dagger:hilt-compiler:$hilt_version"
8181
implementation "androidx.hilt:hilt-navigation-compose:$hilt_navigationcompose_version"
8282

83-
// compose navigation
84-
implementation "androidx.navigation:navigation-compose:$compose_navigation_version"
85-
8683
// accompanist navigation animation
8784
implementation "com.google.accompanist:accompanist-navigation-animation:$navigation_animation_version"
8885

app/src/main/java/ir/codroid/taskino/navigation/destination/ListComposable.kt

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
package ir.codroid.taskino.navigation.destination
22

33
import androidx.compose.animation.ExperimentalAnimationApi
4+
import androidx.compose.animation.core.tween
5+
import androidx.compose.animation.slideInHorizontally
6+
import androidx.compose.animation.slideOutHorizontally
47
import androidx.compose.runtime.LaunchedEffect
58
import androidx.compose.runtime.getValue
9+
import androidx.compose.runtime.mutableStateOf
10+
import androidx.compose.runtime.saveable.rememberSaveable
11+
import androidx.compose.runtime.setValue
612
import androidx.navigation.NavGraphBuilder
713
import androidx.navigation.NavType
814
import com.google.accompanist.navigation.animation.composable
915
import androidx.navigation.navArgument
1016
import ir.codroid.taskino.ui.screen.list.ListScreen
1117
import ir.codroid.taskino.ui.viewmodel.SharedViewModel
18+
import ir.codroid.taskino.util.Action
1219
import ir.codroid.taskino.util.Constants.LIST_ARGUMENT_KEY
1320
import ir.codroid.taskino.util.Constants.LIST_SCREEN
1421
import ir.codroid.taskino.util.toAction
@@ -20,18 +27,39 @@ fun NavGraphBuilder.listComposable(
2027
) {
2128
composable(
2229
route = LIST_SCREEN,
30+
enterTransition = {
31+
slideInHorizontally(
32+
initialOffsetX = { fullWidth -> fullWidth },
33+
animationSpec = tween(
34+
durationMillis = 700
35+
)
36+
)
37+
},
38+
exitTransition = {
39+
slideOutHorizontally(
40+
targetOffsetX = { fullWidth -> fullWidth },
41+
animationSpec = tween(
42+
durationMillis = 700
43+
)
44+
)
45+
},
2346
arguments = listOf(navArgument(LIST_ARGUMENT_KEY) {
2447
type = NavType.StringType
2548
})
2649
) { navBackStackEntry ->
2750
val action = navBackStackEntry.arguments?.getString(LIST_ARGUMENT_KEY).toAction()
28-
LaunchedEffect(key1 = action){
29-
viewModel.action.value = action
51+
var myAction by rememberSaveable() { mutableStateOf(Action.NO_ACTION) }
52+
LaunchedEffect(key1 = myAction) {
53+
if (myAction != action) {
54+
myAction = action
55+
viewModel.action.value = action
56+
}
3057
}
3158
val dataBaseAction by viewModel.action
3259
ListScreen(
3360
action = dataBaseAction,
3461
viewModel = viewModel,
35-
navigateToTaskScreen = navigateToTaskScreen)
62+
navigateToTaskScreen = navigateToTaskScreen
63+
)
3664
}
3765
}

app/src/main/java/ir/codroid/taskino/navigation/destination/TaskComposable.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ fun NavGraphBuilder.taskComposable(
2525
slideInHorizontally(
2626
initialOffsetX = { fullWidth -> -fullWidth },
2727
animationSpec = tween(
28-
durationMillis = 300
28+
durationMillis = 700
2929
)
3030
)
3131
},
3232
exitTransition = {
3333
slideOutHorizontally(
3434
targetOffsetX = { fullWidth -> -fullWidth },
3535
animationSpec = tween(
36-
durationMillis = 300
36+
durationMillis = 700
3737
)
3838
)
3939
},

app/src/main/java/ir/codroid/taskino/ui/screen/list/ListScreen.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ fun ListScreen(
3333
viewModel: SharedViewModel,
3434
navigateToTaskScreen: (Int) -> Unit
3535
) {
36-
LaunchedEffect(key1 = true) {
37-
viewModel.getAllTasks()
38-
viewModel.readSortSate()
39-
}
4036
LaunchedEffect(key1 = action){
4137
viewModel.handleDatabaseAction(action = action)
4238
}

app/src/main/java/ir/codroid/taskino/ui/viewmodel/SharedViewModel.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ir.codroid.taskino.ui.viewmodel
22

3+
import androidx.compose.runtime.LaunchedEffect
34
import androidx.compose.runtime.MutableState
45
import androidx.compose.runtime.mutableStateOf
56
import androidx.lifecycle.ViewModel
@@ -73,7 +74,12 @@ class SharedViewModel @Inject constructor(
7374
val action: MutableState<Action> = mutableStateOf(Action.NO_ACTION)
7475

7576

76-
fun getAllTasks() {
77+
init {
78+
getAllTasks()
79+
readSortSate()
80+
81+
}
82+
private fun getAllTasks() {
7783
_allTasks.value = RequestState.Loading
7884
try {
7985

@@ -120,7 +126,7 @@ class SharedViewModel @Inject constructor(
120126
}
121127
}
122128

123-
fun readSortSate() {
129+
private fun readSortSate() {
124130
viewModelScope.launch(Dispatchers.IO) {
125131
dataStoreRepository.readSortState
126132
.map { priorityName ->

build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ buildscript {
55
datastore_version = '1.0.0'
66
hilt_version = '2.44'
77
hilt_navigationcompose_version = '1.0.0'
8-
compose_navigation_version = '2.5.3'
98
lottie_version = '5.2.0'
109
navigation_animation_version = '0.31.5-beta'
1110
}

0 commit comments

Comments
 (0)