diff --git a/JustOneThing.xcodeproj/project.pbxproj b/JustOneThing.xcodeproj/project.pbxproj index 61fb484..bbbe2b9 100644 --- a/JustOneThing.xcodeproj/project.pbxproj +++ b/JustOneThing.xcodeproj/project.pbxproj @@ -10,6 +10,9 @@ 5407E5022A2A8AE200EF00B8 /* JustOneThingApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5483903A2A2A773900BF5624 /* JustOneThingApp.swift */; }; 5407E5032A2A8AE700EF00B8 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5483903C2A2A773900BF5624 /* ContentView.swift */; }; 5407E5042A2A8AEA00EF00B8 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 5483903E2A2A773A00BF5624 /* Assets.xcassets */; }; + 5416C7382A56A867002196EE /* InterfaceExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5416C7372A56A867002196EE /* InterfaceExtensions.swift */; }; + 5416C7392A56A867002196EE /* InterfaceExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5416C7372A56A867002196EE /* InterfaceExtensions.swift */; }; + 5416C73A2A56A867002196EE /* InterfaceExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5416C7372A56A867002196EE /* InterfaceExtensions.swift */; }; 5431ED7A2A2A77CA00515680 /* JustOneThingWatch.app in Embed Watch Content */ = {isa = PBXBuildFile; fileRef = 5431ED6D2A2A77C900515680 /* JustOneThingWatch.app */; platformFilter = ios; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; }; 5472018C2A36563A005B2FCC /* WidgetAssets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 54928C372A35F8B800095445 /* WidgetAssets.xcassets */; }; 5472018E2A36563B005B2FCC /* Widget.intentdefinition in Sources */ = {isa = PBXBuildFile; fileRef = 54928C1C2A35F89900095445 /* Widget.intentdefinition */; }; @@ -112,6 +115,7 @@ /* Begin PBXFileReference section */ 5407E5062A35B96E00EF00B8 /* JustOneThingWatch.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = JustOneThingWatch.entitlements; sourceTree = ""; }; + 5416C7372A56A867002196EE /* InterfaceExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InterfaceExtensions.swift; sourceTree = ""; }; 5431ED6D2A2A77C900515680 /* JustOneThingWatch.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = JustOneThingWatch.app; sourceTree = BUILT_PRODUCTS_DIR; }; 547201942A461554005B2FCC /* PhoneWidget.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = PhoneWidget.entitlements; sourceTree = ""; }; 547201952A461558005B2FCC /* WatchWidget.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = WatchWidget.entitlements; sourceTree = ""; }; @@ -203,6 +207,7 @@ 54EB3C1E2A5434A30082B059 /* Thing.swift */, 54928C4A2A35F8CB00095445 /* JustOneThingWidget.swift */, 5493A81A2A471E36001EBA08 /* JustOneThingProvider.swift */, + 5416C7372A56A867002196EE /* InterfaceExtensions.swift */, 54928C372A35F8B800095445 /* WidgetAssets.xcassets */, 54928C392A35F8B800095445 /* Info.plist */, 54928C1C2A35F89900095445 /* Widget.intentdefinition */, @@ -493,6 +498,7 @@ 54EB3C1F2A5434A30082B059 /* Thing.swift in Sources */, 54928C212A35F89A00095445 /* Widget.intentdefinition in Sources */, 5493A81B2A471E37001EBA08 /* JustOneThingProvider.swift in Sources */, + 5416C7382A56A867002196EE /* InterfaceExtensions.swift in Sources */, 547201972A461DBE005B2FCC /* JustOneThingWidget.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -504,6 +510,7 @@ 54EB3C202A5434A30082B059 /* Thing.swift in Sources */, 547201962A461DBE005B2FCC /* JustOneThingWidget.swift in Sources */, 5493A81C2A471E37001EBA08 /* JustOneThingProvider.swift in Sources */, + 5416C7392A56A867002196EE /* InterfaceExtensions.swift in Sources */, 5472018E2A36563B005B2FCC /* Widget.intentdefinition in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -515,6 +522,7 @@ 54EB3C212A5434A30082B059 /* Thing.swift in Sources */, 547201902A36563C005B2FCC /* Widget.intentdefinition in Sources */, 5493A81D2A471E37001EBA08 /* JustOneThingProvider.swift in Sources */, + 5416C73A2A56A867002196EE /* InterfaceExtensions.swift in Sources */, 54928C4B2A35F8CB00095445 /* JustOneThingWidget.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/WidgetShared/InterfaceExtensions.swift b/WidgetShared/InterfaceExtensions.swift new file mode 100644 index 0000000..93f06ce --- /dev/null +++ b/WidgetShared/InterfaceExtensions.swift @@ -0,0 +1,56 @@ +// +// InterfaceExtensions.swift +// JustOneThing +// +// Created by Alan Francis on 06/07/2023. +// + +import SwiftUI + + +extension Color { + static func randomForegroundColor(_ colorScheme:ColorScheme = .light) -> Color { + if( colorScheme == .light ) { + return Color(red: .random(in: 0...0.5), + green: .random(in: 0...0.5), + blue: .random(in: 0...0.5)) + } else { + return Color(red: .random(in: 0.5...1.0), + green: .random(in: 0.5...1.0), + blue: .random(in: 0.5...1.0)) + } + } +} + +public struct FitSystemFont: ViewModifier { + public var lineLimit: Int? + public var fontSize: CGFloat? + public var minimumScaleFactor: CGFloat + public var percentage: CGFloat + + func rightSize(geometry:GeometryProxy) -> CGFloat { + min( + min(geometry.size.width, geometry.size.height) * percentage, + fontSize ?? CGFloat.greatestFiniteMagnitude + ) + } + + public func body(content: Content) -> some View { + GeometryReader { geometry in + content + .font(.system(size: rightSize(geometry: geometry), + weight: .bold, + design: .rounded)) + .bold() + .lineLimit(self.lineLimit) + .minimumScaleFactor(self.minimumScaleFactor) + .position(x: geometry.frame(in: .local).midX, y: geometry.frame(in: .local).midY) + } + } +} + +public extension View { + func fitSystemFont(lineLimit: Int? = nil, fontSize: CGFloat? = nil, minimumScaleFactor: CGFloat = 0.01, percentage: CGFloat = 1) -> ModifiedContent { + return modifier(FitSystemFont(lineLimit: lineLimit, fontSize: fontSize, minimumScaleFactor: minimumScaleFactor, percentage: percentage)) + } +} diff --git a/WidgetShared/JustOneThingProvider.swift b/WidgetShared/JustOneThingProvider.swift index 7deee7f..264b6df 100644 --- a/WidgetShared/JustOneThingProvider.swift +++ b/WidgetShared/JustOneThingProvider.swift @@ -11,12 +11,12 @@ import Intents struct ThingDataStore { public var things:[Thing] = [ - Thing(text: "The First Thing", createdAt: Date()), - Thing(text: "The Second Thing", createdAt: Date()), - Thing(text: "The Third Thing", createdAt: Date()), - Thing(text: "The Fourth Thing", createdAt: Date()), - Thing(text: "The Fifth Thing", createdAt: Date()), - Thing(text: "The Sixth Thing", createdAt: Date()), + Thing(text: "Eat", createdAt: Date()), + Thing(text: "Dont forget to pack your toothbrush", createdAt: Date()), + Thing(text: "Buy Toothpaste", createdAt: Date()), + Thing(text: "Passport", createdAt: Date()), + Thing(text: "Cables", createdAt: Date()), + Thing(text: "Get some dollars", createdAt: Date()), ] public var shuffledThings:[Thing] { diff --git a/WidgetShared/JustOneThingWidget.swift b/WidgetShared/JustOneThingWidget.swift index 62a8d85..fb06ac7 100644 --- a/WidgetShared/JustOneThingWidget.swift +++ b/WidgetShared/JustOneThingWidget.swift @@ -16,12 +16,23 @@ struct JustOneThingWidgetBundle: WidgetBundle { } } +struct ThingView : View { + let thing: Thing + @Environment(\.colorScheme) private var colorScheme + + var body: some View { + Text(thing.text) + .fitSystemFont(lineLimit: 4) + .multilineTextAlignment(.center) + .foregroundColor(.randomForegroundColor(colorScheme)) + } +} struct ThingEntryView : View { var entry: ThingEntry var body: some View { - Text(entry.thing.text) + ThingView(thing: entry.thing).padding() } } @@ -41,9 +52,9 @@ struct JustOneThingWidget: Widget { struct Widget_Previews: PreviewProvider { static var previews: some View { - ThingEntryView(entry: ThingEntry(thing: .placeholder)) + ThingEntryView(entry: ThingEntry(thing: Thing(text: "dont forget toothepase", createdAt: Date()))) #if os(watchOS) - .previewContext(WidgetPreviewContext(family: .accessoryRectangular)) + .previewContext(WidgetPreviewContext(family: .accessoryCircular)) #else .previewContext(WidgetPreviewContext(family: .systemMedium)) #endif