Skip to content

Commit

Permalink
Feat: Dimillian#171 show entered turnips price values instead of min …
Browse files Browse the repository at this point in the history
…max and average (Dimillian#175)

* feat(Turnips): use entered prices instead of min-max or average ones

* Fix Dimillian#171

* fix(Turnips): select another tab change the values between average and min max

* fix(Turnips): remove the visual glitch while switching from tab to tab

* feat(Turnips): use acText color for entered price values
  • Loading branch information
renaudjenny authored May 23, 2020
1 parent ceba2fd commit 4af12fb
Show file tree
Hide file tree
Showing 9 changed files with 245 additions and 75 deletions.
8 changes: 8 additions & 0 deletions ACHNBrowserUI/ACHNBrowserUI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
4CEEFBD92477D9E900663590 /* TurnipsMinMaxPriceRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CEEFBD82477D9E900663590 /* TurnipsMinMaxPriceRow.swift */; };
4CEEFBDB2477E58500663590 /* TurnipsPriceRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CEEFBDA2477E58500663590 /* TurnipsPriceRow.swift */; };
690A72C924752BF4001E7294 /* villagersLikes in Resources */ = {isa = PBXBuildFile; fileRef = 690A72C824752BF4001E7294 /* villagersLikes */; };
69157B7E2471A5A1005B9002 /* TodayMysteryIslandsSection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69157B6D247121ED005B9002 /* TodayMysteryIslandsSection.swift */; };
69157B7F2471A5A1005B9002 /* TodayNookazonSection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 693E2BC3246689C500B85CB8 /* TodayNookazonSection.swift */; };
Expand Down Expand Up @@ -213,6 +215,8 @@
4C7F2F772461F10300930928 /* TurnipsChartVerticalLegend.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TurnipsChartVerticalLegend.swift; sourceTree = "<group>"; };
4C7FE78224574FB50011E8AB /* ActivityIndicator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActivityIndicator.swift; sourceTree = "<group>"; };
4C7FE788245B57A10011E8AB /* AdaptsToSoftwareKeyboard.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AdaptsToSoftwareKeyboard.swift; sourceTree = "<group>"; };
4CEEFBD82477D9E900663590 /* TurnipsMinMaxPriceRow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TurnipsMinMaxPriceRow.swift; sourceTree = "<group>"; };
4CEEFBDA2477E58500663590 /* TurnipsPriceRow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TurnipsPriceRow.swift; sourceTree = "<group>"; };
4CF14B74246B3E9E00F740BF /* TurnipsChartValuesView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TurnipsChartValuesView.swift; sourceTree = "<group>"; };
4CF8049A24635E54005C6C63 /* Path.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Path.swift; sourceTree = "<group>"; };
4F92A4522470AC01003A2911 /* CollectionRowView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CollectionRowView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -511,6 +515,8 @@
isa = PBXGroup;
children = (
69518369245977D1005D28E0 /* TurnipsAveragePriceRow.swift */,
4CEEFBD82477D9E900663590 /* TurnipsMinMaxPriceRow.swift */,
4CEEFBDA2477E58500663590 /* TurnipsPriceRow.swift */,
);
path = rows;
sourceTree = "<group>";
Expand Down Expand Up @@ -1010,6 +1016,7 @@
69157B812471A5A1005B9002 /* RowLoadingView.swift in Sources */,
69157B822471A5A1005B9002 /* ItemDetailViewModel.swift in Sources */,
69157B832471A5A1005B9002 /* TurnipsChartMinBuyPriceCurve.swift in Sources */,
4CEEFBD92477D9E900663590 /* TurnipsMinMaxPriceRow.swift in Sources */,
69157B842471A5A1005B9002 /* ItemRowView.swift in Sources */,
69157B852471A5A1005B9002 /* VillagersListView.swift in Sources */,
69157B862471A5A1005B9002 /* MysteryIslandRow.swift in Sources */,
Expand Down Expand Up @@ -1081,6 +1088,7 @@
69157BC82471A5A1005B9002 /* TurnipsChartMinMaxCurves.swift in Sources */,
69157BC92471A5A1005B9002 /* TurnipsChartGrid.swift in Sources */,
69157BCA2471A5A1005B9002 /* CollectionRowView.swift in Sources */,
4CEEFBDB2477E58500663590 /* TurnipsPriceRow.swift in Sources */,
693352842473C4B5007EF9AB /* TodaySpecialCharacters.swift in Sources */,
69157BCB2471A5A1005B9002 /* SearchField.swift in Sources */,
69157BCC2471A5A1005B9002 /* CollectionProgressDetailView.swift in Sources */,
Expand Down
3 changes: 3 additions & 0 deletions ACHNBrowserUI/ACHNBrowserUI/extensions/View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ extension View {
.offset(x:offset)
)
}

func eraseToAnyView() -> AnyView { AnyView(self) }
func eraseToAnyViewForRow() -> VStack<AnyView> { VStack { eraseToAnyView() } }
}

extension View {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ public struct TurnipPredictions {
return [averagePrices[base], averagePrices[base + 1]]
}

public init(minBuyPrice: Int?, averagePrices: [Int]?, minMax: [[Int]]?, averageProfits: [Int]?) {
public init(
minBuyPrice: Int?,
averagePrices: [Int]?,
minMax: [[Int]]?,
averageProfits: [Int]?
) {
self.minBuyPrice = minBuyPrice
self.averagePrices = averagePrices
self.minMax = minMax
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,29 @@ public class TurnipPredictionsService: ObservableObject {
private func calculate(values: TurnipFields) -> TurnipPredictions {
let call = "calculate([\(values.buyPrice),\(values.fields.filter{ !$0.isEmpty }.joined(separator: ","))])"
let results = calculatorContext?.evaluateScript(call)


let averagePrices: [Int]? = (results?.toDictionary()["avgPattern"] as? [Int])?
.enumerated()
.map({ index, value in
guard let enteredValue = Int(values.fields[index]) else {
return value
}
return enteredValue
})

let minMax: [[Int]]? = (results?.toDictionary()["minMaxPattern"] as? [[Int]])?
.enumerated()
.map({ index, value in
guard let enteredValue = Int(values.fields[index]) else {
return value
}
return [enteredValue, enteredValue]
})

var averageProfits: [Int]?
if let averagePrices = results?.toDictionary()["avgPattern"] as? [Int],
values.amount > 0, let buyPrice = Int(values.buyPrice){
if let averagePrices = averagePrices,
values.amount > 0,
let buyPrice = Int(values.buyPrice) {
averageProfits = []
let investment = values.amount * buyPrice
for avg in averagePrices {
Expand All @@ -70,8 +89,8 @@ public class TurnipPredictionsService: ObservableObject {
}

return TurnipPredictions(minBuyPrice: results?.toDictionary()["minWeekValue"] as? Int,
averagePrices: results?.toDictionary()["avgPattern"] as? [Int],
minMax: results?.toDictionary()["minMaxPattern"] as? [[Int]],
averagePrices: averagePrices,
minMax: minMax,
averageProfits: averageProfits)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class TurnipsViewModel: ObservableObject {

var turnipsCancellable: AnyCancellable?
var exchangeCancellable: AnyCancellable?

init() {
turnipsCancellable = TurnipPredictionsService.shared.$predictions
.receive(on: RunLoop.main)
Expand Down
55 changes: 40 additions & 15 deletions ACHNBrowserUI/ACHNBrowserUI/views/turnips/TurnipsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,23 +155,14 @@ extension TurnipsView {
Text("PM").fontWeight(.bold)
}
}

if turnipsDisplay == .average {
ForEach(0..<viewModel.averagesPrices!.count) { i in
TurnipsAveragePriceRow(label: self.labels[i],
prices: self.viewModel.averagesPrices![i])
}
} else if turnipsDisplay == .minMax {
ForEach(0..<viewModel.minMaxPrices!.count) { i in
TurnipsAveragePriceRow(label: self.labels[i],
minMaxPrices: self.viewModel.minMaxPrices![i])
}
ForEach(uniqueLabels(for: "average"), id: \.1, content: averagePriceRow(day:identifier:))
}
else if turnipsDisplay == .minMax {
ForEach(uniqueLabels(for: "min max"), id: \.1, content: minMaxPriceRow(day:identifier:))
} else if turnipsDisplay == .profits {
if viewModel.averagesProfits != nil {
ForEach(labels, id: \.self) { day in
TurnipsAveragePriceRow(label: day,
prices: self.viewModel.averagesProfits![self.labels.firstIndex(of: day)!])
}
ForEach(uniqueLabels(for: "profits"), id: \.1, content: averageProfitRow(day:identifier:))
} else {
Text("Please add the amount of turnips you bought and for how much")
.foregroundColor(.acHeaderBackground)
Expand All @@ -192,7 +183,6 @@ extension TurnipsView {
self.presentedSheet = .turnipsForm(subManager: self.subManager)
}
}

}
} else {
Text("Add your in game turnip prices to see predictions")
Expand Down Expand Up @@ -221,6 +211,41 @@ extension TurnipsView {
}
}

extension TurnipsView {
private func averagePriceRow(day: String, identifier: String) -> some View {
guard let dayNumber = labels.firstIndex(of: day),
let prices = viewModel.averagesPrices?[dayNumber],
let minMaxPrices = viewModel.minMaxPrices?[dayNumber] else {
return EmptyView().eraseToAnyViewForRow()
}
return TurnipsAveragePriceRow(label: day, prices: prices, minMaxPrices: minMaxPrices)
.eraseToAnyViewForRow()
}

private func minMaxPriceRow(day: String, identifier: String) -> some View {
guard let dayNumber = labels.firstIndex(of: day),
let prices = viewModel.minMaxPrices?[dayNumber],
let averagePrices = viewModel.averagesPrices?[dayNumber] else {
return EmptyView().eraseToAnyViewForRow()
}
return TurnipsMinMaxPriceRow(label: day, prices: prices, averagePrices: averagePrices)
.eraseToAnyViewForRow()
}

private func averageProfitRow(day: String, identifier: String) -> some View {
guard let dayNumber = labels.firstIndex(of: day),
let prices = viewModel.averagesProfits?[dayNumber] else {
return EmptyView().eraseToAnyViewForRow()
}
return TurnipsAveragePriceRow(label: day, prices: prices, minMaxPrices: [])
.eraseToAnyViewForRow()
}

private func uniqueLabels(for uniqueIdentifier: String) -> [(String, String)] {
labels.map { ($0, uniqueIdentifier + $0) }
}
}

struct TurnipsView_Previews: PreviewProvider {
static var previews: some View {
TurnipsView()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// TurnipsAveragePriceROw.swift
// TurnipsAveragePriceRow.swift
// ACHNBrowserUI
//
// Created by Thomas Ricouard on 29/04/2020.
Expand All @@ -10,62 +10,40 @@ import SwiftUI

struct TurnipsAveragePriceRow: View {
let label: String
let prices: [Int]?
let minMaxPrices: [[Int]]?

init(label: String, prices: [Int]) {
self.label = label
self.prices = prices
self.minMaxPrices = nil
}

init(label: String, minMaxPrices: [[Int]]) {
self.label = label
self.prices = nil
self.minMaxPrices = minMaxPrices
}

private func color(price: Int) -> Color {
if price <= 90 {
return .red
} else if price >= 150 {
return .acTabBarBackground
} else {
return .acSecondaryText
}
}

private func color(prices: [Int]) -> Color {
color(price: Int(prices.average))
}

let prices: [Int]
let minMaxPrices: [[Int]]

var body: some View {
HStack {
Text(LocalizedStringKey(label))
.font(.body)
.foregroundColor(.acText)
Spacer()
if prices != nil {
Text("\(prices!.first!)")
.font(.headline)
.fontWeight(.bold)
.foregroundColor(color(price: prices!.first!))
Spacer()
Text("\(prices!.last!)")
.font(.headline)
.fontWeight(.bold)
.foregroundColor(color(price: prices!.last!))
} else if minMaxPrices != nil {
Text("\(minMaxPrices!.first!.first!) - \(minMaxPrices!.first!.last!)")
.font(.headline)
.fontWeight(.bold)
.foregroundColor(color(prices: minMaxPrices!.first!))
Spacer()
Text("\(minMaxPrices!.last!.first!) - \(minMaxPrices!.last!.last!)")
.font(.headline)
.fontWeight(.bold)
.foregroundColor(color(prices: minMaxPrices!.last!))
}
Text("\(prices.first!)")
.font(.headline)
.fontWeight(.bold)
.foregroundColor(color(meridian: .am))
Spacer()
Text("\(prices.last!)")
.font(.headline)
.fontWeight(.bold)
.foregroundColor(color(meridian: .pm))
}
}
}

extension TurnipsAveragePriceRow: TurnipsPriceRow {
enum Meridian { case am, pm }

var averagePrices: [Int] { prices }

func color(meridian: Meridian) -> Color {
if isEntered(meridian: meridian) {
return .acText
} else if let price = meridian == .am ? prices.first : prices.last {
return color(price: price)
} else {
return .clear
}
}
}
Expand All @@ -74,8 +52,8 @@ struct TurnipsAveragePriceRow_Previews: PreviewProvider {
static var previews: some View {
NavigationView {
List {
TurnipsAveragePriceRow(label: "Mon", prices: [90, 95])
TurnipsAveragePriceRow(label: "Mon", prices: [40, 600])
TurnipsAveragePriceRow(label: "Mon", prices: [90, 95], minMaxPrices: [[90, 90], [81, 141]])
TurnipsAveragePriceRow(label: "Mon", prices: [40, 600], minMaxPrices: [[40, 81], [250, 600]])
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//
// TurnipsMinMaxPriceRow.swift
// ACHNBrowserUI
//
// Created by Renaud JENNY on 22/05/2020.
// Copyright © 2020 Thomas Ricouard. All rights reserved.
//

import SwiftUI

struct TurnipsMinMaxPriceRow: View {
let label: String
let prices: [[Int]]
let averagePrices: [Int]

typealias Meridian = TurnipsAveragePriceRow.Meridian

var body: some View {
HStack {
Text(LocalizedStringKey(label))
.font(.body)
.foregroundColor(.acText)
Spacer()
Text(amMinMaxPrices)
.font(.headline)
.fontWeight(.bold)
.foregroundColor(color(meridian: .am))
Spacer()
Text(pmMinMaxPrices)
.font(.headline)
.fontWeight(.bold)
.foregroundColor(color(meridian: .pm))
}
}
}

extension TurnipsMinMaxPriceRow: TurnipsPriceRow {
var minMaxPrices: [[Int]] { prices }

private func color(meridian: Meridian) -> Color {
let amAveragedPrices = prices.first?.average
let pmAveragedPrices = prices.last?.average
guard let averagedPrice = meridian == .am ? amAveragedPrices : pmAveragedPrices else {
return .clear
}
return isEntered(meridian: meridian) ? .acText : color(price: Int(averagedPrice))
}

private var amMinMaxPrices: String {
guard let min = prices.first?.first,
let max = prices.first?.last else {
return ""
}
if isEntered(meridian: .am) {
return "\(min)"
} else {
return "\(min) - \(max)"
}
}

private var pmMinMaxPrices: String {
guard let min = prices.last?.first,
let max = prices.last?.last else {
return ""
}
if isEntered(meridian: .pm) {
return "\(min)"
} else {
return "\(min) - \(max)"
}
}
}

struct TurnipsMinMaxPriceRow_Previews: PreviewProvider {
static var previews: some View {
NavigationView {
List {
TurnipsMinMaxPriceRow(label: "Mon", prices: [[90, 90], [81, 141]], averagePrices: [90, 95])
TurnipsMinMaxPriceRow(label: "Mon", prices: [[60, 90], [81, 81]], averagePrices: [70, 81])
TurnipsMinMaxPriceRow(label: "Mon", prices: [[40, 81], [250, 600]], averagePrices: [40, 600])
}
}
}
}
Loading

0 comments on commit 4af12fb

Please sign in to comment.