Skip to content

Instantly share code, notes, and snippets.

@allfinlir
Created October 27, 2023 17:26
Show Gist options
  • Save allfinlir/4c7f406832bd3ef05a39191b0e256c05 to your computer and use it in GitHub Desktop.
Save allfinlir/4c7f406832bd3ef05a39191b0e256c05 to your computer and use it in GitHub Desktop.
import SwiftUI
struct EditBetView: View {
@Binding var bet: BetModel
@Environment(\.presentationMode) var presentationMode
@EnvironmentObject var betViewModel: BetViewModel
@AppStorage("savehometeam") var homeTeamData: String = "Home Team"
@AppStorage("saveawayteam") var awayTeamData: String = "Away Team"
@State var chooseLeague: Leagues = .chooseleague
@State var homeTeam: String = ""
@State var awayTeam: String = ""
@State var selectDate = Date()
@State var typeOfBetInput: String = ""
@State var oddsOnBet: String = ""
@State var bettedAmount: String = ""
@State var potentialWinnings: String = ""
@State var countBets: Int = 0
@State var descriptionInput: String = ""
@State var addNewBettor: [String] = [""]
@State var addnewNewBettor: String = ""
@State private var presentAddBettorView: Bool = false
let indicesIndexSet = IndexSet(integer: 0)
var body: some View {
Form {
Group {
Section {
Picker("League", selection: $chooseLeague) {
ForEach(Leagues.allCases, id: \.self) { league in
Text(league.localName)
}
}
Picker("Home team", selection: $homeTeam) {
ForEach(selectLeague(league: chooseLeague), id: \.self) { selectedHomeTeam in
Text(selectedHomeTeam)
}
}
.onChange(of: homeTeam) { _ in
homeTeamData = homeTeam
}
Picker("Away team", selection: $awayTeam) {
ForEach(selectLeague(league: chooseLeague), id: \.self) { selectedAwayTeam in
Text(selectedAwayTeam)
}
}
.onChange(of: awayTeam) { _ in
awayTeamData = awayTeam
}
DatePicker("Game starts", selection: $bet.selectDate, displayedComponents: [.date, .hourAndMinute])
.datePickerStyle(.compact)
TextField("Type of bet: 1X2, O/U, DC, HT/FT....", text: $typeOfBetInput)
// .keyboardType(.decimalPad) // not needed because not only numbers here to be inputed
TextField("Odds?", text: $oddsOnBet)
.keyboardType(.decimalPad)
TextField("Amount/Units on bet..", text: $bettedAmount)
.keyboardType(.decimalPad)
HStack {
Picker("Bettor", selection: $addNewBettor) {
ForEach(betViewModel.newBettors) { bettor in
NewBettorView(bettor: bettor)
.tag(bettor.newBettor)
}
}
HStack {
Text("")
}
.contentShape(Rectangle())
.padding(.leading, 16)
.overlay {
HStack {
// Spacer()
Button(action: {
presentAddBettorView = true
}, label: {
Image(systemName: "plus.circle")
.foregroundStyle(.blue)
})
.buttonStyle(PlainButtonStyle())
}
}
.layoutPriority(1)
}
}
Section(header: Text("Motivate bet: ")) {
TextEditor(text: $descriptionInput)
.frame(height: 200)
Button(action: {
bet.league = chooseLeague.rawValue
bet.homeTeam = homeTeam
bet.awayTeam = awayTeam
/* bet.dateForBet = selectDate
bet.typeOfBet = typeOfBetInput
bet.odds = Double(oddsOnBet) ?? 0.0
bet.betAmount = Double(bettedAmount) ?? 0.0
bet.potentialWin = Double(potentialWinnings) ?? 0.0
bet.description = descriptionInput
bet.bettor = [addnewNewBettor]
betViewModel.editBetInfo(bet: bet) */
presentationMode.wrappedValue.dismiss()
}, label: {
Text("Update bet")
.foregroundColor(.green)
})
}
}
}
.sheet(isPresented: $presentAddBettorView, content: {
AddBettorView(addNewBettor: addnewNewBettor, presentAddBettorView: $presentAddBettorView)
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment