Fix: intent.resolveActivity(...) returns null (instead used different function, which fixes "No browser found" for all sites if browser is actually present).
This commit is contained in:
parent
ae6596f237
commit
5bcda4a7d9
10 changed files with 99 additions and 55 deletions
|
|
@ -26,6 +26,7 @@
|
|||
</intent>
|
||||
<intent>
|
||||
<action android:name="android.intent.action.VIEW" />
|
||||
<category android:name="android.intent.category.DEFAULT" />
|
||||
<data android:scheme="https" />
|
||||
</intent>
|
||||
|
||||
|
|
|
|||
|
|
@ -1,11 +0,0 @@
|
|||
package ua.acclorite.book_story.presentation.data
|
||||
|
||||
fun String.removeTrailingZero(): String {
|
||||
if (!this.contains('.'))
|
||||
return this
|
||||
return this
|
||||
.dropLastWhile { it == '0' }
|
||||
.dropLastWhile { it == '.' }
|
||||
}
|
||||
|
||||
fun Double.removeDigits(digits: Int) = "%.${digits}f".format(this).replace(",", ".")
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
package ua.acclorite.book_story.presentation.data
|
||||
|
||||
import android.content.Intent
|
||||
import androidx.activity.ComponentActivity
|
||||
|
||||
fun String.removeTrailingZero(): String {
|
||||
if (!this.contains('.'))
|
||||
return this
|
||||
return this
|
||||
.dropLastWhile { it == '0' }
|
||||
.dropLastWhile { it == '.' }
|
||||
}
|
||||
|
||||
fun Double.removeDigits(digits: Int) = "%.${digits}f".format(this).replace(",", ".")
|
||||
|
||||
fun Intent.launchActivity(
|
||||
activity: ComponentActivity,
|
||||
openInNewWindow: Boolean = true,
|
||||
success: (() -> Unit)? = null,
|
||||
error: () -> Unit
|
||||
) {
|
||||
if (openInNewWindow) {
|
||||
this.flags = Intent.FLAG_ACTIVITY_NEW_TASK
|
||||
}
|
||||
|
||||
try {
|
||||
activity.baseContext.startActivity(this)
|
||||
} catch (e: Exception) {
|
||||
error()
|
||||
return
|
||||
}
|
||||
|
||||
success?.invoke()
|
||||
}
|
||||
|
|
@ -2,6 +2,7 @@ package ua.acclorite.book_story.presentation.screens.about.data
|
|||
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
|
|
@ -11,6 +12,7 @@ import kotlinx.coroutines.flow.update
|
|||
import kotlinx.coroutines.launch
|
||||
import ua.acclorite.book_story.R
|
||||
import ua.acclorite.book_story.domain.use_case.CheckForUpdates
|
||||
import ua.acclorite.book_story.presentation.data.launchActivity
|
||||
import javax.inject.Inject
|
||||
|
||||
@HiltViewModel
|
||||
|
|
@ -30,14 +32,11 @@ class AboutViewModel @Inject constructor(
|
|||
Uri.parse(event.page)
|
||||
)
|
||||
|
||||
if (intent.resolveActivity(event.context.packageManager) != null) {
|
||||
event.context.startActivity(intent)
|
||||
return@launch
|
||||
}
|
||||
|
||||
intent.launchActivity(event.context as ComponentActivity) {
|
||||
event.noAppsFound()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
is AboutEvent.OnCheckForUpdates -> {
|
||||
viewModelScope.launch {
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package ua.acclorite.book_story.presentation.screens.about.nested.license_info.d
|
|||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.mikepenz.aboutlibraries.Libs
|
||||
|
|
@ -15,6 +16,7 @@ import kotlinx.coroutines.flow.update
|
|||
import kotlinx.coroutines.launch
|
||||
import ua.acclorite.book_story.domain.util.OnNavigate
|
||||
import ua.acclorite.book_story.presentation.data.Screen
|
||||
import ua.acclorite.book_story.presentation.data.launchActivity
|
||||
import javax.inject.Inject
|
||||
|
||||
@HiltViewModel
|
||||
|
|
@ -32,16 +34,13 @@ class LicenseInfoViewModel @Inject constructor() : ViewModel() {
|
|||
Uri.parse(event.page)
|
||||
)
|
||||
|
||||
if (intent.resolveActivity(event.context.packageManager) != null) {
|
||||
event.context.startActivity(intent)
|
||||
return@launch
|
||||
}
|
||||
|
||||
intent.launchActivity(event.context as ComponentActivity) {
|
||||
event.noAppsFound()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun init(screen: Screen.About.LicenseInfo, onNavigate: OnNavigate, context: Context) {
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ import ua.acclorite.book_story.domain.use_case.GetFilesFromDevice
|
|||
import ua.acclorite.book_story.domain.use_case.InsertBook
|
||||
import ua.acclorite.book_story.domain.util.Resource
|
||||
import ua.acclorite.book_story.presentation.data.Screen
|
||||
import ua.acclorite.book_story.presentation.data.launchActivity
|
||||
import javax.inject.Inject
|
||||
|
||||
@OptIn(ExperimentalPermissionsApi::class)
|
||||
|
|
@ -84,9 +85,11 @@ class BrowseViewModel @Inject constructor(
|
|||
val uri = Uri.parse("package:${event.activity.packageName}")
|
||||
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, uri)
|
||||
|
||||
if (intent.resolveActivity(event.activity.packageManager) != null) {
|
||||
event.activity.startActivity(intent)
|
||||
} else {
|
||||
var failure = false
|
||||
intent.launchActivity(event.activity) {
|
||||
failure = true
|
||||
}
|
||||
if (failure) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
@ -99,9 +102,11 @@ class BrowseViewModel @Inject constructor(
|
|||
uri
|
||||
)
|
||||
|
||||
if (intent.resolveActivity(event.activity.packageManager) != null) {
|
||||
event.activity.startActivity(intent)
|
||||
} else {
|
||||
var failure = false
|
||||
intent.launchActivity(event.activity) {
|
||||
failure = true
|
||||
}
|
||||
if (failure) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package ua.acclorite.book_story.presentation.screens.help.data
|
|||
import android.app.SearchManager
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
|
|
@ -11,6 +12,7 @@ import kotlinx.coroutines.flow.asStateFlow
|
|||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import ua.acclorite.book_story.presentation.data.Screen
|
||||
import ua.acclorite.book_story.presentation.data.launchActivity
|
||||
import javax.inject.Inject
|
||||
|
||||
@HiltViewModel
|
||||
|
|
@ -30,14 +32,11 @@ class HelpViewModel @Inject constructor(
|
|||
Uri.parse(event.page)
|
||||
)
|
||||
|
||||
if (intent.resolveActivity(event.context.packageManager) != null) {
|
||||
event.context.startActivity(intent)
|
||||
return@launch
|
||||
}
|
||||
|
||||
intent.launchActivity(event.context as ComponentActivity) {
|
||||
event.noAppsFound()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
is HelpEvent.OnSearchInWeb -> {
|
||||
viewModelScope.launch {
|
||||
|
|
@ -58,14 +57,11 @@ class HelpViewModel @Inject constructor(
|
|||
"${_state.value.textFieldValue.trim()} filetype:txt OR filetype:pdf"
|
||||
)
|
||||
|
||||
if (intent.resolveActivity(event.context.packageManager) != null) {
|
||||
event.context.startActivity(intent)
|
||||
return@launch
|
||||
}
|
||||
|
||||
intent.launchActivity(event.context as ComponentActivity) {
|
||||
event.noAppsFound()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
is HelpEvent.OnUpdateState -> {
|
||||
_state.update {
|
||||
|
|
|
|||
|
|
@ -32,6 +32,7 @@ import ua.acclorite.book_story.domain.use_case.UpdateBooks
|
|||
import ua.acclorite.book_story.domain.util.OnNavigate
|
||||
import ua.acclorite.book_story.domain.util.UIText
|
||||
import ua.acclorite.book_story.presentation.data.Screen
|
||||
import ua.acclorite.book_story.presentation.data.launchActivity
|
||||
import ua.acclorite.book_story.presentation.screens.history.data.HistoryEvent
|
||||
import ua.acclorite.book_story.presentation.screens.library.data.LibraryEvent
|
||||
import javax.inject.Inject
|
||||
|
|
@ -336,13 +337,19 @@ class ReaderViewModel @Inject constructor(
|
|||
"translate: ${event.textToTranslate.trim()}"
|
||||
)
|
||||
|
||||
if (translatorIntent.resolveActivity(event.context.packageManager) != null) {
|
||||
event.context.startActivity(translatorIntent)
|
||||
var translatorFailure = false
|
||||
translatorIntent.launchActivity(event.context) {
|
||||
translatorFailure = true
|
||||
}
|
||||
if (!translatorFailure) {
|
||||
return@launch
|
||||
}
|
||||
|
||||
if (browserIntent.resolveActivity(event.context.packageManager) != null) {
|
||||
event.context.startActivity(browserIntent)
|
||||
var browserFailure = false
|
||||
browserIntent.launchActivity(event.context) {
|
||||
browserFailure = true
|
||||
}
|
||||
if (!browserFailure) {
|
||||
return@launch
|
||||
}
|
||||
|
||||
|
|
@ -361,8 +368,11 @@ class ReaderViewModel @Inject constructor(
|
|||
": ${event.textToDefine.trim()}"
|
||||
)
|
||||
|
||||
if (browserIntent.resolveActivity(event.context.packageManager) != null) {
|
||||
event.context.startActivity(browserIntent)
|
||||
var failure = false
|
||||
browserIntent.launchActivity(event.context) {
|
||||
failure = true
|
||||
}
|
||||
if (!failure) {
|
||||
return@launch
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ import kotlinx.coroutines.Job
|
|||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.yield
|
||||
import ua.acclorite.book_story.presentation.data.launchActivity
|
||||
import javax.inject.Inject
|
||||
|
||||
@OptIn(ExperimentalPermissionsApi::class)
|
||||
|
|
@ -48,9 +49,12 @@ class SettingsViewModel @Inject constructor(
|
|||
val intent = Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS)
|
||||
intent.putExtra(Settings.EXTRA_APP_PACKAGE, event.activity.packageName)
|
||||
|
||||
if (intent.resolveActivity(event.activity.packageManager) != null) {
|
||||
event.activity.startActivity(intent)
|
||||
} else {
|
||||
var failure = false
|
||||
intent.launchActivity(event.activity) {
|
||||
failure = true
|
||||
}
|
||||
|
||||
if (failure) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ import kotlinx.coroutines.launch
|
|||
import kotlinx.coroutines.yield
|
||||
import ua.acclorite.book_story.domain.use_case.CheckForUpdates
|
||||
import ua.acclorite.book_story.presentation.data.Screen
|
||||
import ua.acclorite.book_story.presentation.data.launchActivity
|
||||
import javax.inject.Inject
|
||||
|
||||
@OptIn(ExperimentalPermissionsApi::class)
|
||||
|
|
@ -137,9 +138,11 @@ class StartViewModel @Inject constructor(
|
|||
val uri = Uri.parse("package:${event.activity.packageName}")
|
||||
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, uri)
|
||||
|
||||
if (intent.resolveActivity(event.activity.packageManager) != null) {
|
||||
event.activity.startActivity(intent)
|
||||
} else {
|
||||
var failure = false
|
||||
intent.launchActivity(event.activity) {
|
||||
failure = true
|
||||
}
|
||||
if (failure) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
@ -152,9 +155,11 @@ class StartViewModel @Inject constructor(
|
|||
uri
|
||||
)
|
||||
|
||||
if (intent.resolveActivity(event.activity.packageManager) != null) {
|
||||
event.activity.startActivity(intent)
|
||||
} else {
|
||||
var failure = false
|
||||
intent.launchActivity(event.activity) {
|
||||
failure = true
|
||||
}
|
||||
if (failure) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
@ -203,9 +208,11 @@ class StartViewModel @Inject constructor(
|
|||
val intent = Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS)
|
||||
intent.putExtra(Settings.EXTRA_APP_PACKAGE, event.activity.packageName)
|
||||
|
||||
if (intent.resolveActivity(event.activity.packageManager) != null) {
|
||||
event.activity.startActivity(intent)
|
||||
} else {
|
||||
var failure = false
|
||||
intent.launchActivity(event.activity) {
|
||||
failure = true
|
||||
}
|
||||
if (failure) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue