Skip to content

Commit 838dcd3

Browse files
made shared function for deeplinking
1 parent 28c11cc commit 838dcd3

5 files changed

Lines changed: 40 additions & 38 deletions

File tree

app/src/main/java/com/cornellappdev/transit/ui/components/home/DetailedPlaceSheetContent.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import androidx.compose.ui.Alignment
2424
import androidx.compose.ui.Modifier
2525
import androidx.compose.ui.graphics.Color
2626
import androidx.compose.ui.graphics.vector.ImageVector
27+
import androidx.compose.ui.platform.LocalContext
2728
import androidx.compose.ui.res.vectorResource
2829
import androidx.compose.ui.tooling.preview.Preview
2930
import androidx.compose.ui.tooling.preview.PreviewParameter
@@ -40,6 +41,7 @@ import com.cornellappdev.transit.ui.theme.SecondaryText
4041
import com.cornellappdev.transit.ui.theme.Style
4142
import com.cornellappdev.transit.ui.theme.TransitBlue
4243
import com.cornellappdev.transit.util.BOTTOM_SHEET_MAX_HEIGHT_PERCENT
44+
import com.cornellappdev.transit.util.IntentUtils.openDeepLink
4345

4446
@Composable
4547
fun DetailedPlaceSheetContent(
@@ -51,6 +53,8 @@ fun DetailedPlaceSheetContent(
5153
modifier: Modifier = Modifier,
5254
distanceStringToPlace: (Double?, Double?) -> String
5355
) {
56+
val context = LocalContext.current
57+
5458
Column(
5559
modifier = modifier
5660
.fillMaxWidth()
@@ -98,6 +102,9 @@ fun DetailedPlaceSheetContent(
98102
onFavoriteClick = {
99103
onFavoriteStarClick(ecosystemPlace.toPlace())
100104
},
105+
onDeepLinkClick = {
106+
context.openDeepLink("com.cornellappdev.android.eatery")
107+
},
101108
distanceString = distanceStringToPlace(
102109
ecosystemPlace.latitude,
103110
ecosystemPlace.longitude
@@ -122,6 +129,9 @@ fun DetailedPlaceSheetContent(
122129
onFavoriteClick = {
123130
onFavoriteStarClick(ecosystemPlace.toPlace())
124131
},
132+
onDeepLinkClick = {
133+
context.openDeepLink("com.cornellappdev.uplift")
134+
},
125135
distanceString = distanceStringToPlace(
126136
ecosystemPlace.latitude,
127137
ecosystemPlace.longitude

app/src/main/java/com/cornellappdev/transit/ui/components/home/EateryDetailsContent.kt

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ fun EateryDetailsContent(
3939
eatery: Eatery,
4040
isFavorite: Boolean,
4141
onFavoriteClick: () -> Unit,
42+
onDeepLinkClick: () -> Unit,
4243
distanceString: String,
4344
) {
4445
Column(
@@ -93,31 +94,13 @@ fun EateryDetailsContent(
9394
val (annotatedString, inlineContent) =
9495
stringResource(R.string.view_menu).createDeepLink(R.drawable.eaterylink)
9596

96-
val context = LocalContext.current
97-
9897
Text(
9998
text = annotatedString,
10099
inlineContent = inlineContent,
101100
style = Style.heading2,
102101
color = TransitBlue,
103102
modifier = Modifier.clickable(
104-
onClick = {
105-
val eateryPackage = "com.cornellappdev.android.eatery"
106-
val eateryIntent = context.packageManager.getLaunchIntentForPackage(eateryPackage)
107-
108-
if (eateryIntent != null) {
109-
context.startActivity(eateryIntent)
110-
} else {
111-
try {
112-
val playStoreIntent = Intent(Intent.ACTION_VIEW, "market://details?id=$eateryPackage".toUri())
113-
playStoreIntent.setPackage("com.android.vending")
114-
context.startActivity(playStoreIntent)
115-
} catch (e2: ActivityNotFoundException) {
116-
context.startActivity(Intent(Intent.ACTION_VIEW, "https://play.google.com/store/apps/details?id=$eateryPackage}".toUri()))
117-
118-
}
119-
}
120-
}
103+
onClick = onDeepLinkClick
121104
)
122105
)
123106

app/src/main/java/com/cornellappdev/transit/ui/components/home/GymDetailsContent.kt

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ fun GymDetailsContent(
4444
gym: UpliftGym,
4545
isFavorite: Boolean,
4646
onFavoriteClick: () -> Unit,
47+
onDeepLinkClick: () -> Unit,
4748
distanceString: String
4849
) {
4950
val isOpen = getOpenStatus(gym.operatingHours()).isOpen
@@ -102,31 +103,13 @@ fun GymDetailsContent(
102103
val (annotatedString, inlineContent) =
103104
stringResource(R.string.view_gym).createDeepLink(R.drawable.upliftlink)
104105

105-
val context = LocalContext.current
106-
107106
Text(
108107
text = annotatedString,
109108
inlineContent = inlineContent,
110109
style = Style.heading2,
111110
color = TransitBlue,
112111
modifier = Modifier.clickable(
113-
onClick = {
114-
val upliftPackage = "com.cornellappdev.uplift"
115-
val upliftIntent = context.packageManager.getLaunchIntentForPackage(upliftPackage)
116-
117-
if (upliftIntent != null ) {
118-
context.startActivity(upliftIntent)
119-
} else {
120-
try {
121-
val playStoreIntent = Intent(Intent.ACTION_VIEW, "market://details?id=$upliftPackage".toUri())
122-
playStoreIntent.setPackage("com.android.vending")
123-
context.startActivity(playStoreIntent)
124-
} catch (e: ActivityNotFoundException) {
125-
context.startActivity(Intent(Intent.ACTION_VIEW, "https://play.google.com/store/apps/details?id=$upliftPackage}".toUri()))
126-
127-
}
128-
}
129-
}
112+
onClick = onDeepLinkClick
130113
)
131114
)
132115

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.cornellappdev.transit.util
2+
3+
import android.content.ActivityNotFoundException
4+
import android.content.Context
5+
import android.content.Intent
6+
import androidx.core.net.toUri
7+
8+
object IntentUtils {
9+
fun Context.openDeepLink(packageName: String) {
10+
val launchIntent = packageManager.getLaunchIntentForPackage(packageName)
11+
12+
if (launchIntent != null) {
13+
startActivity(launchIntent)
14+
} else {
15+
try {
16+
val playStoreIntent = Intent(Intent.ACTION_VIEW, "market://details?id=$packageName".toUri())
17+
playStoreIntent.setPackage("com.android.vending")
18+
startActivity(playStoreIntent)
19+
} catch (e2: ActivityNotFoundException) {
20+
startActivity(Intent(Intent.ACTION_VIEW, "https://play.google.com/store/apps/details?id=$packageName}".toUri()))
21+
22+
}
23+
}
24+
}
25+
}

uplift-android

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 4e814635671da30990bea4d07ac603510ca29dbc

0 commit comments

Comments
 (0)