Procházet zdrojové kódy

Auto-scroll to selected tone in picker

Jonas Björkert před 1 rokem
rodič
revize
63a5f623c8

+ 27 - 21
LoopFollow/Alarm/AlarmEditing/Components/AlarmAudioSection.swift

@@ -96,33 +96,39 @@ private struct TonePickerSheet: View {
 
     var body: some View {
         NavigationView {
-            List {
-                ForEach(SoundFile.allCases) { tone in
-                    Button {
-                        selected = tone
-                        AlarmSound.setSoundFile(str: tone.rawValue)
-                        AlarmSound.stop()
-                        AlarmSound.playTest()
-                    } label: {
-                        HStack {
-                            Text(tone.displayName)
-                            if tone == selected {
-                                Spacer()
-                                Image(systemName: "checkmark")
-                                    .foregroundColor(.accentColor)
+            ScrollViewReader { proxy in
+                List {
+                    ForEach(SoundFile.allCases) { tone in
+                        Button {
+                            selected = tone
+                            AlarmSound.setSoundFile(str: tone.rawValue)
+                            AlarmSound.stop()
+                            AlarmSound.playTest()
+                        } label: {
+                            HStack {
+                                Text(tone.displayName)
+                                if tone == selected {
+                                    Spacer()
+                                    Image(systemName: "checkmark")
+                                        .foregroundColor(.accentColor)
+                                }
                             }
                         }
+                        .id(tone)
                     }
                 }
-            }
-            .navigationTitle("Choose Tone")
-            .toolbar {
-                ToolbarItem(placement: .confirmationAction) {
-                    Button("Done") {
-                        AlarmSound.stop()
-                        dismiss()
+                .navigationTitle("Choose Tone")
+                .toolbar {
+                    ToolbarItem(placement: .confirmationAction) {
+                        Button("Done") {
+                            AlarmSound.stop()
+                            dismiss()
+                        }
                     }
                 }
+                .onAppear {
+                    proxy.scrollTo(selected, anchor: .center)
+                }
             }
         }
     }