As shown by the video linked below, when I move off of the main page, the selected picker always defaults back to the 'select exercise' picker. However, I would like it to be able to remember which picker was last selected, and keep the picker selected when moving back to the main page.
Is there any way to programmatically control the selected picker in a view?
Video: https://streamable.com/djmvso
Example of one picker:
var body: some View {
VStack(spacing: -15) {
Text("Exercise")
.font(.system(size: 10))
.if(controlSelection == .exercise) {
$0.bold()
}
Picker("", selection: $workout.currentExerciseChoice) {
ForEach(filteredExercises, id: \.self) { exercise in
HStack {
// Current exercise in the workout.
CircleImageExercise(exercise: exercise,
size: 24,
showDetails: true)
Spacer()
// Current exercise name.
Text(exercise.name ?? "")
.font(.system(size: 14))
.multilineTextAlignment(.center)
.scaledToFit()
.minimumScaleFactor(0.5)
.lineLimit(1)
Spacer()
}
.padding(.leading, 5)
.tag(exercise as Exercise?)
}
}
.frame(height: WKInterfaceDevice.current().screenBounds.height / 4)
.onTapGesture {
controlSelection = .exercise
}
// Update time, weight and reps values to current exercise defaults.
.onChange(of: workout.currentExerciseChoice) { exerciseChoice in
if (exerciseChoice?.isHold ?? false) {
workout.currentTimeChoice = exerciseChoice?.defaultHold ?? 30.0
} else {
workout.currentWeightChoice = exerciseChoice?.defaultWeight ?? 5.0
workout.currentRepChoice = exerciseChoice?.defaultReps ?? 5.0
}
}
}
}
Post
Replies
Boosts
Views
Activity
My complication code is very similar to that found in the Migrating ClockKit complications to WidgetKit tutorial. Except, I am only interested in displaying the app icon as the complication.
I just cannot get any 'custom' images to display in the simulator, nor on a Watch. Image(systemImage: "") work fine, however.
Does the image need to be re-sized to a specific resolution? The original image is 1024×1024, I have tried re-sizing this down to multiple smaller resolutions with no success.
import WidgetKit
import SwiftUI
import Intents
struct Provider: IntentTimelineProvider {
func placeholder(in context: Context) -> SimpleEntry {
SimpleEntry(date: Date(), configuration: ConfigurationIntent())
}
func getSnapshot(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (SimpleEntry) -> ()) {
let entry = SimpleEntry(date: Date(), configuration: configuration)
completion(entry)
}
func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
var entries: [SimpleEntry] = []
// Generate a timeline consisting of five entries an hour apart, starting from the current date.
let currentDate = Date()
for hourOffset in 0 ..< 5 {
let entryDate = Calendar.current.date(byAdding: .hour, value: hourOffset, to: currentDate)!
let entry = SimpleEntry(date: entryDate, configuration: configuration)
entries.append(entry)
}
let timeline = Timeline(entries: entries, policy: .atEnd)
completion(timeline)
}
func recommendations() -> [IntentRecommendation<ConfigurationIntent>] {
return [
IntentRecommendation(intent: ConfigurationIntent(), description: "Treadmill Assistant")
]
}
}
struct SimpleEntry: TimelineEntry {
let date: Date
let configuration: ConfigurationIntent
}
struct Tread_Cadence_ComplicationEntryView : View {
var entry: Provider.Entry
var body: some View {
Image("AppIcon")
//.resizable()
//.frame(width: 50, height: 50)
//.scaledToFill()
}
}
@main
struct Tread_Cadence_Complication: Widget {
let kind: String = "Tread_Cadence_Complication"
var body: some WidgetConfiguration {
IntentConfiguration(kind: kind, intent: ConfigurationIntent.self, provider: Provider()) { entry in
Tread_Cadence_ComplicationEntryView(entry: entry)
}
.configurationDisplayName("Treadmill Assistant")
.description("Opens Treadmill Assistant.")
}
}
struct Tread_Cadence_Complication_Previews: PreviewProvider {
static var previews: some View {
Tread_Cadence_ComplicationEntryView(entry: SimpleEntry(date: Date(), configuration: ConfigurationIntent()))
.previewContext(WidgetPreviewContext(family: .accessoryRectangular))
}
}
Is anyone able to answer any of these questions? There's very little information on them on Apple's website and I am struggling to find information about them online in relation to the Apple Watch.
What exactly are gravity and quaternion?
How are they being calculated (I'm assuming some kind of sensor fusion between the accelerometer and gyroscope)?
What are the units of gravity and quaternion?
I'm working on a companion Watch App for my iOS App.
.onAppear() and .onDisappear() do not run if they're inside a child view of the three main views in my content view. For example, my first page is a list of workouts, If I click and navigate to a workout on the list the .onAppear() on that workout page does not run.
Is this a bug on WatchOS 8 beta 4 or am I doing something wrong? The iOS app has very similar functionality and the .onAppear/.onDisappear all work fine on iOS 15 beta 4. Lastly, they seem to work in previews, just not in simulators or my device.
The content view for the app (Those 3 functions do run):
import SwiftUI
import CoreData
struct ContentView: View {
@Environment(\.managedObjectContext) private var viewContext
var body: some View {
TabView {
WorkoutList()
ExerciseList()
HeartRate()
}
.onAppear {
loadDefaultExercises()
loadDefaultWorkouts()
loadProfile()
}
}
func loadDefaultExercises() -> () {... }
func loadDefaultExercises() -> () {... }
func loadDefaultExercises() -> () {... }
}
The WorkoutList() view (.onAppear/.onDisappears placed in here do run but only once at app launch):
import SwiftUI
struct WorkoutList: View {
@Environment(\.managedObjectContext) private var viewContext
@FetchRequest(
sortDescriptors: [NSSortDescriptor(keyPath: \Workout.name, ascending: true)],
animation: .default)
var workouts: FetchedResults<Workout>
var body: some View {
NavigationView {
List {
ForEach(workouts) { workout in
NavigationLink(destination: WorkoutDetail(workout: workout)) {
WorkoutListRow(workout: workout)
}
}
}
.navigationTitle("Workouts")
.onAppear {
print("On Appear (Working but only once on launch)")
}
}
}
}
The WorkoutDetail page that appears when I click on a workout from WorkoutList() (.onAppear/.onDisappears placed in here on any child views from here do not run):
import SwiftUI
import CoreData
struct WorkoutDetail: View {
@ObservedObject var workout: Workout
var body: some View {
ScrollView {
VStack(alignment: .leading) {... }
}
.onAppear {
print("On Appear (NOT WORKING)")
}
.navigationTitle(workout.name ?? "")
}
}
Thanks in advance.