Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show results #47

Merged
merged 2 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composeApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ kotlin {
languageSettings {
optIn("kotlinx.coroutines.ExperimentalCoroutinesApi")
optIn("androidx.compose.material3.ExperimentalMaterial3Api")
optIn("androidx.compose.foundation.ExperimentalFoundationApi")
optIn("androidx.compose.ui.test.ExperimentalTestApi")
}
}
Expand Down
2 changes: 0 additions & 2 deletions composeApp/src/androidMain/res/values/strings.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<string name="run_tests">Run Test</string>
<string name="back">Back</string>
<string name="dashboard">Dashboard</string>
<string name="test_results">Test Results</string>
<string name="settings">Settings</string>

<string name="TestResults_Overview_Title">Test Results</string>
<string name="TestResults_Overview_Tab_Label">Test Results</string>
<string name="TestResults_UnknownASN">Unknown</string>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.ooni.probe.data.models

data class ResultListItem(
val result: ResultModel,
val network: NetworkModel?,
val measurementsCount: Long,
) {
val idOrThrow get() = result.idOrThrow
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.ooni.probe.data.models

import kotlinx.datetime.Instant
import kotlinx.datetime.LocalDateTime

data class ResultModel(
val id: Id? = null,
val testGroupName: String?,
val startTime: Instant?,
val startTime: LocalDateTime,
val isViewed: Boolean,
val isDone: Boolean,
val dataUsageUp: Long?,
Expand All @@ -17,4 +17,6 @@ data class ResultModel(
data class Id(
val value: Long,
)

val idOrThrow get() = id ?: throw IllegalStateException("Id no available")
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ class NetworkRepository(
)
}
}

private fun Network.toModel(): NetworkModel =
NetworkModel(
id = NetworkModel.Id(id),
networkName = network_name,
ip = ip,
asn = asn,
countryCode = country_code,
networkType = network_type?.let(NetworkType::fromValue),
)
}

