From 392aebcad7c45dfab29992fdc0244d826e1f6d74 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sat, 21 Dec 2024 22:55:35 +0800 Subject: [PATCH] :bug: Fix android widgets --- .../solsynth/solian/widgets/CheckInWidget.kt | 42 ++++++++++----- .../solian/widgets/RandomPostWidget.kt | 54 ++----------------- .../app/src/main/res/xml/check_in_widget.xml | 2 +- 3 files changed, 33 insertions(+), 65 deletions(-) diff --git a/android/app/src/main/kotlin/dev/solsynth/solian/widgets/CheckInWidget.kt b/android/app/src/main/kotlin/dev/solsynth/solian/widgets/CheckInWidget.kt index 132cde4..8f9be0b 100644 --- a/android/app/src/main/kotlin/dev/solsynth/solian/widgets/CheckInWidget.kt +++ b/android/app/src/main/kotlin/dev/solsynth/solian/widgets/CheckInWidget.kt @@ -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,7 +42,9 @@ class CheckInWidget : GlanceAppWidget() { override suspend fun provideGlance(context: Context, id: GlanceId) { provideContent { - GlanceContent(context, currentState()) + GlanceTheme { + GlanceContent(context, currentState()) + } } } @@ -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( + 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,18 +102,18 @@ 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", - style = TextStyle(fontSize = 15.sp) - ) + Text( + text = "You haven't checked in today", + style = TextStyle(fontSize = 15.sp) + ) + } } } diff --git a/android/app/src/main/kotlin/dev/solsynth/solian/widgets/RandomPostWidget.kt b/android/app/src/main/kotlin/dev/solsynth/solian/widgets/RandomPostWidget.kt index b83aab9..cc01592 100644 --- a/android/app/src/main/kotlin/dev/solsynth/solian/widgets/RandomPostWidget.kt +++ b/android/app/src/main/kotlin/dev/solsynth/solian/widgets/RandomPostWidget.kt @@ -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( @@ -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) diff --git a/android/app/src/main/res/xml/check_in_widget.xml b/android/app/src/main/res/xml/check_in_widget.xml index 17cf674..0b33ead 100644 --- a/android/app/src/main/res/xml/check_in_widget.xml +++ b/android/app/src/main/res/xml/check_in_widget.xml @@ -1,6 +1,6 @@