moko-resources icon indicating copy to clipboard operation
moko-resources copied to clipboard

SwiftUI extensions for easier integration

Open darronschall opened this issue 2 years ago • 2 comments
trafficstars

Hello!

I've been using moko-resources, and made some SwiftUI extensions that make integration easier. I added one of these to the README in #486, but have since added a few more.

import shared

extension StringResource {
    func localized() -> String {
        return desc().localized()
    }
}

extension Image {
    init(resourceKey: KeyPath<MR.images, shared.ImageResource>) {
        self.init(uiImage: MR.images()[keyPath: resourceKey].toUIImage()!)
    }
}

extension Text {
    init(resourceKey: KeyPath<MR.strings, StringResource>) {
        self.init(MR.strings()[keyPath: resourceKey].localized())
    }
}

extension Button where Label == Text {
    init(resourceKey: KeyPath<MR.strings, StringResource>, action: @escaping () -> Void) {
        self.init(MR.strings()[keyPath: resourceKey].localized(), action: action)
    }
    
    init(resourceKey: KeyPath<MR.strings, StringResource>, role: ButtonRole?, action: @escaping () -> Void) {
        self.init(MR.strings()[keyPath: resourceKey].localized(), role: role, action: action)
    }
}

These extensions make integration with SwiftUI easy, e.g.:

Image(resourceKey: \.logo)
Button(resourceKey: \.menu__logout, role: .destructive) {
    viewModel.logout()
}
Text(resourceKey: \.welcome__body)

It might be nice to incorporate these into moko-resources for other projects using SwiftUI.

darronschall avatar Jun 28 '23 16:06 darronschall

Will it work with this Canvas ?

FelixFalkovsky avatar Jul 30 '23 16:07 FelixFalkovsky

import SwiftUI
import shared
import RswiftResources

extension Text {
    init(
        resourceKey: KeyPath<MR.strings, ResourcesStringResource>,
        bundle: KeyPath<MR.strings, ResourcesStringResource>
    ) {
        self.init(
            LocalizedStringKey(MR.strings()[keyPath: resourceKey].resourceId),
            bundle: MR.strings()[keyPath: resourceKey].bundle
        )
    }
}


Text(resourceKey: \.menu_broadcast, bundle: \.menu_broadcast)

FelixFalkovsky avatar Jul 31 '23 12:07 FelixFalkovsky