fun Network.toModel(): NetworkModel =
NetworkModel(
id = NetworkModel.Id(id),
networkName = network_name,
ip = ip,
asn = asn,
countryCode = country_code,
networkType = network_type?.let(NetworkType::fromValue),
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,51 @@ package org.ooni.probe.data.repositories
import app.cash.sqldelight.coroutines.asFlow
import app.cash.sqldelight.coroutines.mapToList
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.withContext
import kotlinx.datetime.Instant
import org.ooni.probe.Database
import org.ooni.probe.data.Network
import org.ooni.probe.data.Result
import org.ooni.probe.data.SelectAllWithNetwork
import org.ooni.probe.data.models.NetworkModel
import org.ooni.probe.data.models.ResultListItem
import org.ooni.probe.data.models.ResultModel
import org.ooni.probe.data.models.TestDescriptorModel
import org.ooni.probe.shared.toEpoch
import org.ooni.probe.shared.toLocalDateTime

class ResultRepository(
private val database: Database,
private val backgroundDispatcher: CoroutineDispatcher,
) {
fun list() =
fun list(): Flow<List<ResultModel>> =
database.resultQueries
.selectAll()
.asFlow()
.mapToList(backgroundDispatcher)
.map { list -> list.map { it.toModel() } }
.map { list -> list.mapNotNull { it.toModel() } }

fun listWithNetwork(): Flow<List<ResultListItem>> =
database.resultQueries
.selectAllWithNetwork()
.asFlow()
.mapToList(backgroundDispatcher)
.map { list -> list.mapNotNull { it.toModel() } }

fun getById(resultId: ResultModel.Id): Flow<ResultModel?> =
database.resultQueries
.selectById(resultId.value)
.asFlow()
.mapToList(backgroundDispatcher)
.map { it.firstOrNull()?.toModel() }

suspend fun create(model: ResultModel) {
withContext(backgroundDispatcher) {
database.resultQueries.insert(
id = model.id?.value,
test_group_name = model.testGroupName,
start_time = model.startTime?.toEpochMilliseconds(),
start_time = model.startTime.toEpoch(),
is_viewed = if (model.isViewed) 1 else 0,
is_done = if (model.isDone) 1 else 0,
data_usage_up = model.dataUsageUp,
Expand All @@ -39,18 +58,49 @@ class ResultRepository(
)
}
}
}

private fun Result.toModel(): ResultModel? {
return ResultModel(
id = ResultModel.Id(id),
testGroupName = test_group_name,
startTime = start_time?.toLocalDateTime() ?: return null,
isViewed = is_viewed == 1L,
isDone = is_done == 1L,
dataUsageUp = data_usage_up,
dataUsageDown = data_usage_down,
failureMessage = failure_msg,
networkId = network_id?.let(NetworkModel::Id),
testDescriptorId = descriptor_runId?.let(TestDescriptorModel::Id),
)
}

private fun Result.toModel(): ResultModel =
ResultModel(
id = ResultModel.Id(id),
testGroupName = test_group_name,
startTime = start_time?.let(Instant::fromEpochMilliseconds),
isViewed = is_viewed == 1L,
isDone = is_done == 1L,
dataUsageUp = data_usage_up,
dataUsageDown = data_usage_down,
failureMessage = failure_msg,
networkId = network_id?.let(NetworkModel::Id),
testDescriptorId = descriptor_runId?.let(TestDescriptorModel::Id),
)
private fun SelectAllWithNetwork.toModel(): ResultListItem? {
return ResultListItem(
result =
Result(
id = id,
test_group_name = test_group_name,
start_time = start_time,
is_viewed = is_viewed,
is_done = is_done,
data_usage_up = data_usage_up,
data_usage_down = data_usage_down,
failure_msg = failure_msg,
network_id = network_id,
descriptor_runId = descriptor_runId,
).toModel() ?: return null,
network =
id_?.let { networkId ->
Network(
id = networkId,
network_name = network_name,
ip = ip,
asn = asn,
country_code = country_code,
network_type = network_type,
).toModel()
},
measurementsCount = measurementsCount,
)
}
17 changes: 13 additions & 4 deletions composeApp/src/commonMain/kotlin/org/ooni/probe/di/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import org.ooni.engine.NetworkTypeFinder
import org.ooni.engine.OonimkallBridge
import org.ooni.engine.TaskEventMapper
import org.ooni.probe.Database
import org.ooni.probe.data.models.TestResult
import org.ooni.probe.data.models.ResultModel
import org.ooni.probe.data.repositories.ResultRepository
import org.ooni.probe.domain.GetResult
import org.ooni.probe.domain.GetResults
import org.ooni.probe.shared.PlatformInfo
import org.ooni.probe.ui.dashboard.DashboardViewModel
import org.ooni.probe.ui.result.ResultViewModel
Expand All @@ -32,6 +35,7 @@ class Dependencies(

private val json by lazy { buildJson() }
private val database by lazy { buildDatabase(databaseDriverFactory) }
private val resultRepository by lazy { ResultRepository(database, backgroundDispatcher) }

// Engine

Expand All @@ -50,16 +54,21 @@ class Dependencies(
)
}

// Domain

private val getResults by lazy { GetResults(resultRepository) }
private val getResult by lazy { GetResult(resultRepository) }

// ViewModels

val dashboardViewModel get() = DashboardViewModel(engine)

fun resultsViewModel(goToResult: (TestResult.Id) -> Unit) = ResultsViewModel(goToResult)
fun resultsViewModel(goToResult: (ResultModel.Id) -> Unit) = ResultsViewModel(goToResult, getResults::invoke)

fun resultViewModel(
resultId: TestResult.Id,
resultId: ResultModel.Id,
onBack: () -> Unit,
) = ResultViewModel(resultId, onBack)
) = ResultViewModel(resultId, onBack, getResult::invoke)

companion object {
@VisibleForTesting
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.ooni.probe.domain

import org.ooni.probe.data.models.ResultModel
import org.ooni.probe.data.repositories.ResultRepository

class GetResult(
private val resultRepository: ResultRepository,
) {
operator fun invoke(resultId: ResultModel.Id) = resultRepository.getById(resultId)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.ooni.probe.domain

import org.ooni.probe.data.repositories.ResultRepository

class GetResults(
private val resultRepository: ResultRepository,
) {
operator fun invoke() = resultRepository.listWithNetwork()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.ooni.probe.shared

import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
import kotlinx.datetime.LocalDate
import kotlinx.datetime.LocalDateTime
import kotlinx.datetime.TimeZone
import kotlinx.datetime.toInstant
import kotlinx.datetime.toLocalDateTime
import kotlinx.datetime.todayIn

fun LocalDateTime.toEpoch() = toInstant(TimeZone.currentSystemDefault()).toEpochMilliseconds()

fun Long.toLocalDateTime() = Instant.fromEpochMilliseconds(this).toLocalDateTime(TimeZone.currentSystemDefault())

fun LocalDate.Companion.today() = Clock.System.todayIn(TimeZone.currentSystemDefault())

fun LocalDateTime.Companion.now() = Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault())
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import androidx.navigation.NavController
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.compose.currentBackStackEntryAsState
import ooniprobe.composeapp.generated.resources.Res
import ooniprobe.composeapp.generated.resources.TestResults_Overview_Tab_Label
import ooniprobe.composeapp.generated.resources.dashboard
import ooniprobe.composeapp.generated.resources.ic_dashboard
import ooniprobe.composeapp.generated.resources.ic_history
import ooniprobe.composeapp.generated.resources.ic_settings
import ooniprobe.composeapp.generated.resources.settings
import ooniprobe.composeapp.generated.resources.test_results
import org.jetbrains.compose.resources.painterResource
import org.jetbrains.compose.resources.stringResource

Expand Down Expand Up @@ -64,7 +64,7 @@ private val Screen.titleRes
get() =
when (this) {
Screen.Dashboard -> Res.string.dashboard
Screen.Results -> Res.string.test_results
Screen.Results -> Res.string.TestResults_Overview_Tab_Label
Screen.Settings -> Res.string.settings
else -> throw IllegalArgumentException("Only main screens allowed in bottom navigation")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import org.ooni.probe.data.models.TestResult
import org.ooni.probe.data.models.ResultModel
import org.ooni.probe.di.Dependencies
import org.ooni.probe.ui.dashboard.DashboardScreen
import org.ooni.probe.ui.result.ResultScreen
Expand Down Expand Up @@ -51,11 +51,11 @@ fun Navigation(
route = Screen.Result.NAV_ROUTE,
arguments = Screen.Result.ARGUMENTS,
) { entry ->
val resultId = entry.arguments?.getString("resultId") ?: return@composable
val resultId = entry.arguments?.getLong("resultId") ?: return@composable
val viewModel =
viewModel {
dependencies.resultViewModel(
resultId = TestResult.Id(resultId),
resultId = ResultModel.Id(resultId),
onBack = { navController.navigateUp() },
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package org.ooni.probe.ui.navigation

import androidx.navigation.NavType
import androidx.navigation.navArgument
import org.ooni.probe.data.models.TestResult
import org.ooni.probe.data.models.ResultModel

sealed class Screen(
val route: String,
Expand All @@ -14,11 +14,11 @@ sealed class Screen(
data object Settings : Screen("settings")

data class Result(
val resultId: TestResult.Id,
val resultId: ResultModel.Id,
) : Screen("results/${resultId.value}") {
companion object {
const val NAV_ROUTE = "results/{resultId}"
val ARGUMENTS = listOf(navArgument("resultId") { type = NavType.StringType })
val ARGUMENTS = listOf(navArgument("resultId") { type = NavType.LongType })
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import ooniprobe.composeapp.generated.resources.Res
import ooniprobe.composeapp.generated.resources.back
import ooniprobe.composeapp.generated.resources.test_results
import org.jetbrains.compose.resources.stringResource

@Composable
Expand All @@ -21,7 +20,7 @@ fun ResultScreen(
Column {
TopAppBar(
title = {
Text(stringResource(Res.string.test_results))
Text(state.result?.testGroupName.orEmpty())
},
navigationIcon = {
IconButton(onClick = { onEvent(ResultViewModel.Event.BackClicked) }) {
Expand All @@ -32,7 +31,5 @@ fun ResultScreen(
}
},
)

Text(state.result.id.value)
}
}
Loading