I’ve trying to do that ExecuteShortcutIntent
changes viewModel wasExecuted
to true, and then it would making the user defaults shortcutWasExecuted
variable changes too, and then modify the ShortcutView
due to @ObservedObject var viewModel
and .onChange
. but it turns out that onChange
is not called, even that wasExecuted
changes to true
. Is there something wrong in the approach i’m trying to use? What I’m doing wrong?
<code>public class ShortcutsViewModel: ObservableObject {
@AppStorage("shortcutWasExecuted") public var wasExecuted: Bool = false
}
struct ShortcutView: View {
@ObservedObject var viewModel: ShortcutsViewModel = ShortcutsViewModel()
var body: some View {
VStack(content: {
Button(intent: ExecuteShortcutIntent(shortcutId: shortcutViewData!.id)) {
Image(shortcutViewData!.icon, bundle: .main)
.resizable()
.renderingMode(.template)
.scaledToFit()
.foregroundStyle(Color(uiColor: ShortcutColors.getColor(colorString: shortcutViewData!.color)))
.frame(width: iconWidth, height: iconHeight)
}
.buttonStyle(.borderedProminent)
.buttonBorderShape(.roundedRectangle(radius: 4))
.tint(Color("ShortcutBackground", bundle: .main))
.shadow(color: Color("ShortcutShadow", bundle: .main).opacity(shortcutBackgroundShadowOpacity), radius: shortcutBackgroundShadowRadius, x: 0, y: 0)
.controlSize(shortcutButtonSize)
Text(shortcutViewData!.description)
.foregroundStyle(Color("ShortcutDescription", bundle: .main))
.lineLimit(2)
})
.frame(width: shortcutWidth)
.onChange(of: viewModel.wasExecuted, {
viewModel.executeShortcuts(shortcutId: shortcutViewData?.id)
})
}
}
struct ExecuteShortcutIntent: AppIntent {
func perform() async throws -> some IntentResult {
let viewModel = ShortcutsViewModel()
viewModel.wasExecuted = true
return .result()
}
}
</code>
<code>public class ShortcutsViewModel: ObservableObject {
@AppStorage("shortcutWasExecuted") public var wasExecuted: Bool = false
}
struct ShortcutView: View {
@ObservedObject var viewModel: ShortcutsViewModel = ShortcutsViewModel()
var body: some View {
VStack(content: {
Button(intent: ExecuteShortcutIntent(shortcutId: shortcutViewData!.id)) {
Image(shortcutViewData!.icon, bundle: .main)
.resizable()
.renderingMode(.template)
.scaledToFit()
.foregroundStyle(Color(uiColor: ShortcutColors.getColor(colorString: shortcutViewData!.color)))
.frame(width: iconWidth, height: iconHeight)
}
.buttonStyle(.borderedProminent)
.buttonBorderShape(.roundedRectangle(radius: 4))
.tint(Color("ShortcutBackground", bundle: .main))
.shadow(color: Color("ShortcutShadow", bundle: .main).opacity(shortcutBackgroundShadowOpacity), radius: shortcutBackgroundShadowRadius, x: 0, y: 0)
.controlSize(shortcutButtonSize)
Text(shortcutViewData!.description)
.foregroundStyle(Color("ShortcutDescription", bundle: .main))
.lineLimit(2)
})
.frame(width: shortcutWidth)
.onChange(of: viewModel.wasExecuted, {
viewModel.executeShortcuts(shortcutId: shortcutViewData?.id)
})
}
}
struct ExecuteShortcutIntent: AppIntent {
func perform() async throws -> some IntentResult {
let viewModel = ShortcutsViewModel()
viewModel.wasExecuted = true
return .result()
}
}
</code>
public class ShortcutsViewModel: ObservableObject {
@AppStorage("shortcutWasExecuted") public var wasExecuted: Bool = false
}
struct ShortcutView: View {
@ObservedObject var viewModel: ShortcutsViewModel = ShortcutsViewModel()
var body: some View {
VStack(content: {
Button(intent: ExecuteShortcutIntent(shortcutId: shortcutViewData!.id)) {
Image(shortcutViewData!.icon, bundle: .main)
.resizable()
.renderingMode(.template)
.scaledToFit()
.foregroundStyle(Color(uiColor: ShortcutColors.getColor(colorString: shortcutViewData!.color)))
.frame(width: iconWidth, height: iconHeight)
}
.buttonStyle(.borderedProminent)
.buttonBorderShape(.roundedRectangle(radius: 4))
.tint(Color("ShortcutBackground", bundle: .main))
.shadow(color: Color("ShortcutShadow", bundle: .main).opacity(shortcutBackgroundShadowOpacity), radius: shortcutBackgroundShadowRadius, x: 0, y: 0)
.controlSize(shortcutButtonSize)
Text(shortcutViewData!.description)
.foregroundStyle(Color("ShortcutDescription", bundle: .main))
.lineLimit(2)
})
.frame(width: shortcutWidth)
.onChange(of: viewModel.wasExecuted, {
viewModel.executeShortcuts(shortcutId: shortcutViewData?.id)
})
}
}
struct ExecuteShortcutIntent: AppIntent {
func perform() async throws -> some IntentResult {
let viewModel = ShortcutsViewModel()
viewModel.wasExecuted = true
return .result()
}
}