Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support SPM #5

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Mac OS X
.DS_Store

# SPM
.swiftpm

## Build generated
build/
DerivedData

## Various settings
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata

## Other
*.xccheckout
*.moved-aside
*.xcuserstate
*.xcscmblueprint

## Obj-C/Swift specific
*.hmap
*.ipa

# Swift Package Manager
.build/
21 changes: 21 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// swift-tools-version: 5.9

import PackageDescription

let package = Package(
name: "RHLinePlot",
platforms: [.iOS(.v13)],
products: [
.library(
name: "RHLinePlot",
targets: ["RHLinePlot"]),
],
targets: [
.target(
name: "RHLinePlot"),
.testTarget(
name: "RHLinePlotTests",
dependencies: ["RHLinePlot"]
),
]
)
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ Play around with the example app to see possible customizations and the Robinhoo
## Installation
### Cocoapods
`pod install RHLinePlot`
### Swift Package Manager
```swift
.package(url: "https://github.com/aunnnn/RHLinePlot.git", .upToNextMajor(from: "0.1.1")),
```

Or just use the source however you like. The library is in folder `RHLinePlot`.

Expand Down
2 changes: 1 addition & 1 deletion RHLinePlot.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ A line plot library inspired by the Robinhood app.
s.author = { 'Wirawit Rueopas' => '[email protected]' }
s.source = { :git => 'https://github.com/aunnnn/RHLinePlot.git', :tag => s.version.to_s }

s.source_files = 'RHLinePlot/*.swift'
s.source_files = 'Sources/RHLinePlot/*.swift'
s.swift_version = '5.0'

