update chartbar for XTR

This commit is contained in:
Mikhail Filimonov 2024-06-13 17:59:31 -03:00
parent 2150d65f78
commit 8ad27b9ef2
8 changed files with 56 additions and 60 deletions

View File

@ -72,6 +72,18 @@ enum BaseConstants {
return numberFormatter
}()
static let starNumberFormatter: NumberFormatter = {
let numberFormatter = NumberFormatter()
numberFormatter.allowsFloats = true
numberFormatter.numberStyle = .decimal
numberFormatter.usesGroupingSeparator = true
numberFormatter.groupingSeparator = " "
numberFormatter.minimumIntegerDigits = 1
numberFormatter.minimumFractionDigits = 0
numberFormatter.maximumFractionDigits = 2
return numberFormatter
}()
static let detailsNumberFormatter: NumberFormatter = {
let detailsNumberFormatter = NumberFormatter()
detailsNumberFormatter.allowsFloats = false

View File

@ -13,7 +13,23 @@ import Cocoa
import UIKit
#endif
public enum GraphCurrency : String {
case xtr = "XTR"
case ton = "TON"
var formatter: NumberFormatter {
switch self {
case .xtr:
return BaseConstants.starNumberFormatter
case .ton:
return BaseConstants.tonNumberFormatter
}
}
}
public class StackedBarsChartController: BaseChartController {
let barsController: BarsComponentController
let zoomedBarsController: BarsComponentController
@ -23,12 +39,12 @@ public class StackedBarsChartController: BaseChartController {
}
}
public init(chartsCollection: ChartsCollection, isCrypto: Bool = false, rate: Double = 1.0) {
public init(chartsCollection: ChartsCollection, currency: GraphCurrency? = nil, drawCurrency:((CGContext, UIColor, CGPoint)->Void)? = nil, rate: Double = 1.0) {
let horizontalScalesRenderer = HorizontalScalesRenderer()
let verticalScalesRenderer = VerticalScalesRenderer()
var secondaryScalesRenderer: VerticalScalesRenderer?
if isCrypto {
verticalScalesRenderer.isCrypto = true
if let _ = currency {
verticalScalesRenderer.drawCurrency = drawCurrency
secondaryScalesRenderer = VerticalScalesRenderer()
secondaryScalesRenderer?.isRightAligned = true
}
@ -38,10 +54,10 @@ public class StackedBarsChartController: BaseChartController {
verticalScalesRenderer: verticalScalesRenderer,
secondaryScalesRenderer: secondaryScalesRenderer,
previewBarsChartRenderer: BarChartRenderer())
if isCrypto {
if let currency {
barsController.conversionRate = rate
barsController.verticalLimitsNumberFormatter = BaseConstants.tonNumberFormatter
barsController.detailsNumberFormatter = BaseConstants.tonNumberFormatter
barsController.verticalLimitsNumberFormatter = currency.formatter
barsController.detailsNumberFormatter = currency.formatter
}
zoomedBarsController = BarsComponentController(isZoomed: true,
mainBarsRenderer: BarChartRenderer(),

View File

@ -9,7 +9,7 @@
import Foundation
#if os(macOS)
import Cocoa
typealias UIColor = NSColor
public typealias UIColor = NSColor
#else
import UIKit
#endif
@ -26,7 +26,7 @@ class VerticalScalesRenderer: BaseChartRenderer {
var axisXWidth: CGFloat = GView.oneDevicePixel
var isRightAligned: Bool = false
var isCrypto: Bool = false
var drawCurrency:((CGContext, UIColor, CGPoint)->Void)?
var horizontalLinesColor: GColor = .black {
didSet {
@ -122,45 +122,6 @@ class VerticalScalesRenderer: BaseChartRenderer {
context.strokeLineSegments(between: lineSegments)
}
func drawTonSymbol(context: CGContext, color: UIColor, at point: CGPoint) {
let width: CGFloat = 8.0
let height: CGFloat = 7.5
let cornerRadius: CGFloat = 0.5
let topPoint = CGPoint(x: point.x + width / 2, y: point.y)
let bottomPoint = CGPoint(x: point.x + width / 2, y: point.y + height)
let leftTopPoint = CGPoint(x: point.x, y: point.y)
let rightTopPoint = CGPoint(x: point.x + width, y: point.y)
context.saveGState()
context.beginPath()
context.move(to: CGPoint(x: leftTopPoint.x + cornerRadius, y: leftTopPoint.y))
context.addArc(tangent1End: leftTopPoint, tangent2End: bottomPoint, radius: cornerRadius)
context.addLine(to: CGPoint(x: bottomPoint.x, y: bottomPoint.y - cornerRadius + GView.oneDevicePixel))
context.move(to: CGPoint(x: rightTopPoint.x - cornerRadius, y: rightTopPoint.y))
context.addArc(tangent1End: rightTopPoint, tangent2End: bottomPoint, radius: cornerRadius)
context.addLine(to: CGPoint(x: bottomPoint.x, y: bottomPoint.y - cornerRadius + GView.oneDevicePixel))
context.move(to: CGPoint(x: leftTopPoint.x + cornerRadius, y: leftTopPoint.y))
context.addLine(to: CGPoint(x: rightTopPoint.x - cornerRadius, y: rightTopPoint.y))
context.move(to: topPoint)
context.addLine(to: CGPoint(x: bottomPoint.x, y: bottomPoint.y - 1.0))
context.setLineWidth(1.0)
context.setLineCap(.round)
context.setFillColor(UIColor.clear.cgColor)
context.setStrokeColor(color.withAlphaComponent(1.0).cgColor)
context.setAlpha(color.alphaValue)
context.strokePath()
context.restoreGState()
}
func drawVerticalLabels(_ labels: [LinesChartLabel], attributes: [NSAttributedString.Key: Any]) {
if isRightAligned {
for label in labels {
@ -176,9 +137,9 @@ class VerticalScalesRenderer: BaseChartRenderer {
let textNode = LabelNode.layoutText(attributedString, bounds.size)
var xOffset = 0.0
if self.isCrypto {
if let drawCurrency {
xOffset += 11.0
drawTonSymbol(context: context, color: attributes[.foregroundColor] as? UIColor ?? .black, at: CGPoint(x: chartFrame.minX, y: y + 4.0))
drawCurrency(context, attributes[.foregroundColor] as? UIColor ?? .black, CGPoint(x: chartFrame.minX, y: y + 4.0))
}
textNode.1.draw(CGRect(origin: CGPoint(x: chartFrame.minX + xOffset, y: y), size: textNode.0.size), in: context, backingScaleFactor: deviceScale)

View File

@ -60,3 +60,5 @@ class TonNumberFormatter: NumberFormatter {
return balanceText
}
}

View File

@ -21,7 +21,7 @@ func makeCIColor(color: GColor) -> CIColor {
#endif
}
extension GColor {
public extension GColor {
var redValue: CGFloat{ return makeCIColor(color: self).red }
var greenValue: CGFloat{ return makeCIColor(color: self).green }
var blueValue: CGFloat{ return makeCIColor(color: self).blue }

View File

@ -20,6 +20,6 @@ public typealias GView = UIView
#endif
extension GView {
public extension GView {
static let oneDevicePixel: CGFloat = (1.0 / max(2, min(1, deviceScale)))
}

View File

@ -9,12 +9,12 @@ public struct StarsRevenueStats: Equatable {
public let currentBalance: Int64
public let availableBalance: Int64
public let overallRevenue: Int64
public let withdrawEnabled: Bool
}
public let revenueGraph: StatsGraph
public let balances: Balances
public let usdRate: Double
init(revenueGraph: StatsGraph, balances: Balances, usdRate: Double) {
self.revenueGraph = revenueGraph
self.balances = balances
@ -58,8 +58,7 @@ extension StarsRevenueStats.Balances {
init(apiStarsRevenueStatus: Api.StarsRevenueStatus) {
switch apiStarsRevenueStatus {
case let .starsRevenueStatus(flags, currentBalance, availableBalance, overallRevenue):
let _ = flags
self.init(currentBalance: currentBalance, availableBalance: availableBalance, overallRevenue: overallRevenue)
self.init(currentBalance: currentBalance, availableBalance: availableBalance, overallRevenue: overallRevenue, withdrawEnabled: ((flags & (1 << 0)) != 0))
}
}
}

View File

@ -565,7 +565,7 @@ public struct StoryListContextState: Equatable {
public var isCached: Bool
public var hasCache: Bool
public var allEntityFiles: [MediaId: TelegramMediaFile]
public var isLoading: Bool
public init(
peerReference: PeerReference?,
items: [Item],
@ -574,7 +574,8 @@ public struct StoryListContextState: Equatable {
loadMoreToken: AnyHashable?,
isCached: Bool,
hasCache: Bool,
allEntityFiles: [MediaId: TelegramMediaFile]
allEntityFiles: [MediaId: TelegramMediaFile],
isLoading: Bool
) {
self.peerReference = peerReference
self.items = items
@ -584,6 +585,7 @@ public struct StoryListContextState: Equatable {
self.isCached = isCached
self.hasCache = hasCache
self.allEntityFiles = allEntityFiles
self.isLoading = isLoading
}
}
@ -625,7 +627,7 @@ public final class PeerStoryListContext: StoryListContext {
self.peerId = peerId
self.isArchived = isArchived
self.stateValue = State(peerReference: nil, items: [], pinnedIds: Set(), totalCount: 0, loadMoreToken: AnyHashable(0 as Int), isCached: true, hasCache: false, allEntityFiles: [:])
self.stateValue = State(peerReference: nil, items: [], pinnedIds: Set(), totalCount: 0, loadMoreToken: AnyHashable(0 as Int), isCached: true, hasCache: false, allEntityFiles: [:], isLoading: false)
let _ = (account.postbox.transaction { transaction -> (PeerReference?, [State.Item], [Int32], Int, [MediaId: TelegramMediaFile], Bool) in
let key = ValueBoxKey(length: 8 + 1)
@ -714,7 +716,7 @@ public final class PeerStoryListContext: StoryListContext {
return
}
var updatedState = State(peerReference: peerReference, items: items, pinnedIds: Set(pinnedIds), totalCount: totalCount, loadMoreToken: AnyHashable(0 as Int), isCached: true, hasCache: hasCache, allEntityFiles: allEntityFiles)
var updatedState = State(peerReference: peerReference, items: items, pinnedIds: Set(pinnedIds), totalCount: totalCount, loadMoreToken: AnyHashable(0 as Int), isCached: true, hasCache: hasCache, allEntityFiles: allEntityFiles, isLoading: false)
updatedState.items.sort(by: { lhs, rhs in
let lhsPinned = updatedState.pinnedIds.contains(lhs.storyItem.id)
let rhsPinned = updatedState.pinnedIds.contains(rhs.storyItem.id)
@ -1280,7 +1282,11 @@ public final class SearchStoryListContext: StoryListContext {
return self.statePromise.get()
}
private var isLoadingMore: Bool = false
private var isLoadingMore: Bool = false {
didSet {
self.stateValue.isLoading = isLoadingMore
}
}
private var requestDisposable: Disposable?
private var updatesDisposable: Disposable?
@ -1292,7 +1298,7 @@ public final class SearchStoryListContext: StoryListContext {
self.account = account
self.source = source
self.stateValue = State(peerReference: nil, items: [], pinnedIds: Set(), totalCount: 0, loadMoreToken: AnyHashable(""), isCached: false, hasCache: false, allEntityFiles: [:])
self.stateValue = State(peerReference: nil, items: [], pinnedIds: Set(), totalCount: 0, loadMoreToken: AnyHashable(""), isCached: false, hasCache: false, allEntityFiles: [:], isLoading: false)
self.statePromise.set(.single(self.stateValue))
self.loadMore(completion: nil)