Implemented more details in Home view
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
package com.sockenklaus.batterytracker.ui.composables
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
|
||||
@Composable
|
||||
fun BatteryDetails(){
|
||||
|
||||
}
|
||||
@@ -1,14 +1,15 @@
|
||||
package com.sockenklaus.batterytracker.ui.composables
|
||||
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.LazyListState
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.material.*
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.runtime.livedata.observeAsState
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import androidx.navigation.NavController
|
||||
@@ -16,6 +17,7 @@ import com.sockenklaus.batterytracker.R
|
||||
import com.sockenklaus.batterytracker.room.entities.Battery
|
||||
import com.sockenklaus.batterytracker.ui.composables.util.MyOutlinedTextFieldWithSuffix
|
||||
import com.sockenklaus.batterytracker.ui.models.HomeViewModel
|
||||
import java.lang.reflect.GenericDeclaration
|
||||
|
||||
@OptIn(ExperimentalMaterialApi::class)
|
||||
@Composable
|
||||
@@ -44,11 +46,15 @@ fun Home(navController: NavController) {
|
||||
) {
|
||||
items(filteredList){ battery ->
|
||||
ListItem(
|
||||
text = { Text(battery.name) },
|
||||
text = {
|
||||
ListPrimaryText(battery)
|
||||
},
|
||||
secondaryText = {
|
||||
if(battery.declaredCapacity != null){
|
||||
Text("Capacity: ${battery.declaredCapacity} Ah")
|
||||
}
|
||||
ListSecondaryText(
|
||||
min = model.getMinChargeById(battery.id),
|
||||
avg = model.getAvgChargeById(battery.id),
|
||||
max = model.getMaxChargeById(battery.id)
|
||||
)
|
||||
}
|
||||
)
|
||||
Divider(modHorizontalPadding)
|
||||
@@ -57,29 +63,43 @@ fun Home(navController: NavController) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
class HomeFragment : Fragment() {
|
||||
|
||||
@OptIn(ExperimentalMaterialApi::class)
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
): View {
|
||||
|
||||
return ComposeView(requireContext()).apply {
|
||||
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
|
||||
|
||||
setContent {
|
||||
BatteryTrackerTheme() {
|
||||
|
||||
@Composable
|
||||
fun ListPrimaryText(
|
||||
battery: Battery
|
||||
){
|
||||
Row(
|
||||
horizontalArrangement = Arrangement.SpaceBetween
|
||||
){
|
||||
Text(
|
||||
text = battery.name,
|
||||
fontWeight = FontWeight.Bold,
|
||||
modifier = Modifier.alignByBaseline()
|
||||
)
|
||||
if(battery.declaredCapacity != null){
|
||||
CompositionLocalProvider(LocalContentAlpha provides ContentAlpha.medium) {
|
||||
Text(
|
||||
text = "Cap: ${battery.declaredCapacity} Ah",
|
||||
style = MaterialTheme.typography.body2,
|
||||
modifier = Modifier.alignByBaseline()
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDestroyView() {
|
||||
super.onDestroyView()
|
||||
|
||||
@Composable
|
||||
fun ListSecondaryText(
|
||||
min: Double?,
|
||||
avg: Double?,
|
||||
max: Double?
|
||||
) {
|
||||
Row(
|
||||
horizontalArrangement = Arrangement.SpaceBetween
|
||||
) {
|
||||
|
||||
Text(if(min != null) "Min.: $min Ah" else "")
|
||||
Text(if(avg != null) "Avg.: $avg Ah" else "")
|
||||
Text(if(max != null) "Max.: $max Ah" else "")
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.sockenklaus.batterytracker.ui.models
|
||||
|
||||
import android.app.Application
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.setValue
|
||||
|
||||
@@ -1,11 +1,50 @@
|
||||
package com.sockenklaus.batterytracker.ui.models
|
||||
|
||||
import android.app.Application
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.livedata.observeAsState
|
||||
import androidx.lifecycle.*
|
||||
import com.sockenklaus.batterytracker.room.BatteryTrackerDB
|
||||
import java.math.BigDecimal
|
||||
import java.math.RoundingMode
|
||||
|
||||
class HomeViewModel(application: Application) : AndroidViewModel(application) {
|
||||
|
||||
private val db = BatteryTrackerDB.getInstance(application)
|
||||
val batteries = db.batteryDao().getBatteries().asLiveData()
|
||||
private val batteriesAndCharges = db.batteryDao().getBatteriesAndCharges().asLiveData()
|
||||
|
||||
@Composable
|
||||
fun getMinChargeById(batteryId: Int): Double? {
|
||||
val _batteriesAndCharges by batteriesAndCharges.observeAsState()
|
||||
val batteryAndCharges = _batteriesAndCharges?.find{ it.battery.id == batteryId }
|
||||
|
||||
if(batteryAndCharges != null && batteryAndCharges.charges.isNotEmpty()){
|
||||
return batteryAndCharges.charges.map { it.charge }.min()
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun getMaxChargeById(batteryId: Int): Double? {
|
||||
val _batteriesAndCharges by batteriesAndCharges.observeAsState()
|
||||
val batteryAndCharges = _batteriesAndCharges?.find { it.battery.id == batteryId }
|
||||
|
||||
if(batteryAndCharges != null && batteryAndCharges.charges.isNotEmpty()) {
|
||||
return batteryAndCharges.charges.map { it.charge }.max()
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun getAvgChargeById(batteryId: Int): Double? {
|
||||
val _batteriesAndCharges by batteriesAndCharges.observeAsState()
|
||||
val batteryAndCharges = _batteriesAndCharges?.find{ it.battery.id == batteryId }
|
||||
|
||||
if(batteryAndCharges != null && batteryAndCharges.charges.isNotEmpty()) {
|
||||
return BigDecimal(batteryAndCharges.charges.map { it.charge }.average()).setScale(2, RoundingMode.HALF_UP).toDouble()
|
||||
}
|
||||
return null
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user