Skip to content

Commit

Permalink
Implement checklist update functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
masato1230 committed Dec 21, 2022
1 parent f5d72e8 commit 759643f
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class DocPureeRequestBodyCollectInterceptor : Interceptor {
val responseBody = buffer.readUtf8()
Log.d("RequestBody", responseBody)
LogHistorySource.successfullyLoggedJsonHistory.add(responseBody)
LogHistorySource.successfullyLoggedJson.value = responseBody
}

return response
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package jp.co.tokubai.docpuree.source

object CheckListSource {
val checkList = mutableListOf<Class<*>>()
val checkList = mutableListOf<CheckListItem>()
}

data class CheckListItem(
val clazz: Class<*>,
var isLogged: Boolean,
)
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package jp.co.tokubai.docpuree.source

import kotlinx.coroutines.flow.MutableStateFlow

object LogHistorySource {
// Map of serialized Class to Json
val serializedClassHistory = mutableListOf<Pair<String, String>>()

// List of json successfully logged
val successfullyLoggedJsonHistory = mutableListOf<String>()
val successfullyLoggedJson = MutableStateFlow("")

// Successfully Logged classes
val successfullyLoggedClassHistory: List<Pair<String, String>>
Expand All @@ -18,4 +21,8 @@ object LogHistorySource {
}
}
}

fun getClassFromLoggedJson(loggedJson: String) : String? {
return serializedClassHistory.firstOrNull { loggedJson.contains(it.second) }?.first
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package jp.co.tokubai.docpuree.ui.checklist

import android.util.Log
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
Expand All @@ -9,6 +10,8 @@ import androidx.compose.material.Button
import androidx.compose.material.Scaffold
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp

Expand All @@ -24,13 +27,19 @@ private fun CheckListContent(
modifier: Modifier = Modifier,
viewModel: CheckListViewModel,
) {
// TODO delete
val successLog = viewModel.latestLog.collectAsState()
LaunchedEffect(successLog) {
Log.d("Success", successLog.value)
}

LazyColumn(modifier = modifier, contentPadding = PaddingValues(12.dp)) {
items(viewModel.checkList) { clazz ->
items(viewModel.checkList) { checkListItem ->
Button(
onClick = { },
modifier = Modifier.fillMaxWidth(),
) {
Text(text = clazz.simpleName)
Text(text = checkListItem.clazz.simpleName)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,34 @@
package jp.co.tokubai.docpuree.ui.checklist

import android.util.Log
import androidx.lifecycle.ViewModel
import jp.co.tokubai.docpuree.source.CheckListSource
import jp.co.tokubai.docpuree.source.LogHistorySource
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach

class CheckListViewModel : ViewModel() {
init {
observeLoggedClass()
}

val checkList = CheckListSource.checkList
val latestLog = LogHistorySource.successfullyLoggedJson

private fun observeLoggedClass() {
LogHistorySource.successfullyLoggedJson.onEach { successfullyLoggedJson ->
val nextCheckListItem = CheckListSource.checkList.firstOrNull { !it.isLogged }
nextCheckListItem?.let { checkItem ->
val isCheckItemIsLogged =
checkItem.clazz.simpleName == LogHistorySource.getClassFromLoggedJson(
successfullyLoggedJson
)
if (isCheckItemIsLogged) {
nextCheckListItem.isLogged = true
Log.d("CheckList", CheckListSource.checkList.toString())
}
}
}.launchIn(GlobalScope)
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package jp.co.tokubai.docpuree.ui.searchlog

import androidx.lifecycle.ViewModel
import jp.co.tokubai.docpuree.source.CheckListItem
import jp.co.tokubai.docpuree.source.CheckListSource

class SearchLogViewModel : ViewModel() {

fun addLogToCheckList(clazz: Class<*>) {
CheckListSource.checkList.add(clazz)
CheckListSource.checkList.add(CheckListItem(clazz, false))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class MainActivity : ComponentActivity() {
Button(onClick = {
val intent = Intent(this@MainActivity, DocPureeActivity::class.java)
startActivity(intent)
Puree.send(RecipeSearch("onClick", (1..10).random()))
Puree.send(RecipeSearch("onClick", (1..100).random()))
}) {
Text(text = "DocPureeActivity")
}
Expand Down

0 comments on commit 759643f

Please sign in to comment.