Skip to content

Commit

Permalink
implemented logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Smithy-x committed Mar 9, 2024
1 parent ac4f143 commit 6569145
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 97 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/io/vonley/mi/ui/TabItem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import androidx.compose.runtime.Composable
import io.vonley.mi.R
import io.vonley.mi.ui.screens.consoles.presentation.ConsolesView
import io.vonley.mi.ui.screens.ftp.presentation.FTPView
import io.vonley.mi.ui.screens.home.HomeView
import io.vonley.mi.ui.screens.home.presentation.HomeView
import io.vonley.mi.ui.screens.packages.presentation.RepositoryView
import io.vonley.mi.ui.screens.settings.SettingsView

Expand Down
38 changes: 19 additions & 19 deletions app/src/main/java/io/vonley/mi/ui/compose/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import androidx.compose.material.Button
import androidx.compose.material.Scaffold
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
Expand All @@ -34,7 +33,7 @@ import io.vonley.mi.ui.MainViewModel
import io.vonley.mi.ui.TabItem
import io.vonley.mi.ui.screens.consoles.presentation.ConsolesView
import io.vonley.mi.ui.screens.ftp.presentation.FTPView
import io.vonley.mi.ui.screens.home.HomeView
import io.vonley.mi.ui.screens.home.presentation.HomeView
import io.vonley.mi.ui.screens.packages.presentation.RepositoryView
import io.vonley.mi.ui.screens.settings.SettingsView

Expand Down Expand Up @@ -73,23 +72,6 @@ fun MainView() {
}
}

@Composable
fun scaffold(navController: NavHostController, builder: ComposableFun) {
Scaffold(
topBar = { TopBar() },
bottomBar = {
BottomNavigationBar(navController)
},
content = { padding -> // We have to pass the scaffold inner padding to our content. That's why we use Box.
Box(modifier = Modifier.padding(padding)) {
builder()
}
},
backgroundColor = Color.White // Set background color to avoid the white flashing when you switch between screens
)
}


@Composable
fun Root() {
val navController: NavHostController = rememberNavController()
Expand Down Expand Up @@ -150,6 +132,24 @@ fun Root() {
}
}

@Composable
fun scaffold(navController: NavHostController, builder: ComposableFun) {
Scaffold(
topBar = { TopBar() },
bottomBar = {
BottomNavigationBar(navController)
},
content = { padding -> // We have to pass the scaffold inner padding to our content. That's why we use Box.
Box(modifier = Modifier.padding(padding)) {
builder()
}
},
backgroundColor = Color.White // Set background color to avoid the white flashing when you switch between screens
)
}



