Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unit mess and make it user preference. #60

Open
hossain-khan opened this issue Jan 7, 2025 · 2 comments
Open

Fix unit mess and make it user preference. #60

hossain-khan opened this issue Jan 7, 2025 · 2 comments

Comments

@hossain-khan
Copy link
Owner

hossain-khan commented Jan 7, 2025

Metric / imperial? Remove hard coded stuff.

Edit: so far this is not exactly a big mess because both weather services currently used has the unit millimeter for both rain and snow.

@hossain-khan
Copy link
Owner Author

Here are the steps to make the mm and cm units configurable by the user in your Android app:

  1. Identify Hard-Coded Units:

    • Located in app/src/main/java/dev/hossain/weatheralert/data/Models.kt in the WeatherAlertCategory enum.
    • Located in app/src/main/java/dev/hossain/weatheralert/notification/Notification.kt in the triggerNotification function.
    • Located in app/src/main/java/dev/hossain/weatheralert/work/WeatherCheckWorker.kt in the triggerNotification function.
    • Located in app/src/main/java/dev/hossain/weatheralert/ui/alertslist/CurrentAlertScreen.kt in the AlertTileData class.
  2. Manage User Preferences:

    • Use the UserPreferences and PreferencesManager classes found in app/src/main/java/dev/hossain/weatheralert/data/.
  3. Update Code to Use Configurable Units:

    • Modify the WeatherAlertCategory enum to remove the hard-coded units.
    • Update the triggerNotification functions to fetch units from user preferences.
    • Modify the AlertSettingsScreen to allow users to select their preferred unit.

Here is an example of the changes needed:

  • Update WeatherAlertCategory enum:

    enum class WeatherAlertCategory(val label: String) {
        SNOW_FALL("Snow"),
        RAIN_FALL("Rain"),
    }
  • Modify triggerNotification function:

    internal fun triggerNotification(
        context: Context,
        snowTomorrow: Double,
        rainTomorrow: Double,
        snowThreshold: Float,
        rainThreshold: Float,
        unit: String
    ) {
        val notificationText = buildString {
            if (snowTomorrow > snowThreshold) append("Snowfall: $snowTomorrow $unit\n")
            if (rainTomorrow > rainThreshold) append("Rainfall: $rainTomorrow $unit")
        }
        // Rest of the function remains unchanged
    }
  • Add Unit Selection in AlertSettingsScreen:

    // Assuming you have a dropdown or similar UI element for unit selection
    data class Event(val unit: String) : CircuitUiEvent
  1. Save and Retrieve User Preferences:
    • Use UserPreferences and PreferencesManager to save and retrieve the user's selected unit.

These steps should help you make the units configurable by the user. You can view the full results and details in the GitHub UI here.

@hossain-khan
Copy link
Owner Author

To model the unit correctly based on Rain or Snow and make it configurable by the user, follow these steps:

  1. Update Data Models:

    • Modify WeatherAlertCategory to remove hard-coded units.
    • Add unit settings to UserPreferences.
  2. Update Notification Logic:

    • Fetch the unit from user preferences when triggering notifications.
  3. Update UI for Configuring Alerts:

    • Add options for users to select their preferred units for both rain and snow alerts.

Here are example changes for each step:

  • Update WeatherAlertCategory Enum:

    enum class WeatherAlertCategory(val label: String) {
        SNOW_FALL("Snow"),
        RAIN_FALL("Rain"),
    }
  • Modify User Preferences:

    object UserPreferences {
        val savedAlerts = stringPreferencesKey("configured_user_alerts")
        val rainUnit = stringPreferencesKey("rain_unit")
        val snowUnit = stringPreferencesKey("snow_unit")
    }
  • Update triggerNotification Function:

    internal fun triggerNotification(
        context: Context,
        snowTomorrow: Double,
        rainTomorrow: Double,
        snowThreshold: Float,
        rainThreshold: Float,
        snowUnit: String,
        rainUnit: String
    ) {
        val notificationText = buildString {
            if (snowTomorrow > snowThreshold) append("Snowfall: $snowTomorrow $snowUnit\n")
            if (rainTomorrow > rainThreshold) append("Rainfall: $rainTomorrow $rainUnit")
        }
        // Rest of the function remains unchanged
    }
  • Add Unit Selection in AlertSettingsScreen:

    // Assuming you have a dropdown or similar UI element for unit selection
    data class Event(val rainUnit: String, val snowUnit: String) : CircuitUiEvent

These steps should ensure that the units are correctly modeled based on the type of alert and are configurable by the user. You can view the full results and details in the GitHub UI here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant