Created
June 27, 2024 05:31
-
-
Save allfinlir/603c49a5d1c020042bfd4204b93790c3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
import CoreData | |
struct AddBetView: View { | |
@Environment(\.managedObjectContext) var moc | |
@Environment(\.presentationMode) var presentationMode | |
@EnvironmentObject var betViewModel: BetViewModel | |
@AppStorage("savehometeam") var homeTeamData: String = "Home Team" | |
@AppStorage("saveawayteam") var awayTeamData: String = "Away Team" | |
@AppStorage("savebaskethometeam") var basketHomeTeamData: String = "Home Team" | |
@AppStorage("savebasketawayteam") var basketAwayTeamData: String = "Away Team" | |
@AppStorage("saveIceHockeyhometeam") var iceHockeyHomeTeamData: String = "Home Team" | |
@AppStorage("saveIceHockeyawayteam") var iceHockeyAwayTeamData: String = "Away Team" | |
@FetchRequest(sortDescriptors: []) var bettors: FetchedResults<BettorsArrayEntity> | |
@State var chooseSport: SelectSport = .basket | |
@State var chooseBasketLeague: BasketLeagues = .chooseLeague | |
@State var chooseLeague: Leagues = .chooseleague | |
@State var chooseIceHockeyLeague: IceHockeyLeagues = .chooseLeague | |
@State var homeTeam: String = "Select Team" | |
@State var awayTeam: String = "Select Team" | |
@State var basketHomeTeam: String = "Select Team" | |
@State var basketAwayTeam: String = "Select Team" | |
@State var iceHockeyHomeTeam: String = "Select Team" | |
@State var iceHockeyAwayTeam: String = "Select Team" | |
@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 selectedBettor: String = "Rikard" | |
@State private var presentAddBettorView: Bool = false | |
var body: some View { | |
Form { | |
Group { | |
Section(header: Text("New bet")) { | |
Picker("Sport", selection: $chooseSport) { | |
ForEach(SelectSport.allCases, id: \.self) { sport in | |
Text(sport.rawValue.capitalized) | |
} | |
} | |
switch chooseSport { | |
case .basket: | |
Picker("League", selection: $chooseBasketLeague) { | |
ForEach(BasketLeagues.allCases, id: \.self) { league in | |
Text(league.localName) | |
} | |
} | |
Picker("Home team", selection: $homeTeam) { | |
ForEach(selectBasketLeague(league: chooseBasketLeague), id: \.self) { selectedHomeTeam in | |
Text(selectedHomeTeam) | |
// .tag(selectedHomeTeam) | |
} | |
} | |
.onChange(of: basketHomeTeam) { | |
basketHomeTeamData = basketHomeTeam | |
} | |
Picker("Away team", selection: $awayTeam) { | |
ForEach(selectBasketLeague(league: chooseBasketLeague), id: \.self) { selectedAwayTeam in | |
Text(selectedAwayTeam) | |
// .tag(selectedAwayTeam) | |
} | |
} | |
.onChange(of: basketAwayTeam) { | |
basketAwayTeamData = basketAwayTeam | |
} | |
case .football: | |
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) | |
// .tag(selectedHomeTeam) | |
} | |
} | |
.onChange(of: homeTeam) { | |
homeTeamData = homeTeam | |
} | |
Picker("Away team", selection: $awayTeam) { | |
ForEach(selectLeague(league: chooseLeague), id: \.self) { selectedAwayTeam in | |
Text(selectedAwayTeam) | |
// .tag(selectedAwayTeam) | |
} | |
} | |
.onChange(of: awayTeam) { | |
awayTeamData = awayTeam | |
} | |
case .iceHockey: | |
Picker("League", selection: $chooseIceHockeyLeague) { | |
ForEach(IceHockeyLeagues.allCases, id: \.self) { league in | |
Text(league.localName) | |
} | |
} | |
Picker("Home team", selection: $homeTeam) { | |
ForEach(selectHockeyLeague(league: chooseIceHockeyLeague), id: \.self) { selectedHomeTeam in | |
Text(selectedHomeTeam) | |
// .tag(selectedHomeTeam) | |
} | |
} | |
.onChange(of: iceHockeyHomeTeam) { | |
iceHockeyHomeTeamData = iceHockeyHomeTeam | |
} | |
Picker("Away team", selection: $awayTeam) { | |
ForEach(selectHockeyLeague(league: chooseIceHockeyLeague), id: \.self) { selectedAwayTeam in | |
Text(selectedAwayTeam) | |
// .tag(selectedAwayTeam) | |
} | |
} | |
.onChange(of: iceHockeyAwayTeam) { | |
iceHockeyAwayTeamData = iceHockeyAwayTeam | |
} | |
} | |
DatePicker("Game starts", selection: $selectDate, displayedComponents: [.date, .hourAndMinute]) | |
.datePickerStyle(.compact) | |
TextField("Type of bet: 1X2, O/U, DC, HT/FT....", text: $typeOfBetInput) | |
TextField("Odds?", text: $oddsOnBet) | |
.keyboardType(.decimalPad) | |
TextField("Amount/Units on bet..", text: $bettedAmount) | |
.keyboardType(.decimalPad) | |
HStack { | |
Picker("Bettor", selection: $selectedBettor) { | |
if bettors.count > 0 { | |
ForEach(bettors, id: \.self) { bettor in | |
Text(bettor.newBettor ?? "Rikard") | |
.tag(bettor.newBettor ?? "Rikard") | |
} | |
} else { | |
Text("Select") | |
.tag("Select") | |
} | |
} | |
HStack { | |
Text("") | |
} | |
.contentShape(Rectangle()) | |
.padding(.leading, 16) | |
.overlay { | |
HStack { | |
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) | |
switch chooseSport { | |
case .basket: | |
Button(action: { | |
saveBasketBet() | |
}, label: { | |
Text("Save bet") | |
.foregroundColor(.green) | |
}) | |
case .football: | |
Button(action: { | |
pressSaveButton() | |
}, label: { | |
Text("Save bet") | |
.foregroundColor(.green) | |
}) | |
case .iceHockey: | |
Button(action: { | |
saveIceHockeyBet() | |
}, label: { | |
Text("Save bet") | |
.foregroundColor(.green) | |
}) | |
} | |
} | |
} | |
} | |
.sheet(isPresented: $presentAddBettorView, content: { | |
AddBettorView(presentAddBettorView: $presentAddBettorView, selectedBettor: $selectedBettor) | |
.presentationDetents([.medium]) | |
.presentationBackground(.thinMaterial) | |
}) | |
} | |
var oddsOnBetReplaceComma: String { | |
oddsOnBet.replacingOccurrences(of: ",", with: ".") | |
} | |
var bettedAmountReplaceComma: String { | |
bettedAmount.replacingOccurrences(of: ",", with: ".") | |
} | |
func pressSaveButton() { | |
betViewModel.addBet(sport: chooseSport.rawValue, league: chooseLeague.rawValue, country: selectedLeagueForCountryFlag(country: chooseLeague), homeTeam: homeTeam, awayTeam: awayTeam, dateForBet: selectDate, typeOfBet: typeOfBetInput, odds: Double(oddsOnBetReplaceComma) ?? 0.0, betAmount: Double(bettedAmountReplaceComma) ?? 0.0, bettor: selectedBettor, potentialWin: Double(potentialWinnings) ?? 0.0, betCount: countBets, description: descriptionInput) | |
presentationMode.wrappedValue.dismiss() | |
} | |
func saveIceHockeyBet() { | |
betViewModel.addIceHockeyBet(sport: chooseSport.rawValue, league: chooseIceHockeyLeague.rawValue, country: iceHockeySelectedLeagueForCountryFlag(country: chooseIceHockeyLeague), homeTeam: iceHockeyHomeTeam, awayTeam: iceHockeyAwayTeam, dateForBet: selectDate, typeOfBet: typeOfBetInput, odds: Double(oddsOnBetReplaceComma) ?? 0.0, betAmount: Double(bettedAmountReplaceComma) ?? 0.0, bettor: selectedBettor, potentialWin: Double(potentialWinnings) ?? 0.0, betCount: countBets, description: descriptionInput) | |
presentationMode.wrappedValue.dismiss() | |
} | |
func saveBasketBet() { | |
betViewModel.addBasketBet(sport: chooseSport.rawValue, league: chooseBasketLeague.rawValue, country: basketSelectedLeagueForCountryFlag(country: chooseBasketLeague), homeTeam: basketHomeTeam, awayTeam: basketAwayTeam, dateForBet: selectDate, typeOfBet: typeOfBetInput, odds: Double(oddsOnBetReplaceComma) ?? 0.0, betAmount: Double(bettedAmountReplaceComma) ?? 0.0, bettor: selectedBettor, potentialWin: Double(potentialWinnings) ?? 0.0, betCount: countBets, description: descriptionInput) | |
presentationMode.wrappedValue.dismiss() | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import SwiftUI | |
enum SelectSport: String, CaseIterable { | |
case basket | |
case football | |
case iceHockey | |
} | |
enum Leagues: String, CaseIterable { | |
case chooseleague = "Choose League" | |
case laLiga = "LaLiga" | |
case premierLeague = "Premier League" | |
case bundesliga = "Bundesliga" | |
case serieA = "Serie A" | |
case ligue1 = "Ligue 1" | |
var localName: LocalizedStringKey { LocalizedStringKey(rawValue)} | |
} | |
enum IceHockeyLeagues: String, CaseIterable { | |
case chooseLeague = "Choose League" | |
case SHL = "SHL" | |
case NHL = "NHL" | |
var localName: LocalizedStringKey { LocalizedStringKey(rawValue)} | |
} | |
enum BasketLeagues: String, CaseIterable { | |
case chooseLeague = "Choose League" | |
case NBA = "NBA" | |
case WNBA = "WNBA" | |
case euroleague = "Euro League" | |
case ligaACB = "Liga ACB" | |
case superLig = "Super Lig" | |
case bundesligaBBL = "Bundesliga BBL" | |
case legaBasketSerieA = "Lega Basket Serie A" | |
case LNBProA = "LNB Pro A" | |
case A1League = "A1 League" | |
case nationalBasketballLeague = "National Basketball League" | |
case chineseBasketballLeague = "Chinese Basketball League" | |
var localName: LocalizedStringKey { LocalizedStringKey(rawValue)} | |
} | |
func selectBasketLeague(league: BasketLeagues) -> [String] { | |
switch league { | |
case .chooseLeague: | |
return ["Select Team"] | |
case .NBA: | |
return ["Select Team", | |
"Atlanta Hawks", | |
"Boston Celtics", | |
"Brooklyn Nets", | |
"Charlotte Hornets", | |
"Chicago Bulls", | |
"Cleveland Cavaliers", | |
"Dallas Mavericks", | |
"Denver Nuggets", | |
"Detroit Pistons", | |
"Golden State Warriors", | |
"Houston Rockets", | |
"Indiana Pacers", | |
"LA Clippers", | |
"Los Angeles Lakers", | |
"Memphis Grizzlies", | |
"Miami Heat", | |
"Milwaukee Bucks", | |
"Minnesota Timberwolves", | |
"New Orleans Pelicans", | |
"New York Knicks", | |
"Oklahoma City Thunder", | |
"Orlando Magic", | |
"Philadelphia 76ers", | |
"Phoenix Suns", | |
"Portland Trail Blazers", | |
"Sacramento Kings", | |
"San Antonio Spurs", | |
"Toronto Raptors", | |
"Utah Jazz", | |
"Washington Wizards" | |
] | |
case .WNBA: | |
return ["Select Team", | |
"Atlanta Dream D", | |
"Chicago Sky D", | |
"Connecticut Sun D", | |
"Dallas Wings D", | |
"Indiana Fever D", | |
"Las Vegas Aces D", | |
"Los Angeles Sparks D", | |
"Minnesota Lynx D", | |
"New York Liberty D", | |
"Phoenix Mercury D", | |
"Seattle Storm D", | |
"Washington Mystics D" | |
] | |
case .euroleague: | |
return ["Select Team", | |
"ALBA Berlin", | |
"Anadolu Efes Istanbul", | |
"AS Monaco", | |
"Baskonia Vitoria-Gasteiz", | |
"Crvena Zvezda Meridianbet Belgrade", | |
"EA7 Emporio Armani Milan", | |
"FC Barcelona", | |
"FC Bayern Munich", | |
"Fenerbahce Beko Istanbul", | |
"LDLC ASVEL Villeurbanne", | |
"Maccabi Playtika Tel Aviv", | |
"Olympiacos Piraeus", | |
"Panathinaikos AKTOR Athens", | |
"Partizan Mozzart Bet Belgrade", | |
"Real Madrid", | |
"Valencia Basket", | |
"Virtus Segafredo Bologna", | |
"Zalgiris Kaunas" | |
] | |
case .ligaACB: | |
return ["Select Team", | |
"Barca", | |
"Baskonia", | |
"BAXI Manresa", | |
"Bilbao Basket", | |
"Casademont Zaragoza", | |
"CB Girona", | |
"CB Granada", | |
"CD Maristas Palencia", | |
"Gran Canaria", | |
"Joventut Badalona", | |
"Lenovo Tenerife", | |
"Monbus Obradoiro", | |
"MoraBanc Andorra", | |
"Real Madrid", | |
"Rio Breogan", | |
"UCAM Murcia CB", | |
"Unicaja", | |
"Valencia Basket" | |
] | |
case .superLig: | |
return ["Select Team", | |
"Anadolu Efes", | |
"Bahcesehir Kol.", | |
"Besiktas", | |
"Bursaspor", | |
"Buyukcekmece", | |
"Çağdaş Bodrum Spor", | |
"Darussafaka", | |
"Fenerbahce", | |
"Galatasaray", | |
"Manisa", | |
"Merkezefendi", | |
"Petkim Spor", | |
"Pinar Karsiyaka", | |
"Samsunspor", | |
"Tofas", | |
"Turk Telekom" | |
] | |
case .bundesligaBBL: | |
return ["Select Team", | |
"Alba Berlin", | |
"Bamberg", | |
"Basketball Braunschweig", | |
"Bayern", | |
"Bonn", | |
"Chemnitz", | |
"Crailsheim Merlins", | |
"Gottingen", | |
"Hamburg", | |
"Heidelberg", | |
"Ludwigsburg", | |
"Oldenburg", | |
"Rostock", | |
"Syntainics MBC", | |
"Tubingen", | |
"Ulm", | |
"Vechta", | |
"Wurzburg" | |
] | |
case .legaBasketSerieA: | |
return ["Select Team", | |
"Basket Napoli", | |
"Brescia", | |
"Brindisi", | |
"Cremona", | |
"Olimpia Milano", | |
"Pesaro", | |
"Pistoia", | |
"Reggiana", | |
"Sassari", | |
"Scafati", | |
"Tortona", | |
"Trento", | |
"Treviso", | |
"Varese", | |
"Venezia", | |
"Virtus Bologna" | |
] | |
case .LNBProA: | |
return ["Select Team", | |
"Ada Blois", | |
"Boulogne-Levallois", | |
"Chalon/Saone", | |
"Cholet", | |
"Dijon", | |
"Gravelines-Dunkerque", | |
"JL Bourg", | |
"Le Mans", | |
"Le Portel", | |
"Limoges", | |
"Lyon-Villeurbanne", | |
"Monaco", | |
"Nancy", | |
"Nanterre", | |
"Paris", | |
"Roanne", | |
"Saint Quentin", | |
"Strasbourg" | |
] | |
case .A1League: | |
return ["Select Team", | |
"AEK Athens", | |
"Apollon Patras", | |
"Aris", | |
"AS Karditsas", | |
"Kolossos Rhodes", | |
"Lavrio", | |
"Maroussi", | |
"Olympiacos", | |
"PAOK", | |
"Panathinaikos", | |
"Peristeri", | |
"Promitheas" | |
] | |
case .nationalBasketballLeague: | |
return ["Select Team", | |
"Adelaide", | |
"Brisbane Bullets", | |
"Cairns Taipans", | |
"Illawarra Hawks", | |
"Melbourne United", | |
"New Zealand Breakers", | |
"Perth", | |
"South East Melbourne", | |
"Sydney", | |
"Tasmania JackJumpers" | |
] | |
case .chineseBasketballLeague: | |
return ["Select Team", | |
"Beijing", | |
"Beijing Royal Fighters", | |
"Fujian", | |
"Guangdong", | |
"Guangzhou", | |
"Jiangsu Dragons", | |
"Jilin", | |
"Liaoning", | |
"Nanjing Tongxi", | |
"Ningbo Rockets", | |
"Qingdao", | |
"Shaanxi Wolves", | |
"Shandong", | |
"Shanghai", | |
"Shanxi Zhongyu", | |
"Shenzhen", | |
"Sichuan", | |
"Tianjin", | |
"Xinjiang", | |
"Zhejiang Chouzhou", | |
"Zhejiang Guangsha"] | |
} | |
} | |
func selectHockeyLeague(league: IceHockeyLeagues) -> [String] { | |
switch league { | |
case .chooseLeague: | |
return ["Select Team"] | |
case .SHL: | |
return ["Select Team", "HV71", "Frölunda", "Färjestad", "Skellefteå"] | |
case .NHL: | |
return ["Select Team", "Washington Capitals", "Boston Bruins", "Vancouver Canucks", "New York Rangers"] | |
} | |
} | |
func selectLeague(league: Leagues) -> [String] { | |
switch league { | |
case .chooseleague: | |
return ["Select Team"] | |
case .laLiga: | |
return ["Select Team", "Almeria", "Ath. Bilbao", "Atletico Madrid", "Cadiz CF", "Celta Vigo", "Elche", "Espanyol", "FC Barcelona", "Getafe", "Girona", "Mallorca", "Osasuna", "Real Betis", "Real Madrid", "Real Sociedad", "Sevilla", "Valencia", "Valladolid", "Vallencano", "Villarreal"] | |
case .premierLeague: | |
return ["Select Team", "Arsenal", "Aston Villa", "Bournemouth", "Brentford", "Brighton", "Chelsea", "Crystal Palace", "Everton", "Fulham", "Leeds", "Leicester", "Liverpool", "Man City", "Man United", "Newcastle", "Nottingham Forest", "Southhampton", "Tottenham", "West ham", "Wolverhampton"] | |
case .bundesliga: | |
return ["Select Team", "Augsburg", "B.Mönchengladbach", "Bayern Munchen", "Bochum", "Dortmund", "Eintracht Frankfurt", "FC Köln", "Freiburg", "Hertha Berlin", "Hoffenheim", "Leverkusen", "Mainz", "RB Leipzig", "Schalke 04", "Stuttgart", "Union Berlin", "Werder Bremen", "Wolfsburg"] | |
case .serieA: | |
return ["Select Team", "Atalanta", "Bologna", "Cremonese", "Empoli", "Fiorentina", "Inter", "Juventus", "Lazio", "Lecce", "Milan", "Monza", "Napoli", "Roma", "Salernitana", "Sampdoria", "Sassuolo", "Spezia", "Torino", "Udinese", "Verona"] | |
case .ligue1: | |
return ["Select Team", "AC Ajaccio", "Angers", "Auxerre", "Brest", "Clermont", "Lens", "Lille", "Lorient", "Lyon", "Marseille", "Monaco", "Montpellier", "Nantes", "Nice", "PSG", "Reims", "Rennes", "Strasbourg", "Toulouse", "Troyes"] | |
} | |
} | |
func selectedLeagueForCountryFlag(country: Leagues) -> String { | |
switch country { | |
case .chooseleague: | |
return "" | |
case .laLiga: | |
return "spain" | |
case .premierLeague: | |
return "england" | |
case .bundesliga: | |
return "germany" | |
case .serieA: | |
return "italy" | |
case .ligue1: | |
return "france" | |
} | |
} | |
func basketSelectedLeagueForCountryFlag(country: BasketLeagues) -> String { | |
switch country { | |
case .chooseLeague: | |
return "" | |
case .NBA: | |
return "unitedstates" | |
case .WNBA: | |
return "unitedstates" | |
case .euroleague: | |
return "europe" | |
case .ligaACB: | |
return "spain" | |
case .superLig: | |
return "turkey" | |
case .bundesligaBBL: | |
return "germany" | |
case .legaBasketSerieA: | |
return "italy" | |
case .LNBProA: | |
return "france" | |
case .A1League: | |
return "greece" | |
case .nationalBasketballLeague: | |
return "australia" | |
case .chineseBasketballLeague: | |
return "china" | |
} | |
} | |
func iceHockeySelectedLeagueForCountryFlag(country: IceHockeyLeagues) -> String { | |
switch country { | |
case .chooseLeague: | |
return "" | |
case .SHL: | |
return "sweden" | |
case .NHL: | |
return "unitedstates" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment