Skip to content

Commit

Permalink
Add option to use private custom tabs when available (#613)
Browse files Browse the repository at this point in the history
Co-authored-by: Dessalines <[email protected]>
  • Loading branch information
twizmwazin and dessalines authored Jun 13, 2023
1 parent ff0d3b4 commit 17c3342
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 19 deletions.
3 changes: 2 additions & 1 deletion app/src/main/java/com/jerboa/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

MarkdownHelper.init(this, appSettingsViewModel.appSettings.value?.useCustomTabs ?: true)
MarkdownHelper.init(this, appSettingsViewModel.appSettings.value?.useCustomTabs ?: true, appSettingsViewModel.appSettings.value?.usePrivateTabs ?: false)
window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))

val accountSync = getCurrentAccountSync(accountViewModel)
Expand Down Expand Up @@ -608,6 +608,7 @@ class MainActivity : ComponentActivity() {
AboutActivity(
navController = navController,
useCustomTabs = appSettings?.useCustomTabs ?: true,
usePrivateTabs = appSettings?.usePrivateTabs ?: false,
)
}
}
Expand Down
8 changes: 6 additions & 2 deletions app/src/main/java/com/jerboa/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -375,10 +375,14 @@ fun LazyListState.isScrolledToEnd(): Boolean {
return out
}

fun openLink(url: String, ctx: Context, useCustomTab: Boolean) {
fun openLink(url: String, ctx: Context, useCustomTab: Boolean, usePrivateTab: Boolean) {
if (useCustomTab) {
val intent = CustomTabsIntent.Builder()
.build()
.build().apply {
if (usePrivateTab) {
intent.putExtra("com.google.android.apps.chrome.EXTRA_OPEN_NEW_INCOGNITO_TAB", true)
}
}
intent.launchUrl(ctx, Uri.parse(url))
} else {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
Expand Down
17 changes: 16 additions & 1 deletion app/src/main/java/com/jerboa/db/AppDB.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ data class AppSettings(
defaultValue = "1",
)
val useCustomTabs: Boolean,
@ColumnInfo(
name = "use_private_tabs",
defaultValue = "0",
)
val usePrivateTabs: Boolean,
)

@Dao
Expand Down Expand Up @@ -353,8 +358,17 @@ val MIGRATION_12_13 = object : Migration(12, 13) {
}
}

val MIGRATION_13_14 = object : Migration(13, 14) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL(UPDATE_APP_CHANGELOG_UNVIEWED)
database.execSQL(
"ALTER TABLE AppSettings add column use_private_tabs INTEGER NOT NULL default 0",
)
}
}

@Database(
version = 13,
version = 14,
entities = [Account::class, AppSettings::class],
exportSchema = true,
)
Expand Down Expand Up @@ -391,6 +405,7 @@ abstract class AppDB : RoomDatabase() {
MIGRATION_10_11,
MIGRATION_11_12,
MIGRATION_12_13,
MIGRATION_13_14,
)
// Necessary because it can't insert data on creation
.addCallback(object : Callback() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ fun CommentFooterLine(
@Preview
@Composable
fun CommentNodesPreview() {
MarkdownHelper.init(LocalContext.current, true)
MarkdownHelper.init(LocalContext.current, useCustomTabs = true, usePrivateTabs = false)
val comments = listOf(
sampleSecondReplyCommentView,
sampleCommentView,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import io.noties.markwon.linkify.LinkifyPlugin
object MarkdownHelper {
private var markwon: Markwon? = null

fun init(context: Context, useCustomTabs: Boolean) {
fun init(context: Context, useCustomTabs: Boolean, usePrivateTabs: Boolean) {
val loader = ImageLoader.Builder(context)
.crossfade(true)
.placeholder(R.drawable.ic_launcher_foreground)
Expand All @@ -50,7 +50,7 @@ object MarkdownHelper {
.usePlugin(object : AbstractMarkwonPlugin() {
override fun configureConfiguration(builder: MarkwonConfiguration.Builder) {
builder.linkResolver { view, link ->
openLink(link, view.context, useCustomTabs)
openLink(link, view.context, useCustomTabs, usePrivateTabs)
}
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ fun CommunityActivity(
navController.navigate(route = "post/${postView.post.id}")
},
onPostLinkClick = { url ->
openLink(url, ctx, appSettingsViewModel.appSettings.value?.useCustomTabs ?: true)
openLink(url, ctx, appSettingsViewModel.appSettings.value?.useCustomTabs ?: true, appSettingsViewModel.appSettings.value?.usePrivateTabs ?: false)
},
onSaveClick = { postView ->
account?.also { acct ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ fun MainPostListingsContent(
navController.navigate(route = "post/${postView.post.id}")
},
onPostLinkClick = { url ->
openLink(url, ctx, appSettingsViewModel.appSettings.value?.useCustomTabs ?: true)
openLink(url, ctx, appSettingsViewModel.appSettings.value?.useCustomTabs ?: true, appSettingsViewModel.appSettings.value?.usePrivateTabs ?: false)
},
onSaveClick = { postView ->
account?.also { acct ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ fun UserTabs(
navController.navigate(route = "post/${postView.post.id}")
},
onPostLinkClick = { url ->
openLink(url, ctx, appSettingsViewModel.appSettings.value?.useCustomTabs ?: true)
openLink(url, ctx, appSettingsViewModel.appSettings.value?.useCustomTabs ?: true, appSettingsViewModel.appSettings.value?.usePrivateTabs ?: false)
},
onSaveClick = { postView ->
account?.also { acct ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ fun PostActivity(
},
onPostClick = {},
onPostLinkClick = { url ->
openLink(url, ctx, appSettingsViewModel.appSettings.value?.useCustomTabs ?: true)
openLink(url, ctx, appSettingsViewModel.appSettings.value?.useCustomTabs ?: true, appSettingsViewModel.appSettings.value?.usePrivateTabs ?: false)
},
onSaveClick = {
account?.also { acct ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const val mastodonLink = "https://mastodon.social/@LemmyDev"
fun AboutActivity(
navController: NavController,
useCustomTabs: Boolean,
usePrivateTabs: Boolean,
) {
Log.d("jerboa", "Got to About activity")

Expand All @@ -52,6 +53,10 @@ fun AboutActivity(

val snackbarHostState = remember { SnackbarHostState() }

fun openLink(link: String) {
openLink(link, ctx, useCustomTabs, usePrivateTabs)
}

Scaffold(
snackbarHost = { SnackbarHost(snackbarHostState) },
topBar = {
Expand All @@ -69,7 +74,7 @@ fun AboutActivity(
)
},
onClick = {
openLink("$githubUrl/blob/main/RELEASES.md", ctx, useCustomTabs)
openLink("$githubUrl/blob/main/RELEASES.md", ctx, useCustomTabs, usePrivateTabs)
},
)
SettingsDivider()
Expand All @@ -83,7 +88,7 @@ fun AboutActivity(
)
},
onClick = {
openLink("$githubUrl/issues", ctx, useCustomTabs)
openLink("$githubUrl/issues")
},
)
SettingsMenuLink(
Expand All @@ -95,7 +100,7 @@ fun AboutActivity(
)
},
onClick = {
openLink(jerboaMatrixChat, ctx, useCustomTabs)
openLink(jerboaMatrixChat)
},
)
SettingsMenuLink(
Expand All @@ -107,7 +112,7 @@ fun AboutActivity(
)
},
onClick = {
openLink(donateLink, ctx, useCustomTabs)
openLink(donateLink)
},
)
SettingsDivider()
Expand All @@ -122,7 +127,7 @@ fun AboutActivity(
)
},
onClick = {
openLink(jerboaLemmyLink, ctx, useCustomTabs)
openLink(jerboaLemmyLink)
},
)
SettingsMenuLink(
Expand All @@ -134,7 +139,7 @@ fun AboutActivity(
)
},
onClick = {
openLink(mastodonLink, ctx, useCustomTabs)
openLink(mastodonLink)
},
)
SettingsDivider()
Expand All @@ -155,7 +160,7 @@ fun AboutActivity(
)
},
onClick = {
openLink(githubUrl, ctx, useCustomTabs)
openLink(githubUrl)
},
)
}
Expand Down Expand Up @@ -183,5 +188,5 @@ fun SettingsHeader(
@Preview
@Composable
fun AboutPreview() {
AboutActivity(navController = rememberNavController(), useCustomTabs = false)
AboutActivity(navController = rememberNavController(), useCustomTabs = false, usePrivateTabs = false)
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ fun LookAndFeelActivity(
settings?.showVotingArrowsInListView ?: true,
)
val useCustomTabsState = rememberBooleanSettingState(settings?.useCustomTabs ?: true)
val usePrivateTabsState = rememberBooleanSettingState(settings?.usePrivateTabs ?: false)

val snackbarHostState = remember { SnackbarHostState() }

Expand All @@ -79,6 +80,7 @@ fun LookAndFeelActivity(
showCommentActionBarByDefault = showCommentActionBarByDefaultState.value,
showVotingArrowsInListView = showVotingArrowsInListViewState.value,
useCustomTabs = useCustomTabsState.value,
usePrivateTabs = usePrivateTabsState.value,
),
)
}
Expand Down Expand Up @@ -200,6 +202,13 @@ fun LookAndFeelActivity(
},
onCheckedChange = { updateAppSettings() },
)
SettingsCheckbox(
state = usePrivateTabsState,
title = {
Text(text = stringResource(id = R.string.look_and_feel_use_private_tabs))
},
onCheckedChange = { updateAppSettings() },
)
}
},
)
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
<string name="look_and_feel_post_view_list">List</string>
<string name="look_and_feel_show_navigation_bar">Show navigation bar</string>
<string name="look_and_feel_use_custom_tabs">Use custom tabs</string>
<string name="look_and_feel_use_private_tabs">Use private custom tabs if available</string>
<string name="settings_activity_settings">Settings</string>
<string name="settings_activity_look_and_feel">Look and feel</string>
<string name="settings_activity_account_settings">%1$s settings</string>
Expand Down

0 comments on commit 17c3342

Please sign in to comment.