Feat: Added sunset and sunrise to weather widget.

Signed-off-by: HeCodes2Much <wayne6324@gmail.com>
This commit is contained in:
HeCodes2Much
2024-06-12 14:33:16 +01:00
parent 9c6c89f735
commit 63465c088e
7 changed files with 121 additions and 19 deletions

View File

@@ -1,4 +1,4 @@
package com.github.droidworksstudio.launcher.ui.numberpicker
package com.github.droidworksstudio.launcher.adapter.numberpicker
import android.view.LayoutInflater
import android.view.View

View File

@@ -25,8 +25,8 @@ data class Main(
data class Sys(
val country: String,
val sunrise: Int,
val sunset: Int
val sunrise: Long,
val sunset: Long
)
data class Weather(

View File

@@ -35,7 +35,7 @@ import androidx.navigation.NavController
import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.navigateUp
import androidx.navigation.ui.popBackStack
import com.github.droidworksstudio.common.hasInternetPermission
import com.github.droidworksstudio.common.isTablet
import com.github.droidworksstudio.common.showLongToast
@@ -275,10 +275,10 @@ class MainActivity : AppCompatActivity(), LocationListener {
}
}
override fun onSupportNavigateUp(): Boolean {
override fun onSupportpopBackStack(): Boolean {
val navController = findNavController(R.id.nav_host_fragment_content_main)
return navController.navigateUp(appBarConfiguration)
|| super.onSupportNavigateUp()
return navController.popBackStack(appBarConfiguration)
|| super.onSupportpopBackStack()
}
@RequiresApi(Build.VERSION_CODES.R)
@@ -310,7 +310,7 @@ class MainActivity : AppCompatActivity(), LocationListener {
private fun backToHomeScreen() {
navController = findNavController(R.id.nav_host_fragment_content_main)
if (navController.currentDestination?.id != R.id.HomeFragment)
navController.popBackStack(R.id.HomeFragment, false)
navController.navigate(R.id.HomeFragment)
}
private fun checkForUpdates() {

View File

@@ -144,6 +144,11 @@ class WidgetFragment : Fragment(),
// Prepare UI elements concurrently
withContext(Dispatchers.Main) {
binding.apply {
val weatherWidgetDrawable = weatherRoot.background
if (weatherWidgetDrawable is GradientDrawable) {
weatherWidgetDrawable.setColor(widgetBackgroundColor)
}
weatherCity.setTextColor(widgetTextColor)
weatherTemperature.setTextColor(widgetTextColor)
weatherDescription.setTextColor(widgetTextColor)
@@ -151,6 +156,9 @@ class WidgetFragment : Fragment(),
weatherHumidity.setTextColor(widgetTextColor)
weatherRefresh.setTextColor(widgetTextColor)
weatherLastRun.setTextColor(widgetTextColor)
sunsetText.setTextColor(widgetTextColor)
sunriseText.setTextColor(widgetTextColor)
weatherRefresh.typeface = ResourcesCompat.getFont(requireActivity(), R.font.weather)
}
}
@@ -173,10 +181,21 @@ class WidgetFragment : Fragment(),
weatherIcon.setImageBitmap(weatherIconBitmap) // Ensure this matches your ImageView ID
weatherIcon.setColorFilter(widgetTextColor)
val weatherWidgetDrawable = weatherRoot.background
if (weatherWidgetDrawable is GradientDrawable) {
weatherWidgetDrawable.setColor(widgetBackgroundColor)
}
val sunriseIconBitmap = createSunIcon(context, getString(R.string.sunrise_icon))
sunriseIcon.setImageBitmap(sunriseIconBitmap)
sunriseIcon.setColorFilter(widgetTextColor)
val sunsetIconBitmap = createSunIcon(context, getString(R.string.sunset_icon))
sunsetIcon.setImageBitmap(sunsetIconBitmap)
sunsetIcon.setColorFilter(widgetTextColor)
val sunriseTime = convertTimestampToReadableDate(weatherResponse.sys.sunrise)
sunriseText.text = getString(R.string.widget_sunrise_time, sunriseTime)
val sunsetTime = convertTimestampToReadableDate(weatherResponse.sys.sunset)
sunsetText.text = getString(R.string.widget_sunset_time, sunsetTime)
weatherRoot.visibility = View.VISIBLE
}
}
@@ -197,12 +216,28 @@ class WidgetFragment : Fragment(),
paint.typeface = weatherFont
paint.style = Paint.Style.FILL
paint.color = ContextCompat.getColor(context, R.color.white)
paint.textSize = 180f
paint.textSize = 200f
paint.textAlign = Paint.Align.CENTER
canvas.drawText(text, 128f, 200f, paint)
return bitmap
}
private fun createSunIcon(context: Context, text: String): Bitmap {
val bitmap = Bitmap.createBitmap(128, 128, 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 = 96f
paint.textAlign = Paint.Align.CENTER
canvas.drawText(text, 64f, 96f, paint)
return bitmap
}
private fun setWeatherIcon(context: Context, id: Int): String {
val icon: String
val idDivided = id / 100
@@ -239,6 +274,11 @@ class WidgetFragment : Fragment(),
lifecycleScope.launch {
if (!preferenceHelper.showBatteryWidget) return@launch
try {
val weatherBatteryDrawable = binding.batteryRoot.background
if (weatherBatteryDrawable is GradientDrawable) {
weatherBatteryDrawable.setColor(preferenceHelper.widgetBackgroundColor)
}
binding.batteryLevel.setTextColor(preferenceHelper.widgetTextColor)
binding.batteryCount.setTextColor(preferenceHelper.widgetTextColor)
binding.chargingStatus.setTextColor(preferenceHelper.widgetTextColor)
@@ -247,10 +287,6 @@ class WidgetFragment : Fragment(),
binding.batteryVoltage.setTextColor(preferenceHelper.widgetTextColor)
binding.batteryTemperature.setTextColor(preferenceHelper.widgetTextColor)
val weatherBatteryDrawable = binding.batteryRoot.background
if (weatherBatteryDrawable is GradientDrawable) {
weatherBatteryDrawable.setColor(preferenceHelper.widgetBackgroundColor)
}
binding.batteryRoot.visibility = View.VISIBLE
} catch (e: Exception) {
Log.e("Battery", "Failed to fetch battery data: ${e.message}")

View File

@@ -21,7 +21,7 @@ import com.github.droidworksstudio.launcher.helper.PreferenceHelper
import com.github.droidworksstudio.launcher.listener.OnSwipeTouchListener
import com.github.droidworksstudio.launcher.listener.ScrollEventListener
import com.github.droidworksstudio.launcher.ui.bottomsheetdialog.ColorBottomSheetDialogFragment
import com.github.droidworksstudio.launcher.ui.numberpicker.NumberPickerAdapter
import com.github.droidworksstudio.launcher.adapter.numberpicker.NumberPickerAdapter
import com.github.droidworksstudio.launcher.utils.Constants
import com.github.droidworksstudio.launcher.viewmodel.PreferenceViewModel
import com.google.android.material.dialog.MaterialAlertDialogBuilder

View File

@@ -91,8 +91,8 @@
</LinearLayout>
<LinearLayout
android:id="@+id/weather_info"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/topHeaderWeather"
@@ -159,6 +159,69 @@
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
<!-- Horizontal LinearLayout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/weather_info"
android:layout_marginTop="22dp"
android:baselineAligned="false"
android:gravity="center_vertical"
android:orientation="horizontal">
<!-- Left Vertical LinearLayout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
tools:ignore="UseCompoundDrawables">
<!-- ImageView -->
<ImageView
android:id="@+id/sunrise_icon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:contentDescription="@string/widget_weather_description" />
<!-- TextView -->
<TextView
android:id="@+id/sunrise_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/sunrise_icon"
android:gravity="center_horizontal" />
</LinearLayout>
<!-- Right Vertical LinearLayout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
tools:ignore="UseCompoundDrawables">
<!-- ImageView -->
<ImageView
android:id="@+id/sunset_icon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:contentDescription="@string/widget_weather_description" />
<!-- TextView -->
<TextView
android:id="@+id/sunset_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/sunset_icon"
android:gravity="center_horizontal" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
<RelativeLayout

View File

@@ -12,6 +12,9 @@
<string name="widget_weather_temp" translatable="false">%1$.0f %2$s</string>
<string name="widget_weather_description" translatable="false">%1$s</string>
<string name="widget_sunset_time" translatable="false">%1$s</string>
<string name="widget_sunrise_time" translatable="false">%1$s</string>
<string name="widget_weather_mps" translatable="false">m/s</string>
<string name="widget_weather_mph" translatable="false">mil/h</string>