Skip to content

Commit

Permalink
Fix import through intent (readium#438)
Browse files Browse the repository at this point in the history
  • Loading branch information
qnga committed Jan 12, 2024
1 parent 3965b06 commit 4bd2893
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 19 deletions.
2 changes: 1 addition & 1 deletion test-app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
</activity>

<activity
android:name=".utils.R2DispatcherActivity"
android:name=".utils.ImportActivity"
android:launchMode="singleInstance"
android:noHistory="true"
android:theme="@android:style/Theme.NoDisplay"
Expand Down
4 changes: 0 additions & 4 deletions test-app/src/main/java/org/readium/r2/testapp/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

intent.data?.let {
viewModel.importPublicationFromStorage(it)
}

val navView: BottomNavigationView = findViewById(R.id.nav_view)
val navHostFragment =
supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@
package org.readium.r2.testapp

import android.app.Application
import android.net.Uri
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.launch
import org.readium.r2.testapp.domain.Bookshelf
import org.readium.r2.testapp.domain.ImportError
import org.readium.r2.testapp.utils.EventChannel
Expand All @@ -34,10 +32,6 @@ class MainViewModel(
.onEach { sendImportFeedback(it) }
.launchIn(viewModelScope)
}
fun importPublicationFromStorage(uri: Uri) =
viewModelScope.launch {
app.bookshelf.importPublicationFromStorage(uri)
}

private fun sendImportFeedback(event: Bookshelf.Event) {
when (event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,34 @@ import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.core.content.IntentCompat
import org.readium.r2.testapp.Application
import org.readium.r2.testapp.MainActivity
import timber.log.Timber

class R2DispatcherActivity : Activity() {
class ImportActivity : Activity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
dispatchIntent(intent)

importPublication(intent)

val newIntent = Intent(this, MainActivity::class.java).apply {
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
}
startActivity(newIntent)

finish()
}

private fun dispatchIntent(intent: Intent) {
private fun importPublication(intent: Intent) {
val uri = uriFromIntent(intent)
?: run {
Timber.d("Got an empty intent.")
return
}
val newIntent = Intent(this, MainActivity::class.java).apply {
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
data = uri
}
startActivity(newIntent)

val app = application as Application
app.bookshelf.importPublicationFromStorage(uri)
}

private fun uriFromIntent(intent: Intent): Uri? =
Expand Down

0 comments on commit 4bd2893

Please sign in to comment.