Search code examples
swiftswiftuicombine

How to update SwiftUI view after @AppStorage in a separate struct gets updated


I have a following class:

struct PriceFormatter {
    @AppStorage(UserDefaultsKey.savedCurrency)
    var savedCurrency: String?

    let price: Float
    
    init(price: Float) {
        self.price = price
    }
    
    var formatted: String {
        return "\(savedCurrency) \(price)"
    }
}

And the following view:

struct PriceText: View {
    let price: Float
    
    var body: some View {
        Text(PriceFormatter(price: self.price).formatted)
    }
}

I want the view to get rerendered after savedCurrency from UserDefaults changes.

I made it work easily, when @AppStorage was a part of the view, but I'm not sure what to do in this case. I tried to use @ObservableObject with @Published or tried to make a Combine Publisher and subscribe to it but also had no success.


Solution

  • UPDATE

    As pointed by @Klaas starting with iOS 14.5 one can use @AppStorage inside the ObservableObject (https://developer.apple.com/documentation/ios-ipados-release-notes/ios-ipados-14_5-release-notes)

    Working solution would now look something like this:

    class PriceFormatter: ObservableObject {
        @AppStorage(UserDefaultsKey.savedCurrency)
        var savedCurrency: String?
    
        let price: Float
        
        init(price: Float) {
            self.price = price
        }
        
        var formatted: String {
            return "\(savedCurrency) \(price)"
        }
    }
    

    And use like this:

    struct PriceText: View {
        @StateObject var formatter = PriceFormatter(price: 5)
        
        var body: some View {
            Text(formatter.formatted)
        }
    }
    

    Old Answer. Valid for iOS < 14.5

    As pointed by @Asperi @AppStorage should be used only in View. Implicitly it is also written here: https://developer.apple.com/documentation/swiftui/appstorage