-
-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
395 additions
and
48 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
56 changes: 56 additions & 0 deletions
56
app/src/main/java/me/ash/reader/data/model/preference/OpenLinkPreference.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,56 @@ | ||
package me.ash.reader.data.model.preference | ||
|
||
import android.content.Context | ||
import androidx.compose.ui.text.style.Hyphens.Companion.Auto | ||
import androidx.datastore.preferences.core.Preferences | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.launch | ||
import me.ash.reader.R | ||
import me.ash.reader.ui.ext.DataStoreKeys | ||
import me.ash.reader.ui.ext.dataStore | ||
import me.ash.reader.ui.ext.put | ||
|
||
sealed class OpenLinkPreference(val value: Int) : Preference() { | ||
object AutoPreferCustomTabs : OpenLinkPreference(0) | ||
object AutoPreferDefaultBrowser : OpenLinkPreference(1) | ||
object CustomTabs : OpenLinkPreference(2) | ||
object DefaultBrowser : OpenLinkPreference(3) | ||
object SpecificBrowser: OpenLinkPreference(4) | ||
object AlwaysAsk: OpenLinkPreference(5) | ||
|
||
override fun put(context: Context, scope: CoroutineScope) { | ||
scope.launch { | ||
context.dataStore.put( | ||
DataStoreKeys.OpenLink, | ||
value | ||
) | ||
} | ||
} | ||
|
||
fun toDesc(context: Context): String = | ||
when (this) { | ||
AutoPreferCustomTabs -> context.getString(R.string.auto_customtabs) | ||
AutoPreferDefaultBrowser -> context.getString(R.string.auto_default_browser) | ||
CustomTabs -> context.getString(R.string.custom_tabs) | ||
DefaultBrowser -> context.getString(R.string.default_browser) | ||
SpecificBrowser -> context.getString(R.string.specific_browser) | ||
AlwaysAsk -> context.getString(R.string.always_ask) | ||
} | ||
|
||
companion object { | ||
|
||
val default = CustomTabs | ||
val values = listOf(AutoPreferCustomTabs, AutoPreferDefaultBrowser, CustomTabs, DefaultBrowser, SpecificBrowser, AlwaysAsk) | ||
|
||
fun fromPreferences(preferences: Preferences) = | ||
when (preferences[DataStoreKeys.OpenLink.key]) { | ||
0 -> AutoPreferCustomTabs | ||
1 -> AutoPreferDefaultBrowser | ||
2 -> CustomTabs | ||
3 -> DefaultBrowser | ||
4 -> SpecificBrowser | ||
5 -> AlwaysAsk | ||
else -> AutoPreferCustomTabs | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
app/src/main/java/me/ash/reader/data/model/preference/OpenLinkSpecificBrowserPreference.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,54 @@ | ||
package me.ash.reader.data.model.preference | ||
|
||
import android.content.Context | ||
import android.content.pm.PackageManager | ||
import android.os.Build | ||
import androidx.datastore.preferences.core.Preferences | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.launch | ||
import me.ash.reader.R | ||
import me.ash.reader.ui.ext.DataStoreKeys | ||
import me.ash.reader.ui.ext.dataStore | ||
import me.ash.reader.ui.ext.put | ||
|
||
data class OpenLinkSpecificBrowserPreference( | ||
val packageName: String? | ||
) : Preference() { | ||
|
||
override fun put(context: Context, scope: CoroutineScope) { | ||
scope.launch { | ||
context.dataStore.put( | ||
DataStoreKeys.OpenLinkAppSpecificBrowser, | ||
packageName.orEmpty() | ||
) | ||
} | ||
} | ||
|
||
fun toDesc(context: Context): String { | ||
val pm = context.packageManager | ||
return runCatching { | ||
pm.run { | ||
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { | ||
getApplicationInfo( | ||
this@OpenLinkSpecificBrowserPreference.packageName!!, | ||
PackageManager.ApplicationInfoFlags.of(0) | ||
) | ||
} else { | ||
getApplicationInfo(this@OpenLinkSpecificBrowserPreference.packageName!!, 0) | ||
} | ||
} | ||
}.map { | ||
it.loadLabel(pm) | ||
}.getOrDefault(context.getString(R.string.open_link_specific_browser_not_selected)).let { | ||
context.getString(R.string.specific_browser_name, it) | ||
} | ||
} | ||
|
||
companion object { | ||
val default = OpenLinkSpecificBrowserPreference(null) | ||
fun fromPreferences(preferences: Preferences): OpenLinkSpecificBrowserPreference { | ||
val packageName = preferences[DataStoreKeys.OpenLinkAppSpecificBrowser.key] | ||
return OpenLinkSpecificBrowserPreference(packageName) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.