Skip to content

Commit

Permalink
Add export button to account settings (#54)
Browse files Browse the repository at this point in the history
jocmp authored Jan 24, 2024
1 parent 8a68745 commit 05dfc09
Showing 8 changed files with 100 additions and 4 deletions.
12 changes: 11 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -3,8 +3,8 @@
xmlns:tools="http://schemas.android.com/tools">

<application
android:allowBackup="true"
android:name=".MainApplication"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
@@ -23,7 +23,17 @@

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

</activity>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>

</manifest>
46 changes: 46 additions & 0 deletions app/src/main/java/com/jocmp/basilreader/OPMLExporter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.jocmp.basilreader

import android.content.Context
import android.content.Intent
import android.net.Uri
import androidx.core.content.FileProvider
import com.jocmp.basil.Account
import java.io.File
import java.nio.file.Files
import java.nio.file.StandardCopyOption

class OPMLExporter(
private val context: Context,
) {
fun export(account: Account) {
val exports = File(context.filesDir, "exports")
exports.mkdirs()
val export = File(exports, "${account.displayName}.xml")

val source = File(account.opmlFile.path).toPath()
val target = export.toPath()

val result = Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING)

return try {
val uri = context.fileURI(result.toFile())
val shareIntent = Intent(Intent.ACTION_SEND).apply {
type = "text/xml"
putExtra(Intent.EXTRA_STREAM, uri)
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}

context.startActivity(
Intent.createChooser(
shareIntent,
context.getString(R.string.opml_exporter_chooser_title, account.displayName)
)
)
} catch (e: IllegalArgumentException) {
// return
}
}
}

private fun Context.fileURI(file: File) =
FileProvider.getUriForFile(this, "${packageName}.fileprovider", file)
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
package com.jocmp.basilreader.ui.accounts

import android.content.Context
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext
import com.jocmp.basil.Account
import com.jocmp.basilreader.OPMLExporter
import org.koin.androidx.compose.koinViewModel

@Composable
fun AccountSettingsScreen(
viewModel: AccountSettingsViewModel = koinViewModel(),
goBack: () -> Unit,
) {
val context = LocalContext.current

AccountSettingsView(
defaultDisplayName = viewModel.displayName,
removeAccount = {
viewModel.removeAccount()
goBack()
},
submit = viewModel::submitName
submit = viewModel::submitName,
exportOPML = {
context.exportOPML(account = viewModel.account)
}
)
}
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@ fun AccountSettingsView(
defaultDisplayName: String,
removeAccount: () -> Unit,
submit: (displayName: String) -> Unit,
exportOPML: () -> Unit,
) {
val scope = rememberCoroutineScope()
val snackbarHostState = remember { SnackbarHostState() }
@@ -70,6 +71,11 @@ fun AccountSettingsView(
) {
Text(stringResource(R.string.account_settings_submit))
}
Button(
onClick = exportOPML
) {
Text("Export")
}
Button(onClick = { setRemoveDialogOpen(true) }) {
Text(stringResource(R.string.account_settings_delete_account_button))
}
@@ -109,6 +115,7 @@ fun AccountSettingsViewPreview() {
AccountSettingsView(
defaultDisplayName = "Feedbin",
removeAccount = {},
submit = {}
submit = {},
exportOPML = {}
)
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.jocmp.basilreader.ui.accounts

import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.neverEqualPolicy
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.jocmp.basil.Account
import com.jocmp.basil.AccountManager
import com.jocmp.basilreader.AppPreferences
import kotlinx.coroutines.flow.Flow
@@ -13,13 +16,20 @@ class AccountSettingsViewModel(
): ViewModel() {
private val args = AccountSettingsArgs(savedStateHandle)

private val account = accountManager.findByID(args.accountID)!!
private val _account = mutableStateOf(
accountManager.findByID(args.accountID)!!,
policy = neverEqualPolicy()
)

val account: Account
get() = _account.value

val displayName: String
get() = account.displayName

fun submitName(displayName: String) {
account.displayName = displayName
_account.value = account
}

fun removeAccount() {
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.jocmp.basilreader.ui.accounts

import android.content.Context
import com.jocmp.basil.Account
import com.jocmp.basilreader.OPMLExporter

fun Context.exportOPML(account: Account) {
OPMLExporter(this).export(account)
}
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -34,4 +34,5 @@
<string name="account_settings_delete_account_message">Are you sure you want to delete the account \"%1$s\"? This cannot be undone.</string>
<string name="account_settings_delete_account_submit">Delete</string>
<string name="account_settings_save_success_message">Settings saved</string>
<string name="opml_exporter_chooser_title">Export %1$s Feeds</string>
</resources>
4 changes: 4 additions & 0 deletions app/src/main/res/xml/file_paths.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<paths>
<files-path name="exports" path="exports/"/>
</paths>

0 comments on commit 05dfc09

Please sign in to comment.