🐛 Fix android widgets
This commit is contained in:
parent
e9e3a4c474
commit
392aebcad7
@ -6,6 +6,7 @@ import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import androidx.glance.GlanceId
|
||||
import androidx.glance.GlanceModifier
|
||||
import androidx.glance.GlanceTheme
|
||||
import androidx.glance.action.clickable
|
||||
import androidx.glance.appwidget.GlanceAppWidget
|
||||
import androidx.glance.appwidget.provideContent
|
||||
@ -25,8 +26,10 @@ import androidx.glance.text.Text
|
||||
import androidx.glance.text.TextStyle
|
||||
import com.google.gson.FieldNamingPolicy
|
||||
import com.google.gson.GsonBuilder
|
||||
import dev.solsynth.solian.MainActivity
|
||||
import dev.solsynth.solian.data.InstantAdapter
|
||||
import dev.solsynth.solian.data.SolarCheckInRecord
|
||||
import es.antonborri.home_widget.actionStartActivity
|
||||
import java.time.Instant
|
||||
import java.time.LocalDate
|
||||
import java.time.OffsetDateTime
|
||||
@ -39,9 +42,11 @@ class CheckInWidget : GlanceAppWidget() {
|
||||
|
||||
override suspend fun provideGlance(context: Context, id: GlanceId) {
|
||||
provideContent {
|
||||
GlanceTheme {
|
||||
GlanceContent(context, currentState())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun GlanceContent(context: Context, currentState: HomeWidgetGlanceState) {
|
||||
@ -53,18 +58,27 @@ class CheckInWidget : GlanceAppWidget() {
|
||||
val resultTierSymbols = listOf("大凶", "凶", "中平", "吉", "大吉")
|
||||
|
||||
val prefs = currentState.preferences
|
||||
val checkInRaw = prefs.getString("pas_check_in_record", null)
|
||||
val checkInRaw: String? = prefs.getString("pas_check_in_record", null)
|
||||
|
||||
val checkIn: SolarCheckInRecord? =
|
||||
checkInRaw?.let { checkInRaw ->
|
||||
gson.fromJson(checkInRaw, SolarCheckInRecord::class.java)
|
||||
} ?: null;
|
||||
|
||||
Column(
|
||||
modifier = GlanceModifier
|
||||
.fillMaxWidth()
|
||||
.fillMaxHeight()
|
||||
.background(Color.White)
|
||||
.background(GlanceTheme.colors.widgetBackground)
|
||||
.padding(16.dp)
|
||||
.clickable(
|
||||
onClick = actionStartActivity<MainActivity>(
|
||||
context,
|
||||
Uri.parse("https://sn.solsynth.dev")
|
||||
)
|
||||
)
|
||||
) {
|
||||
if (checkInRaw != null) {
|
||||
val checkIn: SolarCheckInRecord =
|
||||
gson.fromJson(checkInRaw, SolarCheckInRecord::class.java)
|
||||
if (checkIn != null) {
|
||||
val dateFormatter = DateTimeFormatter.ofPattern("EEE, MM/dd")
|
||||
|
||||
val checkDate = checkIn.createdAt.atZone(ZoneId.of("UTC")).toLocalDate()
|
||||
@ -73,11 +87,11 @@ class CheckInWidget : GlanceAppWidget() {
|
||||
Column {
|
||||
Text(
|
||||
text = resultTierSymbols[checkIn.resultTier],
|
||||
style = TextStyle(fontSize = 25.sp, fontFamily = FontFamily.Serif)
|
||||
style = TextStyle(fontSize = 17.sp)
|
||||
)
|
||||
Text(
|
||||
text = "+${checkIn.resultExperience} EXP",
|
||||
style = TextStyle(fontSize = 15.sp, fontFamily = FontFamily.Monospace)
|
||||
style = TextStyle(fontSize = 13.sp, fontFamily = FontFamily.Monospace)
|
||||
)
|
||||
}
|
||||
Spacer(modifier = GlanceModifier.height(8.dp))
|
||||
@ -88,14 +102,13 @@ class CheckInWidget : GlanceAppWidget() {
|
||||
ZoneId.systemDefault()
|
||||
)
|
||||
.format(dateFormatter),
|
||||
style = TextStyle(fontSize = 13.sp)
|
||||
style = TextStyle(fontSize = 11.sp)
|
||||
)
|
||||
}
|
||||
|
||||
return@Column;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Text(
|
||||
text = "You haven't checked in today",
|
||||
@ -103,3 +116,4 @@ class CheckInWidget : GlanceAppWidget() {
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
import HomeWidgetGlanceState
|
||||
import HomeWidgetGlanceStateDefinition
|
||||
import android.content.Context
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.BitmapFactory
|
||||
import android.net.Uri
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.graphics.Color
|
||||
@ -9,17 +9,13 @@ import androidx.compose.ui.unit.sp
|
||||
import androidx.glance.GlanceId
|
||||
import androidx.glance.GlanceModifier
|
||||
import androidx.glance.GlanceTheme
|
||||
import androidx.glance.Image
|
||||
import androidx.glance.ImageProvider
|
||||
import androidx.glance.action.clickable
|
||||
import androidx.glance.appwidget.GlanceAppWidget
|
||||
import androidx.glance.appwidget.cornerRadius
|
||||
import androidx.glance.appwidget.provideContent
|
||||
import androidx.glance.background
|
||||
import androidx.glance.currentState
|
||||
import androidx.glance.layout.Alignment
|
||||
import androidx.glance.layout.Column
|
||||
import androidx.glance.layout.ContentScale
|
||||
import androidx.glance.layout.Row
|
||||
import androidx.glance.layout.Spacer
|
||||
import androidx.glance.layout.fillMaxHeight
|
||||
@ -39,10 +35,6 @@ import dev.solsynth.solian.MainActivity
|
||||
import dev.solsynth.solian.data.InstantAdapter
|
||||
import dev.solsynth.solian.data.SolarPost
|
||||
import es.antonborri.home_widget.actionStartActivity
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import okhttp3.Response
|
||||
import okio.IOException
|
||||
import java.time.Instant
|
||||
import java.time.LocalDateTime
|
||||
import java.time.ZoneId
|
||||
@ -52,45 +44,18 @@ class RandomPostWidget : GlanceAppWidget() {
|
||||
override val stateDefinition: GlanceStateDefinition<*>?
|
||||
get() = HomeWidgetGlanceStateDefinition()
|
||||
|
||||
private val defaultUrl = "https://api.sn.solsynth.dev"
|
||||
|
||||
override suspend fun provideGlance(context: Context, id: GlanceId) {
|
||||
provideContent {
|
||||
GlanceTheme {
|
||||
GlanceContent(context, currentState(), null)
|
||||
GlanceContent(context, currentState())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private val client = OkHttpClient()
|
||||
|
||||
private fun resizeBitmap(bitmap: Bitmap, maxWidth: Int, maxHeight: Int): Bitmap {
|
||||
val aspectRatio = bitmap.width.toFloat() / bitmap.height.toFloat()
|
||||
val newWidth = if (bitmap.width > maxWidth) maxWidth else bitmap.width
|
||||
val newHeight = (newWidth / aspectRatio).toInt()
|
||||
val resizedBitmap = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, true)
|
||||
return resizedBitmap
|
||||
}
|
||||
|
||||
private fun loadImageFromUrl(url: String): Bitmap? {
|
||||
val request = Request.Builder().url(url).build()
|
||||
|
||||
return try {
|
||||
val response: Response = client.newCall(request).execute()
|
||||
val inputStream = response.body?.byteStream()
|
||||
val bitmap = BitmapFactory.decodeStream(inputStream)
|
||||
resizeBitmap(bitmap, 120, 120)
|
||||
} catch (e: IOException) {
|
||||
e.printStackTrace()
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun GlanceContent(
|
||||
context: Context,
|
||||
currentState: HomeWidgetGlanceState,
|
||||
avatar: Bitmap?
|
||||
) {
|
||||
val prefs = currentState.preferences
|
||||
val postRaw = prefs.getString("int_random_post", null)
|
||||
@ -109,7 +74,7 @@ class RandomPostWidget : GlanceAppWidget() {
|
||||
modifier = GlanceModifier
|
||||
.fillMaxWidth()
|
||||
.fillMaxHeight()
|
||||
.background(Color.White)
|
||||
.background(GlanceTheme.colors.widgetBackground)
|
||||
.padding(16.dp)
|
||||
.clickable(
|
||||
onClick = actionStartActivity<MainActivity>(
|
||||
@ -120,17 +85,6 @@ class RandomPostWidget : GlanceAppWidget() {
|
||||
) {
|
||||
if (data != null) {
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
if (avatar != null) {
|
||||
Image(
|
||||
provider = ImageProvider(bitmap = avatar),
|
||||
contentDescription = null,
|
||||
modifier = GlanceModifier.width(36.dp).height(36.dp)
|
||||
.cornerRadius(18.dp),
|
||||
contentScale = ContentScale.Crop
|
||||
)
|
||||
Spacer(modifier = GlanceModifier.width(8.dp))
|
||||
}
|
||||
|
||||
Text(
|
||||
text = data.publisher.nick,
|
||||
style = TextStyle(fontSize = 15.sp)
|
||||
|
@ -1,6 +1,6 @@
|
||||
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:initialLayout="@layout/glance_default_loading_layout"
|
||||
android:minWidth="120dp"
|
||||
android:minWidth="40dp"
|
||||
android:minHeight="40dp"
|
||||
android:resizeMode="horizontal|vertical"
|
||||
android:updatePeriodMillis="10000">
|
||||
|
Loading…
Reference in New Issue
Block a user