Skip to content

Commit

Permalink
integrate candle chart in quote table app
Browse files Browse the repository at this point in the history
  • Loading branch information
kosyloa committed May 3, 2024
1 parent d8a5064 commit bc796d8
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 53 deletions.
24 changes: 16 additions & 8 deletions DXFeedFramework.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@
6469F8C82A3B25C900846831 /* MarketEvent+Access.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6469F8C72A3B25C900846831 /* MarketEvent+Access.swift */; };
6469F8CF2A3B300A00846831 /* MetricCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6469F8CB2A3B2F1200846831 /* MetricCell.swift */; };
6469F8D02A3B301C00846831 /* TimeInterval+Ext.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6469F8CD2A3B2F9900846831 /* TimeInterval+Ext.swift */; };
6469F8D32A3B401700846831 /* Colors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6469F8D22A3B401700846831 /* Colors.swift */; };
6469F8D42A3B401700846831 /* Colors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6469F8D22A3B401700846831 /* Colors.swift */; };
6469F8D52A3B401700846831 /* Colors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6469F8D22A3B401700846831 /* Colors.swift */; };
6469F8D62A3B408900846831 /* Endpoint+Ext.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64B6277C2A3762D000196D07 /* Endpoint+Ext.swift */; };
Expand All @@ -220,10 +219,14 @@
647A513D2BE4DDB600B8B8C9 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 647A51372BE4DDB600B8B8C9 /* Assets.xcassets */; };
647A513E2BE4DDB600B8B8C9 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 647A51392BE4DDB600B8B8C9 /* Preview Assets.xcassets */; };
647A513F2BE4DDB600B8B8C9 /* DXFeedCandleChartMacApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 647A513B2BE4DDB600B8B8C9 /* DXFeedCandleChartMacApp.swift */; };
647A51412BE4DF3700B8B8C9 /* CandleStickChart.swift in Sources */ = {isa = PBXBuildFile; fileRef = 643F42002BDFE25D00A2176D /* CandleStickChart.swift */; };
647A51442BE4DFD600B8B8C9 /* Colors.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 646979712A3B5AF60003A9BA /* Colors.xcassets */; };
647A51452BE4E02700B8B8C9 /* CandleStickChart.swift in Sources */ = {isa = PBXBuildFile; fileRef = 643F42002BDFE25D00A2176D /* CandleStickChart.swift */; };
647A51482BE4EE0600B8B8C9 /* CandleStickChart.swift in Sources */ = {isa = PBXBuildFile; fileRef = 643F42002BDFE25D00A2176D /* CandleStickChart.swift */; };
647A51492BE4F71700B8B8C9 /* Colors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6469F8D22A3B401700846831 /* Colors.swift */; };
647A514F2BE5113A00B8B8C9 /* Color+Ext.swift in Sources */ = {isa = PBXBuildFile; fileRef = 647A514B2BE5102800B8B8C9 /* Color+Ext.swift */; };
647A51502BE5129200B8B8C9 /* Color+Ext.swift in Sources */ = {isa = PBXBuildFile; fileRef = 647A514B2BE5102800B8B8C9 /* Color+Ext.swift */; };
647A51512BE512E100B8B8C9 /* CandleStickChart.swift in Sources */ = {isa = PBXBuildFile; fileRef = 643F42002BDFE25D00A2176D /* CandleStickChart.swift */; };
647A51532BE5141600B8B8C9 /* Color+Ext.swift in Sources */ = {isa = PBXBuildFile; fileRef = 647A514B2BE5102800B8B8C9 /* Color+Ext.swift */; };
64820AAF2BB2E26100BDFD0B /* DXOtcMarketOrderTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64820AAE2BB2E26100BDFD0B /* DXOtcMarketOrderTest.swift */; };
6482F3D12BA492A60079AC3D /* SymbolsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6482F3D02BA492A60079AC3D /* SymbolsViewController.swift */; };
6482F3D22BA492A60079AC3D /* SymbolsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6482F3D02BA492A60079AC3D /* SymbolsViewController.swift */; };
Expand Down Expand Up @@ -793,6 +796,7 @@
647A51392BE4DDB600B8B8C9 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
647A513A2BE4DDB600B8B8C9 /* DXFeedCandleChartMac.entitlements */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.entitlements; path = DXFeedCandleChartMac.entitlements; sourceTree = "<group>"; };
647A513B2BE4DDB600B8B8C9 /* DXFeedCandleChartMacApp.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DXFeedCandleChartMacApp.swift; sourceTree = "<group>"; };
647A514B2BE5102800B8B8C9 /* Color+Ext.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Color+Ext.swift"; sourceTree = "<group>"; };
64820AAE2BB2E26100BDFD0B /* DXOtcMarketOrderTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DXOtcMarketOrderTest.swift; sourceTree = "<group>"; };
6482F3D02BA492A60079AC3D /* SymbolsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SymbolsViewController.swift; sourceTree = "<group>"; };
6482F3D62BA49AB70079AC3D /* AddSymbolsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddSymbolsViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1436,6 +1440,7 @@
647A51382BE4DDB600B8B8C9 /* Preview Content */,
647A513A2BE4DDB600B8B8C9 /* DXFeedCandleChartMac.entitlements */,
647A513B2BE4DDB600B8B8C9 /* DXFeedCandleChartMacApp.swift */,
647A514B2BE5102800B8B8C9 /* Color+Ext.swift */,
643F42002BDFE25D00A2176D /* CandleStickChart.swift */,
);
path = DXFeedCandleChartMac;
Expand Down Expand Up @@ -2496,7 +2501,8 @@
buildActionMask = 2147483647;
files = (
647A513F2BE4DDB600B8B8C9 /* DXFeedCandleChartMacApp.swift in Sources */,
647A51412BE4DF3700B8B8C9 /* CandleStickChart.swift in Sources */,
647A51512BE512E100B8B8C9 /* CandleStickChart.swift in Sources */,
647A51532BE5141600B8B8C9 /* Color+Ext.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -2518,6 +2524,7 @@
files = (
647A51452BE4E02700B8B8C9 /* CandleStickChart.swift in Sources */,
643F41F52BDFE1B000A2176D /* DXFeedCandleChartApp.swift in Sources */,
647A51502BE5129200B8B8C9 /* Color+Ext.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -2574,12 +2581,13 @@
64B6273A2A375C0F00196D07 /* QuoteCell.swift in Sources */,
640F8A532BA9C8D600C7BE22 /* SymbolsDataProvider.swift in Sources */,
6482F3D72BA49AB70079AC3D /* AddSymbolsViewController.swift in Sources */,
6469F8D32A3B401700846831 /* Colors.swift in Sources */,
647A514F2BE5113A00B8B8C9 /* Color+Ext.swift in Sources */,
6469F8D62A3B408900846831 /* Endpoint+Ext.swift in Sources */,
64B6273B2A375C0F00196D07 /* PaddingLabel.swift in Sources */,
64B627402A375C0F00196D07 /* SceneDelegate.swift in Sources */,
647A51482BE4EE0600B8B8C9 /* CandleStickChart.swift in Sources */,
6469F8D82A3B4AA400846831 /* MetricCell.swift in Sources */,
647A51492BE4F71700B8B8C9 /* Colors.swift in Sources */,
646D19E22A3C994000C82315 /* DXFTimer.swift in Sources */,
64B6273E2A375C0F00196D07 /* QuoteTableViewController.swift in Sources */,
64B627392A375C0F00196D07 /* AppDelegate.swift in Sources */,
Expand Down Expand Up @@ -3254,7 +3262,7 @@
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
IPHONEOS_DEPLOYMENT_TARGET = 17.2;
IPHONEOS_DEPLOYMENT_TARGET = 17.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down Expand Up @@ -3289,7 +3297,7 @@
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
IPHONEOS_DEPLOYMENT_TARGET = 17.2;
IPHONEOS_DEPLOYMENT_TARGET = 17.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down Expand Up @@ -3519,7 +3527,7 @@
INFOPLIST_KEY_UIMainStoryboardFile = Main;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down Expand Up @@ -3553,7 +3561,7 @@
INFOPLIST_KEY_UIMainStoryboardFile = Main;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down
2 changes: 1 addition & 1 deletion Samples/DXFeedCandleChart/DXFeedCandleChartApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import SwiftUI
struct DXFeedCandleChartApp: App {
var body: some Scene {
WindowGroup {
CandleStickChart(symbol: "AAPL")
CandleStickChart(symbol: "AAPL", endpoint: nil)
}
}
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
67 changes: 36 additions & 31 deletions Samples/DXFeedCandleChartMac/CandleStickChart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ struct StockPrice: Identifiable {
self.high = Decimal(candle.high)
self.low = Decimal(candle.low)
}

}


Expand Down Expand Up @@ -128,32 +127,39 @@ class CandleList: ObservableObject, SnapshotDelegate {

@Published var candles: [StockPrice]

init(symbol: String) {
init(symbol: String, endpoint: DXEndpoint?) {
self.symbol = symbol
self.candles = [StockPrice]()
try? createSubscription()
try? createSubscription(endpoint: endpoint)
fetchInfo()
}

func createSubscription() throws {
endpoint = try DXEndpoint.create().connect("demo.dxfeed.com:7300")
feed = endpoint.getFeed()
func createSubscription(endpoint: DXEndpoint?) throws {
if let endpoint = endpoint {
self.endpoint = endpoint
} else {
self.endpoint = try DXEndpoint.create().connect("demo.dxfeed.com:7300")
}
feed = self.endpoint.getFeed()
subscription = try feed?.createSubscription([Candle.self])
snapshotProcessor = SnapshotProcessor()
snapshotProcessor.add(self)
try subscription?.add(listener: snapshotProcessor)
}

func fetchInfo() {
let reader = DXInstrumentProfileReader()
let result = try? reader.readFromFile(address: "https://demo:[email protected]/ipf?SYMBOL=\(symbol)")
guard let result = result else {
return
}
result.forEach { profile in
currency = profile.currency
descriptionString = profile.descriptionStr
DispatchQueue.global(qos: .background).async {
let reader = DXInstrumentProfileReader()
let result = try? reader.readFromFile(address: "https://demo:[email protected]/ipf?SYMBOL=\(self.symbol)")
guard let result = result else {
return
}
result.forEach { profile in
self.currency = profile.currency
self.descriptionString = profile.descriptionStr
}
}

}

func updateDate(date: Date, type: CandleType) {
Expand Down Expand Up @@ -187,14 +193,13 @@ struct CandleStickChart: View {
let dateFormatter: DateFormatter
let symbol: String

init(symbol: String, type: CandleType = .month, date: Date? = nil) {
init(symbol: String, type: CandleType = .month, date: Date? = nil, endpoint: DXEndpoint?) {
dateFormatter = DateFormatter()
dateFormatter.dateStyle = DateFormatter.Style.short
dateFormatter.timeStyle = DateFormatter.Style.none

self.symbol = symbol
self.list = CandleList(symbol: symbol)

self.list = CandleList(symbol: symbol, endpoint: endpoint)
_type = State(initialValue: type)
if let date = date {
_date = State(initialValue: date)
Expand All @@ -210,38 +215,38 @@ struct CandleStickChart: View {

Section {
chart.frame(height: max(reader.size.height/2, 300))
}.listRowBackground(Color.cellBackground)
}.listRowBackground(Color.sectionBackground)

Section("Chart parameters") {
Picker("Candle type", selection: $type) {
ForEach(CandleType.allCases, id: \.self) { category in
Text(String(describing: category).capitalized).tag(category)
}
}.onChange(of: type) { _, newValue in
}.onChange(of: type) { value in
selectedPrice = nil
list.updateDate(date: date, type: newValue)
}.pickerStyle(SegmentedPickerStyle())
list.updateDate(date: date, type: value)
}
.pickerStyle(SegmentedPickerStyle())
.foregroundStyle(Color.labelText)

.foregroundStyle(.text)
DatePicker(
selection: $date,
displayedComponents: [.date]
) {
Text("Choose from time")
}.onChange(of: date) { _, newValue in
}.onChange(of: date) { value in
selectedPrice = nil
xAxisValues = CandleStickChart.calculateXaxisValues(firstValue: newValue)
list.updateDate(date: newValue, type: type)
xAxisValues = CandleStickChart.calculateXaxisValues(firstValue: value)
list.updateDate(date: value, type: type)
}
.datePickerStyle(.compact)
.foregroundStyle(.text)
.foregroundStyle(Color.labelText)

}
.listRowBackground(Color.cellBackground)
.listRowSeparator(.hidden)
}.listRowSeparator(.hidden)
.listRowBackground(Color.sectionBackground)
}
.preferredColorScheme(.dark)
.background(.tableBackground)
.background(Color.viewBackground)
.scrollContentBackground(.hidden)

}
Expand Down Expand Up @@ -322,7 +327,7 @@ struct CandleStickChart: View {
.frame(width: boxWidth, alignment: .leading)
.background {
RoundedRectangle(cornerRadius: 13)
.fill(Color.priceBackground)
.fill(Color.infoBackground)
.foregroundStyle(.thickMaterial)
.padding(.horizontal, -8)
.padding(.vertical, -4)
Expand Down
24 changes: 24 additions & 0 deletions Samples/DXFeedCandleChartMac/Color+Ext.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
//
// Copyright (C) 2024 Devexperts LLC. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
//

import SwiftUI

#if os(macOS)
extension Color {
static let viewBackground = Color.tableBackground
static let sectionBackground = Color.cellBackground
static let infoBackground = Color.priceBackground
static let labelText = Color.text
}
#elseif os(iOS)
extension Color {
static let viewBackground = Color(UIColor.tableBackground)
static let sectionBackground = Color(UIColor.cellBackground)
static let infoBackground = Color(UIColor.priceBackground)
static let labelText = Color(UIColor.text)
}
#endif
3 changes: 2 additions & 1 deletion Samples/DXFeedCandleChartMac/DXFeedCandleChartMacApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ struct DXFeedCandleChartMacApp: App {
WindowGroup {
CandleStickChart(symbol: symbol,
type: .week,
date: Calendar.current.date(byAdding: .year, value: -4, to: Date()))
date: Calendar.current.date(byAdding: .year, value: -4, to: Date()),
endpoint: nil)
.navigationTitle("CandleChart: \(symbol)")
}
.defaultSize(width: 800, height: 800)
Expand Down
7 changes: 3 additions & 4 deletions Samples/QuoteTableApp/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="22505" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="Fbd-3S-cKV">
<device id="retina6_12" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="22504"/>
<capability name="Named colors" minToolsVersion="9.0"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
Expand Down Expand Up @@ -59,7 +58,7 @@
<constraint firstAttribute="bottom" secondItem="p7V-3p-HxW" secondAttribute="bottom" constant="5" id="wLD-93-Kad"/>
</constraints>
</view>
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" allowsSelection="NO" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="-1" estimatedSectionHeaderHeight="-1" sectionFooterHeight="1" estimatedSectionFooterHeight="-1" translatesAutoresizingMaskIntoConstraints="NO" id="EH9-zJ-QLZ">
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="-1" estimatedSectionHeaderHeight="-1" sectionFooterHeight="1" estimatedSectionFooterHeight="-1" translatesAutoresizingMaskIntoConstraints="NO" id="EH9-zJ-QLZ">
<rect key="frame" x="10" y="103" width="373" height="675"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<label key="tableFooterView" opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="NOTICE: Data is delayed at least 15 minutes." textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="5WY-rQ-KMd">
Expand Down Expand Up @@ -388,10 +387,10 @@
</scenes>
<designables>
<designable name="OfZ-3h-hz9">
<size key="intrinsicContentSize" width="41.666666666666671" height="30.333333333333332"/>
<size key="intrinsicContentSize" width="24.666666666666668" height="20.333333333333332"/>
</designable>
<designable name="my2-SE-epx">
<size key="intrinsicContentSize" width="45.333333333333329" height="30.333333333333332"/>
<size key="intrinsicContentSize" width="28.333333333333332" height="20.333333333333332"/>
</designable>
</designables>
<color key="tintColor" name="TextColor"/>
Expand Down
Loading

0 comments on commit bc796d8

Please sign in to comment.