I’ve a efficiency challenge (I assume) with my widget, let me clarify first what I need to obtain.
I am making an attempt to create an interactive widget that acts as a calculator, The code is working effectively, however the widget it is not very responsive if I am making an attempt to rapidly press the buttons.
I discovered a very good instance equivalent to this one, that works effectively when buttons are pressed rapidly : https://apps.apple.com/us/app/calculator-widget-simple-calc/id6465680493
Do you’ve gotten any options on doable enhancements ? I used to be fascinated with Consumer Defaults efficiency, however unsure what to assume, since I am simply utilizing some primary stuff there.
Thanks on your assist.
Right here the intent I am utilizing when the buttons are pressed :
struct CalculatorDigitIntent: AppIntent {
static var title: LocalizedStringResource = "Calculator"
@Parameter(title: "Digit")
var digit: String
init() {}
init(digit: String) {
self.digit = digit
}
func carry out() async throws -> some IntentResult {
UserDefaults.commonplace.double(forKey: "calculatorAmount")
var calculatorAmount = UserDefaults.commonplace.double(forKey: "calculatorAmount")
change digit {
case "00":
if calculatorAmount != 0 {
calculatorAmount *= 100
}
case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9":
if let digit = Double(digit) {
if calculatorAmount != 0 || digit != 0 {
calculatorAmount = calculatorAmount * 10 + digit
}
}
default:
break
}
UserDefaults.commonplace.set(calculatorAmount, forKey: "calculatorAmount")
return .end result()
}
}
Right here is the timeline supplier :
struct CalculatorProvider: AppIntentTimelineProvider {
func placeholder(in context: Context) -> FXWCalculatorEntry {
CalculatorEntry(date: Date(), quantity: 0.0)
}
func snapshot(for configuration: ConfigurationAppIntent, in context: Context) async -> CalculatorEntry {
CalculatorEntry(date: Date(), quantity: 0.0)
}
func timeline(for configuration: ConfigurationAppIntent, in context: Context) async -> Timeline {
let digits = getCalculatorDigits()
let entry = CalculatorEntry(date: Date(), quantity: digits)
let timeline = Timeline(entries: [entry], coverage: .by no means)
return timeline
}
func getCalculatorDigits() -> Double {
return UserDefaults.commonplace.double(forKey: "calculatorAmount")
}
}
From my understanding from documentation, every time when an app intent it is known as, the timeline will probably be refreshed.
In my opinion code, I simply have easy buttons like this one :
Button(intent: CalculatorDigitIntent(digit: "4")) {
Textual content("4")
.font(.system(dimension: 20).daring())
.foregroundStyle(Colour(hex: "#252F2C"))
}