Skip to content

Commit

Permalink
prepare mongodb access
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed Nov 15, 2023
1 parent e216a62 commit 7222f83
Show file tree
Hide file tree
Showing 12 changed files with 264 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
app/release/*
!app/release/.gitkeep

app/sekret.properties

*.iml
.gradle
/local.properties
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,60 @@
package dev.datlag.burningseries.module

import de.jensklingenberg.ktorfit.Ktorfit
import de.jensklingenberg.ktorfit.ktorfitBuilder
import dev.datlag.burningseries.common.launchIO
import dev.datlag.burningseries.network.JsonBase
import dev.datlag.burningseries.network.state.EpisodeStateMachine
import dev.datlag.burningseries.network.state.HomeStateMachine
import dev.datlag.burningseries.network.state.SearchStateMachine
import io.ktor.client.*
import io.realm.kotlin.mongodb.App
import io.realm.kotlin.mongodb.Credentials
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import org.kodein.di.DI
import org.kodein.di.bindSingleton
import org.kodein.di.instance

object NetworkModule {

private const val TAG_KTORFIT_JSONBASE = "JsonBaseKtorfit"
const val NAME = "NetworkModule"

@OptIn(DelicateCoroutinesApi::class)
val di = DI.Module(NAME) {
import(DatabaseModule.di)

bindSingleton {
ktorfitBuilder {
httpClient(instance<HttpClient>())
}
}
bindSingleton(TAG_KTORFIT_JSONBASE) {
val builder = instance<Ktorfit.Builder>()
builder.build {
baseUrl("https://jsonbase.com/")
}
}
bindSingleton {
val jsonBaseKtor: Ktorfit = instance(TAG_KTORFIT_JSONBASE)
jsonBaseKtor.create<JsonBase>()
}
bindSingleton {
HomeStateMachine(instance())
}
bindSingleton {
SearchStateMachine(instance())
}
bindSingleton {
EpisodeStateMachine(instance())
}
bindSingleton {
App.create("").also {
GlobalScope.launchIO {
it.login(Credentials.anonymous())
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dev.datlag.burningseries.ui.screen.initial.series

import com.arkivanov.decompose.router.slot.ChildSlot
import com.arkivanov.decompose.value.Value
import dev.datlag.burningseries.model.Series
import dev.datlag.burningseries.model.state.SeriesState
import dev.datlag.burningseries.ui.navigation.Component
import dev.datlag.burningseries.ui.navigation.DialogComponent
Expand All @@ -26,4 +27,5 @@ interface SeriesComponent : Component {
fun showDialog(config: DialogConfig)

fun toggleFavorite(): Any?
fun itemClicked(episode: Series.Episode): Any?
}
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,9 @@ private fun CompactScreen(component: SeriesComponent) {
)
}

SeriesContent(current.series)
SeriesContent(current.series) {
component.itemClicked(it)
}
}

DisposableEffect(state) {
Expand Down Expand Up @@ -392,7 +394,9 @@ private fun DefaultScreen(component: SeriesComponent) {
}
}

SeriesContent(current.series)
SeriesContent(current.series) {
component.itemClicked(it)
}
}
VerticalScrollbar(rememberScrollbarAdapter(state))

Expand All @@ -407,8 +411,10 @@ private fun DefaultScreen(component: SeriesComponent) {
}
}

private fun LazyListScope.SeriesContent(content: Series) {
private fun LazyListScope.SeriesContent(content: Series, onEpisodeClick: (Series.Episode) -> Unit) {
items(content.episodes, key = { it.href }) { episode ->
EpisodeItem(episode)
EpisodeItem(episode) {
onEpisodeClick(episode)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ import dev.datlag.burningseries.common.launchIO
import dev.datlag.burningseries.database.BurningSeries
import dev.datlag.burningseries.model.BSUtil
import dev.datlag.burningseries.model.Series
import dev.datlag.burningseries.model.state.EpisodeAction
import dev.datlag.burningseries.model.state.SeriesAction
import dev.datlag.burningseries.model.state.SeriesState
import dev.datlag.burningseries.network.state.EpisodeStateMachine
import dev.datlag.burningseries.network.state.SeriesStateMachine
import dev.datlag.burningseries.ui.navigation.DialogComponent
import dev.datlag.burningseries.ui.screen.initial.series.dialog.language.LanguageDialogComponent
Expand Down Expand Up @@ -61,6 +63,8 @@ class SeriesScreenComponent(
)
}.stateIn(ioScope(), SharingStarted.Lazily, database.burningSeriesQueries.seriesByHref(commonHref.value).executeAsOneOrNull()?.favoriteSince?.let { it > 0 } ?: false)

private val episodeStateMachine by di.instance<EpisodeStateMachine>()

private val dialogNavigation = SlotNavigation<DialogConfig>()
private val _dialog = childSlot(
source = dialogNavigation
Expand Down Expand Up @@ -96,6 +100,12 @@ class SeriesScreenComponent(

init {
backHandler.register(backCallback)

ioScope().launchIO {
episodeStateMachine.state.collect {
println(it)
}
}
}

@Composable
Expand Down Expand Up @@ -125,6 +135,10 @@ class SeriesScreenComponent(
)
}

override fun itemClicked(episode: Series.Episode): Any? = ioScope().launchIO {
episodeStateMachine.dispatch(EpisodeAction.Load(episode))
}

private fun loadNewSeason(season: Series.Season) = ioScope().launchIO {
(currentSeries.value ?: currentSeries.firstOrNull())?.let { series ->
seriesStateMachine.dispatch(SeriesAction.Load(series.hrefBuilder(season.value)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ import kotlinx.coroutines.delay
import kotlin.math.roundToInt

@Composable
fun EpisodeItem(content: Series.Episode) {
fun EpisodeItem(content: Series.Episode, onClick: () -> Unit) {
val blurHash = remember(content.href) { BlurHash.random() }
val enabled = content.hosters.isNotEmpty()

Row(
modifier = Modifier.padding(vertical = 4.dp).fillMaxWidth().height(100.dp).onClick(enabled) {
// ToDo("play episode")
onClick()
}.ifTrue(enabled) { bounceClick(0.95F) }.ifFalse(enabled) { alpha(0.5F) },
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalAlignment = Alignment.CenterVertically
Expand Down
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ plugins {
alias(libs.plugins.multiplatform) apply false
alias(libs.plugins.osdetector) apply false
alias(libs.plugins.protobuf) apply false
alias(libs.plugins.realm) apply false
alias(libs.plugins.serialization) apply false
alias(libs.plugins.sqldelight) apply false
alias(libs.plugins.complete.kotlin)
Expand Down
4 changes: 4 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ osdetector = "1.7.3"
parcelable = "1.2.0"
protobuf = "0.9.4"
protoc = "3.24.4"
realm = "1.11.0"
serialization-json = "1.6.0"
splashscreen = "1.0.1"
sqldelight = "2.0.0"
Expand Down Expand Up @@ -90,6 +91,8 @@ napier = { group = "io.github.aakira", name = "napier", version.ref = "napier" }
parcelable = { group = "com.arkivanov.essenty", name = "parcelable", version.ref = "parcelable" }
protobuf = { group = "com.google.protobuf", name = "protobuf-kotlin", version.ref = "protoc" }
protoc = { group = "com.google.protobuf", name = "protoc", version.ref = "protoc" }
realm = { group = "io.realm.kotlin", name = "library-base", version.ref = "realm" }
realm-sync = { group = "io.realm.kotlin", name = "library-sync", version.ref = "realm" }
serialization-json = { group = "org.jetbrains.kotlinx", name = "kotlinx-serialization-json", version.ref = "serialization-json" }
splashscreen = { group = "androidx.core", name = "core-splashscreen", version.ref = "splashscreen" }
stdlib = { group = "org.jetbrains.kotlin", name = "kotlin-stdlib", version.ref = "kotlin" }
Expand Down Expand Up @@ -118,6 +121,7 @@ multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotl
osdetector = { id = "com.google.osdetector", version.ref = "osdetector" }
parcelize = { id = "kotlin-parcelize", version.ref = "kotlin" }
protobuf = { id = "com.google.protobuf", version.ref = "protobuf" }
realm = { id = "io.realm.kotlin", version.ref = "realm" }
serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
sqldelight = { id = "app.cash.sqldelight", version.ref = "sqldelight" }
versions = { id = "com.github.ben-manes.versions", version.ref = "versions" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package dev.datlag.burningseries.model.algorithm

import kotlin.math.abs
import kotlin.math.sin

data object MD5 {

private val INIT_A = 0x67452301
private val INIT_B = 0xEFCDAB89L.toInt()
private val INIT_C = 0x98BADCFEL.toInt()
private val INIT_D = 0x10325476

private val SHIFT_AMTS = intArrayOf(
7, 12, 17, 22,
5, 9, 14, 20,
4, 11, 16, 23,
6, 10, 15, 21
)

private val TABLE_T = IntArray(64) {
((1L shl 32) * abs(sin(it + 1.0))).toLong().toInt()
}

fun compute(message: ByteArray): ByteArray {
val messageLenBytes = message.size
val numBlocks = ((messageLenBytes + 8) ushr 6) + 1
val totalLen = numBlocks shl 6
val paddingBytes = ByteArray(totalLen - messageLenBytes)
paddingBytes[0] = 0x80.toByte()
var messageLenBits = (messageLenBytes shl 3).toLong()

for (i in 0..7) {
paddingBytes[paddingBytes.size - 8 + i] = messageLenBits.toByte()
messageLenBits = messageLenBits ushr 8
}

var a = INIT_A
var b = INIT_B
var c = INIT_C
var d = INIT_D
val buffer = IntArray(16)

for (i in 0 until numBlocks) {
var index = i shl 6

for (j in 0..63) {
val temp = if (index < messageLenBytes) message[index] else
paddingBytes[index - messageLenBytes]
buffer[j ushr 2] = (temp.toInt() shl 24) or (buffer[j ushr 2] ushr 8)
index++
}

val originalA = a
val originalB = b
val originalC = c
val originalD = d

for (j in 0..63) {
val div16 = j ushr 4
var f = 0
var bufferIndex = j
when (div16) {
0 -> {
f = (b and c) or (b.inv() and d)
}

1 -> {
f = (b and d) or (c and d.inv())
bufferIndex = (bufferIndex * 5 + 1) and 0x0F
}

2 -> {
f = b xor c xor d;
bufferIndex = (bufferIndex * 3 + 5) and 0x0F
}

3 -> {
f = c xor (b or d.inv());
bufferIndex = (bufferIndex * 7) and 0x0F
}
}

val temp = b + (a + f + buffer[bufferIndex] + TABLE_T[j]).rotateLeft(SHIFT_AMTS[(div16 shl 2) or (j and 3)])
a = d
d = c
c = b
b = temp
}

a += originalA
b += originalB
c += originalC
d += originalD
}

val md5 = ByteArray(16)
var count = 0

for (i in 0..3) {
var n = if (i == 0) a else (if (i == 1) b else (if (i == 2) c else d))

for (j in 0..3) {
md5[count++] = n.toByte()
n = n ushr 8
}
}
return md5
}

@OptIn(ExperimentalStdlibApi::class)
fun hexString(byteArray: ByteArray): String {
return compute(byteArray).toHexString()
}

fun hexString(value: String): String {
return hexString(value.encodeToByteArray())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package dev.datlag.burningseries.model.state

import dev.datlag.burningseries.model.Series

sealed interface EpisodeState {
data object Waiting : EpisodeState
data class Loading(val episode: Series.Episode) : EpisodeState
data class Success(val results: Collection<String>) : EpisodeState
data class Error(val msg: String) : EpisodeState
}

sealed interface EpisodeAction {
data class Load(val episode: Series.Episode) : EpisodeAction
}
3 changes: 3 additions & 0 deletions network/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {
alias(libs.plugins.ksp)
alias(libs.plugins.android.library)
alias(libs.plugins.ktorfit) apply false
alias(libs.plugins.realm)
}

val artifact = VersionCatalog.artifactName("network")
Expand Down Expand Up @@ -31,6 +32,8 @@ kotlin {
api(libs.ktsoup)
api(libs.ktsoup.fs)
api(libs.ktsoup.ktor)
api(libs.realm)
api(libs.realm.sync)
}
}
}
Expand Down
Loading

0 comments on commit 7222f83

Please sign in to comment.