Skip to content

Commit

Permalink
Display only certain phase option
Browse files Browse the repository at this point in the history
fixes #73
  • Loading branch information
xbjfk committed Mar 22, 2024
1 parent 806fdaf commit 1d8608c
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 4 deletions.
7 changes: 7 additions & 0 deletions CubeTime/Helper/Helper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ extension Array where Element: Equatable {
}
}

extension Collection {
/// Returns the element at the specified index if it is within bounds, otherwise nil.
subscript (safe index: Index) -> Element? {
return indices.contains(index) ? self[index] : nil
}
}

extension RandomAccessCollection where Element: Comparable {
func insertionIndex(of value: Element) -> Index {
var slice : SubSequence = self[...]
Expand Down
13 changes: 12 additions & 1 deletion CubeTime/StopwatchManager/StopwatchManager+Stats.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,19 @@ extension StopwatchManager {
if timeListFilter.isEmpty {
timeListSolvesFiltered = timeListSolves
} else {


timeListSolvesFiltered = timeListSolves.filter{
formatSolveTime(secs: $0.time).hasPrefix(timeListFilter) ||
let time = if let multiphaseSolve = $0 as? MultiphaseSolve,
let phase = timeListShownPhase,
let phases = multiphaseSolve.phases,
let time = ([0] + phases).chunked().map({ $0[1] - $0[0] })[safe: Int(phase)] {
time
} else {
$0.time
}

return formatSolveTime(secs: time).hasPrefix(timeListFilter) ||
($0.comment ?? "").lowercased().contains(timeListFilter.lowercased())
}
}
Expand Down
6 changes: 6 additions & 0 deletions CubeTime/StopwatchManager/StopwatchManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,12 @@ class StopwatchManager: ObservableObject {
}
}

@Published var timeListShownPhase: Int16? = nil {
didSet {
changedTimeListSort()
}
}

var timeListReloadSolve: ((Solve) -> ())?
var timeListSelectAll: (() -> ())?

Expand Down
10 changes: 10 additions & 0 deletions CubeTime/TimeList/TimeListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ struct SortByMenu: View {

var body: some View {
Menu {
if let phaseCount = (stopwatchManager.currentSession as? MultiphaseSession)?.phaseCount {
Menu("Phase") {
Picker("", selection: $stopwatchManager.timeListShownPhase) {
Text("All phases").tag(Optional<Int16>.none)
ForEach(0..<phaseCount, id: \.self) { idx in
Text("Phase \(idx + 1)").tag(Optional<Int16>.some(idx))
}
}
}
}
#warning("TODO: headers not working")
Section("Sort by") {
Picker("", selection: $stopwatchManager.timeListSortBy) {
Expand Down
25 changes: 22 additions & 3 deletions CubeTime/TimeList/TimeListViewInner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ final class TimeListViewController: UICollectionViewController, UICollectionView
typealias Snapshot = NSDiffableDataSourceSnapshot<Int, Solve>
private let timeResuseIdentifier = "TimeCard"


#warning("TODO: subscribe to changes of dynamic type?")
private lazy var cardHeight: CGFloat = {
if traitCollection.preferredContentSizeCategory > UIContentSizeCategory.extraLarge {
return 60
Expand Down Expand Up @@ -165,7 +167,8 @@ final class TimeListViewController: UICollectionViewController, UICollectionView
let stopwatchManager: StopwatchManager
let onClickSolve: (Solve) -> ()

var subscriber: AnyCancellable?
var subscribers: Set<AnyCancellable> = []
var shownPhase: Int16? = nil

lazy var dataSource = makeDataSource()

Expand All @@ -178,10 +181,18 @@ final class TimeListViewController: UICollectionViewController, UICollectionView

super.init(collectionViewLayout: layout)

subscriber = stopwatchManager.$timeListSolvesFiltered
stopwatchManager.$timeListSolvesFiltered
.sink(receiveValue: { [weak self] i in
self?.applySnapshot(i)
})
.store(in: &subscribers)

stopwatchManager.$timeListShownPhase
.sink(receiveValue: { [weak self] newValue in
self?.shownPhase = newValue
self?.collectionView.reloadData()
})
.store(in: &subscribers)
}

required init?(coder: NSCoder) {
Expand All @@ -198,7 +209,15 @@ final class TimeListViewController: UICollectionViewController, UICollectionView
let solveCellRegistration = UICollectionView.CellRegistration<TimeCardCell, Solve> { [weak self] cell, _, item in
guard let self else { return }

cell.label.text = item.timeText
if let multiphaseSolve = item as? MultiphaseSolve,
let phase = shownPhase,
let phases = multiphaseSolve.phases,
let time = ([0] + phases).chunked().map({ $0[1] - $0[0] })[safe: Int(phase)] {
// let time = phases[safe: Int(phase)] {
cell.label.text = formatSolveTime(secs: time)
} else {
cell.label.text = item.timeText
}
cell.label.frame = CGRect(origin: .zero, size: cell.frame.size)

cell.item = item
Expand Down

0 comments on commit 1d8608c

Please sign in to comment.