Skip to content

Commit

Permalink
finished activation
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed Nov 22, 2023
1 parent 545d4e7 commit ae37fea
Show file tree
Hide file tree
Showing 19 changed files with 287 additions and 175 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import androidx.lifecycle.LifecycleOwner
import androidx.multidex.MultiDexApplication
import dev.datlag.burningseries.model.common.systemProperty
import dev.datlag.burningseries.module.NetworkModule
import dev.datlag.burningseries.network.state.NetworkStateSaver
import dev.datlag.burningseries.other.StateSaver
import dev.datlag.sekret.NativeLoader
import io.github.aakira.napier.DebugAntilog
import io.github.aakira.napier.Napier
import kotlinx.coroutines.runBlocking
import org.kodein.di.DI
import org.kodein.di.DIAware
import org.kodein.di.bindSingleton
Expand Down Expand Up @@ -42,5 +44,8 @@ class App : MultiDexApplication(), DIAware, DefaultLifecycleObserver {
defaultAllowRestrictedHeaders?.ifBlank { null }?.let {
systemProperty("sun.net.http.allowRestrictedHeaders", it)
}
runBlocking {
NetworkStateSaver.firebaseUser?.delete()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.arkivanov.essenty.lifecycle.essentyLifecycle
import com.google.android.gms.cast.framework.CastContext
import dev.datlag.burningseries.common.lifecycle.LocalLifecycleOwner
import dev.datlag.burningseries.common.lifecycle.collectAsStateWithLifecycle
import dev.datlag.burningseries.network.state.NetworkStateSaver
import dev.datlag.burningseries.ui.KeyEventDispatcher
import dev.datlag.burningseries.ui.PIPActions
import dev.datlag.burningseries.ui.PIPEventDispatcher
Expand All @@ -35,6 +36,8 @@ import io.kamel.image.config.LocalKamelConfig
import io.kamel.image.config.resourcesFetcher
import io.kamel.image.config.resourcesIdMapper
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.runBlocking
import org.kodein.di.instance
import java.util.concurrent.Executors

class MainActivity : AppCompatActivity() {
Expand Down
37 changes: 25 additions & 12 deletions app/desktop/src/jvmMain/kotlin/dev/datlag/burningseries/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import dev.datlag.burningseries.common.lifecycle.LocalLifecycleOwner
import dev.datlag.burningseries.model.common.systemProperty
import dev.datlag.burningseries.module.NetworkModule
import dev.datlag.burningseries.SharedRes
import dev.datlag.burningseries.network.state.NetworkStateSaver
import dev.datlag.burningseries.other.StateSaver
import dev.datlag.burningseries.ui.navigation.NavHostComponent
import dev.datlag.burningseries.window.disposableSingleWindowApplication
Expand All @@ -36,19 +37,11 @@ import io.kamel.image.config.Default
import io.kamel.image.config.LocalKamelConfig
import io.kamel.image.config.resourcesFetcher
import io.kamel.image.config.svgDecoder
import kotlinx.coroutines.runBlocking
import org.kodein.di.DI
import java.io.File

fun main(vararg args: String) {
runWindow()
}

@OptIn(ExperimentalDecomposeApi::class)
private fun runWindow() {
val appTitle = StringDesc.Resource(SharedRes.strings.app_name).localized()
AppIO.applyTitle(appTitle)
Napier.base(DebugAntilog())

StateSaver.sekretLibraryLoaded = NativeLoader.loadLibrary("sekret", systemProperty("compose.application.resources.dir")?.let { File(it) })

FirebasePlatform.initializeFirebasePlatform(object : FirebasePlatform() {
Expand All @@ -59,15 +52,35 @@ private fun runWindow() {
override fun log(msg: String) = println(msg)
})

val di = DI {
import(NetworkModule.di)
}

Runtime.getRuntime().addShutdownHook(Thread {
runBlocking {
NetworkStateSaver.firebaseUser?.delete()
}
})

runWindow(di)

runBlocking {
NetworkStateSaver.firebaseUser?.delete()
}
}

@OptIn(ExperimentalDecomposeApi::class)
private fun runWindow(di: DI) {
val appTitle = StringDesc.Resource(SharedRes.strings.app_name).localized()
AppIO.applyTitle(appTitle)
Napier.base(DebugAntilog())

val windowState = WindowState()
val lifecycle = LifecycleRegistry()
val lifecycleOwner = object : LifecycleOwner {
override val lifecycle: Lifecycle = lifecycle
}
val backDispatcher = BackDispatcher()
val di = DI {
import(NetworkModule.di)
}
val root = NavHostComponent(
componentContext = DefaultComponentContext(
lifecycle,
Expand Down
3 changes: 2 additions & 1 deletion app/shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ kotlin {

api(libs.windowsize.multiplatform)
api(libs.insetsx)
api(libs.webview)

api(libs.ktor)
api(libs.ktor.content.negotiation)
Expand Down Expand Up @@ -98,6 +97,7 @@ kotlin {
api(libs.cast)
api(libs.cast.framework)
api(libs.accompanist.uicontroller)
api(libs.webview.android)
}
}
val desktopMain by getting {
Expand All @@ -111,6 +111,7 @@ kotlin {
api(libs.ktor.jvm)
api(libs.appdirs)
api(libs.vlcj)
api(libs.webview.desktop)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,55 @@
package dev.datlag.burningseries.ui.screen.initial.series.activate.component

import androidx.compose.foundation.layout.Box
import android.annotation.SuppressLint
import android.webkit.WebView
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import com.multiplatform.webview.web.rememberWebViewNavigator
import com.multiplatform.webview.web.rememberWebViewState
import com.google.accompanist.web.rememberWebViewNavigator
import com.google.accompanist.web.rememberWebViewState
import dev.datlag.burningseries.SharedRes
import dev.datlag.burningseries.common.withIOContext
import dev.datlag.burningseries.common.withMainContext
import dev.datlag.burningseries.model.BSUtil
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive

@SuppressLint("SetJavaScriptEnabled")
@Composable
actual fun WebView(url: String, modifier: Modifier, onScraped: (String) -> Unit) {
val state = rememberWebViewState(url)
val navigator = rememberWebViewNavigator()
var webView = remember<WebView?> { null }
val scrapingJs = SharedRes.assets.scrape_hoster_android.readText(LocalContext.current)

com.multiplatform.webview.web.WebView(

com.google.accompanist.web.WebView(
state = state,
modifier = modifier,
navigator = navigator
captureBackPresses = true,
client = WebViewClient(
allowedHosts = setOf(BSUtil.HOST_BS_TO)
),
onCreated = {
it.settings.allowFileAccess = false
it.settings.javaScriptEnabled = true
it.settings.javaScriptCanOpenWindowsAutomatically = false
it.settings.mediaPlaybackRequiresUserGesture = true
webView = it
}
)

LaunchedEffect(navigator) {
LaunchedEffect(webView) {
withIOContext {
do {
delay(3000)
withMainContext {
navigator.evaluateJavaScript(scrapingJs) {
onScraped(it)
webView?.evaluateJavascript(scrapingJs) { result ->
result?.let(onScraped)
}
}
} while (isActive)
} while (isActive && webView != null)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package dev.datlag.burningseries.ui.screen.initial.series.activate.component

import android.webkit.WebResourceRequest
import android.webkit.WebResourceResponse
import android.webkit.WebView
import com.google.accompanist.web.AccompanistWebViewClient

data class WebViewClient(
private val allowedHosts: Set<String> = setOf()
) : AccompanistWebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
return if (request?.url?.host != null) {
!allowedHosts.contains(request.url.host)
} else {
super.shouldOverrideUrlLoading(view, request)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import dev.datlag.burningseries.model.Stream
import dev.datlag.burningseries.ui.navigation.Component
import dev.datlag.burningseries.ui.navigation.DialogComponent
import dev.datlag.burningseries.ui.screen.initial.series.activate.component.DialogConfig
import kotlinx.coroutines.flow.StateFlow

interface ActivateComponent : Component {

val onDeviceReachable: Boolean
val episode: Series.Episode
val isSaving: StateFlow<Boolean>

val dialog: Value<ChildSlot<DialogConfig, DialogComponent>>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ package dev.datlag.burningseries.ui.screen.initial.series.activate
import androidx.compose.foundation.layout.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBackIosNew
import androidx.compose.material.icons.filled.Save
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.arkivanov.decompose.extensions.compose.jetbrains.subscribeAsState
import dev.icerock.moko.resources.compose.stringResource
import dev.datlag.burningseries.SharedRes
import dev.datlag.burningseries.common.lifecycle.collectAsStateWithLifecycle
import dev.datlag.burningseries.model.BSUtil
import dev.datlag.burningseries.ui.custom.state.UnreachableState
import dev.datlag.burningseries.ui.screen.initial.series.activate.component.WebView
Expand All @@ -36,6 +39,21 @@ fun ActivateScreen(component: ActivateComponent) {
contentDescription = stringResource(SharedRes.strings.back)
)
}
},
actions = {
Box(
contentAlignment = Alignment.Center
) {
val isSaving by component.isSaving.collectAsStateWithLifecycle()

if (isSaving) {
Icon(
imageVector = Icons.Default.Save,
contentDescription = stringResource(SharedRes.strings.saving)
)
CircularProgressIndicator()
}
}
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import dev.datlag.burningseries.ui.screen.initial.series.activate.component.Dial
import dev.datlag.burningseries.ui.screen.initial.series.activate.dialog.error.ErrorDialogComponent
import dev.datlag.burningseries.ui.screen.initial.series.activate.dialog.success.SuccessDialogComponent
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import kotlinx.serialization.json.Json
import org.kodein.di.instance
Expand All @@ -45,6 +46,8 @@ class ActivateScreenComponent(
private val json by di.instance<Json>()
private val savedData: MutableSet<String> = mutableSetOf()

override val isSaving = saveState.map { it is SaveState.Saving }.stateIn(ioScope(), SharingStarted.Lazily, saveState.value is SaveState.Saving)

private val dialogNavigation = SlotNavigation<DialogConfig>()
override val dialog = childSlot(
source = dialogNavigation
Expand Down
1 change: 1 addition & 0 deletions app/shared/src/commonMain/resources/MR/base/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@
<string name="exit_fullscreen">Exit Fullscreen</string>
<string name="episode_progress">%s - %s</string>
<string name="activate_unreachable">Unable to activate with your current network as the website is unreachable.</string>
<string name="saving">Saving</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.awt.ComposePanel
import androidx.compose.ui.awt.SwingPanel
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.multiplatform.webview.web.WebContent
import dev.datlag.burningseries.SharedRes
import dev.datlag.burningseries.common.withIOContext
import dev.datlag.burningseries.common.withMainContext
Expand Down
5 changes: 3 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ grpc = "1.59.0"
instesx = "0.1.0-alpha10"
jsunpacker = "1.0.2"
kamel = "0.8.3"
kcef = "2023.10.13"
kodein = "7.21.0"
kotlin = "1.9.20"
ksp = "1.9.20-1.0.14"
Expand All @@ -46,7 +47,6 @@ sqldelight = "2.0.0"
turbine = "1.0.0"
versions = "0.50.0"
vlcj = "4.8.2"
webview = "1.7.2"
windowsize-multiplatform = "0.3.1"
window-styler = "0.3.2"

Expand Down Expand Up @@ -124,7 +124,8 @@ sqldelight-native = { group = "app.cash.sqldelight", name = "native-driver", ver
turbine = { group = "app.cash.turbine", name = "turbine", version.ref = "turbine" }
test = { group = "org.jetbrains.kotlin", name = "kotlin-test", version.ref = "kotlin" }
vlcj = { group = "uk.co.caprica", name = "vlcj", version.ref = "vlcj" }
webview = { group = "io.github.kevinnzou", name = "compose-webview-multiplatform", version.ref = "webview" }
webview-android = { group = "com.google.accompanist", name = "accompanist-webview", version.ref = "accompanist" }
webview-desktop = { group = "dev.datlag", name = "kcef", version.ref = "kcef" }
windowsize-multiplatform = { group = "dev.chrisbanes.material3", name = "material3-window-size-class-multiplatform", version.ref = "windowsize-multiplatform" }
window-styler = { group = "com.mayakapps.compose", name = "window-styler", version.ref = "window-styler" }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.datlag.burningseries.network.firebase

import dev.datlag.burningseries.model.HosterScraping
import dev.datlag.burningseries.model.common.suspendCatching
import dev.datlag.burningseries.network.Firestore
import dev.gitlive.firebase.auth.FirebaseUser
import dev.gitlive.firebase.firestore.DocumentReference
Expand All @@ -13,8 +14,12 @@ actual object FireStore {
firestoreApi: Firestore,
idList: List<String>
): List<String> {
return firestore.collection("stream").where("id", inArray = idList).get().documents.map {
it.get<String>("url")
return (suspendCatching {
firestore.collection("stream").where("id", inArray = idList).get().documents.map {
it.get<String>("url")
}
}.getOrNull() ?: emptyList()).ifEmpty {
RESTFireStore.getStreams(firestore, firestoreApi, idList)
}
}

Expand All @@ -25,20 +30,22 @@ actual object FireStore {
data: HosterScraping.Firestore
): Boolean {
if (firestore == null) {
return false
return RESTFireStore.addStream(firebaseUser, firestore, firestoreApi, data)
}

val existing = firestore
.collection("stream")
.where("id", equalTo = data.id)
.get()
.documents
.firstOrNull()
?.reference
?: firestore.collection("stream").document
return suspendCatching {
val existing = firestore
.collection("stream")
.where("id", equalTo = data.id)
.get()
.documents
.firstOrNull()
?.reference
?: firestore.collection("stream").document

return firestore.runTransaction {
set(documentRef = existing, data = data, merge = true)
}.get(existing).exists
firestore.runTransaction {
set(documentRef = existing, data = data, merge = true)
}
}.isSuccess || RESTFireStore.addStream(firebaseUser, firestore, firestoreApi, data)
}
}
Loading

0 comments on commit ae37fea

Please sign in to comment.