Feat: add icon to weather widget.
Signed-off-by: HeCodes2Much <wayne6324@gmail.com>
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
)
|
||||
@@ -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")
|
||||
|
||||
@@ -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() {
|
||||
|
||||
BIN
app/src/main/res/font/weather.ttf
Normal file
BIN
app/src/main/res/font/weather.ttf
Normal file
Binary file not shown.
@@ -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" />
|
||||
|
||||
|
||||
@@ -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>
|
||||
|
||||
|
||||
19
app/src/main/res/values/weathericons.xml
Normal file
19
app/src/main/res/values/weathericons.xml
Normal file
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<string name="humidity_icon" translatable="false"></string>
|
||||
<string name="pressure_icon" translatable="false"></string>
|
||||
<string name="speed_icon" translatable="false"></string>
|
||||
<string name="sunrise_icon" translatable="false"></string>
|
||||
<string name="sunset_icon" translatable="false"></string>
|
||||
|
||||
<string name="weather_sunny" translatable="false"></string>
|
||||
<string name="weather_clear_night" translatable="false"></string>
|
||||
<string name="weather_foggy" translatable="false"></string>
|
||||
<string name="weather_cloudy" translatable="false"></string>
|
||||
<string name="weather_rainy" translatable="false"></string>
|
||||
<string name="weather_snowy" translatable="false"></string>
|
||||
<string name="weather_thunder" translatable="false"></string>
|
||||
<string name="weather_drizzle" translatable="false"></string>
|
||||
<string name="rain" translatable="false"></string>
|
||||
<string name="snow" translatable="false"></string>
|
||||
</resources>
|
||||
Reference in New Issue
Block a user