-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from alexmercerind/feat/backup
feat: `SettingsActivity` history import/export
- Loading branch information
Showing
12 changed files
with
244 additions
and
47 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
32 changes: 32 additions & 0 deletions
32
app/src/main/java/com/alexmercerind/audire/repository/ImportExportRepository.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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.alexmercerind.audire.repository | ||
|
||
import android.app.Application | ||
import android.net.Uri | ||
import com.alexmercerind.audire.models.HistoryItem | ||
import com.google.gson.Gson | ||
import com.google.gson.reflect.TypeToken | ||
|
||
class ImportExportRepository(private val application: Application) { | ||
private val gson = Gson() | ||
|
||
suspend fun import(uri: Uri) { | ||
val input = application.contentResolver.openInputStream(uri) | ||
input?.use { | ||
val json = it.readBytes().toString(Charsets.UTF_16) | ||
val historyItems: List<HistoryItem> = gson.fromJson( | ||
json, object : TypeToken<List<HistoryItem>>() {}.type | ||
) | ||
for (historyItem in historyItems) { | ||
HistoryRepository(application).insert(historyItem) | ||
} | ||
} | ||
} | ||
|
||
suspend fun export(uri: Uri, historyItems: List<HistoryItem>) { | ||
val output = application.contentResolver.openOutputStream(uri) | ||
output?.use { | ||
val json = gson.toJson(historyItems) | ||
it.write(json.toByteArray(Charsets.UTF_16)) | ||
} | ||
} | ||
} |
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
23 changes: 18 additions & 5 deletions
23
app/src/main/java/com/alexmercerind/audire/ui/SettingsViewModel.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,17 +1,30 @@ | ||
package com.alexmercerind.audire.ui | ||
|
||
import android.app.Application | ||
import android.net.Uri | ||
import androidx.lifecycle.AndroidViewModel | ||
import com.alexmercerind.audire.repository.HistoryRepository | ||
import com.alexmercerind.audire.repository.ImportExportRepository | ||
import com.alexmercerind.audire.repository.SettingsRepository | ||
import kotlinx.coroutines.flow.first | ||
|
||
class SettingsViewModel(application: Application) : AndroidViewModel(application) { | ||
private val repository = SettingsRepository(application) | ||
private val historyRepository = HistoryRepository(application) | ||
private val settingsRepository = SettingsRepository(application) | ||
private val importExportRepository = ImportExportRepository(application) | ||
|
||
val theme = repository.theme | ||
val theme = settingsRepository.theme | ||
|
||
val systemColorScheme = repository.systemColorScheme | ||
val systemColorScheme = settingsRepository.systemColorScheme | ||
|
||
fun setTheme(value: String) = repository.setTheme(value) | ||
fun setTheme(value: String) = settingsRepository.setTheme(value) | ||
|
||
fun setSystemColorScheme(value: Boolean) = repository.setSystemColorScheme(value) | ||
fun setSystemColorScheme(value: Boolean) = settingsRepository.setSystemColorScheme(value) | ||
|
||
suspend fun import(uri: Uri) = importExportRepository.import(uri) | ||
|
||
suspend fun export(uri: Uri) = importExportRepository.export( | ||
uri, | ||
historyRepository.getAll().first() | ||
) | ||
} |
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 android:height="24dp" android:tint="#FFFFFF" | ||
android:viewportHeight="24" android:viewportWidth="24" | ||
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<path android:fillColor="@android:color/white" android:pathData="M19,9h-4V3H9v6H5l7,7 7,-7zM5,18v2h14v-2H5z"/> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<vector android:height="24dp" android:tint="#FFFFFF" | ||
android:viewportHeight="24" android:viewportWidth="24" | ||
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<path android:fillColor="@android:color/white" android:pathData="M9,16h6v-6h4l-7,-7 -7,7h4zM5,18h14v2L5,20z"/> | ||
</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
24 changes: 0 additions & 24 deletions
24
app/src/main/res/menu-v26/history_material_toolbar_menu.xml
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,14 +1,16 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<menu xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:android="http://schemas.android.com/apk/res/android"> | ||
|
||
<menu xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto"> | ||
<item | ||
android:id="@+id/settings" | ||
android:icon="@drawable/baseline_settings_24" | ||
android:title="@string/settings" | ||
app:iconTint="?attr/colorOnSurfaceVariant" | ||
app:showAsAction="never" /> | ||
<item | ||
android:id="@+id/about" | ||
android:icon="@drawable/baseline_info_24" | ||
android:title="@string/about" /> | ||
</menu> | ||
android:title="@string/about" | ||
app:iconTint="?attr/colorOnSurfaceVariant" | ||
app:showAsAction="never" /> | ||
</menu> |
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