Skip to content

Commit

Permalink
Add sort menu and options (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
rizwankce authored Oct 8, 2021
1 parent 16bf25d commit 8a37452
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions Shared/App/SwiftUIView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@

import SwiftUI

enum SortOption {
case amountLowToHigh
case amountHighToLow
case dateOldestFirst
case dateNewestFirst
}

struct ContentView: View {
@State var balanceSheet: [Balance] = [
Balance(amount: 567.8, title: "paid for popcorn", action: .withdrawal),
Expand All @@ -32,6 +39,24 @@ struct ContentView: View {
.padding(.trailing)
}
}

Menu {
Button("Amount: Low to High", action: {
sortBy(.amountLowToHigh)
})
Button("Amount: High to Low", action: {
sortBy(.amountHighToLow)
})
Button("Date: Oldest first", action: {
sortBy(.dateOldestFirst)
})
Button("Date: Newest first", action: {
sortBy(.dateNewestFirst)
})
} label: {
Image(systemName: "arrow.up.arrow.down")
}
.padding()
}
List {
ForEach(balanceSheet) {
Expand Down Expand Up @@ -64,12 +89,34 @@ struct ContentView: View {
}
}
}

private func addItem() {
}

private func deleteRow(at indexSet: IndexSet) {
balanceSheet.remove(atOffsets: indexSet)
}

private func sortBy(_ option: SortOption) {
switch option {
case .amountLowToHigh:
balanceSheet.sort {
$0.amount < $1.amount
}
case .amountHighToLow:
balanceSheet.sort {
$0.amount > $1.amount
}
case .dateOldestFirst:
balanceSheet.sort {
$0.date > $1.date
}
case .dateNewestFirst:
balanceSheet.sort {
$0.date < $1.date
}
}
}
}

struct ContentView_Previews: PreviewProvider {
Expand Down

0 comments on commit 8a37452

Please sign in to comment.