Skip to content

Commit

Permalink
Merge pull request #4 from antonmartinsson/one-point-oh
Browse files Browse the repository at this point in the history
One point oh
  • Loading branch information
antonmartinsson authored Jan 28, 2023
2 parents a8a76e6 + 3be6b0c commit ae1aa80
Show file tree
Hide file tree
Showing 10 changed files with 468 additions and 352 deletions.
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,19 @@ GaugeView(title: "Speed", value: 100, maxValue: 1000, colors: [.red, .orange, .y
Additionally, as space for additional information is quite limited within the gauge, you can initialize a gauge with some additional information using three different (optional) strings. This information will be revealed to the user with a quick flip animation when a tap on the gauge view is recorded.

```swift
let description = "This is a semi-clever reference."
let secondaryTitle = "#BTTF"
let strap = "This is a semi-clever reference."
let title = "#BTTF"
let body = "This is how fast you need to go to trigger the flux capacitor."
let additionalInfo = GaugeAdditionalInfo(secondaryTitle: secondaryTitle, description: description, body: body)
let additionalInfo = GaugeAdditionalInfo(strap: strap, title: title, body: body)

GaugeView(title: "Speed", value: 88, colors: [.red, .orange, .yellow, .green], additionalInfo: additionalInfo)
```

If you leave any of the strings as nil, the Text instance responsible for displaying that string will not be rendered. So if you just want to create a gauge with a short description and no secondary title or body, this is also a valid init:

```swift
let description = "This is a description."
let additionalInfo = GaugeAdditionalInfo(secondaryTitle: nil, description: description, body: nil)
let strap = "This is a semi-clever reference."
let additionalInfo = GaugeAdditionalInfo(strap: strap, title: nil, body: nil)

GaugeView(title: "Speed", value: 88, colors: [.red, .orange, .yellow, .green], additionalInfo: additionalInfo)
```
Expand All @@ -67,6 +67,15 @@ Finally, if you're feeling like you don't need any built in text elements, you c
GaugeView(colors: [.red, .orange, .yellow, .green])
```

Should you have the need to control the colors of the indicator or the text within the gauge, there are four view modifiers you can use to do so.

```swift
.gaugeTitleColor(.blue)
.gaugeValueColor(.yellow)
.gaugeIndicatorColor(.blue)
.gaugeBackTint(.yellow)
```

## Roadmap

While I don't have many concrete plans for GaugeKit at the moment, I do plan to fiddle around with it and improve it best I can from time to time. If you have any feature requests or ideas you think I should take into consideration, please feel free to contact me here on GitHub.
45 changes: 28 additions & 17 deletions Sources/GaugeKit/CrossPlatform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,40 @@
import SwiftUI

class CrossPlatform {
static func mainScreenSize() -> CGSize {
static func mainScreenSize() -> CGSize {
#if os(macOS)
if let screenSize = NSScreen.main?.frame.size {
return screenSize
}
else {
// Use a generic FHD size so any calculations will still work
return CGSize(width: 1920, height: 1080)
}
if let screenSize = NSScreen.main?.frame.size {
return screenSize
}
else {
// Use a generic FHD size so any calculations will still work
return CGSize(width: 1920, height: 1080)
}
#elseif os(iOS)
return UIScreen.main.bounds.size
return UIScreen.main.bounds.size
#elseif os(watchOS)
return WKInterfaceDevice.current().screenBounds.size
return WKInterfaceDevice.current().screenBounds.size
#endif
}
static func systemBackgroundColor() -> Color {
}
static func systemBackgroundColor() -> Color {
#if os(macOS)
Color(NSColor.windowBackgroundColor)
Color(NSColor.windowBackgroundColor)
#elseif os(iOS)
Color(UIColor.systemBackground)
Color(UIColor.systemBackground)
#elseif os(watchOS)
Color.black
Color.black
#endif
}
}

static func systemLabelColor() -> Color {
#if os(macOS)
Color(NSColor.labelColor)
#elseif os(iOS)
Color(UIColor.label)
#elseif os(watchOS)
Color.black
#endif
}
}

81 changes: 42 additions & 39 deletions Sources/GaugeKit/GaugeBackView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,55 +8,58 @@
import SwiftUI

public struct GaugeAdditionalInfo {
public let preTitle: String?
public let largeTitle: String?
public let body: String?
public let strap: String?
public let title: String?
public let body: String?
}

/**
If additional info is provided to the gauge, this view will be revealed with a flip animation when the user taps on the gauge.
- Parameters:
- flipped: A binding of a boolean that determines whether or not the gauge is "flipped over".
- additionalInfo: The info to display within the view.
- flipped: A binding of a boolean that determines whether or not the gauge is "flipped over".
- additionalInfo: The info to display within the view.
*/
struct GaugeBackView: View {
@Binding var flipped: Bool
let additionalInfo: GaugeAdditionalInfo

var body: some View {
let flipAngle = Angle(degrees: flipped ? -180 : 0)

GeometryReader { geometry in
VStack {
if let preTitle = additionalInfo.preTitle {
Text(preTitle)
}
if let title = additionalInfo.largeTitle {
Text(title)
.font(.system(size: geometry.size.width / 5, weight: .bold))
.padding(.bottom, 4)
}
if let body = additionalInfo.body {
Text(body)
}
}
.position(x: geometry.size.width / 2, y: geometry.size.height / 2)
}
.opacity(flipped ? 1 : 0)
.rotation3DEffect(flipAngle, axis: (x: 0, y: 1, z: 0))
.rotation3DEffect(Angle(degrees: 180), axis: (x: 0, y: 1, z: 0))
}
@Environment(\.backTintColor) var color

@Binding var flipped: Bool
let additionalInfo: GaugeAdditionalInfo

var body: some View {
let flipAngle = Angle(degrees: flipped ? -180 : 0)

GeometryReader { geometry in
VStack {
if let preTitle = additionalInfo.strap {
Text(preTitle)
}
if let title = additionalInfo.title {
Text(title)
.font(.system(size: geometry.size.width / 5, weight: .bold))
.padding(.bottom, 4)
}
if let body = additionalInfo.body {
Text(body)
}
}
.foregroundColor(color)
.position(x: geometry.size.width / 2, y: geometry.size.height / 2)
}
.opacity(flipped ? 1 : 0)
.rotation3DEffect(flipAngle, axis: (x: 0, y: 1, z: 0))
.rotation3DEffect(Angle(degrees: 180), axis: (x: 0, y: 1, z: 0))
}
}


struct GaugeBackView_Previews: PreviewProvider {
static var previews: some View {
let info = GaugeAdditionalInfo(preTitle: "Hejsan",
largeTitle: "Svejsan",
body: "Hörru")
GaugeBackView(flipped: .constant(true),
additionalInfo: info)
}
static var previews: some View {
let info = GaugeAdditionalInfo(strap: "Hejsan",
title: "Svejsan",
body: "Hörru")
GaugeBackView(flipped: .constant(true),
additionalInfo: info)
}
}
134 changes: 67 additions & 67 deletions Sources/GaugeKit/GaugeExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,71 +10,71 @@ import Foundation
import SwiftUI

extension GaugeView {
/**
Initializes a gauge with a value and an array of colors to create a background gradient from.
Defaults the max value to 100.
- Parameters:
- title: A short title that will be displayed in the center of the gauge, below its value.
- value: An integer between 0 and 100 to visualize using the gauge.
- colors: An array of Color instances to create the gauge's background gradient from..
*/
public init(value: Int?, colors: [Color]) {
self.title = nil
self.value = value
self.maxValue = 100
self.colors = colors
self.additionalInfo = nil
}
/**
Initializes a gauge with a title, a value and an array of colors to create a background gradient from.
Defaults the max value to 100.
- Parameters:
- title: A short title that will be displayed in the center of the gauge, below its value.
- value: An integer between 0 and 100 to visualize using the gauge.
- colors: An array of Color instances to create the gauge's background gradient from..
*/
public init(title: String?, value: Int?, colors: [Color]) {
self.title = title
self.value = value
self.maxValue = 100
self.colors = colors
self.additionalInfo = nil
}
/**
Initializes a gauge with a title, a value, an array of colors to create a background gradient from,
as well as some additional information to display on the back of the gauge once it is tapped by the user.
Defaults the max value to 100.
- Parameters:
- title: A short title that will be displayed in the center of the gauge, below its value.
- value: An integer between 0 and 100 to visualize using the gauge.
- colors: An array of Color instances to create the gauge's background gradient from..
- additionalInfo: A struct that contains three optional strings to display on the back of the gauge.
*/
public init(title: String?, value: Int?, colors: [Color], additionalInfo: GaugeAdditionalInfo) {
self.title = title
self.value = value
self.maxValue = 100
self.colors = colors
self.additionalInfo = additionalInfo
}
/**
Initializes a very basic gauge without an indicator or title.
- Parameters:
- colors: An array of Color instances to create the gauge's background gradient from..
*/
public init(colors: [Color]) {
self.title = nil
self.value = nil
self.maxValue = 0
self.colors = colors
self.additionalInfo = nil
}
/**
Initializes a gauge with a value and an array of colors to create a background gradient from.
Defaults the max value to 100.
- Parameters:
- title: A short title that will be displayed in the center of the gauge, below its value.
- value: An integer between 0 and 100 to visualize using the gauge.
- colors: An array of Color instances to create the gauge's background gradient from..
*/
public init(value: Int?, colors: [Color]) {
self.title = nil
self.value = value
self.maxValue = 100
self.colors = colors
self.additionalInfo = nil
}
/**
Initializes a gauge with a title, a value and an array of colors to create a background gradient from.
Defaults the max value to 100.
- Parameters:
- title: A short title that will be displayed in the center of the gauge, below its value.
- value: An integer between 0 and 100 to visualize using the gauge.
- colors: An array of Color instances to create the gauge's background gradient from..
*/
public init(title: String?, value: Int?, colors: [Color]) {
self.title = title
self.value = value
self.maxValue = 100
self.colors = colors
self.additionalInfo = nil
}
/**
Initializes a gauge with a title, a value, an array of colors to create a background gradient from,
as well as some additional information to display on the back of the gauge once it is tapped by the user.
Defaults the max value to 100.
- Parameters:
- title: A short title that will be displayed in the center of the gauge, below its value.
- value: An integer between 0 and 100 to visualize using the gauge.
- colors: An array of Color instances to create the gauge's background gradient from..
- additionalInfo: A struct that contains three optional strings to display on the back of the gauge.
*/
public init(title: String?, value: Int?, colors: [Color], additionalInfo: GaugeAdditionalInfo) {
self.title = title
self.value = value
self.maxValue = 100
self.colors = colors
self.additionalInfo = additionalInfo
}
/**
Initializes a very basic gauge without an indicator or title.
- Parameters:
- colors: An array of Color instances to create the gauge's background gradient from..
*/
public init(colors: [Color]) {
self.title = nil
self.value = nil
self.maxValue = 0
self.colors = colors
self.additionalInfo = nil
}
}
Loading

0 comments on commit ae1aa80

Please sign in to comment.