Skip to content

Commit

Permalink
fix: app resets on orientation change
Browse files Browse the repository at this point in the history
closes #409
closes #410
closes #411
closes #412
  • Loading branch information
Bnyro committed Oct 8, 2024
1 parent eec6acc commit 682301a
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.Saver
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalConfiguration
Expand All @@ -28,6 +31,11 @@ import com.bnyro.contacts.presentation.screens.contacts.model.ContactsModel
import com.bnyro.contacts.presentation.screens.settings.model.ThemeModel
import com.bnyro.contacts.presentation.screens.sms.model.SmsModel

val RouteSaver = Saver<MutableState<HomeRoutes>, Int>(
save = { HomeRoutes.all.indexOfFirst { item -> item.route == it.value } },
restore = { mutableStateOf(HomeRoutes.all[it].route) }
)

@Composable
fun HomeNavContainer(
initialTab: HomeRoutes,
Expand All @@ -38,7 +46,7 @@ fun HomeNavContainer(
) {
val navController = rememberNavController()

var selectedRoute by remember {
var selectedRoute by rememberSaveable(saver = RouteSaver) {
mutableStateOf(initialTab)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ sealed class NavRoutes {
}

@Serializable
sealed class HomeRoutes() {
sealed class HomeRoutes {
@Serializable
data class Phone(
val phoneNumber: String? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import com.bnyro.contacts.R

Expand All @@ -14,7 +14,7 @@ fun TopBarMoreMenu(
options: List<String> = emptyList(),
onOptionClick: (Int) -> Unit = {}
) {
var expandedOptions by remember {
var expandedOptions by rememberSaveable {
mutableStateOf(false)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand Down Expand Up @@ -76,22 +77,22 @@ import com.bnyro.contacts.util.RingtonePickContract
@Composable
fun SingleContactScreen(contact: ContactData, viewModel: ContactsModel, onClose: () -> Unit) {
val context = LocalContext.current
var showDelete by remember {
var showDelete by rememberSaveable {
mutableStateOf(false)
}
var showEditor by remember {
var showEditor by rememberSaveable {
mutableStateOf(false)
}
var showZoomablePhoto by remember {
var showZoomablePhoto by rememberSaveable {
mutableStateOf(false)
}
var showShortcutDialog by remember {
var showShortcutDialog by rememberSaveable {
mutableStateOf(false)
}
var showShareDialog by remember {
var showShareDialog by rememberSaveable {
mutableStateOf(false)
}
var isFavorite by remember {
var isFavorite by rememberSaveable {
mutableStateOf(contact.favorite)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand Down Expand Up @@ -81,27 +82,27 @@ fun ContactsPage(
mutableStateOf(viewModel.initialContactData)
}

var showEditorScreen by remember {
var showEditorScreen by rememberSaveable {
mutableStateOf(false)
}

var showDelete by remember {
var showDelete by rememberSaveable {
mutableStateOf(false)
}

var filterOptions by remember {
mutableStateOf(FilterOptions.default())
}

var showSearch by remember {
var showSearch by rememberSaveable {
mutableStateOf(false)
}

var showFilterDialog by remember {
var showFilterDialog by rememberSaveable {
mutableStateOf(false)
}

var showImportSimDialog by remember {
var showImportSimDialog by rememberSaveable {
mutableStateOf(false)
}

Expand Down Expand Up @@ -143,7 +144,8 @@ fun ContactsPage(
true -> {
TopAppBar(
title = {
var expanded by remember { mutableStateOf(false) }
var expanded by rememberSaveable { mutableStateOf(false) }

Row(
Modifier
.padding(8.dp)
Expand All @@ -167,7 +169,7 @@ fun ContactsPage(
expanded = expanded,
onDismissRequest = { expanded = false }
) {
ContactsSource.values().forEachIndexed { index, source ->
ContactsSource.entries.forEachIndexed { index, source ->
DropdownMenuItem(
text = { Text(stringResource(id = source.stringRes)) },
onClick = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand Down Expand Up @@ -54,7 +55,7 @@ fun ContactItem(
val viewModel: ContactsModel = viewModel(factory = ContactsModel.Factory)
val themeModel: ThemeModel = viewModel()

var showContactScreen by remember {
var showContactScreen by rememberSaveable {
mutableStateOf(false)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand Down Expand Up @@ -77,14 +78,14 @@ fun SmsListScreen(
onClickMessage: (address: String, contactData: ContactData?) -> Unit
) {
val themeModel: ThemeModel = viewModel()
var showNumberPicker by remember {
var showNumberPicker by rememberSaveable {
mutableStateOf(false)
}
var showSearch by remember {
var showSearch by rememberSaveable {
mutableStateOf(false)
}

var selectedThread by remember {
var selectedThread by rememberSaveable {
mutableStateOf<SmsThread?>(null)
}
Scaffold(
Expand Down Expand Up @@ -114,7 +115,8 @@ fun SmsListScreen(
onNavigate.invoke(NavRoutes.About)
}
}
})
}
)
}
)
},
Expand Down

0 comments on commit 682301a

Please sign in to comment.