Skip to content

Commit

Permalink
Allow any file type when importing
Browse files Browse the repository at this point in the history
Adds "application/*" mimetype when importing and
add an error message if it fails. This will allow
both XML and OPML files to get imported.
  • Loading branch information
jocmp committed Jul 10, 2024
1 parent 400c108 commit 0a38a79
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.capyreader.app.common

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.Uri
import androidx.activity.result.contract.ActivityResultContract
import androidx.annotation.CallSuper

open class GetOPMLContent : ActivityResultContract<Unit, Uri?>() {
@CallSuper
override fun createIntent(context: Context, input: Unit): Intent {
return Intent(Intent.ACTION_GET_CONTENT)
.addCategory(Intent.CATEGORY_OPENABLE)
.setType("text/xml,text/x-opml,application/*")
.putExtra(Intent.EXTRA_MIME_TYPES, listOf("text/xml", "text/x-opml", "application/*").toTypedArray())
}

final override fun getSynchronousResult(
context: Context,
input: Unit
): SynchronousResult<Uri?>? = null

final override fun parseResult(resultCode: Int, intent: Intent?): Uri? {
return intent.takeIf { resultCode == Activity.RESULT_OK }?.data
}
}
16 changes: 12 additions & 4 deletions app/src/main/java/com/capyreader/app/transfers/OPMLImportWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.content.Context
import android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC
import android.net.Uri
import android.widget.Toast
import androidx.annotation.StringRes
import androidx.core.app.NotificationCompat
import androidx.work.Constraints
import androidx.work.CoroutineWorker
Expand All @@ -28,6 +29,8 @@ import kotlinx.coroutines.withContext
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import java.util.UUID
import java.util.concurrent.ExecutionException
import javax.xml.parsers.ParserConfigurationException
import kotlin.math.roundToInt


Expand All @@ -49,9 +52,14 @@ class OPMLImportWorker(
val opmlURI = Uri.parse(uriValue)

setForeground(createForegroundInfo())
import(opmlURI)

showCompleteToast()
try {
import(opmlURI)

showToast(R.string.opml_import_toast_complete)
} catch (e: Throwable) {
showToast(R.string.opml_import_toast_failed)
}

return Result.success()
}
Expand Down Expand Up @@ -128,10 +136,10 @@ class OPMLImportWorker(
notificationManager.createNotificationChannel(channel)
}

private suspend fun showCompleteToast() = withContext(Dispatchers.Main) {
private suspend fun showToast(@StringRes string: Int) = withContext(Dispatchers.Main) {
Toast.makeText(
applicationContext,
applicationContext.getString(R.string.opml_import_toast_complete),
applicationContext.getString(string),
Toast.LENGTH_SHORT
).show()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.platform.LocalContext
import com.capyreader.app.common.GetOPMLContent
import com.capyreader.app.transfers.OPMLExporter
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
Expand All @@ -20,13 +21,13 @@ fun SettingsScreen(
val coroutineScope = rememberCoroutineScope()

val picker = rememberLauncherForActivityResult(
ActivityResultContracts.GetContent()
GetOPMLContent()
) { uri ->
viewModel.startOPMLImport(uri = uri)
}

val importOPML = {
picker.launch("text/xml")
picker.launch(Unit)
}

val exportOPML = {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
<string name="opml_import_progress_content_text_start">Starting import</string>
<string name="settings_import_progress">Importing subscriptions %1$d of %2$d</string>
<string name="opml_import_toast_complete">Import complete</string>
<string name="opml_import_toast_failed">Import failed</string>
<string name="settings_section_display_appearance">Display &amp; Appearance</string>
<string name="theme_menu_label">Theme</string>
<string name="theme_menu_option_light">Light</string>
Expand Down

0 comments on commit 0a38a79

Please sign in to comment.