Skip to content

Commit

Permalink
update for macos
Browse files Browse the repository at this point in the history
  • Loading branch information
workingDog committed Oct 1, 2020
1 parent 44ef296 commit f243b7a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.1
// swift-tools-version:5.2
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
Expand Down
20 changes: 12 additions & 8 deletions Sources/ClockTimePicker/ClockHand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ public struct ClockHand: View {
@State var handleColor = Color.primary
@State var handleRimColor = Color.primary

let impact = UIImpactFeedbackGenerator(style: .light)
#if os(iOS)
let impact = UIImpactFeedbackGenerator(style: .light)
#endif

@State private var prevHour = 0

Expand Down Expand Up @@ -70,7 +72,7 @@ public struct ClockHand: View {
}
}

var dragHandle: some View {
public var dragHandle: some View {
return ZStack {
Circle().overlay(
Circle().stroke(self.handleRimColor, lineWidth: self.options.handleRimWidth)
Expand All @@ -79,7 +81,7 @@ public struct ClockHand: View {
}
}

func startHandPos(_ geom: GeometryProxy) {
public func startHandPos(_ geom: GeometryProxy) {
// the looks
handColor = handType == .hour ? options.hourHandColor : options.minuteHandColor
handWidth = handType == .hour ? options.hourHandWidth : options.minuteHandWidth
Expand Down Expand Up @@ -108,7 +110,7 @@ public struct ClockHand: View {
}
}

func updateClockHour(_ angleDeg: Float) {
public func updateClockHour(_ angleDeg: Float) {
// normalise to 0-360
let theAngle = normalise(angleDeg)
// 1 hour is 30 deg
Expand All @@ -127,10 +129,12 @@ public struct ClockHand: View {
clockDate = newDate
}
adjustClockDate()
if options.impactFeedbackOn && Calendar.current.component(.hour, from: clockDate) != prevHour {
prevHour = Calendar.current.component(.hour, from: clockDate)
impact.impactOccurred()
}
#if os(iOS)
if options.impactFeedbackOn && Calendar.current.component(.hour, from: clockDate) != prevHour {
prevHour = Calendar.current.component(.hour, from: clockDate)
impact.impactOccurred()
}
#endif
}

private func updateClockMinute(_ angleDeg: Float) {
Expand Down
21 changes: 16 additions & 5 deletions Sources/ClockTimePicker/ClockLooks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

import Foundation
import SwiftUI
#if os(iOS)
import UIKit
#elseif os(OSX)
import AppKit
#endif


public class ClockLooks : ObservableObject {
Expand Down Expand Up @@ -59,12 +64,18 @@ public class ClockLooks : ObservableObject {
// the text showing the hour and minutes at the center of the clock
@Published public var centerTextFont: Font = Font.custom("Helvetica", size: 20)
@Published public var centerBackgroundColor: Color = Color.primary
@Published public var centerForegroundColor: Color = Color(UIColor.systemBackground)

// AM:PM
@Published public var ampmSelectedColor: UIColor = UIColor.white
@Published public var ampmNormalColor: UIColor = UIColor.black
@Published public var ampmTintColor: UIColor = UIColor.black
#if os(iOS)
// AM:PM
@Published public var ampmSelectedColor: UIColor = UIColor.white
@Published public var ampmNormalColor: UIColor = UIColor.black
@Published public var ampmTintColor: UIColor = UIColor.black

@Published public var centerForegroundColor: Color = Color(.systemBackground)
#else
@Published public var centerForegroundColor: Color = Color(NSColor.windowBackgroundColor)
#endif

// x and y offset from clock center, of the AM:PM picker as the proportion of the clock size
@Published public var ampmPickerXoffset = CGFloat(0.0)
@Published public var ampmPickerYoffset = CGFloat(0.06)
Expand Down
24 changes: 13 additions & 11 deletions Sources/ClockTimePicker/ClockPickerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ public struct ClockPickerView : View {
public init(date: Binding<Date>, options: ClockLooks = ClockLooks()) {
self._clockDate = date
self.options = options
// to control the AM:PM picker colors
UISegmentedControl.appearance().selectedSegmentTintColor = self.options.ampmTintColor
UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: self.options.ampmSelectedColor], for: .selected)
UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: self.options.ampmNormalColor], for: .normal)
// to control the AM:PM picker colors in ios
#if os(iOS)
UISegmentedControl.appearance().selectedSegmentTintColor = self.options.ampmTintColor
UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: self.options.ampmSelectedColor], for: .selected)
UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: self.options.ampmNormalColor], for: .normal)
#endif
}

public var body: some View {
Expand All @@ -53,7 +55,7 @@ public struct ClockPickerView : View {
}
}

var clockWithHands: some View {
public var clockWithHands: some View {
Group {
ClockFace(period: self.$period, options: self.options)
ClockHand(clockDate: self.$clockDate, handType: .hour, period: self.$period, options: self.options)
Expand All @@ -62,7 +64,7 @@ public struct ClockPickerView : View {
}
}

var clockWithoutHands: some View {
public var clockWithoutHands: some View {
Group {
ClockNoHands(clockDate: self.$clockDate, period: self.$period, options: self.options, handType: self.$hand)
HStack {
Expand All @@ -73,7 +75,7 @@ public struct ClockPickerView : View {
}
}

var periodPicker: some View {
public var periodPicker: some View {
Picker(selection: Binding<Int>(
get: { self.period }, set: { self.adjustClockDate($0) }), label: Text("")) {
ForEach(0..<periodTypes.count) {
Expand All @@ -82,7 +84,7 @@ public struct ClockPickerView : View {
}.pickerStyle(SegmentedPickerStyle()).scaledToFit()
}

func asHours() -> String {
public func asHours() -> String {
var theHours = Calendar.current.component(.hour, from: clockDate)
if period == 1 {
if theHours < 12 {
Expand All @@ -92,18 +94,18 @@ public struct ClockPickerView : View {
return String(theHours)
}

func asMinutes() -> String {
public func asMinutes() -> String {
formatter.dateFormat = "mm"
return formatter.string(from: clockDate)
}

func asText() -> String {
public func asText() -> String {
formatter.dateFormat = "HH:mm"
return formatter.string(from: clockDate)
}

// change the hour according to the period setting
func adjustClockDate(_ value: Int) {
public func adjustClockDate(_ value: Int) {
self.period = value
let theMinutes = Calendar.current.component(.minute, from: clockDate)
var theHours = Calendar.current.component(.hour, from: clockDate)
Expand Down

0 comments on commit f243b7a

Please sign in to comment.