Files
PlantGuide/PlantGuide/Domain/Entities/CareNotificationPreferences.swift
Trey t 136dfbae33 Add PlantGuide iOS app with plant identification and care management
- Implement camera capture and plant identification workflow
- Add Core Data persistence for plants, care schedules, and cached API data
- Create collection view with grid/list layouts and filtering
- Build plant detail views with care information display
- Integrate Trefle botanical API for plant care data
- Add local image storage for captured plant photos
- Implement dependency injection container for testability
- Include accessibility support throughout the app

Bug fixes in this commit:
- Fix Trefle API decoding by removing duplicate CodingKeys
- Fix LocalCachedImage to load from correct PlantImages directory
- Set dateAdded when saving plants for proper collection sorting

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-23 12:18:01 -06:00

130 lines
4.6 KiB
Swift

//
// CareNotificationPreferences.swift
// PlantGuide
//
// Per-plant notification preferences for care task reminders.
//
import Foundation
// MARK: - CareNotificationPreferences
/// User preferences for which care task types should trigger notifications.
///
/// Each plant can have its own notification preferences, allowing users to
/// enable or disable reminders for specific types of care tasks.
struct CareNotificationPreferences: Codable, Sendable, Equatable {
/// Whether watering reminder notifications are enabled
var wateringEnabled: Bool
/// Whether fertilizing reminder notifications are enabled
var fertilizingEnabled: Bool
/// Whether repotting reminder notifications are enabled
var repottingEnabled: Bool
/// Whether pruning reminder notifications are enabled
var pruningEnabled: Bool
// MARK: - Initialization
/// Creates a new CareNotificationPreferences instance.
///
/// - Parameters:
/// - wateringEnabled: Whether watering reminders are enabled. Defaults to true.
/// - fertilizingEnabled: Whether fertilizing reminders are enabled. Defaults to true.
/// - repottingEnabled: Whether repotting reminders are enabled. Defaults to false.
/// - pruningEnabled: Whether pruning reminders are enabled. Defaults to false.
init(
wateringEnabled: Bool = true,
fertilizingEnabled: Bool = true,
repottingEnabled: Bool = false,
pruningEnabled: Bool = false
) {
self.wateringEnabled = wateringEnabled
self.fertilizingEnabled = fertilizingEnabled
self.repottingEnabled = repottingEnabled
self.pruningEnabled = pruningEnabled
}
// MARK: - Convenience Methods
/// Returns whether notifications are enabled for the given task type.
///
/// - Parameter taskType: The type of care task to check.
/// - Returns: `true` if notifications are enabled for this task type.
func isEnabled(for taskType: CareTaskType) -> Bool {
switch taskType {
case .watering:
return wateringEnabled
case .fertilizing:
return fertilizingEnabled
case .repotting:
return repottingEnabled
case .pruning:
return pruningEnabled
case .pestControl:
return false // Not currently supported
}
}
/// Returns a copy with the notification preference updated for the given task type.
///
/// - Parameters:
/// - taskType: The type of care task to update.
/// - enabled: Whether notifications should be enabled.
/// - Returns: A new `CareNotificationPreferences` with the updated value.
func setting(_ taskType: CareTaskType, enabled: Bool) -> CareNotificationPreferences {
var updated = self
switch taskType {
case .watering:
updated.wateringEnabled = enabled
case .fertilizing:
updated.fertilizingEnabled = enabled
case .repotting:
updated.repottingEnabled = enabled
case .pruning:
updated.pruningEnabled = enabled
case .pestControl:
break // Not currently supported
}
return updated
}
// MARK: - Storage
/// UserDefaults key prefix for storing per-plant notification preferences
private static let keyPrefix = "care_notification_prefs_"
/// Saves the preferences to UserDefaults for the given plant ID.
///
/// - Parameter plantID: The unique identifier of the plant.
func save(for plantID: UUID) {
let key = Self.keyPrefix + plantID.uuidString
if let data = try? JSONEncoder().encode(self) {
UserDefaults.standard.set(data, forKey: key)
}
}
/// Loads the preferences from UserDefaults for the given plant ID.
///
/// - Parameter plantID: The unique identifier of the plant.
/// - Returns: The stored preferences, or a default instance if none exist.
static func load(for plantID: UUID) -> CareNotificationPreferences {
let key = keyPrefix + plantID.uuidString
guard let data = UserDefaults.standard.data(forKey: key),
let preferences = try? JSONDecoder().decode(CareNotificationPreferences.self, from: data) else {
return CareNotificationPreferences()
}
return preferences
}
/// Removes the stored preferences for the given plant ID.
///
/// - Parameter plantID: The unique identifier of the plant.
static func remove(for plantID: UUID) {
let key = keyPrefix + plantID.uuidString
UserDefaults.standard.removeObject(forKey: key)
}
}