Skip to content

Commit

Permalink
Reused open google play function (refactoring)
Browse files Browse the repository at this point in the history
  • Loading branch information
sirekanian committed Jan 6, 2024
1 parent b24d296 commit 5de0db7
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
}
}
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/org/sirekanyan/outline/ext/Context.kt
Original file line number Diff line number Diff line change
@@ -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")
}
}
16 changes: 4 additions & 12 deletions app/src/main/java/org/sirekanyan/outline/ext/PackageManager.kt
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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)
}
3 changes: 2 additions & 1 deletion app/src/main/java/org/sirekanyan/outline/ui/AboutDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
}
Expand Down

0 comments on commit 5de0db7

Please sign in to comment.