Skip to content

Commit

Permalink
support instant apps
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed May 11, 2024
1 parent 679bf7e commit 6fc42fa
Show file tree
Hide file tree
Showing 17 changed files with 232 additions and 282 deletions.
1 change: 1 addition & 0 deletions composeApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ kotlin {
implementation(libs.android.credentials.play.services)

implementation(libs.translate)
implementation(libs.instantapps)
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion composeApp/src/androidMain/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
xmlns:tools="http://schemas.android.com/tools"
xmlns:dist="http://schemas.android.com/apk/distribution"
android:targetSandboxVersion="2">

<dist:module dist:instant="true" />

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
Expand Down Expand Up @@ -29,6 +33,7 @@
android:icon="@android:drawable/ic_menu_compass"
android:label="@string/app_name"
android:usesCleartextTraffic="false"
android:networkSecurityConfig="@xml/network_security"
android:hardwareAccelerated="true"
android:enableOnBackInvokedCallback="true"
android:appCategory="news"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package dev.datlag.aniflow.other

import android.content.Context
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import com.google.android.gms.instantapps.InstantApps
import dev.datlag.aniflow.findActivity

actual class InstantAppHelper(private val context: Context) {

actual val isInstantApp: Boolean
get() = InstantApps.getPackageManagerCompat(context).isInstantApp

actual fun showInstallPrompt() {
context.findActivity()?.let {
InstantApps.showInstallPrompt(it, null, 1337, null)
}
}
}

@Composable
actual fun rememberInstantAppHelper(): InstantAppHelper {
val context = LocalContext.current

return remember(context) { InstantAppHelper(context) }
}
19 changes: 19 additions & 0 deletions composeApp/src/androidMain/res/xml/network_security.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="false">
<trust-anchors>
<certificates src="system" />
</trust-anchors>
</base-config>
<domain-config cleartextTrafficPermitted="false">
<domain includeSubdomains="true">anilist.co</domain>
<domain includeSubdomains="true">googleapis.com</domain>
<domain includeSubdomains="true">trace.moe</domain>
<domain includeSubdomains="true">nekosapi.com</domain>

<trust-anchors>
<certificates src="system" />
<certificates src="user" />
</trust-anchors>
</domain-config>
</network-security-config>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package dev.datlag.aniflow.other

import androidx.compose.runtime.Composable

expect class InstantAppHelper {
val isInstantApp: Boolean

fun showInstallPrompt()
}

@Composable
expect fun rememberInstantAppHelper(): InstantAppHelper
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ data object StateSaver {
mediumOverview[id] = index to offset
}

var settingsOverview: Int = 0
var settingsOverviewOffset: Int = 0

data object Home {
var airingOverview: Int = 0
var airingOverviewOffset: Int = 0
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package dev.datlag.aniflow.ui.custom

import androidx.compose.runtime.Composable
import dev.datlag.aniflow.other.InstantAppHelper
import dev.datlag.aniflow.other.rememberInstantAppHelper

@Composable
fun InstantAppContent(
onInstantApp: @Composable (InstantAppHelper) -> Unit = {},
content: @Composable () -> Unit
) {
val helper = rememberInstantAppHelper()

if (helper.isInstantApp) {
onInstantApp(helper)
} else {
content()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import dev.datlag.aniflow.LocalHaze
import dev.datlag.aniflow.SharedRes
import dev.datlag.aniflow.anilist.model.User
import dev.datlag.aniflow.anilist.type.MediaType
import dev.datlag.aniflow.other.rememberInstantAppHelper
import dev.datlag.tooling.compose.ifFalse
import dev.datlag.tooling.decompose.lifecycle.collectAsStateWithLifecycle
import dev.icerock.moko.resources.compose.fontFamilyResource
Expand Down Expand Up @@ -76,17 +77,23 @@ fun CollapsingToolbar(

LargeTopAppBar(
navigationIcon = {
val helper = rememberInstantAppHelper()

IconButton(
modifier = Modifier.ifFalse(isCollapsed) {
background(MaterialTheme.colorScheme.surface.copy(alpha = 0.75F), CircleShape)
},
onClick = {
onProfileClick()
if (helper.isInstantApp) {
helper.showInstallPrompt()
} else {
onProfileClick()
}
}
) {
val user by userFlow.collectAsStateWithLifecycle(null)
val tintColor = LocalContentColor.current
var colorFilter by remember(user) { mutableStateOf<ColorFilter?>(ColorFilter.tint(tintColor)) }
var colorFilter by remember(user) { mutableStateOf<ColorFilter?>(null) }

AsyncImage(
model = user?.avatar?.large,
Expand Down
Loading

0 comments on commit 6fc42fa

Please sign in to comment.