Skip to content

Commit

Permalink
fix: intent handling using listener and singleTop
Browse files Browse the repository at this point in the history
  • Loading branch information
Yash-Garg committed May 4, 2023
1 parent 91a9b18 commit b8cb71a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleInstance"
android:theme="@style/Theme.Qbit">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
14 changes: 10 additions & 4 deletions app/src/main/kotlin/dev/yashgarg/qbit/ui/server/ServerFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.fragment.app.setFragmentResultListener
import androidx.fragment.app.viewModels
Expand Down Expand Up @@ -44,6 +45,11 @@ class ServerFragment : Fragment(R.layout.server_fragment) {

exitTransition = MaterialSharedAxis(MaterialSharedAxis.Z, true)
reenterTransition = MaterialSharedAxis(MaterialSharedAxis.Z, false)

activity?.addOnNewIntentListener {
val bundle = bundleOf(MainActivity.TORRENT_INTENT_KEY to it?.data.toString())
handleAddIntent(bundle)
}
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -108,9 +114,9 @@ class ServerFragment : Fragment(R.layout.server_fragment) {
}
}

private fun handleAddIntent() {
val uri: String? = arguments?.getString(MainActivity.TORRENT_INTENT_KEY)
arguments?.clear()
private fun handleAddIntent(bundle: Bundle?) {
val uri: String? = (bundle ?: arguments)?.getString(MainActivity.TORRENT_INTENT_KEY)
(bundle ?: arguments)?.clear()
if (!uri.isNullOrEmpty()) {
when {
linkValidator.isValid(uri) -> viewModel.addTorrentUrl(uri)
Expand Down Expand Up @@ -207,7 +213,7 @@ class ServerFragment : Fragment(R.layout.server_fragment) {

viewModel.intent
.flowWithLifecycle(viewLifecycleOwner.lifecycle)
.onEach { handleAddIntent() }
.onEach { handleAddIntent(null) }
.launchIn(viewLifecycleOwner.lifecycleScope)
}

Expand Down

0 comments on commit b8cb71a

Please sign in to comment.