From 53ffcd02285786f811bea12f8b2bbe5a9838f2bb Mon Sep 17 00:00:00 2001
From: CranberrySoup <142951702+CranberrySoup@users.noreply.github.com>
Date: Thu, 21 Mar 2024 20:08:08 +0100
Subject: [PATCH 1/4] First steps for multiplatform api
---
.idea/gradle.xml | 7 +--
app/build.gradle.kts | 2 +
.../com/lagradost/cloudstream3/MainAPI.kt | 2 -
.../lagradost/cloudstream3/mvvm/Lifecycle.kt | 16 +++++
build.gradle.kts | 6 +-
library/build.gradle.kts | 58 +++++++++++++++++++
library/src/androidMain/AndroidManifest.xml | 2 +
.../kotlin/com/lagradost/api/BuildConfig.kt | 7 +++
.../kotlin/com/lagradost/api/Log.kt | 21 +++++++
.../kotlin/com/lagradost/api/BuildConfig.kt | 7 +++
.../kotlin/com/lagradost/api/Log.kt | 8 +++
.../com/lagradost/cloudstream3/MainApi.kt | 3 +
.../cloudstream3/mvvm/ArchComponentExt.kt | 35 ++++-------
.../kotlin/com/lagradost/api/BuildConfig.kt | 7 +++
.../jvmMain/kotlin/com/lagradost/api/Log.kt | 19 ++++++
settings.gradle.kts | 3 +-
16 files changed, 168 insertions(+), 35 deletions(-)
create mode 100644 app/src/main/java/com/lagradost/cloudstream3/mvvm/Lifecycle.kt
create mode 100644 library/build.gradle.kts
create mode 100644 library/src/androidMain/AndroidManifest.xml
create mode 100644 library/src/androidMain/kotlin/com/lagradost/api/BuildConfig.kt
create mode 100644 library/src/androidMain/kotlin/com/lagradost/api/Log.kt
create mode 100644 library/src/commonMain/kotlin/com/lagradost/api/BuildConfig.kt
create mode 100644 library/src/commonMain/kotlin/com/lagradost/api/Log.kt
create mode 100644 library/src/commonMain/kotlin/com/lagradost/cloudstream3/MainApi.kt
rename {app/src/main/java => library/src/commonMain/kotlin}/com/lagradost/cloudstream3/mvvm/ArchComponentExt.kt (86%)
create mode 100644 library/src/jvmMain/kotlin/com/lagradost/api/BuildConfig.kt
create mode 100644 library/src/jvmMain/kotlin/com/lagradost/api/Log.kt
diff --git a/.idea/gradle.xml b/.idea/gradle.xml
index c5c0ff3b..d7c08c9c 100644
--- a/.idea/gradle.xml
+++ b/.idea/gradle.xml
@@ -4,17 +4,16 @@
diff --git a/app/build.gradle.kts b/app/build.gradle.kts
index c2ba2907..6fd97387 100644
--- a/app/build.gradle.kts
+++ b/app/build.gradle.kts
@@ -232,6 +232,8 @@ dependencies {
implementation("androidx.work:work-runtime:2.9.0")
implementation("androidx.work:work-runtime-ktx:2.9.0")
implementation("com.github.Blatzar:NiceHttp:0.4.11") // HTTP Lib
+
+ implementation(project(":library"))
}
tasks.register("androidSourcesJar", Jar::class) {
diff --git a/app/src/main/java/com/lagradost/cloudstream3/MainAPI.kt b/app/src/main/java/com/lagradost/cloudstream3/MainAPI.kt
index 273e267b..df49a516 100644
--- a/app/src/main/java/com/lagradost/cloudstream3/MainAPI.kt
+++ b/app/src/main/java/com/lagradost/cloudstream3/MainAPI.kt
@@ -743,8 +743,6 @@ fun base64Encode(array: ByteArray): String {
}
}
-class ErrorLoadingException(message: String? = null) : Exception(message)
-
fun MainAPI.fixUrlNull(url: String?): String? {
if (url.isNullOrEmpty()) {
return null
diff --git a/app/src/main/java/com/lagradost/cloudstream3/mvvm/Lifecycle.kt b/app/src/main/java/com/lagradost/cloudstream3/mvvm/Lifecycle.kt
new file mode 100644
index 00000000..3df5197c
--- /dev/null
+++ b/app/src/main/java/com/lagradost/cloudstream3/mvvm/Lifecycle.kt
@@ -0,0 +1,16 @@
+package com.lagradost.cloudstream3.mvvm
+
+import androidx.lifecycle.LifecycleOwner
+import androidx.lifecycle.LiveData
+
+/** NOTE: Only one observer at a time per value */
+fun LifecycleOwner.observe(liveData: LiveData, action: (t: T) -> Unit) {
+ liveData.removeObservers(this)
+ liveData.observe(this) { it?.let { t -> action(t) } }
+}
+
+/** NOTE: Only one observer at a time per value */
+fun LifecycleOwner.observeNullable(liveData: LiveData, action: (t: T) -> Unit) {
+ liveData.removeObservers(this)
+ liveData.observe(this) { action(it) }
+}
diff --git a/build.gradle.kts b/build.gradle.kts
index 06af44d0..8a48b427 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -26,6 +26,6 @@ plugins {
id("com.google.devtools.ksp") version "1.9.22-1.0.16" apply false
}
-tasks.register("clean") {
- delete(rootProject.layout.buildDirectory)
-}
+//tasks.register("clean") {
+// delete(rootProject.layout.buildDirectory)
+//}
diff --git a/library/build.gradle.kts b/library/build.gradle.kts
new file mode 100644
index 00000000..c66db5e1
--- /dev/null
+++ b/library/build.gradle.kts
@@ -0,0 +1,58 @@
+plugins {
+ kotlin("multiplatform")
+ id("maven-publish")
+ id("com.android.library")
+}
+
+kotlin {
+ androidTarget()
+ jvm()
+
+ sourceSets {
+ commonMain.dependencies {
+ implementation("com.github.Blatzar:NiceHttp:0.4.11") // HTTP Lib
+ implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.13.1") /* JSON Parser
+ ^ Don't Bump Jackson above 2.13.1 , Crashes on Android TV's and FireSticks that have Min API
+ Level 25 or Less. */
+ implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0")
+ }
+ }
+}
+
+repositories {
+ mavenLocal()
+ maven("https://jitpack.io")
+}
+
+tasks.withType {
+ kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8.toString()
+}
+
+android {
+ compileSdk = 34
+ sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
+
+ defaultConfig {
+ minSdk = 21
+ targetSdk = 33
+ }
+
+ // If this is the same com.lagradost.cloudstream3.R stops working
+ namespace = "com.lagradost.api"
+
+ compileOptions {
+ sourceCompatibility = JavaVersion.VERSION_1_8
+ targetCompatibility = JavaVersion.VERSION_1_8
+ }
+}
+
+//publishing {
+// publications {
+// create("maven") {
+// groupId = "com.lagradost"
+// artifactId = "api"
+// version = "0.1.0"
+// from(components["java"])
+// }
+// }
+//}
diff --git a/library/src/androidMain/AndroidManifest.xml b/library/src/androidMain/AndroidManifest.xml
new file mode 100644
index 00000000..568741e5
--- /dev/null
+++ b/library/src/androidMain/AndroidManifest.xml
@@ -0,0 +1,2 @@
+
+
\ No newline at end of file
diff --git a/library/src/androidMain/kotlin/com/lagradost/api/BuildConfig.kt b/library/src/androidMain/kotlin/com/lagradost/api/BuildConfig.kt
new file mode 100644
index 00000000..eda0eab5
--- /dev/null
+++ b/library/src/androidMain/kotlin/com/lagradost/api/BuildConfig.kt
@@ -0,0 +1,7 @@
+package com.lagradost.api
+
+actual class BuildConfig {
+ actual companion object {
+ actual val DEBUG = true
+ }
+}
\ No newline at end of file
diff --git a/library/src/androidMain/kotlin/com/lagradost/api/Log.kt b/library/src/androidMain/kotlin/com/lagradost/api/Log.kt
new file mode 100644
index 00000000..12524411
--- /dev/null
+++ b/library/src/androidMain/kotlin/com/lagradost/api/Log.kt
@@ -0,0 +1,21 @@
+package com.lagradost.api
+
+import android.util.Log
+
+actual object Log {
+ actual fun d(tag: String, message: String) {
+ Log.d(tag, message)
+ }
+
+ actual fun i(tag: String, message: String) {
+ Log.i(tag, message)
+ }
+
+ actual fun w(tag: String, message: String) {
+ Log.w(tag, message)
+ }
+
+ actual fun e(tag: String, message: String) {
+ Log.e(tag, message)
+ }
+}
\ No newline at end of file
diff --git a/library/src/commonMain/kotlin/com/lagradost/api/BuildConfig.kt b/library/src/commonMain/kotlin/com/lagradost/api/BuildConfig.kt
new file mode 100644
index 00000000..5f4b1d92
--- /dev/null
+++ b/library/src/commonMain/kotlin/com/lagradost/api/BuildConfig.kt
@@ -0,0 +1,7 @@
+package com.lagradost.api
+
+expect class BuildConfig {
+ companion object {
+ val DEBUG: Boolean
+ }
+}
\ No newline at end of file
diff --git a/library/src/commonMain/kotlin/com/lagradost/api/Log.kt b/library/src/commonMain/kotlin/com/lagradost/api/Log.kt
new file mode 100644
index 00000000..4b8e6329
--- /dev/null
+++ b/library/src/commonMain/kotlin/com/lagradost/api/Log.kt
@@ -0,0 +1,8 @@
+package com.lagradost.api
+
+expect object Log {
+ fun d(tag: String, message: String)
+ fun i(tag: String, message: String)
+ fun w(tag: String, message: String)
+ fun e(tag: String, message: String)
+}
\ No newline at end of file
diff --git a/library/src/commonMain/kotlin/com/lagradost/cloudstream3/MainApi.kt b/library/src/commonMain/kotlin/com/lagradost/cloudstream3/MainApi.kt
new file mode 100644
index 00000000..87ee4815
--- /dev/null
+++ b/library/src/commonMain/kotlin/com/lagradost/cloudstream3/MainApi.kt
@@ -0,0 +1,3 @@
+package com.lagradost.cloudstream3
+
+class ErrorLoadingException(message: String? = null) : Exception(message)
\ No newline at end of file
diff --git a/app/src/main/java/com/lagradost/cloudstream3/mvvm/ArchComponentExt.kt b/library/src/commonMain/kotlin/com/lagradost/cloudstream3/mvvm/ArchComponentExt.kt
similarity index 86%
rename from app/src/main/java/com/lagradost/cloudstream3/mvvm/ArchComponentExt.kt
rename to library/src/commonMain/kotlin/com/lagradost/cloudstream3/mvvm/ArchComponentExt.kt
index 817d7db3..d3b4999a 100644
--- a/app/src/main/java/com/lagradost/cloudstream3/mvvm/ArchComponentExt.kt
+++ b/library/src/commonMain/kotlin/com/lagradost/cloudstream3/mvvm/ArchComponentExt.kt
@@ -1,10 +1,7 @@
package com.lagradost.cloudstream3.mvvm
-import android.util.Log
-import androidx.lifecycle.LifecycleOwner
-import androidx.lifecycle.LiveData
-import com.bumptech.glide.load.HttpException
-import com.lagradost.cloudstream3.BuildConfig
+import com.lagradost.api.BuildConfig
+import com.lagradost.api.Log
import com.lagradost.cloudstream3.ErrorLoadingException
import kotlinx.coroutines.*
import java.io.InterruptedIOException
@@ -49,18 +46,6 @@ inline fun debugWarning(assert: () -> Boolean, message: () -> String) {
}
}
-/** NOTE: Only one observer at a time per value */
-fun LifecycleOwner.observe(liveData: LiveData, action: (t: T) -> Unit) {
- liveData.removeObservers(this)
- liveData.observe(this) { it?.let { t -> action(t) } }
-}
-
-/** NOTE: Only one observer at a time per value */
-fun LifecycleOwner.observeNullable(liveData: LiveData, action: (t: T) -> Unit) {
- liveData.removeObservers(this)
- liveData.observe(this) { action(it) }
-}
-
sealed class Resource {
data class Success(val value: T) : Resource()
data class Failure(
@@ -158,14 +143,14 @@ fun throwAbleToResource(
"Connection Timeout\nPlease try again later."
)
}
- is HttpException -> {
- Resource.Failure(
- false,
- throwable.statusCode,
- null,
- throwable.message ?: "HttpException"
- )
- }
+// is HttpException -> {
+// Resource.Failure(
+// false,
+// throwable.statusCode,
+// null,
+// throwable.message ?: "HttpException"
+// )
+// }
is UnknownHostException -> {
Resource.Failure(true, null, null, "Cannot connect to server, try again later.\n${throwable.message}")
}
diff --git a/library/src/jvmMain/kotlin/com/lagradost/api/BuildConfig.kt b/library/src/jvmMain/kotlin/com/lagradost/api/BuildConfig.kt
new file mode 100644
index 00000000..eda0eab5
--- /dev/null
+++ b/library/src/jvmMain/kotlin/com/lagradost/api/BuildConfig.kt
@@ -0,0 +1,7 @@
+package com.lagradost.api
+
+actual class BuildConfig {
+ actual companion object {
+ actual val DEBUG = true
+ }
+}
\ No newline at end of file
diff --git a/library/src/jvmMain/kotlin/com/lagradost/api/Log.kt b/library/src/jvmMain/kotlin/com/lagradost/api/Log.kt
new file mode 100644
index 00000000..e9a0e6b4
--- /dev/null
+++ b/library/src/jvmMain/kotlin/com/lagradost/api/Log.kt
@@ -0,0 +1,19 @@
+package com.lagradost.api
+
+actual object Log {
+ actual fun d(tag: String, message: String) {
+ println("DEBUG $tag: $message")
+ }
+
+ actual fun i(tag: String, message: String) {
+ println("INFO $tag: $message")
+ }
+
+ actual fun w(tag: String, message: String) {
+ println("WARNING $tag: $message")
+ }
+
+ actual fun e(tag: String, message: String) {
+ println("ERROR $tag: $message")
+ }
+}
\ No newline at end of file
diff --git a/settings.gradle.kts b/settings.gradle.kts
index 17070047..eabd9f0e 100644
--- a/settings.gradle.kts
+++ b/settings.gradle.kts
@@ -1,3 +1,4 @@
rootProject.name = "CloudStream"
-include(":app")
\ No newline at end of file
+include(":app")
+include(":library")
\ No newline at end of file
From 6940e0d2247e4b2b0e5fe58a9e4f032da144d98a Mon Sep 17 00:00:00 2001
From: CranberrySoup <142951702+CranberrySoup@users.noreply.github.com>
Date: Wed, 3 Apr 2024 01:26:07 +0200
Subject: [PATCH 2/4] Buildconfig testing
---
app/build.gradle.kts | 6 +++++-
build.gradle.kts | 2 ++
library/build.gradle.kts | 13 +++++++++++++
.../kotlin/com/lagradost/api/BuildConfig.kt | 7 -------
.../kotlin/com/lagradost/api/BuildConfig.kt | 7 -------
.../jvmMain/kotlin/com/lagradost/api/BuildConfig.kt | 7 -------
6 files changed, 20 insertions(+), 22 deletions(-)
delete mode 100644 library/src/androidMain/kotlin/com/lagradost/api/BuildConfig.kt
delete mode 100644 library/src/commonMain/kotlin/com/lagradost/api/BuildConfig.kt
delete mode 100644 library/src/jvmMain/kotlin/com/lagradost/api/BuildConfig.kt
diff --git a/app/build.gradle.kts b/app/build.gradle.kts
index 6fd97387..c0495f62 100644
--- a/app/build.gradle.kts
+++ b/app/build.gradle.kts
@@ -13,6 +13,7 @@ plugins {
val tmpFilePath = System.getProperty("user.home") + "/work/_temp/keystore/"
val prereleaseStoreFile: File? = File(tmpFilePath).listFiles()?.first()
+var isLibraryDebug = false
fun String.execute() = ByteArrayOutputStream().use { baot ->
if (project.exec {
@@ -103,6 +104,7 @@ android {
)
}
debug {
+ isLibraryDebug = true
isDebuggable = true
applicationIdSuffix = ".debug"
proguardFiles(
@@ -233,7 +235,9 @@ dependencies {
implementation("androidx.work:work-runtime-ktx:2.9.0")
implementation("com.github.Blatzar:NiceHttp:0.4.11") // HTTP Lib
- implementation(project(":library"))
+ implementation(project(":library") {
+ this.extra.set("isDebug", isLibraryDebug)
+ })
}
tasks.register("androidSourcesJar", Jar::class) {
diff --git a/build.gradle.kts b/build.gradle.kts
index 8a48b427..21291217 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -9,6 +9,8 @@ buildscript {
classpath("com.android.tools.build:gradle:8.2.1")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.22")
classpath("org.jetbrains.dokka:dokka-gradle-plugin:1.9.10")
+ // Universal build config
+ classpath("com.codingfeline.buildkonfig:buildkonfig-gradle-plugin:0.15.1")
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle.kts files
diff --git a/library/build.gradle.kts b/library/build.gradle.kts
index c66db5e1..a9d27ecd 100644
--- a/library/build.gradle.kts
+++ b/library/build.gradle.kts
@@ -1,7 +1,10 @@
+import com.codingfeline.buildkonfig.compiler.FieldSpec
+
plugins {
kotlin("multiplatform")
id("maven-publish")
id("com.android.library")
+ id("com.codingfeline.buildkonfig")
}
kotlin {
@@ -28,6 +31,16 @@ tasks.withType {
kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8.toString()
}
+buildkonfig {
+ packageName = "com.lagradost.api"
+ exposeObjectWithName = "BuildConfig"
+
+ defaultConfigs {
+ val isDebug = kotlin.runCatching { extra.get("isDebug") }.getOrNull() == true
+ buildConfigField(FieldSpec.Type.BOOLEAN, "DEBUG", isDebug.toString())
+ }
+}
+
android {
compileSdk = 34
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
diff --git a/library/src/androidMain/kotlin/com/lagradost/api/BuildConfig.kt b/library/src/androidMain/kotlin/com/lagradost/api/BuildConfig.kt
deleted file mode 100644
index eda0eab5..00000000
--- a/library/src/androidMain/kotlin/com/lagradost/api/BuildConfig.kt
+++ /dev/null
@@ -1,7 +0,0 @@
-package com.lagradost.api
-
-actual class BuildConfig {
- actual companion object {
- actual val DEBUG = true
- }
-}
\ No newline at end of file
diff --git a/library/src/commonMain/kotlin/com/lagradost/api/BuildConfig.kt b/library/src/commonMain/kotlin/com/lagradost/api/BuildConfig.kt
deleted file mode 100644
index 5f4b1d92..00000000
--- a/library/src/commonMain/kotlin/com/lagradost/api/BuildConfig.kt
+++ /dev/null
@@ -1,7 +0,0 @@
-package com.lagradost.api
-
-expect class BuildConfig {
- companion object {
- val DEBUG: Boolean
- }
-}
\ No newline at end of file
diff --git a/library/src/jvmMain/kotlin/com/lagradost/api/BuildConfig.kt b/library/src/jvmMain/kotlin/com/lagradost/api/BuildConfig.kt
deleted file mode 100644
index eda0eab5..00000000
--- a/library/src/jvmMain/kotlin/com/lagradost/api/BuildConfig.kt
+++ /dev/null
@@ -1,7 +0,0 @@
-package com.lagradost.api
-
-actual class BuildConfig {
- actual companion object {
- actual val DEBUG = true
- }
-}
\ No newline at end of file
From 20a61584dfa6dd0ff6920f4979e075bf6ac7a923 Mon Sep 17 00:00:00 2001
From: CranberrySoup <142951702+CranberrySoup@users.noreply.github.com>
Date: Thu, 4 Apr 2024 19:04:46 +0200
Subject: [PATCH 3/4] Fix publishing and classes.jar
---
app/build.gradle.kts | 28 ++++++++++++++++++++++------
library/build.gradle.kts | 19 ++++++++-----------
2 files changed, 30 insertions(+), 17 deletions(-)
diff --git a/app/build.gradle.kts b/app/build.gradle.kts
index c0495f62..3073c3d6 100644
--- a/app/build.gradle.kts
+++ b/app/build.gradle.kts
@@ -1,5 +1,6 @@
import com.android.build.gradle.internal.cxx.configure.gradleLocalProperties
import org.jetbrains.dokka.gradle.DokkaTask
+import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.util.archivesName
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import java.io.ByteArrayOutputStream
import java.net.URL
@@ -240,16 +241,31 @@ dependencies {
})
}
-tasks.register("androidSourcesJar", Jar::class) {
+tasks.register("androidSourcesJar") {
archiveClassifier.set("sources")
from(android.sourceSets.getByName("main").java.srcDirs) // Full Sources
}
-// For GradLew Plugin
-tasks.register("makeJar", Copy::class) {
- from("build/intermediates/compile_app_classes_jar/prereleaseDebug")
- into("build")
- include("classes.jar")
+tasks.register("copyJar") {
+ from(
+ "build/intermediates/compile_app_classes_jar/prereleaseDebug",
+ "../library/build/libs"
+ )
+ into("build/app-classes")
+ include("classes.jar", "library-jvm*.jar")
+ // Remove the version
+ rename("library-jvm.*.jar", "library-jvm.jar")
+}
+
+// Merge the app classes and the library classes into classes.jar
+tasks.register("makeJar") {
+ dependsOn(tasks.getByName("copyJar"))
+ from(
+ zipTree("build/app-classes/classes.jar"),
+ zipTree("build/app-classes/library-jvm.jar")
+ )
+ destinationDirectory.set(layout.buildDirectory)
+ archivesName = "classes"
}
tasks.withType {
diff --git a/library/build.gradle.kts b/library/build.gradle.kts
index a9d27ecd..42a8c943 100644
--- a/library/build.gradle.kts
+++ b/library/build.gradle.kts
@@ -8,6 +8,7 @@ plugins {
}
kotlin {
+ version = "1.0.0"
androidTarget()
jvm()
@@ -58,14 +59,10 @@ android {
targetCompatibility = JavaVersion.VERSION_1_8
}
}
-
-//publishing {
-// publications {
-// create("maven") {
-// groupId = "com.lagradost"
-// artifactId = "api"
-// version = "0.1.0"
-// from(components["java"])
-// }
-// }
-//}
+publishing {
+ publications {
+ withType {
+ groupId = "com.lagradost.api"
+ }
+ }
+}
\ No newline at end of file
From 8bea97947ff296c6bb1eddf3664e0b45ce093232 Mon Sep 17 00:00:00 2001
From: CranberrySoup <142951702+CranberrySoup@users.noreply.github.com>
Date: Thu, 4 Apr 2024 17:07:24 +0000
Subject: [PATCH 4/4] Update build.gradle.kts
---
build.gradle.kts | 3 ---
1 file changed, 3 deletions(-)
diff --git a/build.gradle.kts b/build.gradle.kts
index 2dd73e4d..ab1918fe 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -10,9 +10,6 @@ buildscript {
classpath("org.jetbrains.dokka:dokka-gradle-plugin:1.9.10")
// Universal build config
classpath("com.codingfeline.buildkonfig:buildkonfig-gradle-plugin:0.15.1")
-
- // NOTE: Do not place your application dependencies here; they belong
- // in the individual module build.gradle.kts files
}
}