Parcourir la source

Reused open google play function (refactoring)

Vadik Sirekanyan il y a 2 ans
Parent
commit
5de0db72e9

+ 2 - 2
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")
                 }
             }

+ 14 - 0
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")
+    }
+}

+ 4 - 12
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)
 }

+ 2 - 1
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()
                 }
             }