-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
255 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
app/src/main/kotlin/com/zjutjh/ijh/ui/component/ElectricityStatusCard.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package com.zjutjh.ijh.ui.component | ||
|
||
import android.content.Context | ||
import androidx.compose.animation.AnimatedContent | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.filled.ElectricBolt | ||
import androidx.compose.material3.CircularProgressIndicator | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.text.style.TextAlign | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.compose.ui.unit.dp | ||
import com.zjutjh.ijh.R | ||
import com.zjutjh.ijh.model.ElectricityBalance | ||
import com.zjutjh.ijh.ui.theme.IJhTheme | ||
import com.zjutjh.ijh.util.LoadResult | ||
import java.time.LocalDateTime | ||
import java.time.format.DateTimeFormatter | ||
import java.time.format.FormatStyle | ||
|
||
@Composable | ||
fun ElectricityStatusCard( | ||
modifier: Modifier = Modifier, | ||
balance: LoadResult<ElectricityBalance?>, | ||
) { | ||
val context = LocalContext.current | ||
val subtitle = remember(balance) { | ||
prompt(context, if (balance is LoadResult.Loading) null else LocalDateTime.now()) | ||
} | ||
|
||
GlanceCard( | ||
modifier = modifier, | ||
title = stringResource(id = R.string.dorm_electricity), | ||
subtitle = subtitle, | ||
icon = Icons.Default.ElectricBolt | ||
) { | ||
AnimatedContent( | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.padding(16.dp), | ||
targetState = balance, | ||
contentAlignment = Alignment.Center, | ||
label = "Loading", | ||
) { | ||
when (it) { | ||
is LoadResult.Loading -> Column( | ||
horizontalAlignment = Alignment.CenterHorizontally | ||
) { | ||
CircularProgressIndicator() | ||
Text( | ||
text = stringResource(id = R.string.loading), | ||
textAlign = TextAlign.Center | ||
) | ||
} | ||
|
||
is LoadResult.Ready -> { | ||
val text = if (it.data == null) { | ||
"N/A" | ||
} else { | ||
"${it.data.total} KW•h" | ||
} | ||
|
||
Text( | ||
modifier = Modifier.padding(vertical = 8.dp), | ||
color = MaterialTheme.colorScheme.primary, | ||
text = text, | ||
style = MaterialTheme.typography.displaySmall, | ||
textAlign = TextAlign.Center, | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun prompt(context: Context, syncTime: LocalDateTime? = null): String = buildString { | ||
val divider = " • " | ||
append(context.getString(R.string.balance)) | ||
append(divider) | ||
if (syncTime == null) { | ||
append(context.getString(R.string.unknown)) | ||
} else { | ||
append(DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM).format(syncTime)) | ||
} | ||
} | ||
|
||
|
||
@Preview | ||
@Composable | ||
private fun ElectricityStatusCardPreview() { | ||
IJhTheme { | ||
ElectricityStatusCard( | ||
balance = LoadResult.Ready( | ||
ElectricityBalance( | ||
total = 200f, | ||
totalAmount = 100f, | ||
subsidy = 100f, | ||
subsidyAmount = 50f, | ||
surplus = 100f, | ||
surplusAmount = 50f | ||
) | ||
) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
core/common/src/main/kotlin/com/zjutjh/ijh/model/ElectricityBalance.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.zjutjh.ijh.model | ||
|
||
import androidx.compose.runtime.Stable | ||
|
||
@Stable | ||
data class ElectricityBalance( | ||
val total: Float, | ||
val totalAmount: Float, | ||
val subsidy: Float, | ||
val subsidyAmount: Float, | ||
val surplus: Float, | ||
val surplusAmount: Float | ||
) |
8 changes: 8 additions & 0 deletions
8
core/data/src/main/kotlin/com/zjutjh/ijh/data/ElectricityRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.zjutjh.ijh.data | ||
|
||
import com.zjutjh.ijh.model.ElectricityBalance | ||
|
||
interface ElectricityRepository { | ||
|
||
suspend fun getBalance(): ElectricityBalance | ||
} |
Oops, something went wrong.