-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
📁 Implement Backup and Restore (#31)
* Added backup description to strings.xml. * Updated export functionality to create the "Grit" directory if it doesn't exist. * Added a new backup entry in the settings page. * Updated settings to include backup page. * Added new icon. * Updated navigation. Signed-off-by: shub39 <cptnshubham39@gmail.com>
- Loading branch information
Showing
7 changed files
with
220 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
app/src/main/java/com/shub39/grit/core/presentation/settings/Backup.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,178 @@ | ||
package com.shub39.grit.core.presentation.settings | ||
|
||
import android.net.Uri | ||
import androidx.activity.compose.rememberLauncherForActivityResult | ||
import androidx.activity.result.contract.ActivityResultContracts | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.size | ||
import androidx.compose.foundation.layout.widthIn | ||
import androidx.compose.foundation.lazy.LazyColumn | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.filled.CheckCircle | ||
import androidx.compose.material.icons.filled.Warning | ||
import androidx.compose.material3.CircularProgressIndicator | ||
import androidx.compose.material3.ExperimentalMaterial3Api | ||
import androidx.compose.material3.Icon | ||
import androidx.compose.material3.ListItem | ||
import androidx.compose.material3.Text | ||
import androidx.compose.material3.TextButton | ||
import androidx.compose.material3.TopAppBar | ||
import androidx.compose.runtime.Composable | ||
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.Modifier | ||
import androidx.compose.ui.res.painterResource | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.unit.dp | ||
import com.shub39.grit.R | ||
import com.shub39.grit.core.domain.backup.ExportRepo | ||
import com.shub39.grit.core.domain.backup.ExportState | ||
import com.shub39.grit.core.domain.backup.RestoreRepo | ||
import com.shub39.grit.core.domain.backup.RestoreResult | ||
import com.shub39.grit.core.domain.backup.RestoreState | ||
import com.shub39.grit.core.presentation.components.BetterIconButton | ||
import com.shub39.grit.core.presentation.components.PageFill | ||
import kotlinx.coroutines.launch | ||
import org.koin.compose.koinInject | ||
|
||
@OptIn(ExperimentalMaterial3Api::class) | ||
@Composable | ||
fun Backup( | ||
exportRepo: ExportRepo = koinInject(), | ||
restoreRepo: RestoreRepo = koinInject() | ||
) = PageFill { | ||
val coroutineScope = rememberCoroutineScope() | ||
|
||
var exportState by remember { mutableStateOf(ExportState.IDLE) } | ||
var restoreState by remember { mutableStateOf(RestoreState.IDLE) } | ||
|
||
var uri by remember { mutableStateOf<Uri?>(null) } | ||
val launcher = rememberLauncherForActivityResult( | ||
contract = ActivityResultContracts.OpenDocument() | ||
) { uri = it } | ||
|
||
Column( | ||
modifier = Modifier | ||
.widthIn(max = 700.dp) | ||
.fillMaxSize() | ||
) { | ||
TopAppBar( | ||
title = { | ||
Text( | ||
text = stringResource(R.string.backup) | ||
) | ||
} | ||
) | ||
|
||
LazyColumn( | ||
modifier = Modifier.fillMaxWidth() | ||
) { | ||
item { | ||
ListItem( | ||
headlineContent = { | ||
Text( | ||
text = stringResource(R.string.export) | ||
) | ||
}, | ||
supportingContent = { | ||
Text( | ||
text = stringResource(R.string.export_desc) | ||
) | ||
}, | ||
trailingContent = { | ||
BetterIconButton( | ||
onClick = { | ||
if (exportState == ExportState.IDLE) { | ||
coroutineScope.launch { | ||
exportState = ExportState.EXPORTING | ||
|
||
exportRepo.exportToJson() | ||
|
||
exportState = ExportState.EXPORTED | ||
} | ||
} | ||
} | ||
) { | ||
when (exportState) { | ||
ExportState.IDLE -> Icon( | ||
painter = painterResource(R.drawable.round_play_arrow_24), | ||
contentDescription = "Start" | ||
) | ||
ExportState.EXPORTING -> CircularProgressIndicator(modifier = Modifier.size(24.dp)) | ||
ExportState.EXPORTED -> Icon( | ||
imageVector = Icons.Default.CheckCircle, | ||
contentDescription = "Done" | ||
) | ||
} | ||
} | ||
} | ||
) | ||
} | ||
|
||
item { | ||
ListItem( | ||
headlineContent = { | ||
Text( | ||
text = stringResource(R.string.restore) | ||
) | ||
}, | ||
supportingContent = { | ||
Text( | ||
text = stringResource(R.string.restore_desc) | ||
) | ||
}, | ||
trailingContent = { | ||
if (uri == null) { | ||
TextButton( | ||
onClick = { launcher.launch(arrayOf("application/json")) } | ||
) { | ||
Text( | ||
text = stringResource(R.string.choose_file) | ||
) | ||
} | ||
} else { | ||
BetterIconButton( | ||
onClick = { | ||
if (restoreState == RestoreState.IDLE) { | ||
coroutineScope.launch { | ||
restoreState = RestoreState.RESTORING | ||
|
||
val result = restoreRepo.restoreSongs(uri!!) | ||
|
||
restoreState = if (result == RestoreResult.Success) { | ||
RestoreState.RESTORED | ||
} else { | ||
RestoreState.FAILURE | ||
} | ||
} | ||
} | ||
} | ||
) { | ||
when (restoreState) { | ||
RestoreState.IDLE -> Icon( | ||
painter = painterResource(R.drawable.round_play_arrow_24), | ||
contentDescription = "Start" | ||
) | ||
RestoreState.RESTORING -> CircularProgressIndicator(modifier = Modifier.size(24.dp)) | ||
RestoreState.RESTORED -> Icon( | ||
imageVector = Icons.Default.CheckCircle, | ||
contentDescription = "Done" | ||
) | ||
RestoreState.FAILURE -> Icon( | ||
imageVector = Icons.Default.Warning, | ||
contentDescription = "Fail" | ||
) | ||
} | ||
} | ||
} | ||
} | ||
) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#000000" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp"> | ||
|
||
<path android:fillColor="@android:color/white" android:pathData="M8,6.82v10.36c0,0.79 0.87,1.27 1.54,0.84l8.14,-5.18c0.62,-0.39 0.62,-1.29 0,-1.69L9.54,5.98C8.87,5.55 8,6.03 8,6.82z"/> | ||
|
||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters