From 5de0db72e911ad91499f0689c4714644edf5a8eb Mon Sep 17 00:00:00 2001 From: Vadik Sirekanyan Date: Sat, 6 Jan 2024 15:09:10 +0400 Subject: [PATCH] Reused open google play function (refactoring) --- .../sirekanyan/outline/NotSupportedContent.kt | 4 ++-- .../java/org/sirekanyan/outline/ext/Context.kt | 14 ++++++++++++++ .../org/sirekanyan/outline/ext/PackageManager.kt | 16 ++++------------ .../org/sirekanyan/outline/ui/AboutDialog.kt | 3 ++- 4 files changed, 22 insertions(+), 15 deletions(-) create mode 100644 app/src/main/java/org/sirekanyan/outline/ext/Context.kt diff --git a/app/src/main/java/org/sirekanyan/outline/NotSupportedContent.kt b/app/src/main/java/org/sirekanyan/outline/NotSupportedContent.kt index 057bab9..4ed139d 100644 --- a/app/src/main/java/org/sirekanyan/outline/NotSupportedContent.kt +++ b/app/src/main/java/org/sirekanyan/outline/NotSupportedContent.kt @@ -10,8 +10,8 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.remember import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource +import org.sirekanyan.outline.ext.installOutline import org.sirekanyan.outline.ext.isOutlineInstalled -import org.sirekanyan.outline.ext.openGooglePlay import org.sirekanyan.outline.ext.openOutline @Composable @@ -39,7 +39,7 @@ fun NotSupportedContent(onDismissRequest: () -> Unit) { Text("Open Outline") } } else { - TextButton(onClick = { onDismissRequest(); openGooglePlay(context) }) { + TextButton(onClick = { onDismissRequest(); installOutline(context) }) { Text("Install Outline") } } diff --git a/app/src/main/java/org/sirekanyan/outline/ext/Context.kt b/app/src/main/java/org/sirekanyan/outline/ext/Context.kt new file mode 100644 index 0000000..f8e5149 --- /dev/null +++ b/app/src/main/java/org/sirekanyan/outline/ext/Context.kt @@ -0,0 +1,14 @@ +package org.sirekanyan.outline.ext + +import android.content.Context +import android.content.Intent +import android.net.Uri + +fun Context.openGooglePlay(uri: String) { + try { + startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(uri))) + } catch (exception: Exception) { + logDebug("Cannot open Google Play", exception) + showToast("Cannot open Google Play") + } +} diff --git a/app/src/main/java/org/sirekanyan/outline/ext/PackageManager.kt b/app/src/main/java/org/sirekanyan/outline/ext/PackageManager.kt index 443a94f..8ea3c25 100644 --- a/app/src/main/java/org/sirekanyan/outline/ext/PackageManager.kt +++ b/app/src/main/java/org/sirekanyan/outline/ext/PackageManager.kt @@ -1,11 +1,8 @@ package org.sirekanyan.outline.ext import android.content.Context -import android.content.Intent -import android.content.Intent.ACTION_VIEW import android.content.pm.PackageManager.GET_ACTIVITIES import android.content.pm.PackageManager.NameNotFoundException -import android.net.Uri private const val OUTLINE_PACKAGE = "org.outline.android.client" private const val OUTLINE_PLAY_LINK = "https://play.google.com/store/apps/details?id=$OUTLINE_PACKAGE" @@ -22,21 +19,16 @@ fun openOutline(context: Context) { try { val intent = context.packageManager.getLaunchIntentForPackage(OUTLINE_PACKAGE) if (intent == null) { - openGooglePlay(context) + installOutline(context) } else { context.startActivity(intent) } } catch (exception: Exception) { logDebug("Cannot open Outline", exception) - openGooglePlay(context) + installOutline(context) } } -fun openGooglePlay(context: Context) { - try { - context.startActivity(Intent(ACTION_VIEW, Uri.parse(OUTLINE_PLAY_LINK))) - } catch (exception: Exception) { - logDebug("Cannot open Google Play", exception) - context.showToast("Cannot open Google Play") - } +fun installOutline(context: Context) { + context.openGooglePlay(OUTLINE_PLAY_LINK) } diff --git a/app/src/main/java/org/sirekanyan/outline/ui/AboutDialog.kt b/app/src/main/java/org/sirekanyan/outline/ui/AboutDialog.kt index 0d7f642..b128ed9 100644 --- a/app/src/main/java/org/sirekanyan/outline/ui/AboutDialog.kt +++ b/app/src/main/java/org/sirekanyan/outline/ui/AboutDialog.kt @@ -32,6 +32,7 @@ import androidx.compose.ui.unit.dp import org.sirekanyan.outline.BuildConfig import org.sirekanyan.outline.R import org.sirekanyan.outline.ext.logDebug +import org.sirekanyan.outline.ext.openGooglePlay import org.sirekanyan.outline.ext.showToast import org.sirekanyan.outline.isDebugBuild import org.sirekanyan.outline.isPlayFlavor @@ -92,7 +93,7 @@ fun AboutDialogContent(onDismiss: () -> Unit) { val packageName = "org.sirekanyan.outline" val playUri = "https://play.google.com/store/apps/details?id=$packageName" AboutItem(IconPlayStore, "Rate on Play Store") { - context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(playUri))) + context.openGooglePlay(playUri) onDismiss() } }