s.ios.deployment_target = '13.0'
Expand Down
28 changes: 22 additions & 6 deletions RHLinePlot.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@
30C49EA6243F0E1A005D2B54 = {
isa = PBXGroup;
children = (
30C49EB2243F0E1A005D2B54 /* RHLinePlot */,
30C49EBD243F0E1A005D2B54 /* RHLinePlotTests */,
A3B7489A2D3FF1D500C0E7C1 /* Sources */,
A3B7489B2D3FF1DD00C0E7C1 /* Tests */,
30A9089924403AF40094A1FC /* RHLinePlotStatic */,
30C49EB1243F0E1A005D2B54 /* Products */,
30A9089F24403B090094A1FC /* Frameworks */,
Expand Down Expand Up @@ -131,6 +131,22 @@
path = RHLinePlotTests;
sourceTree = "<group>";
};
A3B7489A2D3FF1D500C0E7C1 /* Sources */ = {
isa = PBXGroup;
children = (
30C49EB2243F0E1A005D2B54 /* RHLinePlot */,
);
path = Sources;
sourceTree = "<group>";
};
A3B7489B2D3FF1DD00C0E7C1 /* Tests */ = {
isa = PBXGroup;
children = (
30C49EBD243F0E1A005D2B54 /* RHLinePlotTests */,
);
path = Tests;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXHeadersBuildPhase section */
Expand Down Expand Up @@ -399,11 +415,11 @@
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
CODE_SIGN_STYLE = Automatic;
DEFINES_MODULE = YES;
DEVELOPMENT_TEAM = B786UKMNU5;
DEVELOPMENT_TEAM = X7NSG6U2E5;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
DYLIB_INSTALL_NAME_BASE = "@rpath";
INFOPLIST_FILE = RHLinePlot/Info.plist;
INFOPLIST_FILE = Sources/RHLinePlot/Info.plist;
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
LD_RUNPATH_SEARCH_PATHS = (
Expand All @@ -429,11 +445,11 @@
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
CODE_SIGN_STYLE = Automatic;
DEFINES_MODULE = YES;
DEVELOPMENT_TEAM = B786UKMNU5;
DEVELOPMENT_TEAM = X7NSG6U2E5;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
DYLIB_INSTALL_NAME_BASE = "@rpath";
INFOPLIST_FILE = RHLinePlot/Info.plist;
INFOPLIST_FILE = Sources/RHLinePlot/Info.plist;
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
LD_RUNPATH_SEARCH_PATHS = (
Expand Down
58 changes: 58 additions & 0 deletions Sources/RHLinePlot/GlowingIndicator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//
// GlowingIndicator.swift
// RHLinePlot
//
// Created by Wirawit Rueopas on 4/9/20.
// Copyright © 2020 Wirawit Rueopas. All rights reserved.
//

import SwiftUI

/// Default indicator with glowing effect. Used to show latest value in a line plot.
public struct GlowingIndicator: View {

@State var isGlowing: Bool = false
@Environment(\.rhLinePlotConfig) var rhLinePlotConfig

public init() {}

private var glowingAnimation: Animation {
Animation
.easeInOut(duration: rhLinePlotConfig.glowingIndicatorGlowAnimationDuration)
.delay(rhLinePlotConfig.glowingIndicatorDelayBetweenGlow)
.repeatForever(autoreverses: false)
}

private var glowingBackground: some View {
Circle()
.scaleEffect(isGlowing ? rhLinePlotConfig.glowingIndicatorBackgroundScaleEffect : 1)
.opacity(isGlowing ? 0.0 : 1)
.animation(glowingAnimation, value: self.isGlowing)
.frame(width: rhLinePlotConfig.glowingIndicatorWidth, height: rhLinePlotConfig.glowingIndicatorWidth)
}

public var body: some View {
Circle()
.frame(width: rhLinePlotConfig.glowingIndicatorWidth, height: rhLinePlotConfig.glowingIndicatorWidth)
.background(glowingBackground)
.onAppear {
withAnimation {
self.isGlowing = true
}
}
.onDisappear {
withAnimation {
self.isGlowing = false
}
}
}
}


struct GlowingIndicator_Previews: PreviewProvider {
static var previews: some View {
Group {
GlowingIndicator()
}.previewLayout(.fixed(width: 200, height: 200))
}
}
22 changes: 22 additions & 0 deletions Sources/RHLinePlot/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
</dict>
</plist>
20 changes: 20 additions & 0 deletions Sources/RHLinePlot/LinePlotValueStick.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// LinePlotValueStick.swift
// RHLinePlot
//
// Created by Wirawit Rueopas on 4/9/20.
// Copyright © 2020 Wirawit Rueopas. All rights reserved.
//

import SwiftUI

/// Vertical thin line to show currently selected value within a line plot
struct LinePlotValueStick: View {

var lineWidth: CGFloat = 1

var body: some View {
Rectangle()
.frame(maxWidth: self.lineWidth, maxHeight: .infinity)
}
}
91 changes: 91 additions & 0 deletions Sources/RHLinePlot/Misc.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//
// Misc.swift
// RHLinePlot
//
// Created by Wirawit Rueopas on 4/9/20.
// Copyright © 2020 Wirawit Rueopas. All rights reserved.
//

import SwiftUI

/// Find highest and lowest values
func findHighestAndLowest<V: Comparable>(values: [V]) -> (highest: V, lowest: V) {
precondition(values.count > 0, "values must not be empty")
var highest = values[0]
var lowest = values[0]
for v in values {
if v > highest {
highest = v
}
if v < lowest {
lowest = v
}
}
return (highest, lowest)
}

/// Find the index of `value` on a **sorted** `array`. If not found, return the index to the left.
///
/// I.e. for `[1, 3, 5, 10, 30, 55]
///
/// Search for `5` will return `2`. Search for `10` will return `3`. Search for `9` (or any between `5-9`) will return `2` since it's the index on the left.
///
/// In other words, segments of this array are: `1-2`, `3-4`, `5-9`, `10-29`, `30-54`, `55-inf`.
/// and this function will return the active segment in log(N).
func binarySearchOrIndexToTheLeft<A: RandomAccessCollection, T: Comparable>(array: A, value: T) -> Int? where A.Element == T, A.Index == Int {
if array.isEmpty { return nil }

func search(from: Int, to: Int) -> Int? {
let middle = (to + from)/2

// Base case: middle is exactly the one
if array[middle] == value {
return middle
}

// Base case: Down to final one,
// Return only if value is more than itself ("Nearest smaller")
else if from == to {
if value >= array[from] {
return from
} else { // Nope
return nil
}
}
else if from > to {
return nil
}
else if value < array[middle] {
return search(from: from, to: middle-1)
}
else {
// value > array[middle]
// If right side is nil, should return middle
return search(from: middle+1, to: to) ?? middle
}
}
return search(from: 0, to: array.count-1)
}

extension Comparable {
func clamp(low: Self, high: Self) -> Self {
return min(max(low, self), high)
}
}

extension Path {
/// Apply laser light style to the shape
func laserLightStroke(lineWidth: CGFloat) -> some View {
let content = self
return ZStack {
content.stroke(lineWidth: lineWidth*3)
.blur(radius: 3*lineWidth)
content.stroke(lineWidth: lineWidth*2)
.blur(radius: 2*lineWidth)
content.stroke(Color.white, style: StrokeStyle(
lineWidth: lineWidth,
lineCap: .round,
lineJoin: .round))
}
}
}
Loading