Post

Replies

Boosts

Views

Activity

iOS 17 App Intent Widget → How do i make a filler when their is no information in the intent data?
Hi! I'm working on a widget using the new app intent & widgetkit features. Until now everything works, but I can't figure out how to give for example a line of text or a dummy data when the intent data is empty. For example in the Widgetsmith app, you can put pictures on your homescreen using widgets. But from the moment that you delete the picture in the app, the widget changes to a text screen where they have placed information about changing the widget to another option. Can you help me? My code so far: // // TimersIntentWidget.swift // // Created by Mees Akveld on 25/09/2023. // import Foundation import AppIntents import WidgetKit import SwiftUI @available(iOS 17, *) struct PresetIntentWidget: AppIntentTimelineProvider { func timeline(for configuration: selectPresetIntent, in context: Context) async -> Timeline<PresetEntry> { let entry = PresetEntry(date: Date(), presetModel: configuration.preset) let timeline = Timeline(entries: [entry], policy: .never) return timeline } func snapshot(for configuration: selectPresetIntent, in context: Context) async -> PresetEntry { PresetEntry(date: Date(), presetModel: configuration.preset) } func placeholder(in context: Context) -> PresetEntry { PresetEntry(date: Date(), presetModel: WidgetPreset.allPresets[0]) } } @available(iOS 17, *) struct PresetEntry: TimelineEntry { let date: Date let presetModel: WidgetPreset } @available(iOS 17, *) struct PresetWidgetView: View { let entry: PresetEntry private func isColorTheme() -> Int { let defaults = UserDefaults(suiteName: "group.MeesAkveld.ShareDefaults") let colorTheme = defaults?.integer(forKey: "ColorTheme") ?? 1 if colorTheme == 0 { return 1 } else { return colorTheme } } var body: some View { GeometryReader { geometry in ZStack { Image("bannerT\(isColorTheme())_Small") .resizable() .aspectRatio(contentMode: .fill) .scaleEffect(1.11) HStack { Rectangle() .cornerRadius(20) .frame(width: geometry.size.width - 20, height: geometry.size.height - 20, alignment: .center) .foregroundStyle(customBlue2Func()) Spacer() } .padding(.leading, 10) HStack { VStack(alignment: .leading, spacing: 10) { Image(systemName: entry.presetModel.icon != "" ? entry.presetModel.icon : "timer") .font(.system(size: 30)) .multilineTextAlignment(.center) .frame(width: 18) .offset(x: 10) VStack(alignment: .leading){ Text(entry.presetModel.title != "" ? entry.presetModel.title : "") .bold() Text(timeToText(hours: entry.presetModel.hours, minutes: entry.presetModel.minutes, seconds: entry.presetModel.seconds)) } .lineLimit(entry.presetModel.id == "noPresets" ? 2 : 1) } .foregroundColor(customBlueFunc()) Spacer() } .padding(25) } .widgetURL(URL(string:"preset:///\(entry.presetModel.id)")) } } } @available(iOS 17, *) struct PresetWidgetConfiguration: Widget { var body: some WidgetConfiguration { AppIntentConfiguration( kind: "presetWidgetIntent", intent: selectPresetIntent.self, provider: PresetIntentWidget() ) { entry in PresetWidgetView(entry: entry) } .configurationDisplayName("Presets") .description("View and activate a preset of your choosing from your homescreen.") .contentMarginsDisabled() .supportedFamilies([.systemSmall]) } } @available(iOS 17, *) struct PresetIntentWidget_Previews: PreviewProvider { static var previews: some View { PresetWidgetView(entry: PresetEntry(date: Date(), presetModel: WidgetPreset(id: "", title: "Title", icon: "", hours: 1, minutes: 1, seconds: 1, showEditView: false))) } } @available(iOS 17, *) struct WidgetPreset: AppEntity, Decodable { var id: String var title: String var icon: String var hours: Int var minutes: Int var seconds: Int var showEditView: Bool static var typeDisplayRepresentation: TypeDisplayRepresentation = "Preset" static var defaultQuery = WidgetPresetQuery() var displayRepresentation: DisplayRepresentation { DisplayRepresentation(title: "\(title)", subtitle: "\(timeToText(hours: hours, minutes: minutes, seconds: seconds))") } static var allPresets: [WidgetPreset]{ get { let defaults = UserDefaults(suiteName: "group.MeesAkveld.ShareDefaults") let presetsData = defaults!.data(forKey: "presets") if let data = presetsData, let loadedPresets = try? JSONDecoder().decode([WidgetPreset].self, from: data) { return loadedPresets } else { return [] } } } } @available(iOS 17, *) struct WidgetPresetQuery: EntityQuery { func entities(for identifiers: [WidgetPreset.ID]) async throws -> [WidgetPreset] { WidgetPreset.allPresets.filter { identifiers.contains($0.id) } } func suggestedEntities() async throws -> [WidgetPreset] { WidgetPreset.allPresets } func defaultResult() async -> WidgetPreset? { WidgetPreset.allPresets.first } } @available(iOS 17, *) struct selectPresetIntent: WidgetConfigurationIntent { static var title: LocalizedStringResource = "Preset" static var description: IntentDescription = IntentDescription("Select a preset") @Parameter(title: "Selected preset:") var preset: WidgetPreset }
0
1
1k
Sep ’23
Export Compliance Information
Hi! I made a timer app and I want to beta test the app using TestFlight but when I'm uploading the app I need to make a choice out of 4 options for the Export Compliance Information. I can't figure out what option I need to choose. The options: Encryption algorithms that are proprietary or not accepted as standard by international standard bodies (IEEE, IETF, ITU, etc.) Standard encryption algorithms instead of, or in addition to, using or accessing the encryption within Apple's operating system Both algorithms mentioned above None of the algorithms mentioned above Any advice? Thanks!
1
0
593
Sep ’23
How to perform an action based on the local time?
Hi! I would like to make text appear based on the local time of the user. The idea is that when it's between 00:00:01 AM and 11:00:00 AM, that the text will say "breakfast" and that when it's between 11:00:00 AM and 03:00:00 PM, the text will say "lunch". Etc. I found this code online but I can't get it working. Can you help me? Possibly with other code? . Contentview: struct Test: View { var now = Date() var breakfastTime = Date.parse("00:00:01") var lunchTime = Date.parse("11:00:00") var dinerTime = Date.parse("16:00:00") func activeDinerCourse() -> String { if now > breakfastTime && now < lunchTime { return "Breakfast" } if now > lunchTime && now < dinerTime { return "Lunch" } if now > dinerTime && now < Date.parse("23:59:59"){ return "Diner" } return "Something went wrong..." } var date = Date.now.formatted(date: .omitted, time: .shortened) var body: some View { VStack { Text("\(date)") Text(activeDinerCourse()) } } } . .Parse extention public extension Date { static func parse(_ string: String, format: String = "HH:mm:ss") -> Date { let dateFormatter = DateFormatter() dateFormatter.timeZone = NSTimeZone.default dateFormatter.dateFormat = format let date = dateFormatter.date(from: string)! return date } func dateString(_ format: String = "HH:mm:ss") -> String { let dateFormatter = DateFormatter() dateFormatter.dateFormat = format return dateFormatter.string(from: self) } } . Thank you!
2
0
1.6k
Jun ’23
Update / End live activity
Hi! I'm working on a timer app. In the app I want that when you start a timer, there also starts a live activity showcasing the remaining time. So for I have got so far that the live activity starts when the timer start, but I don't know how to do the next parts. So I got a couple of questions: How do I stop the live activity when the timer ends? How do I update the live activity (for example if I want to change the title while the timer is counting down?) -> I've found two functions online but they don't work for me. I've included them below. . My code so far that works: The file of the live activity import SwiftUI import WidgetKit import ActivityKit struct TimersWidgetLiveActivitiesView: View { let context: ActivityViewContext<TimerAttributes> private func ColorTheme() -> Int { let defaults = UserDefaults(suiteName: "group.MeesAkveld.ShareDefaults") return defaults?.integer(forKey: "ColorTheme") ?? 1 } var body: some View { ZStack{ Rectangle() .foregroundColor(.blue) HStack(){ Image(systemName: context.state.icon) Text(context.state.title == "" ? "Timer" : context.state.title) Spacer() Text(context.state.time , style: .timer) } .padding() .foregroundColor(.white) } } } struct TimersWidgetLiveActivities: Widget { let kind: String = "TimersWidgetLiveActivities" var body: some WidgetConfiguration { ActivityConfiguration(for: TimerAttributes.self) { context in TimersWidgetLiveActivitiesView(context: context) } dynamicIsland: { context in DynamicIsland { DynamicIslandExpandedRegion(.leading) { Text("Leading") } DynamicIslandExpandedRegion(.trailing) { Text("Trailing") } DynamicIslandExpandedRegion(.bottom) { Text("Bottom") } } compactLeading: { Text("L") } compactTrailing: { Text("T") } minimal: { Text("Min") } } } } . The file of the TimerAttributes import SwiftUI import ActivityKit struct TimerAttributes: ActivityAttributes { public typealias TimerStatus = ContentState public struct ContentState: Codable, Hashable { var time = Date() var title = String() var icon = String() } } . The function that I use to start the Live Activity: func startActivityT1() { let attributes = TimerAttributes() let state = TimerAttributes.TimerStatus(time: Date().addingTimeInterval(Double(vm.totalSecondsFunc() + 3600)), title: "Timer", icon: "timer") activity = try? Activity<TimerAttributes>.request(attributes: attributes, contentState: state, pushType: nil) } . The functions that I found online to update / end the live activity that don't work: Stop Live Activity func stopActivityT1() { let state = TimerAttributes.TimerStatus(time: .now, title: "Timer", icon: "timer") Task { await activity?.end(using: state, dismissalPolicy: .immediate) } } . Update Live Activity func updateActivityT1() { let state = TimerAttributes.TimerStatus(time: Date().addingTimeInterval(Double(vm.totalSecondsFunc() + 3600)), title: oD.titleT1, icon: oD.iconT1) Task { await activity?.update(using: state) } }
1
1
2.1k
Apr ’23
Animation Error: 'animation' was deprecated in iOS 15.0: Use withAnimation or animation(_:value:) instead.
Hi! I'm working on a feature where when I click on a button, I want to rotate the button continuously until I press it again. The animation works but now I get this error: 'animation' was deprecated in iOS 15.0: Use withAnimation or animation(_:value:) instead. I've looked online and couldn't find I solution for this particular problem. This is the code I've used: struct ContentView: View { @EnvironmentObject var oD: ObservableData var body: some View { Button(action: { print("ButtonAction") }){ Image(systemName: (oD.presetsSettings ? "checkmark.circle" : "gear")) .frame(width: 20, height: 20) .animation(.easeInOut(duration: 0.35).delay(0), value: oD.presetsSettings) .rotationEffect((oD.presetsSettings ? .degrees(-10) : .degrees(80))) .animation(.easeInOut(duration: 0.30), value: oD.presetsSettings) .rotationEffect((oD.presetsSettings ? .degrees(-20) : .degrees(20))) .animation(oD.presetsSettings ? .linear(duration: 0.15).repeatForever(autoreverses: true) : .linear(duration: 0.2)) } . The oD.presetsSettings to what I refer is a Bool, with the standard value of false. Is there a way to get rid of the 'error'?
2
0
1.2k
Mar ’23
Picker with attached functions.
Is there a way to attached multiple functions (via action) per selected option in a Picker (Wheelview)? I have this code so far: struct Picker1Zon: View { //Variable @EnvironmentObject var name: SelectNames //Waarden @EnvironmentObject var vuur: VuurValues @EnvironmentObject var aarde: AardeValues @EnvironmentObject var lucht: LuchtValues @EnvironmentObject var water: WaterValues //PickerSelect @EnvironmentObject var picker: PickerSelectValues //Profiles @ObservedObject var profileRam = RamProfile() @ObservedObject var profileStier = StierProfile() @ObservedObject var profileTweelingen = TweelingenProfile() @ObservedObject var profileKreeft = KreeftProfile() @ObservedObject var profileLeeuw = LeeuwProfile() @ObservedObject var profileMaagd = MaagdProfile() @ObservedObject var profileWeegschaal = WeegschaalProfile() @ObservedObject var profileSchorpioen = SchorpioenProfile() @ObservedObject var profileBoogschutter = BoogschutterProfile() @ObservedObject var profileSteenbok = SteenbokProfile() @ObservedObject var profileWaterman = WatermanProfile() @ObservedObject var profileVissen = VissenProfile() var body: some View { VStack{ Picker(selection: $name.Select1Variable , label: Text("")){ Group { Text("Selecteer...").tag(" ") Text("\(profileRam.sterrenbeeldIcon) Ram").tag("Ram") Text("\(profileStier.sterrenbeeldIcon) Stier").tag("Stier") Text("\(profileTweelingen.sterrenbeeldIcon) Tweelingen").tag("Tweelingen") Text("\(profileKreeft.sterrenbeeldIcon) Kreeft").tag("Kreeft") Text("\(profileLeeuw.sterrenbeeldIcon) Leeuw").tag("Leeuw") Text("\(profileMaagd.sterrenbeeldIcon) Maagd").tag("Maagd") } Group { Text("\(profileWeegschaal.sterrenbeeldIcon) Weegschaal").tag("Weegschaal") Text("\(profileSchorpioen.sterrenbeeldIcon) Schorpioen").tag("Schorpioen") Text("\(profileBoogschutter.sterrenbeeldIcon) Boogschutter").tag("Boogschutter") Text("\(profileSteenbok.sterrenbeeldIcon) Steenbok").tag("Steenbok") Text("\(profileWaterman.sterrenbeeldIcon) Waterman").tag("Waterman") Text("\(profileVissen.sterrenbeeldIcon) Vissen").tag("Vissen") } } .pickerStyle(WheelPickerStyle()) .padding(.horizontal, 90.0) .background(content: { Rectangle() .foregroundColor(.white) .padding(.vertical, -15) .cornerRadius(17) .frame(width: 250, height: 200) }) } .animation(.easeInOut(duration: 0.35).delay(0.25), value: picker.picker1) .scaleEffect(picker.picker1 ? 1 : 0) .opacity(picker.picker1 ? 1 : 0.2) .animation(.easeInOut(duration: 0.30), value: picker.picker1) } } . In this code design I have only one observed variable that changes, but I would like to change multiple. Is there a way to do this?
9
0
939
Mar ’23
Picker Animation get's in the way of buttons, what to do?
Hi! I'm making a picker selection tool where when you click on a button, a picker emerges with an animated scale effect. But the problem is that when I want to click on another button, the 'invisible' picker from another button selection is in the way. As if it's there but you can't see it, because of the scale effect animation. . The activation of the animation gets activation with a button with this code: Button(action: { picker.picker1 = true }, label: {     Text("Button")                   }         .font(.system(size: 16))     }      }) . I've used this code for the picker: VStack{     Picker(selection: $name.Select1Variable , label: Text("")){         Group {             Text("Selecteer...").tag("                ")             Text("\(profileRam.sterrenbeeldIcon)  Ram").tag("Ram")             Text("\(profileStier.sterrenbeeldIcon)  Stier").tag("Stier")             Text("\(profileTweelingen.sterrenbeeldIcon)  Tweelingen").tag("Tweelingen")             Text("\(profileKreeft.sterrenbeeldIcon)  Kreeft").tag("Kreeft")             Text("\(profileLeeuw.sterrenbeeldIcon)  Leeuw").tag("Leeuw")             Text("\(profileMaagd.sterrenbeeldIcon)  Maagd").tag("Maagd")             }         Group {             Text("\(profileWeegschaal.sterrenbeeldIcon)  Weegschaal").tag("Weegschaal")             Text("\(profileSchorpioen.sterrenbeeldIcon)  Schorpioen").tag("Schorpioen")             Text("\(profileBoogschutter.sterrenbeeldIcon)  Boogschutter").tag("Boogschutter")             Text("\(profileSteenbok.sterrenbeeldIcon)  Steenbok").tag("Steenbok")             Text("\(profileWaterman.sterrenbeeldIcon)  Waterman").tag("Waterman")             Text("\(profileVissen.sterrenbeeldIcon)  Vissen").tag("Vissen")         }     } .pickerStyle(WheelPickerStyle()) .padding(.horizontal, 80.0) .background(content: {     Rectangle()      .foregroundColor(.white)     .padding(.vertical, -15)     .cornerRadius(17)     .frame(width: 250, height: 200)     }) } .animation(.easeInOut(duration: 0.35).delay(0.25), value: picker.picker1) .scaleEffect(picker.picker1 ? 1 : 0) .animation(.easeInOut(duration: 0.30), value: picker.picker1) . Is there a solution for this problem?
4
0
1.1k
Mar ’23
How to implement EnvironmentObject in my project?
Hi! I'm working on an app where there are multiple menu's and if you chose an option, an observable object changes it value. This happens inside the menu via the action section. For example: vuur.vuurZonValue = 18. Now I have placed these multiple menu's in different files because otherwise Xcode would give me an error that it takes too long to calculate. My observable int data looks like this: import SwiftUI class VuurValues: ObservableObject {     @Published var vuurZonValue = 0         @Published var vuurMaanValue = 0     @Published var vuurAscedantValue = 0     @Published var vuurVenusValue = 0     @Published var vuurMarsValue = 0     @Published var vuurJupiterValue = 0     @Published var vuurSaturnusValue = 0     @Published var vuurMCValue = 0     @Published var vuurUranusValue = 0     @Published var vuurNeptunesValue = 0     @Published var vuurPlutoValue = 0     @Published var vuurTotaal = 0     func sumValues() -> Int {         return vuurZonValue + vuurMaanValue + vuurAscedantValue + vuurVenusValue + vuurMarsValue + vuurJupiterValue + vuurSaturnusValue + vuurMCValue + vuurUranusValue + vuurNeptunesValue + vuurPlutoValue     } } . So when I call the sumValues() function I do it like this: Text(String("\(vuur.sumValues())")) FYI: 'vuur' is how i called the observed object in the file where I want to see the result. I've called the data via this text above below the struct line: @ObservedObject var vuur = VuurValues() . But now the problem is that when I want to read the sum function of all the int data that changed based on the chosen options in the different menu's, it doesn't work. When I insert the text (second box ↑) in the project where everything comes together I just keep getting 0. But when I insert the same text in the file of for example the first menu, it works but I can only change the value of that menu and not the others. Can you help me?
1
0
503
Feb ’23
How to sum observed int data?
Hi! I'm working on an app where the user chooses 11 times an menu option, and after that the user chooses, the intention is that depending on what they chose, that there is a calculation of multiple int data and the result will be shown as text on the screen. Currently every menu option is done. I've added an observable int file and when the preferred option is selected the int data that I've chosen will be changed. So now I've got a lot of observable/observed int data but I don't know how to sum the int data. Can you help me? This is how I set-up the @observable data files: import Foundation import SwiftUI class Select1Variable: ObservableObject {        @Published var Select1Variable = "Selecteer..." }
3
1
600
Feb ’23