Skip to content

TroubleShoot screen to debug application data for debug purpose. #22

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

Merged
merged 1 commit into from
Dec 6, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.byteutility.dev.leetcode.plus.troubleshoot

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.ViewModel
import com.byteutility.dev.leetcode.plus.data.datastore.UserDatastore
import com.byteutility.dev.leetcode.plus.data.repository.weeklyGoal.WeeklyGoalRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.runBlocking
import javax.inject.Inject

/**
* This screen is for troubleshooting debug purpose, so implementation is kept as much as trivial
*/
@Composable
@Preview
fun TroubleShootScreen(viewModel: TroubleShootScreenViewModel = hiltViewModel()) {
var goalOutput by remember { mutableStateOf("") }
var dailyProblemOutput by remember { mutableStateOf("") }
Column(verticalArrangement = Arrangement.Center) {
Spacer(modifier = Modifier.height(50.dp))
TextButton({
goalOutput = ""
goalOutput = runBlocking { viewModel.getWeeklyGoal() }
}) {
Text("Weekly goal data")
}
Text(text = goalOutput)

TextButton({
dailyProblemOutput = ""
dailyProblemOutput = runBlocking { viewModel.getDailyProblem() }
}) {
Text("Daily Problem data")
}
Text(text = dailyProblemOutput)
}
}

@HiltViewModel
class TroubleShootScreenViewModel @Inject constructor(
private val goalRepository: WeeklyGoalRepository,
private val userDatastore: UserDatastore,
) : ViewModel() {
suspend fun getWeeklyGoal(): String {
return goalRepository.weeklyGoal.firstOrNull().toString()
}

suspend fun getDailyProblem(): String {
return userDatastore.getDailyProblem().firstOrNull().toString()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.byteutility.dev.leetcode.plus.troubleshoot.TroubleShootScreen
import com.byteutility.dev.leetcode.plus.ui.screens.login.UserLoginScreen
import com.byteutility.dev.leetcode.plus.ui.screens.targetset.SetWeeklyTargetScreen
import com.byteutility.dev.leetcode.plus.ui.screens.targetstatus.GoalProgressScreen
import com.byteutility.dev.leetcode.plus.ui.screens.userdetails.UserProfileScreen


@Composable
fun LeetCodePlusNavGraph(
navController: NavHostController = rememberNavController(),
Expand Down Expand Up @@ -46,8 +46,14 @@ fun LeetCodePlusNavGraph(
navigationActions.navigateToSetGoal()
}, {
navigationActions.navigateToGoalStatus()
},
{
navigationActions.navigateToTroubleShoot()
}
)
}
composable(route = LeetCodePlusNavigationDestinations.TROUBLE_SHOOT_ROUTE) {
TroubleShootScreen()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ object LeetCodePlusNavigationDestinations {
const val SET_GOAL_ROUTE = "set_goal"
const val LOGIN_ROUTE = "login"
const val GOAL_STATUS_ROUTE = "goal_status"
const val TROUBLE_SHOOT_ROUTE = "trouble_shoot"
}

class LeetCodePlusNavigation(navController: NavController) {
Expand Down Expand Up @@ -43,4 +44,10 @@ class LeetCodePlusNavigation(navController: NavController) {
val popCurrentDestination: () -> Unit = {
navController.popBackStack()
}

val navigateToTroubleShoot: () -> Unit = {
navController.navigate(LeetCodePlusNavigationDestinations.TROUBLE_SHOOT_ROUTE) {
launchSingleTop = true
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.byteutility.dev.leetcode.plus.ui.screens.userdetails
import android.annotation.SuppressLint
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
Expand Down Expand Up @@ -37,11 +38,17 @@ import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
Expand All @@ -57,6 +64,8 @@ import com.byteutility.dev.leetcode.plus.data.model.UserBasicInfo
import com.byteutility.dev.leetcode.plus.data.model.UserContestInfo
import com.byteutility.dev.leetcode.plus.data.model.UserProblemSolvedInfo
import com.byteutility.dev.leetcode.plus.data.model.UserSubmission
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import me.bytebeats.views.charts.pie.PieChart
import me.bytebeats.views.charts.pie.PieChartData
import me.bytebeats.views.charts.pie.render.SimpleSliceDrawer
Expand All @@ -68,10 +77,12 @@ import me.bytebeats.views.charts.simpleChartAnimation
fun UserProfileScreen(
onSetGoal: () -> Unit = {},
onGoalStatus: () -> Unit = {},
onTroubleShoot: () -> Unit = {},
) {
val viewModel: UserDetailsViewModel = hiltViewModel()
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
UserProfileLayout(uiState, onSetGoal, onGoalStatus)
viewModel.startsSync(LocalContext.current)
UserProfileLayout(uiState, onSetGoal, onGoalStatus, onTroubleShoot)
}

@OptIn(ExperimentalMaterial3Api::class)
Expand All @@ -80,11 +91,37 @@ fun UserProfileLayout(
uiState: UserDetailsUiState,
onSetGoal: () -> Unit = {},
onGoalStatus: () -> Unit = {},
onTroubleShoot: () -> Unit = {}
) {
var clickCount by remember { mutableIntStateOf(0) }
var lastClickTime by remember { mutableStateOf(0L) }
val scope = rememberCoroutineScope()

Scaffold(
topBar = {
TopAppBar(
title = { Text(text = "My Profile") },
title = {
/**
* 5 times click in a shorter period will open troubleshoot page
*/
Text(text = "My Profile", modifier = Modifier.clickable {
val currentTime = System.currentTimeMillis()
if (currentTime - lastClickTime <= 1000) {
clickCount++
if (clickCount == 5) {
onTroubleShoot.invoke()
clickCount = 0
}
} else {
clickCount = 1
}
lastClickTime = currentTime
scope.launch {
delay(2000)
clickCount = 0
}
})
},
actions = {
TextButton(onClick = {
if (uiState.isWeeklyGoalSet) {
Expand Down Expand Up @@ -177,7 +214,9 @@ fun UserProblemCategoryStats(
userProblemSolvedInfo: UserProblemSolvedInfo,
) {
Box(
modifier = modifier.padding(8.dp).fillMaxWidth()
modifier = modifier
.padding(8.dp)
.fillMaxWidth()
) {
Card(
elevation = CardDefaults.cardElevation(defaultElevation = 4.dp),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.byteutility.dev.leetcode.plus.ui.screens.userdetails

import android.content.Context
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.byteutility.dev.leetcode.plus.data.model.UserBasicInfo
Expand All @@ -8,6 +9,7 @@ import com.byteutility.dev.leetcode.plus.data.model.UserProblemSolvedInfo
import com.byteutility.dev.leetcode.plus.data.model.UserSubmission
import com.byteutility.dev.leetcode.plus.data.repository.userDetails.UserDetailsRepository
import com.byteutility.dev.leetcode.plus.data.repository.weeklyGoal.WeeklyGoalRepository
import com.byteutility.dev.leetcode.plus.data.worker.UserDetailsSyncWorker
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
Expand Down Expand Up @@ -128,4 +130,6 @@ class UserDetailsViewModel @Inject constructor(
}
}
}

fun startsSync(context: Context) = UserDetailsSyncWorker.enqueuePeriodicWork(context)
}