@Composable
inline fun <reified T : ViewModel> NavBackStackEntry.sharedViewModel(navController: NavHostController): T {
val navGraphRoute = destination.parent?.route ?: return viewModel()
Expand Down
119 changes: 55 additions & 64 deletions app/src/main/java/io/vonley/mi/ui/interop/MainActivityXml.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,98 +22,90 @@ import io.vonley.mi.R
import io.vonley.mi.databinding.ActivityMainXmlBinding

@AndroidEntryPoint
class MainActivityXml : AppCompatActivity(), NavController.OnDestinationChangedListener {
class MainActivityXml : AppCompatActivity() {

private lateinit var navHostFragment: NavHostFragment
private val navController
get() = navHostFragment.navController


private lateinit var appBarConfiguration: AppBarConfiguration

override fun onSupportNavigateUp(): Boolean {
val navController = findNavController(R.id.fragment_container)
return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
}

private fun onDestinationChanged(binding: ActivityMainXmlBinding): (NavController, NavDestination, Bundle?) -> Unit {
return { controller, destination, arguments ->
val i = when (destination.id) {
R.id.fragment_payload -> {
binding.fab.setOnClickListener(null)
binding.fab.setImageDrawable(
ContextCompat.getDrawable(
this@MainActivityXml,
R.drawable.icon_svg_upload_two
)
)
View.VISIBLE
}

R.id.fragment_ftp -> {
binding.fab.setOnClickListener(null)
binding.fab.setImageDrawable(
ContextCompat.getDrawable(
this@MainActivityXml,
R.drawable.ic_svg_upload
)
)
View.VISIBLE
}

R.id.fragment_console -> {
binding.fab.setOnClickListener(null)
binding.fab.setImageDrawable(
ContextCompat.getDrawable(
this@MainActivityXml,
R.drawable.icon_svg_add
)
)
View.VISIBLE
}

R.id.fragment_home -> {
binding.fab.setOnClickListener(null)
binding.fab.setImageDrawable(
ContextCompat.getDrawable(
this@MainActivityXml,
R.drawable.icon_svg_info
)
)
View.VISIBLE
}

else -> View.GONE
}
binding.fab.visibility = i
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
PreviewHomeFragmentView() { binding ->
this.navHostFragment = supportFragmentManager.findFragmentById(R.id.fragment_container) as NavHostFragment
setSupportActionBar(binding.bottomAppBar)
NavigationUI.setupWithNavController(binding.bottomNavigation, navController)
navController.addOnDestinationChangedListener { controller, destination, arguments ->
val i = when (destination.id) {
R.id.fragment_payload -> {
binding.fab.setOnClickListener(null)
binding.fab.setImageDrawable(
ContextCompat.getDrawable(
this@MainActivityXml,
R.drawable.icon_svg_upload_two
)
)
View.VISIBLE
}

R.id.fragment_ftp -> {
binding.fab.setOnClickListener(null)
binding.fab.setImageDrawable(
ContextCompat.getDrawable(
this@MainActivityXml,
R.drawable.ic_svg_upload
)
)
View.VISIBLE
}

R.id.fragment_console -> {
binding.fab.setOnClickListener(null)
binding.fab.setImageDrawable(
ContextCompat.getDrawable(
this@MainActivityXml,
R.drawable.icon_svg_add
)
)
View.VISIBLE
}

R.id.fragment_home -> {
binding.fab.setOnClickListener(null)
binding.fab.setImageDrawable(
ContextCompat.getDrawable(
this@MainActivityXml,
R.drawable.icon_svg_info
)
)
View.VISIBLE
}

else -> View.GONE
}
binding.fab.visibility = i
}
navController.addOnDestinationChangedListener(onDestinationChanged(binding))
}
}
}

override fun onDestinationChanged(
controller: NavController,
destination: NavDestination,
arguments: Bundle?
) {

}

}


@Composable
@Preview(showBackground = true)
fun PreviewHomeFragmentView(binding: ((binding: ActivityMainXmlBinding) -> Unit)? = null) {
AndroidViewBinding(ActivityMainXmlBinding::inflate) {
//this.fragmentContainer
//val myFragment = fragmentContainer.getFragment<HomeFragmentView>()
binding?.invoke(this)
}
}
Expand All @@ -123,7 +115,6 @@ fun PreviewHomeFragmentView(binding: ((binding: ActivityMainXmlBinding) -> Unit)
fun PreviewHomeFragmentView2() {
AndroidView(factory = { ctx ->
val inflate = LayoutInflater.from(ctx).inflate(R.layout.activity_main_xml, null)

inflate
}) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.vonley.mi.ui.screens.home
package io.vonley.mi.ui.screens.home.presentation

import android.os.Bundle
import android.view.LayoutInflater
Expand All @@ -10,6 +10,8 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.ComposeView
Expand All @@ -19,14 +21,16 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.fragment.app.Fragment
import androidx.hilt.navigation.compose.hiltViewModel
import dagger.hilt.android.AndroidEntryPoint
import io.vonley.mi.Mi
import io.vonley.mi.ui.screens.home.article.ArticleCard
import io.vonley.mi.ui.screens.home.log.LogCardBase
import io.vonley.mi.ui.screens.home.presentation.article.ArticleCard
import io.vonley.mi.ui.screens.home.presentation.log.LogCard
import io.vonley.mi.ui.screens.home.presentation.log.LogCardBase


@AndroidEntryPoint
class HomeFragmentView: Fragment() {
class HomeFragmentView : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand All @@ -42,24 +46,41 @@ class HomeFragmentView: Fragment() {

@Composable
fun HomeView() {
val vm = hiltViewModel<HomeViewModel>()
val logs = vm.state.collectAsState()
Column() {
ArticleList() {
LogList()
LogList(logs)
}
}
}

@Composable
fun LogList() {
fun LogList(state: State<HomeState>) {
Text(
text="Logs",
color= Color.Black,
text = "Logs",
color = Color.Black,
modifier = Modifier.padding(16.dp, 8.dp),
fontWeight = FontWeight.Bold,
fontStyle = FontStyle.Normal,
fontSize = 20.sp
)
LogCardBase(title = "Connect to http://192.168.11.248:8080", description = "", background = Mi.Color.QUATERNARY)
Column(modifier = Modifier.verticalScroll(state = ScrollState(0))) {
when (val curr = state.value) {
HomeState.Empty -> {
LogCardBase(
title = "Connect to http://192.168.11.18:8080",
description = "",
background = Mi.Color.QUATERNARY
)
}
is HomeState.Log -> {
curr.logs.forEach {
LogCard(log = it)
}
}
}
}
}

@Composable
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package io.vonley.mi.ui.screens.home.presentation

import androidx.lifecycle.ViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import io.vonley.mi.di.annotations.SharedPreferenceStorage
import io.vonley.mi.di.network.MiServer
import io.vonley.mi.di.network.impl.MiServerImpl
import io.vonley.mi.models.Device
import io.vonley.mi.models.MiCMDResponse
import io.vonley.mi.models.MiResponse
import io.vonley.mi.utils.SharedPreferenceManager
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.update
import javax.inject.Inject

sealed class HomeState {
data object Empty : HomeState()
data class Log(val device: Device, val logs: ArrayList<MiCMDResponse>) : HomeState()
}

@HiltViewModel
class HomeViewModel @Inject constructor(
@SharedPreferenceStorage val manager: SharedPreferenceManager,
val jb: MiServer,
) : ViewModel(), MiServerImpl.MiJbServerListener {

private val _state = MutableStateFlow<HomeState>(HomeState.Empty)
val state: StateFlow<HomeState> get() = _state

init {
jb.add(this)
}

override fun onDeviceConnected(device: Device) = Unit
override fun onLog(string: String) = Unit
override fun onJailbreakSucceeded(message: String) = Unit
override fun onJailbreakFailed(message: String) = Unit
override fun onPayloadSent(msg: String?) = Unit
override fun onUnsupported(s: String) = Unit
override fun onSendPayloadAttempt(attempt: Int) = Unit

override fun onCommand(mi: MiResponse<MiResponse.Cmd>) {
_state.update { currState ->
when (currState) {
HomeState.Empty -> HomeState.Log(mi.device!!, arrayListOf(mi))
is HomeState.Log -> if (currState.device != mi.device) {
currState.copy(mi.device!!, arrayListOf(mi))
} else {
val logs = currState.logs
logs.add(mi)
currState.copy(currState.device, logs)
}
}
}
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.vonley.mi.ui.screens.home.article
package io.vonley.mi.ui.screens.home.presentation.article

import androidx.compose.foundation.Image
import androidx.compose.foundation.ScrollState
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.vonley.mi.ui.screens.home.changelog
package io.vonley.mi.ui.screens.home.presentation.changelog


import androidx.compose.foundation.ScrollState
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.vonley.mi.ui.screens.home.log
package io.vonley.mi.ui.screens.home.presentation.log


import androidx.compose.foundation.ScrollState
Expand Down
Loading

0 comments on commit 6569145

Please sign in to comment.