-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
397 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
...src/androidMain/kotlin/dev/datlag/burningseries/common/PlatformExtendCoroutine.android.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package dev.datlag.burningseries.common | ||
|
||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.MainCoroutineDispatcher | ||
|
||
actual val Dispatchers.DeviceMain: MainCoroutineDispatcher | ||
get() = Main | ||
|
||
actual val Dispatchers.DeviceIO: CoroutineDispatcher | ||
get() = IO | ||
|
||
actual val Dispatchers.DeviceDefault : CoroutineDispatcher | ||
get() = Default |
9 changes: 9 additions & 0 deletions
9
app/shared/src/androidMain/kotlin/dev/datlag/burningseries/module/PlatformModule.android.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package dev.datlag.burningseries.module | ||
|
||
import org.kodein.di.DI | ||
|
||
actual object PlatformModule { | ||
actual val di: DI.Module | ||
get() = TODO("Not yet implemented") | ||
|
||
} |
113 changes: 113 additions & 0 deletions
113
app/shared/src/commonMain/kotlin/dev/datlag/burningseries/common/ExtendCoroutine.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package dev.datlag.burningseries.common | ||
|
||
import com.arkivanov.essenty.lifecycle.Lifecycle | ||
import com.arkivanov.essenty.lifecycle.LifecycleOwner | ||
import com.arkivanov.essenty.lifecycle.doOnDestroy | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.MainCoroutineDispatcher | ||
import kotlinx.coroutines.SupervisorJob | ||
import kotlinx.coroutines.cancel | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.runBlocking | ||
import kotlinx.coroutines.withContext | ||
import kotlin.coroutines.CoroutineContext | ||
|
||
fun <T> MutableStateFlow<T>.safeEmit(value: T, scope: CoroutineScope) { | ||
if (!this.tryEmit(value)) { | ||
scope.launch(ioDispatcher()) { | ||
this@safeEmit.emit(value) | ||
} | ||
} | ||
} | ||
|
||
fun CoroutineScope(context: CoroutineContext, lifecycle: Lifecycle): CoroutineScope { | ||
val scope = CoroutineScope(context) | ||
lifecycle.doOnDestroy(scope::cancel) | ||
return scope | ||
} | ||
|
||
fun LifecycleOwner.coroutineScope(context: CoroutineContext): CoroutineScope = CoroutineScope(context, lifecycle) | ||
|
||
fun LifecycleOwner.ioScope() = CoroutineScope(ioDispatcher() + SupervisorJob(), lifecycle) | ||
fun LifecycleOwner.mainScope() = CoroutineScope(mainDispatcher() + SupervisorJob(), lifecycle) | ||
fun LifecycleOwner.defaultScope() = CoroutineScope(defaultDispatcher() + SupervisorJob(), lifecycle) | ||
|
||
fun mainDispatcher(): MainCoroutineDispatcher = Dispatchers.DeviceMain | ||
fun ioDispatcher(): CoroutineDispatcher = Dispatchers.DeviceIO | ||
fun defaultDispatcher(): CoroutineDispatcher = Dispatchers.DeviceDefault | ||
|
||
fun CoroutineScope.launchIO(block: suspend CoroutineScope.() -> Unit): Job { | ||
return this.launch(ioDispatcher()) { | ||
block() | ||
} | ||
} | ||
|
||
fun CoroutineScope.launchMain(block: suspend CoroutineScope.() -> Unit): Job { | ||
return this.launch(mainDispatcher()) { | ||
block() | ||
} | ||
} | ||
|
||
fun CoroutineScope.launchDefault(block: suspend CoroutineScope.() -> Unit): Job { | ||
return this.launch(defaultDispatcher()) { | ||
block() | ||
} | ||
} | ||
|
||
fun LifecycleOwner.launchIO(block: suspend CoroutineScope.() -> Unit): Job { | ||
return ioScope().launchIO(block) | ||
} | ||
|
||
fun LifecycleOwner.launchMain(block: suspend CoroutineScope.() -> Unit): Job { | ||
return mainScope().launchMain(block) | ||
} | ||
|
||
fun LifecycleOwner.launchDefault(block: suspend CoroutineScope.() -> Unit): Job { | ||
return defaultScope().launchDefault(block) | ||
} | ||
|
||
suspend fun <T> withIOContext( | ||
block: suspend CoroutineScope.() -> T | ||
): T { | ||
return withContext(ioDispatcher()) { | ||
block() | ||
} | ||
} | ||
|
||
suspend fun <T> withMainContext( | ||
block: suspend CoroutineScope.() -> T | ||
): T { | ||
return withContext(mainDispatcher()) { | ||
block() | ||
} | ||
} | ||
|
||
suspend fun <T> withDefaultContext( | ||
block: suspend CoroutineScope.() -> T | ||
): T { | ||
return withContext(defaultDispatcher()) { | ||
block() | ||
} | ||
} | ||
|
||
fun <T> runBlockingIO(block: suspend CoroutineScope.() -> T): T { | ||
return runBlocking(ioDispatcher()) { | ||
block() | ||
} | ||
} | ||
|
||
fun <T> runBlockingMain(block: suspend CoroutineScope.() -> T): T { | ||
return runBlocking(mainDispatcher()) { | ||
block() | ||
} | ||
} | ||
|
||
fun <T> runBlockingDefault(block: suspend CoroutineScope.() -> T): T { | ||
return runBlocking(defaultDispatcher()) { | ||
block() | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
app/shared/src/commonMain/kotlin/dev/datlag/burningseries/common/PlatformExtendCoroutine.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package dev.datlag.burningseries.common | ||
|
||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.MainCoroutineDispatcher | ||
|
||
expect val Dispatchers.DeviceMain: MainCoroutineDispatcher | ||
expect val Dispatchers.DeviceIO: CoroutineDispatcher | ||
expect val Dispatchers.DeviceDefault: CoroutineDispatcher |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,6 @@ object NetworkModule { | |
const val NAME = "NetworkModule" | ||
|
||
val di = DI.Module(NAME) { | ||
|
||
import(PlatformModule.di) | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
app/shared/src/commonMain/kotlin/dev/datlag/burningseries/module/PlatformModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package dev.datlag.burningseries.module | ||
|
||
import org.kodein.di.DI | ||
|
||
expect object PlatformModule { | ||
|
||
val di: DI.Module | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
app/shared/src/desktopMain/kotlin/dev/datlag/burningseries/AppIO.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
...src/desktopMain/kotlin/dev/datlag/burningseries/common/PlatformExtendCoroutine.desktop.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package dev.datlag.burningseries.common | ||
|
||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.MainCoroutineDispatcher | ||
|
||
actual val Dispatchers.DeviceMain: MainCoroutineDispatcher | ||
get() = Main | ||
|
||
actual val Dispatchers.DeviceIO: CoroutineDispatcher | ||
get() = IO | ||
|
||
actual val Dispatchers.DeviceDefault : CoroutineDispatcher | ||
get() = Default |
24 changes: 24 additions & 0 deletions
24
app/shared/src/desktopMain/kotlin/dev/datlag/burningseries/module/PlatformModule.desktop.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package dev.datlag.burningseries.module | ||
|
||
import io.ktor.client.* | ||
import io.ktor.client.engine.okhttp.* | ||
import org.kodein.di.DI | ||
import org.kodein.di.bindSingleton | ||
|
||
actual object PlatformModule { | ||
|
||
private const val NAME = "PlatformModuleDesktop" | ||
|
||
actual val di: DI.Module = DI.Module(NAME) { | ||
bindSingleton { | ||
HttpClient(OkHttp) { | ||
engine { | ||
config { | ||
followRedirects(true) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
app/shared/src/iosMain/kotlin/dev/datlag/burningseries/common/PlatformExtendCoroutine.ios.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package dev.datlag.burningseries.common | ||
|
||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.IO | ||
import kotlinx.coroutines.MainCoroutineDispatcher | ||
|
||
actual val Dispatchers.DeviceMain: MainCoroutineDispatcher | ||
get() = Main | ||
|
||
actual val Dispatchers.DeviceIO: CoroutineDispatcher | ||
get() = IO | ||
|
||
actual val Dispatchers.DeviceDefault : CoroutineDispatcher | ||
get() = Default |
9 changes: 9 additions & 0 deletions
9
app/shared/src/iosMain/kotlin/dev/datlag/burningseries/module/PlatformModule.ios.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package dev.datlag.burningseries.module | ||
|
||
import org.kodein.di.DI | ||
|
||
actual object PlatformModule { | ||
actual val di: DI.Module | ||
get() = TODO("Not yet implemented") | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
model/src/commonMain/kotlin/dev/datlag/burningseries/model/Constants.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package dev.datlag.burningseries.model | ||
|
||
data object Constants { | ||
|
||
data object BurningSeries { | ||
const val PROTOCOL_HTTP = "http://" | ||
const val PROTOCOL_HTTPS = "https://" | ||
|
||
const val HOST_BS_TO = "bs.to" | ||
|
||
val episodeNumberRegex = "[|({]\\s*Ep([.]|isode)?\\s*(\\d+)\\s*[|)}]".toRegex(RegexOption.IGNORE_CASE) | ||
|
||
fun getBurningSeriesLink(href: String, http: Boolean = false, host: String = HOST_BS_TO): String { | ||
return if (!href.matches("^\\w+?://.*".toRegex())) { | ||
if (!href.startsWith("/")) { | ||
"${if (http) PROTOCOL_HTTP else PROTOCOL_HTTPS}${host}/$href" | ||
} else { | ||
"${if (http) PROTOCOL_HTTP else PROTOCOL_HTTPS}${host}${"(?!:|/{2,})(/.*)".toRegex().find(href)?.value}" | ||
} | ||
} else { | ||
href | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.