Pārlūkot izejas kodu

refactor: extract status picker to component

Sam Jaffe 2 nedēļas atpakaļ
vecāks
revīzija
485d99ba26

+ 34 - 0
Todos/View/Components/StatusPicker.swift

@@ -0,0 +1,34 @@
+//
+//  StatusPicker.swift
+//  Todos
+//
+//  Created by Sam Jaffe on 3/7/26.
+//
+
+import SwiftUI
+
+struct StatusPicker: View {
+  @Binding var status : Status
+  var displayIcon : Bool = true
+
+  var body: some View {
+    HStack {
+      if displayIcon {
+        Image(systemName: status.label)
+          .frame(width: 20)
+          .padding(.trailing, -10)
+      }
+      Picker("", selection: $status) {
+        ForEach(Status.allCases) { unit in
+          Text(unit.description).tag(unit)
+        }
+      }
+      .fixedSize(horizontal: true, vertical: false)
+    }
+  }
+}
+
+#Preview {
+  @Previewable @State var status = Status.todo
+  StatusPicker(status: $status, displayIcon: true)
+}

+ 1 - 9
Todos/View/SubTaskView.swift

@@ -20,15 +20,7 @@ struct SubTaskView: View {
         Label("", systemImage: "chevron.right")
           .padding(.trailing, -10)
 
-        Image(systemName: task.status.label)
-          .frame(width: 20)
-          .padding(.trailing, -10)
-        Picker("", selection: $task.status) {
-          ForEach(Status.allCases) { unit in
-            Text(unit.description).tag(unit)
-          }
-        }
-        .fixedSize(horizontal: true, vertical: false)
+        StatusPicker(status: $task.status)
 
         TextField("Task Name", text: $task.name)
           .focused($isFocused)

+ 7 - 15
Todos/View/TaskView.swift

@@ -22,22 +22,14 @@ struct TaskView: View {
   var body: some View {
     VStack {
       HStack {
-        Image(systemName: task.status.label)
-          .frame(width: 20)
-          .padding(.trailing, -10)
-        Picker("", selection: $task.status) {
-          ForEach(Status.allCases) { unit in
-            Text(unit.description).tag(unit)
-          }
-        }
-        .fixedSize(horizontal: true, vertical: false)
-        .onChange(of: task.status) {
-          if task.status.isStrong {
-            task.subtasks
-              .filter({ !$0.status.isStrong })
-              .forEach({ subtask in subtask.status = task.status })
+        StatusPicker(status: $task.status)
+          .onChange(of: task.status) {
+            if task.status.isStrong {
+              task.subtasks
+                .filter({ !$0.status.isStrong })
+                .forEach({ subtask in subtask.status = task.status })
+            }
           }
-        }
 
         TextField("Task Name", text: $task.name)
           .focused($isFocused)