Feat: add icon to weather widget.

Signed-off-by: HeCodes2Much <wayne6324@gmail.com>
This commit is contained in:
HeCodes2Much
2024-06-07 23:09:34 +01:00
parent 76b922cdcc
commit 75afa09bea
8 changed files with 90 additions and 37 deletions

View File

@@ -22,12 +22,7 @@ object DatabaseModule {
@Singleton
fun provideLocalDatabase(
@ApplicationContext context: Context
): AppDatabase =
Room.databaseBuilder(
context,
AppDatabase::class.java,
"app_database"
).build()
): AppDatabase = Room.databaseBuilder(context, AppDatabase::class.java, "app_database").build()
@Provides
@Singleton

View File

@@ -4,6 +4,7 @@ data class WeatherResponse(
val name: String,
val main: Main,
val wind: Wind,
val sys: Sys,
val weather: List<Weather>
)
@@ -22,5 +23,10 @@ data class Main(
)
data class Weather(
val description: String
val description: String,
val id: Int
)
data class Sys(
val country: String
)

View File

@@ -85,8 +85,7 @@ class SettingsFragment : Fragment(),
binding.gesturesSwipeUpControl.text = preferenceHelper.swipeUpAction.getString(context)
binding.gesturesSwipeDownControl.text = preferenceHelper.swipeDownAction.getString(context)
binding.gesturesSwipeLeftControl.text = preferenceHelper.swipeLeftAction.getString(context)
binding.gesturesSwipeRightControl.text =
preferenceHelper.swipeRightAction.getString(context)
binding.gesturesSwipeRightControl.text = preferenceHelper.swipeRightAction.getString(context)
}
@SuppressLint("SetTextI18n")

View File

