Skip to content

Commit

Permalink
Tweak models (#26)
Browse files Browse the repository at this point in the history
* Added tests to ModelsViewModel
  • Loading branch information
Otacon authored Nov 3, 2024
1 parent 1796e49 commit 099912e
Show file tree
Hide file tree
Showing 26 changed files with 1,228 additions and 503 deletions.
17 changes: 10 additions & 7 deletions composeApp/src/commonMain/kotlin/org/cyanotic/olpaka/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import com.cyanotic.olpaka.BuildKonfig
import io.github.aakira.napier.LogLevel
import io.ktor.http.*
import org.cyanotic.olpaka.core.*
import io.github.aakira.napier.Napier
import io.ktor.http.parseUrl
import org.cyanotic.olpaka.core.FirebaseAnalytics
import org.cyanotic.olpaka.core.OlpakaAntilog
import org.cyanotic.olpaka.core.Preferences
import org.cyanotic.olpaka.core.ThemeState
import org.cyanotic.olpaka.feature.main.MainScreen
import org.cyanotic.olpaka.network.EndpointProvider
import org.cyanotic.olpaka.ui.theme.AppTheme
Expand All @@ -21,7 +25,7 @@ fun App() {
KoinApplication({ modules(koinModules) }) {
val koin = getKoin()
val themeState = koin.get<ThemeState>()
LaunchedEffect(Unit){
LaunchedEffect(Unit) {
val analytics = koin.get<FirebaseAnalytics>()
analytics.init()

Expand Down Expand Up @@ -56,10 +60,9 @@ private fun initLogging() {
null
}
}
logLevel?.let {
// Napier.base(DebugAntilog())
// Napier.isEnable(it, null)
}
val antilog = OlpakaAntilog()
logLevel?.let { antilog.currentLevel = it }
Napier.base(antilog)
}

private fun initTheme(preferences: Preferences, themeState: ThemeState) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.cyanotic.olpaka.core

import kotlinx.datetime.Clock

interface DownloadStatsCalculator {

fun calculateDownloadSpeed(
downloadedBytes: Long,
previousBytesDownloaded: Long,
previousUpdateTime: Long
): Long?

fun calculateRemainingTime(
totalBytes: Long,
downloadedBytes: Long,
previousBytesDownloaded: Long,
previousUpdateTime: Long
): Long?
}

class DownloadStatsCalculatorDefault : DownloadStatsCalculator {

override fun calculateDownloadSpeed(
downloadedBytes: Long,
previousBytesDownloaded: Long,
previousUpdateTime: Long
): Long? {
val timeElapsed = Clock.System.now().toEpochMilliseconds() - previousUpdateTime
if (timeElapsed == 0L || downloadedBytes == previousBytesDownloaded) {
return null
}

val speedBytesPerMillisecond = (downloadedBytes - previousBytesDownloaded) / timeElapsed
return speedBytesPerMillisecond * 1000
}

override fun calculateRemainingTime(
totalBytes: Long,
downloadedBytes: Long,
previousBytesDownloaded: Long,
previousUpdateTime: Long
): Long? {

val timeElapsed = Clock.System.now().toEpochMilliseconds() - previousUpdateTime
if (timeElapsed == 0L || downloadedBytes == previousBytesDownloaded) {
return null
}

val speedBytesPerMillisecond = (downloadedBytes - previousBytesDownloaded) / timeElapsed
val speedBytesPerSecond = speedBytesPerMillisecond * 1000

val remainingBytes = totalBytes - downloadedBytes
if(speedBytesPerSecond == 0L){
return null
}
val timeLeftSeconds = remainingBytes / speedBytesPerSecond
return timeLeftSeconds
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.cyanotic.olpaka.core

import io.github.aakira.napier.Antilog
import io.github.aakira.napier.DebugAntilog
import io.github.aakira.napier.LogLevel

class OlpakaAntilog : Antilog() {

private val logger = DebugAntilog()
var currentLevel = LogLevel.INFO

override fun performLog(
priority: LogLevel,
tag: String?,
throwable: Throwable?,
message: String?
) {
if(priority.ordinal >= currentLevel.ordinal){
logger.log(priority, tag, throwable, message)
}
}

}
27 changes: 0 additions & 27 deletions composeApp/src/commonMain/kotlin/org/cyanotic/olpaka/core/Utils.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ import org.koin.dsl.bind
import org.koin.dsl.module

val coreModule = module {

singleOf(::ThemeState)
singleOf(::ModelDownloadStateDefault) bind ModelDownloadState::class
singleOf(::FirebaseAnalytics) bind Analytics::class

factoryOf(::PreferencesDefault) bind Preferences::class
factoryOf(::StringResourcesDefault) bind StringResources::class
factoryOf(::DownloadStatsCalculatorDefault) bind DownloadStatsCalculator::class
factory<CoroutineDispatcher> { Dispatchers.Default }

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,28 @@ package org.cyanotic.olpaka.core.domain

sealed interface Model {

val id: String

data class Downloading(
val id: String,
override val id: String,
val name: String,
val downloaded: Long?,
val size: Long?,
val downloadedBytes: Long,
val sizeBytes: Long,
val speedBytesSecond: Long,
val timeLeftSeconds: Long
) : Model

data class Cached(
val id: String,
override val id: String,
val name: String,
val size: Long,
val quantization: String,
val parameters: String
) : Model

data class Error(
override val id: String,
val name: String,
val message: String,
) : Model
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import androidx.compose.ui.text.TextRange
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.unit.dp
import com.mikepenz.markdown.m3.Markdown
import io.github.aakira.napier.Napier
import olpaka.composeapp.generated.resources.Res
import olpaka.composeapp.generated.resources.app_name
import olpaka.composeapp.generated.resources.chat_assistant_name
Expand Down Expand Up @@ -103,6 +104,7 @@ fun ChatScreen() {
}

val currentState = state
Napier.d(tag = "Chat Screen", message = "Showing $currentState")
if (currentState is ChatState.Content) {
LaunchedEffect(currentState) {
chatListState.animateScrollToItem(chatListState.layoutInfo.totalItemsCount)
Expand Down
Loading

0 comments on commit 099912e

Please sign in to comment.