diff options
Diffstat (limited to 'iosApp/iosApp/Devices/DeviceRow.swift')
-rw-r--r-- | iosApp/iosApp/Devices/DeviceRow.swift | 237 |
1 files changed, 133 insertions, 104 deletions
diff --git a/iosApp/iosApp/Devices/DeviceRow.swift b/iosApp/iosApp/Devices/DeviceRow.swift index f93687a..4553a4b 100644 --- a/iosApp/iosApp/Devices/DeviceRow.swift +++ b/iosApp/iosApp/Devices/DeviceRow.swift @@ -27,131 +27,160 @@ struct DeviceRow: View { case commands } var callback: (Action) -> () + var isCell: Bool = true @State var isSheet: Bool = false var body: some View { - let row = HStack { - /* Device icon */ - let category = Marker.companion.categoryToMarkerType(category: unit.device.category) - Image(MarkerTransformations.markerTypeToImageName(markerType: category)) - .padding(5.0) + if isCell { + let row = HStack { + /* MARK: - Device icon */ + let category = Marker.companion.categoryToMarkerType(category: unit.device.category) + Image(MarkerTransformations.markerTypeToImageName(markerType: category)) + .padding(5.0) + + getSharedContent() + } + /* MARK: - Device actions */ + if #available(iOS 15, *) { + row.swipeActions(edge: .trailing, allowsFullSwipe: false) { + getActionButtons() + } + } else { + row.contextMenu { + getActionButtons() + } + } + } else { VStack { + getSharedContent() HStack { - /* Status icon */ - switch (unit.getStatus()) { - case .online: - Image(systemName: "circle.fill") - .foregroundColor(.systemGreen) - .imageScale(.small) - case .offline: - Image(systemName: "circle.fill") - .foregroundColor(.systemRed) - .imageScale(.small) - default: - EmptyView() - } - - /* Engine stop */ - switch (unit.getEngineStop()) { - case .on: - Image(systemName: "lock.fill") - .foregroundColor(.systemRed) - .imageScale(.small) - case .off: - Image(systemName: "lock.open.fill") - .foregroundColor(.systemGreen) - .imageScale(.small) - default: - EmptyView() - } - - /* Device name */ - Text(unit.device.name) - Spacer() - } - .padding(.bottom, 5.0) - - /* Driver */ - if let contact = unit.device.contact { - HStack { - Label(contact, systemImage: "person") - .labelStyle(SmallLabelStyle()) - Spacer() - } + getActionButtons() } - - /* Speed */ - if let speed = unit.position?.speed { - HStack { - Label(Formatter.companion.formatSpeed( - speed: Double(truncating: speed), - unit: .kmh), - systemImage: "speedometer") - .labelStyle(SmallLabelStyle()) - Spacer() - } + } + .padding(2.0) + } + } + + @ViewBuilder + func getSharedContent() -> some View { + VStack { + HStack { + /* MARK: - Status icon */ + switch (unit.getStatus()) { + case .online: + Image(systemName: "circle.fill") + .foregroundColor(.systemGreen) + .imageScale(.small) + case .offline: + Image(systemName: "circle.fill") + .foregroundColor(.systemRed) + .imageScale(.small) + default: + EmptyView() } - /* Address */ - if let address = unit.position?.address { - HStack { - Label(address, systemImage: "mappin.and.ellipse") - .labelStyle(SmallLabelStyle()) - Spacer() - } + /* MARK: - Engine stop */ + switch (unit.getEngineStop()) { + case .on: + Image(systemName: "lock.fill") + .foregroundColor(.systemRed) + .imageScale(.small) + case .off: + Image(systemName: "lock.open.fill") + .foregroundColor(.systemGreen) + .imageScale(.small) + default: + EmptyView() } - /* Hourmeter */ - if let hourmeter = Int64(truncating: unit.getHourmeter() ?? 0), - hourmeter >= 60 * 60 * 1000 { - HStack { - Label(Formatter.companion.formatHours(millis: hourmeter), - systemImage: "timer") - .labelStyle(SmallLabelStyle()) - Spacer() - } + /* MARK: - Device name */ + Text(unit.device.name) + Spacer() + } + .padding(.bottom, 5.0) + + /* MARK: - Driver */ + if let contact = unit.device.contact { + HStack { + Label(contact, systemImage: "person") + .labelStyle(SmallLabelStyle()) + Spacer() } - - /* Date time */ - if let datetime = unit.position?.fixTime { - HStack { - Label(Formatter.companion.formatDate(str: datetime), - systemImage: "calendar") - .labelStyle(SmallLabelStyle()) - Spacer() - } + } + + /* MARK: - Speed */ + if let speed = unit.position?.speed { + HStack { + Label(Formatter.companion.formatSpeed( + speed: Double(truncating: speed), + unit: .kmh), + systemImage: "speedometer") + .labelStyle(SmallLabelStyle()) + Spacer() } } - } - - /* Device actions */ - if #available(iOS 15, *) { - row.swipeActions(edge: .trailing, allowsFullSwipe: false) { - Button { callback(.commands) } label: { - Label("commands", systemImage: "paperplane") + + /* MARK: - Address */ + if let address = unit.position?.address { + HStack { + Label(address, systemImage: "mappin.and.ellipse") + .labelStyle(SmallLabelStyle()) + Spacer() } - - Button { callback(.reports) } label: { - Label("reports", systemImage: "clock") + } + + /* MARK: - Hourmeter */ + if let hourmeter = Int64(truncating: unit.getHourmeter() ?? 0), + hourmeter >= 60 * 60 * 1000 { + HStack { + Label(Formatter.companion.formatHours(millis: hourmeter), + systemImage: "timer") + .labelStyle(SmallLabelStyle()) + Spacer() } - - Button { callback(.details) } label: { - Label("details", systemImage: "info.circle") + } + + /* MARK: - Date time */ + if let datetime = unit.position?.fixTime { + HStack { + Label(Formatter.companion.formatDate(str: datetime), + systemImage: "calendar") + .labelStyle(SmallLabelStyle()) + Spacer() } } + } + } + + @ViewBuilder + func getActionButtons() -> some View { + let details = Button { callback(.details) } label: { + Label("details", systemImage: "info.circle") + } + + let reports = Button { callback(.reports) } label: { + Label("reports", systemImage: "clock") + } + + let commands = Button { callback(.commands) } label: { + Label("commands", systemImage: "paperplane") + } + + if isCell { + commands + reports + details } else { - row.onLongPressGesture { - self.isSheet = true - } - .actionSheet(isPresented: $isSheet) { - ActionSheet(title: Text("select-action"), message: nil, buttons: [ - .default(Text("details")) { callback(.details) }, - .default(Text("reports")) { callback(.reports) }, - .default(Text("commands")) { callback(.commands) } - ]) + Group { + details + reports + commands } + .frame(maxWidth: .infinity) + .labelStyle(.titleOnly) + .padding(5.0) } } } |