generated from JetBrains/compose-multiplatform-template
-
-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for importing and exporting OPML (#121)
* Add platform specific file managers * Add outlined button component * Add query to get number of feeds * Check if app has feeds when settings screen is initialised * Add xmlutil library for xml serialization * Implement `FeedsOpml` * Add support for passing title when adding feed in the repository * Add function to add OPML feeds in `RssRepository` * Add function to fetch all feeds in blocking manner * Capture exceptions when encoding and decoding feeds opml * Add OPML manager for managing OPML import and export * Make border nullable in `OutlineButton` component * Add OPML section to settings screen * Fix feed fetching stuck in a loop when fetching link from the HTML * Ignore redirects if the feed link didn't change from original * Remove custom network timeouts The custom timeouts are a bit long when importing hundreds of feeds, so removed those and using default values * Move `addOpmlFeeds` function from repository to OPML manager There is no need for our manager to know about OPML feeds. Since it's just logic of looping over OPML feeds, moved it to the manager. * Add space between progress text and progress value * Launch iOS document picker from main thread * Reverse OPML feeds list when importing Since the app orders based on when the feed is created, we are reversing the OPML list in order to try and preserve the order as much as we can. It might still get affected, because we are doing parallel processing (since we are doing joinAll it should be fine, but not entirely sure).
- Loading branch information
1 parent
86ac5d3
commit 2ae8f8d
Showing
29 changed files
with
1,029 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
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
120 changes: 120 additions & 0 deletions
120
shared/src/androidMain/kotlin/dev/sasikanth/rss/reader/filemanager/AndroidFileManager.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,120 @@ | ||
/* | ||
* Copyright 2023 Sasikanth Miriyampalli | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dev.sasikanth.rss.reader.filemanager | ||
|
||
import android.app.Activity | ||
import android.app.Application | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.os.Bundle | ||
import androidx.activity.ComponentActivity | ||
import androidx.activity.result.ActivityResultLauncher | ||
import androidx.activity.result.contract.ActivityResultContracts | ||
import dev.sasikanth.rss.reader.di.scopes.AppScope | ||
import kotlinx.coroutines.channels.Channel | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.flow.receiveAsFlow | ||
import me.tatarka.inject.annotations.Inject | ||
|
||
@Inject | ||
@AppScope | ||
class AndroidFileManager(context: Context) : FileManager { | ||
|
||
private val application = context as Application | ||
private val result = Channel<String?>() | ||
|
||
private lateinit var createDocumentLauncher: ActivityResultLauncher<String> | ||
private lateinit var openDocumentLauncher: ActivityResultLauncher<Array<String>> | ||
|
||
private var content: String? = null | ||
|
||
override suspend fun save(name: String, content: String) { | ||
this.content = content | ||
|
||
if (!this.content.isNullOrBlank()) { | ||
createDocumentLauncher.launch(name) | ||
} | ||
} | ||
|
||
override suspend fun read(): String? { | ||
openDocumentLauncher.launch(arrayOf("application/xml", "text/xml", "text/x-opml")) | ||
return result.receiveAsFlow().first() | ||
} | ||
|
||
internal fun registerActivityWatcher() { | ||
val callback = | ||
object : ActivityLifecycleCallbacksAdapter() { | ||
val launcherIntent = | ||
Intent(Intent.ACTION_MAIN, null).apply { addCategory(Intent.CATEGORY_LAUNCHER) } | ||
val appList = application.packageManager.queryIntentActivities(launcherIntent, 0) | ||
|
||
override fun onActivityCreated(activity: Activity, bundle: Bundle?) { | ||
if ( | ||
activity is ComponentActivity && | ||
appList.any { it.activityInfo.name == activity::class.qualifiedName } | ||
) { | ||
registerDocumentCreateActivityResult(activity) | ||
registerDocumentOpenActivityResult(activity) | ||
} | ||
} | ||
} | ||
application.registerActivityLifecycleCallbacks(callback) | ||
} | ||
|
||
private fun registerDocumentCreateActivityResult(activity: ComponentActivity) { | ||
createDocumentLauncher = | ||
activity.registerForActivityResult( | ||
ActivityResultContracts.CreateDocument("application/xml") | ||
) { uri -> | ||
if (uri == null) return@registerForActivityResult | ||
|
||
val outputStream = application.contentResolver.openOutputStream(uri) | ||
outputStream?.use { it.write(content?.toByteArray()) } | ||
|
||
content = null | ||
} | ||
} | ||
|
||
private fun registerDocumentOpenActivityResult(activity: ComponentActivity) { | ||
openDocumentLauncher = | ||
activity.registerForActivityResult(ActivityResultContracts.OpenDocument()) { uri -> | ||
if (uri == null) return@registerForActivityResult | ||
|
||
val inputStream = application.contentResolver.openInputStream(uri) | ||
inputStream?.use { | ||
val content = it.bufferedReader().readText() | ||
result.trySend(content) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private open class ActivityLifecycleCallbacksAdapter : Application.ActivityLifecycleCallbacks { | ||
override fun onActivityCreated(activity: Activity, bundle: Bundle?) = Unit | ||
|
||
override fun onActivityStarted(activity: Activity) = Unit | ||
|
||
override fun onActivityResumed(activity: Activity) = Unit | ||
|
||
override fun onActivityPaused(activity: Activity) = Unit | ||
|
||
override fun onActivityStopped(activity: Activity) = Unit | ||
|
||
override fun onActivitySaveInstanceState(activity: Activity, bundle: Bundle) = Unit | ||
|
||
override fun onActivityDestroyed(activity: Activity) = Unit | ||
} |
30 changes: 30 additions & 0 deletions
30
.../androidMain/kotlin/dev/sasikanth/rss/reader/filemanager/AndroidFileManagerInitialiser.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,30 @@ | ||
/* | ||
* Copyright 2023 Sasikanth Miriyampalli | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dev.sasikanth.rss.reader.filemanager | ||
|
||
import dev.sasikanth.rss.reader.initializers.Initializer | ||
import me.tatarka.inject.annotations.Inject | ||
|
||
@Inject | ||
class AndroidFileManagerInitializer( | ||
private val fileManager: AndroidFileManager, | ||
) : Initializer { | ||
|
||
override fun initialize() { | ||
fileManager.registerActivityWatcher() | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
shared/src/androidMain/kotlin/dev/sasikanth/rss/reader/filemanager/FileManagerComponent.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 @@ | ||
/* | ||
* Copyright 2023 Sasikanth Miriyampalli | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dev.sasikanth.rss.reader.filemanager | ||
|
||
import dev.sasikanth.rss.reader.di.scopes.AppScope | ||
import dev.sasikanth.rss.reader.initializers.Initializer | ||
import me.tatarka.inject.annotations.IntoSet | ||
import me.tatarka.inject.annotations.Provides | ||
|
||
actual interface FileManagerComponent { | ||
|
||
@IntoSet | ||
@Provides | ||
@AppScope | ||
fun providesAndroidFileManagerInitializer(bind: AndroidFileManagerInitializer): Initializer = bind | ||
|
||
@Provides fun AndroidFileManager.bind(): FileManager = this | ||
} |
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
51 changes: 51 additions & 0 deletions
51
shared/src/commonMain/kotlin/dev/sasikanth/rss/reader/components/OutlinedButton.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,51 @@ | ||
/* | ||
* Copyright 2023 Sasikanth Miriyampalli | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dev.sasikanth.rss.reader.components | ||
|
||
import androidx.compose.foundation.BorderStroke | ||
import androidx.compose.foundation.layout.RowScope | ||
import androidx.compose.material3.ButtonColors | ||
import androidx.compose.material3.ButtonDefaults | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.unit.dp | ||
import dev.sasikanth.rss.reader.ui.AppTheme | ||
|
||
@Composable | ||
fun OutlinedButton( | ||
onClick: () -> Unit, | ||
modifier: Modifier = Modifier, | ||
enabled: Boolean = true, | ||
colors: ButtonColors = | ||
ButtonDefaults.outlinedButtonColors( | ||
containerColor = AppTheme.colorScheme.surfaceContainerLow, | ||
contentColor = AppTheme.colorScheme.tintedForeground | ||
), | ||
border: BorderStroke? = BorderStroke(1.dp, AppTheme.colorScheme.surfaceContainerHigh), | ||
content: @Composable RowScope.() -> Unit | ||
) { | ||
androidx.compose.material3.OutlinedButton( | ||
modifier = modifier, | ||
onClick = onClick, | ||
border = border, | ||
colors = colors, | ||
shape = MaterialTheme.shapes.medium, | ||
content = content, | ||
enabled = enabled | ||
) | ||
} |
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
Oops, something went wrong.