@@ -2,12 +2,17 @@ package com.github.droidworksstudio.launcher.ui.widgets
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.drawable.GradientDrawable
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.core.content.res.ResourcesCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.navigation.NavController
@@ -22,6 +27,7 @@ import com.github.droidworksstudio.launcher.listener.ScrollEventListener
import com.github.droidworksstudio.launcher.utils.Constants
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import java.util.Calendar
import javax.inject.Inject
@AndroidEntryPoint
@@ -79,14 +85,11 @@ class WidgetFragment : Fragment(),
try {
binding.weatherRoot.visibility = View.VISIBLE
val weatherResponse = appHelper.fetchWeatherData(context, latitude, longitude)
val temperatureScale =
if (preferenceHelper.weatherUnits == Constants.Units.Metric) getString(R.string.weather_c) else getString(
R.string.weather_f
)
val speedScale =
if (preferenceHelper.weatherUnits == Constants.Units.Metric) getString(R.string.weather_mps) else getString(
R.string.weather_mph
)
Log.d("weatherResponse", "$weatherResponse")
val temperatureScale = if (preferenceHelper.weatherUnits == Constants.Units.Metric) getString(R.string.weather_c) else getString(
R.string.weather_f
)
val speedScale = if (preferenceHelper.weatherUnits == Constants.Units.Metric) getString(R.string.weather_mps) else getString(R.string.weather_mph)
binding.weatherCity.setTextColor(preferenceHelper.widgetTextColor)
binding.weatherTemperature.setTextColor(preferenceHelper.widgetTextColor)
@@ -95,25 +98,15 @@ class WidgetFragment : Fragment(),
binding.weatherHumidity.setTextColor(preferenceHelper.widgetTextColor)
binding.weatherPressure.setTextColor(preferenceHelper.widgetTextColor)
binding.weatherCity.text =
getString(R.string.widget_weather_location, weatherResponse.name)
binding.weatherTemperature.text =
getString(
R.string.widget_weather_temp,
weatherResponse.main.temp,
temperatureScale
)
binding.weatherDescription.text =
getString(
R.string.widget_weather_description,
weatherResponse.weather[0].description
)
binding.weatherWind.text =
getString(R.string.widget_weather_wind, weatherResponse.wind.speed, speedScale)
binding.weatherHumidity.text =
getString(R.string.widget_weather_humidity, weatherResponse.main.humidity)
binding.weatherPressure.text =
getString(R.string.widget_weather_pressure, weatherResponse.main.pressure)
binding.weatherCity.text = getString(R.string.widget_weather_location, weatherResponse.name, weatherResponse.sys.country)
binding.weatherTemperature.text = getString(R.string.widget_weather_temp, weatherResponse.main.temp, temperatureScale)
binding.weatherDescription.text = getString(R.string.widget_weather_description, weatherResponse.weather[0].description)
binding.weatherWind.text = getString(R.string.widget_weather_wind, weatherResponse.wind.speed, speedScale)
binding.weatherHumidity.text = getString(R.string.widget_weather_humidity, weatherResponse.main.humidity)
binding.weatherPressure.text = getString(R.string.widget_weather_pressure, weatherResponse.main.pressure)
val weatherIcon = createWeatherIcon(context, setWeatherIcon(context, weatherResponse.weather[0].id))
binding.weatherIcon.setImageBitmap(weatherIcon)
val weatherWidgetDrawable = binding.weatherRoot.background
if (weatherWidgetDrawable is GradientDrawable) {
@@ -128,6 +121,46 @@ class WidgetFragment : Fragment(),
}
}
private fun createWeatherIcon(context: Context, text: String): Bitmap {
val bitmap = Bitmap.createBitmap(256, 256, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
val paint = Paint()
val weatherFont = ResourcesCompat.getFont(requireActivity(), R.font.weather)
paint.isAntiAlias = true
paint.isSubpixelText = true
paint.typeface = weatherFont
paint.style = Paint.Style.FILL
paint.color = ContextCompat.getColor(context, R.color.white)
paint.textSize = 180f
paint.textAlign = Paint.Align.CENTER
canvas.drawText(text, 128f, 200f, paint)
return bitmap
}
private fun setWeatherIcon(context: Context, id: Int): String {
var icon = ""
val idDivided = id / 100
if (idDivided * 100 == 800) {
val hourOfDay = Calendar.getInstance().get(Calendar.HOUR_OF_DAY)
icon = if (hourOfDay in 7..19) {
context.getString(R.string.weather_sunny)
} else {
context.getString(R.string.weather_clear_night)
}
} else {
icon = when (idDivided) {
2 -> context.getString(R.string.weather_thunder)
3 -> context.getString(R.string.weather_drizzle)
7 -> context.getString(R.string.weather_foggy)
8 -> context.getString(R.string.weather_cloudy)
6 -> context.getString(R.string.weather_snowy)
5 -> context.getString(R.string.weather_rainy)
else -> ""
}
}
return icon
}
@SuppressLint("ClickableViewAccessibility")
private fun observeSwipeTouchListener() {

Binary file not shown.

View File

@@ -85,6 +85,7 @@
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_marginEnd="18dp"
android:layout_weight="0.3"
android:contentDescription="@string/widget_weather_description" />

View File

@@ -2,7 +2,7 @@
<string name="battery_level" translatable="false">%s%%</string>
<string name="widget_weather_refresh" translatable="false">%1$s</string>
<string name="widget_weather_location" translatable="false">%1$s</string>
<string name="widget_weather_location" translatable="false">%1$s, %2$s</string>
<string name="widget_weather_temp" translatable="false">%1$.2f %2$s</string>
<string name="widget_weather_description" translatable="false">%1$s</string>

View File

@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="humidity_icon" translatable="false">&#xf07a;</string>
<string name="pressure_icon" translatable="false">&#xf079;</string>
<string name="speed_icon" translatable="false">&#xf050;</string>
<string name="sunrise_icon" translatable="false">&#xf051;</string>
<string name="sunset_icon" translatable="false">&#xf052;</string>
<string name="weather_sunny" translatable="false">&#xf00d;</string>
<string name="weather_clear_night" translatable="false">&#xf02e;</string>
<string name="weather_foggy" translatable="false">&#xf014;</string>
<string name="weather_cloudy" translatable="false">&#xf013;</string>
<string name="weather_rainy" translatable="false">&#xf019;</string>
<string name="weather_snowy" translatable="false">&#xf01b;</string>
<string name="weather_thunder" translatable="false">&#xf01e;</string>
<string name="weather_drizzle" translatable="false">&#xf01a;</string>
<string name="rain" translatable="false">&#xf019;</string>
<string name="snow" translatable="false">&#xf01b;</string>
</resources>