Skip to content

Commit a12a39a

Browse files
committed
feat(network): make it able to open social links without protocol from friend profiles
1 parent c6c24a4 commit a12a39a

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

app/src/main/java/friendly/android/ProfileScreen.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ import androidx.compose.ui.platform.LocalContext
5656
import androidx.compose.ui.res.painterResource
5757
import androidx.compose.ui.res.stringResource
5858
import androidx.compose.ui.unit.dp
59-
import androidx.core.net.toUri
6059
import friendly.android.ProfileScreenViewModel.UserProfile
6160
import friendly.sdk.Nickname
6261
import friendly.sdk.SocialLink
@@ -383,7 +382,7 @@ private fun copyToClipboardIn(
383382
}
384383

385384
private fun openSocialLink(context: Context, socialLink: SocialLink) {
386-
val intent = Intent(Intent.ACTION_VIEW, socialLink.string.toUri())
385+
val intent = Intent(Intent.ACTION_VIEW, socialLink.toNormalizedUri())
387386
context.startActivity(intent)
388387
}
389388

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package friendly.android
2+
3+
import android.net.Uri
4+
import android.util.Patterns
5+
import androidx.core.net.toUri
6+
import friendly.sdk.SocialLink
7+
8+
/**
9+
* Automatically uses https protocol if no one was specified.
10+
*
11+
* @return A valid [Uri] if [SocialLink] is a valid URI or null in it is
12+
* invalid.
13+
*/
14+
fun SocialLink.toNormalizedUri(): Uri? {
15+
val string = this.string
16+
17+
if (!Patterns.WEB_URL.matcher(string).matches()) return null
18+
19+
val startsWithHttp = string.startsWith("http://", ignoreCase = true)
20+
val startsWithHttps = string.startsWith("https://", ignoreCase = true)
21+
if (startsWithHttp || startsWithHttps) return string.toUri()
22+
23+
return "https://$string".toUri()
24+
}

0 commit comments

Comments
 (0)