fix/api-calls #25

Merged
emre.kartal merged 3 commits from fix/api-calls into master 8 months ago

@ -15,13 +15,13 @@ struct AllcoinsCounter: View {
var body: some View {
HStack(alignment: .center) {
Image("allcoinIcon")
.resizable()
.frame(width: 17, height: 17, alignment: .leading)
Text(String(appStateContainer.user?.nbCoins ?? 0))
.contentTransition(.numericText())
.fontWeight(.black)
.foregroundColor(foregroundColor)
Image("allcoinIcon")
.resizable()
.frame(width: 17, height: 17, alignment: .leading)
}
.frame(width: 90, height: 40)
.background(backgroundColor)

@ -19,7 +19,7 @@ struct BetCard: View {
VStack(alignment: .leading,spacing: 2){
HStack{
Spacer()
Text("bet_proposed_by_format \(bet.author.capitalized)")
Text("bet_proposed_by_format \(bet.author.capitalized)")
.font(.system(size: 10))
.foregroundColor(AllInColors.grey800Color)

@ -6,22 +6,39 @@
//
import SwiftUI
import Model
struct ChoiceCapsule: View {
let filter: BetFilter
@State var pressed = false
@ObservedObject var viewModel: BetViewModel
var label: String {
switch filter {
case .isPublic:
return String(localized: "bet_public")
case .isInvitation:
return String(localized: "bet_invitation")
case .inProgress:
return String(localized: "bet_current")
case .isFinished:
return String(localized: "bet_finished")
default:
return "NaN"
}
}
var body: some View {
Group {
if(pressed) {
Text("bet_current")
Text(label)
.textStyle(weight: .semibold, color: .white, size: 15)
.padding([.leading,.trailing],13.8)
.padding([.top,.bottom], 7)
.background(AllInColors.lightPurpleColor)
.clipShape(Capsule())
} else {
Text("bet_current")
Text(label)
.textStyle(weight: .regular, color: AllInColors.grey800Color, size: 15)
.padding([.leading,.trailing], 15)
.padding([.top,.bottom], 7)
@ -34,14 +51,13 @@ struct ChoiceCapsule: View {
}
}
.onTapGesture() {
if(!pressed) {
viewModel.filters.insert(filter)
} else {
viewModel.filters.remove(filter)
}
pressed.toggle()
}
}
}
struct ChoiceCapsule_Previews: PreviewProvider {
static var previews: some View {
ChoiceCapsule()
}
}

@ -6,38 +6,49 @@
//
import SwiftUI
import Model
struct Friend: View {
var image: String
var pseudo: String
var user: User
@ObservedObject var viewModel: FriendsViewModel
var StatusValues: (String, Color, Color) {
switch user.friendStatus {
case .friend:
return (String(localized: "generic_delete"), AllInColors.grey400Color, AllInColors.componentBackgroundColor)
case .notFriend:
return (String(localized: "generic_add"), .white, AllInColors.lightPurpleColor)
case .requested:
return (String(localized: "friends_request_sent"), AllInColors.grey400Color, AllInColors.componentBackgroundColor)
default:
return ("NaN", AllInColors.grey400Color, AllInColors.componentBackgroundColor)
}
}
var body: some View {
HStack{
AsyncImage(url: URL(string: image))
AsyncImage(url: URL(string: "https://picsum.photos/536/354"))
.frame(width: 50, height: 50)
.cornerRadius(180)
.scaledToFit()
Text(pseudo)
Text(user.username)
.fontWeight(.medium)
.padding(.leading, 5)
.font(.system(size: 18))
.lineLimit(1)
Spacer()
Button("generic_delete") {}
.frame(width: 90, height: 30)
.foregroundColor(AllInColors.grey400Color)
.font(.system(size: 14))
.background(AllInColors.componentBackgroundColor)
.cornerRadius(5)
Button(StatusValues.0) {
viewModel.toggleFriendStatus(for: user)
}
.minimumScaleFactor(0.3)
.lineLimit(2)
.frame(width: 90, height: 30)
.foregroundColor(StatusValues.1)
.font(.system(size: 14))
.background(StatusValues.2)
.cornerRadius(5)
}
.padding([.trailing,.leading], 25)
}
}
struct Friend_Previews: PreviewProvider {
static var previews: some View {
Friend(image: "https://picsum.photos/536/354", pseudo: "Lucas")
}
}

@ -23,8 +23,8 @@ struct ReviewCard: View {
HStack{
Spacer()
Text("bet_proposed_by_format \(betDetail.bet.author)")
.font(.system(size: 10))
.foregroundColor(AllInColors.grey800Color)
.font(.system(size: 10))
.foregroundColor(AllInColors.grey800Color)
}
Text(betDetail.bet.theme).font(.system(size: 15)).foregroundColor(AllInColors.grey800Color)
@ -50,7 +50,8 @@ struct ReviewCard: View {
.font(.system(size: 25))
.fontWeight(.bold)
}
else{Text(amountBetted.description)
else{
Text(amountBetted.description)
.foregroundColor(.white)
.font(.system(size: 25))
.fontWeight(.bold)
@ -63,7 +64,7 @@ struct ReviewCard: View {
.fontWeight(.bold)
}
Spacer()
}
.frame(width: .infinity)
.padding(.all,10)
@ -74,9 +75,9 @@ struct ReviewCard: View {
.background(
isAWin || betDetail.bet.endBetDate < Date() ?
AnyView(AllInColors.primaryGradient) :
AnyView(Color.black)
AnyView(Color.black)
) .cornerRadius(20, corners: [.bottomLeft,.bottomRight])
.border(width: 1, edges: [.top], color: AllInColors.delimiterGrey)
.border(width: 1, edges: [.top], color: AllInColors.delimiterGrey)
}
.onTapGesture {
showDetails.toggle()

@ -131,7 +131,8 @@
"bet_ends" = "Ends on";
"bet_ended" = "Ended on";
"bet_participate" = "Participate";
"bet_proposed_by_format %@" = "Proposed by %@";
"bet_proposed_by_format %@" = "Proposed by %@";
"bet_proposed_by_format" = "Proposed by";
"bet_players_waiting_format %@" = "%@ joueurs en attente";
"bet_players_format" = "players";
"bet_points_at_stake_format" = "points at stake";
@ -165,6 +166,7 @@
/// Friends
"friends_title" = "Friends";
"friends_request_sent" = "Request sent";
/// Daily reward

@ -132,6 +132,7 @@
"bet_ended" = "A pris fin le";
"bet_participate" = "Participer";
"bet_proposed_by_format %@" = "Proposé par %@";
"bet_proposed_by_format" = "Proposé par";
"bet_players_waiting_format %@" = "%@ players waiting";
"bet_players_format" = "joueurs";
"bet_points_at_stake_format" = "points en jeu";
@ -165,6 +166,7 @@
/// Friends
"friends_title" = "Amis";
"friends_request_sent" = "Requête envoyée";
/// Daily reward

@ -17,21 +17,38 @@ class BetViewModel: ObservableObject {
@Published private(set) var bets: [Bet] = []
@Published var betsOver: [BetDetail] = []
@Published var showingSheet: Bool = false
init() {
getItems()
}
@Published var filters: Set<BetFilter> = [] {
didSet {
getItems()
}
}
private var cancellables = Set<AnyCancellable>()
init() {
getItems()
// Observer for changes in filters
$filters
.sink { [weak self] _ in
self?.getItems()
}
.store(in: &cancellables)
}
func getItems() {
manager.getBets(withIndex: 0, withCount: 20) { bets in
self.bets = bets
manager.getBets(withIndex: 0, withCount: 20, filters: Array(filters)) { bets in
DispatchQueue.main.async {
self.bets = bets
}
}
manager.getBetsOver() { bets in
self.betsOver = bets
if !self.betsOver.isEmpty {
self.showingSheet = true
DispatchQueue.main.async {
self.betsOver = bets
if !self.betsOver.isEmpty {
self.showingSheet = true
}
}
print(bets)
}
}
}

@ -21,10 +21,7 @@ class CurrentBetViewModel: ObservableObject {
func getItems() {
manager.getCurrentBets(withIndex: 0, withCount: 20) { bets in
for bet in bets {
let betDetail = BetDetail(bet: bet, answers: [AnswerDetail(response: "OUI", totalStakes: 120, totalParticipants: 2, highestStake: 200, odds: 1.2), AnswerDetail(response: "NON", totalStakes: 120, totalParticipants: 2, highestStake: 200, odds: 1.2)], participations: [])
self.bets.append(betDetail)
}
self.bets = bets
}
}

@ -13,23 +13,69 @@ class FriendsViewModel: ObservableObject {
@Inject var manager: Manager
@Published private(set) var users: [User] = []
@Published var text: String = "" {
didSet {
if text.isEmpty {
getItems()
} else {
search()
}
}
}
init() {
getItems()
}
func getItems ( ) {
func getItems() {
manager.getFriends() { friends in
DispatchQueue.main.async {
self.users = friends
}
}
}
func deleteItem(indexSet: IndexSet) {
func search() {
guard text.allSatisfy({ $0.isLetter || $0.isNumber }) else {
return
}
manager.getUsers(withName: text) { users in
self.users = users
}
}
func moveltem(from: IndexSet, to: Int) {
func toggleFriendStatus(for user: User) {
guard let index = users.firstIndex(where: { $0.username == user.username }) else { return }
var updatedUser = users[index]
switch updatedUser.friendStatus {
case .friend:
updatedUser.friendStatus = .notFriend
deleteItem(username: user.username)
case .notFriend:
updatedUser.friendStatus = .requested
addItem(username: user.username)
case .requested:
updatedUser.friendStatus = .notFriend
deleteItem(username: user.username)
default:
break
}
users[index] = updatedUser
}
func addItem(title: String) {
func deleteItem(username: String) {
manager.removeFriend(withUsername: username) { statusCode in
}
}
func addItem(username: String) {
manager.addFriend(withUsername: username) { statusCode in
}
}
}

@ -13,23 +13,17 @@ class RankingViewModel: ObservableObject {
@Inject var manager: Manager
@Published private(set) var friends: [User] = []
init() {
getItems()
}
func getItems ( ) {
}
func deleteItem(indexSet: IndexSet) {
}
func moveltem(from: IndexSet, to: Int) {
}
func addItem(title: String) {
func getItems() {
manager.getFriends() { users in
var friends = users
friends.append(AppStateContainer.shared.user!)
self.friends = friends.sorted(by: { $0.nbCoins > $1.nbCoins })
}
}
}

@ -35,13 +35,10 @@ struct BetView: View {
AllInColors.fadeInGradiantCard
ScrollView(.horizontal,showsIndicators: false){
HStack{
ChoiceCapsule()
ChoiceCapsule()
ChoiceCapsule()
ChoiceCapsule()
ChoiceCapsule()
ChoiceCapsule()
ChoiceCapsule()
ChoiceCapsule(filter: .isPublic, viewModel: viewModel)
ChoiceCapsule(filter: .isInvitation, viewModel: viewModel)
ChoiceCapsule(filter: .inProgress, viewModel: viewModel)
ChoiceCapsule(filter: .isFinished, viewModel: viewModel)
}
.padding(.leading,25)
.padding([.top,.bottom],15)

@ -16,16 +16,16 @@ struct DetailsView: View {
if let betType = viewModel.betDetail?.bet.status {
switch betType {
case .inProgress:
return ("bet_status_in_progress", AllInColors.darkPurpleColor)
return (String(localized: "bet_status_in_progress"), AllInColors.darkPurpleColor)
case .waiting, .closing:
return ("bet_status_waiting", AllInColors.pink100)
return (String(localized: "bet_status_waiting"), AllInColors.pink100)
case .finished:
return ("bet_status_finished", AllInColors.grey100Color)
return (String(localized: "bet_status_finished"), AllInColors.grey100Color)
case .cancelled:
return ("bet_status_cancelled", AllInColors.grey100Color)
return (String(localized: "bet_status_cancelled"), AllInColors.grey100Color)
}
} else {
return ("bet_status_unavailable", AllInColors.pink100)
return (String(localized: "bet_status_unavailable"), AllInColors.pink100)
}
}

@ -9,6 +9,7 @@ import SwiftUI
struct FriendsView: View {
@StateObject private var viewModel = FriendsViewModel()
@Binding var showMenu: Bool
var body: some View {
@ -18,12 +19,37 @@ struct FriendsView: View {
.textStyle(weight: .bold, color: AllInColors.grey500Color, size: 25)
.padding([.top,.bottom],15)
HStack {
TextField("Search...", text: $viewModel.text)
.padding(7)
.padding(.horizontal, 25)
.background(Color(.systemGray6))
.cornerRadius(8)
.overlay(
HStack {
Image(systemName: "magnifyingglass")
.foregroundColor(.gray)
.frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
.padding(.leading, 8)
if !viewModel.text.isEmpty {
Button(action: {
self.viewModel.text = ""
}) {
Image(systemName: "multiply.circle.fill")
.foregroundColor(.gray)
.padding(.trailing, 8)
}
}
}
)
.padding(.horizontal, 10)
}
ScrollView(showsIndicators: false){
Friend(image: "https://picsum.photos/536/354", pseudo: "Lucas")
Friend(image: "https://picsum.photos/536/354", pseudo: "Arthur")
Friend(image: "https://picsum.photos/536/354", pseudo: "Lucase")
Friend(image: "https://picsum.photos/536/354", pseudo: "Rayhan")
ForEach(viewModel.users, id: \.self) { friend in
Friend(user: friend, viewModel: viewModel)
}
}
.padding(.top, 25)
Spacer()
@ -32,9 +58,3 @@ struct FriendsView: View {
.background(AllInColors.backgroundColor)
}
}
struct FriendsView_Previews: PreviewProvider {
static var previews: some View {
FriendsView(showMenu: .constant(false))
}
}

@ -87,8 +87,10 @@ struct ProfileView: View {
Spacer()
Image(systemName: "chevron.right")
.resizable()
.frame(width: 8, height: 12)
.foregroundColor(parameters[index].itemColor)
.padding(.trailing, 8)
.padding(.trailing, 14)
}
.padding(.vertical, 15)
.background(parameters[index].backgroundColor)

@ -10,6 +10,7 @@ import SwiftUI
struct RankingView: View {
@Binding var showMenu: Bool
@StateObject var viewModel = RankingViewModel()
var body: some View {
GeometryReader { geometry in
@ -17,121 +18,126 @@ struct RankingView: View {
TopBar(showMenu: self.$showMenu)
Text("ranking_title")
.textStyle(weight: .bold, color: AllInColors.grey500Color, size: 25)
.padding([.top,.bottom],15)
.padding([.top,.bottom], 15)
HStack {
ZStack {
VStack(spacing: 0){
Spacer()
Text("Pseudo")
.fontWeight(.bold)
.padding(.bottom, 4)
.font(.system(size: 16))
.lineLimit(1)
Divider()
.background(AllInColors.lightGrey100Color)
HStack{
Spacer()
Image("allcoinIcon")
if !viewModel.friends.isEmpty {
HStack {
if viewModel.friends.indices.contains(0) {
ZStack {
VStack(spacing: 0) {
Spacer()
Text(viewModel.friends[0].username)
.fontWeight(.bold)
.padding(.bottom, 4)
.font(.system(size: 16))
.lineLimit(1)
Divider()
.background(AllInColors.lightGrey100Color)
HStack {
Spacer()
Image("allcoinIcon")
.resizable()
.frame(width: 18, height: 18, alignment: .leading)
.padding([.top, .bottom], 10)
Text(String(viewModel.friends[0].nbCoins))
.textStyle(weight: .black, color: AllInColors.lightPurpleColor, size: 16)
.padding(.trailing, 18)
Spacer()
}
.frame(width: geometry.size.width * 0.43)
.background(AllInColors.underComponentBackgroundColor)
}
.frame(width: geometry.size.width * 0.43, height: 120)
.background(AllInColors.componentBackgroundColor)
.cornerRadius(41.5, corners: .topLeft)
.cornerRadius(8, corners: .topRight)
.cornerRadius(15, corners: [.bottomLeft, .bottomRight])
Image("defaultUserImage")
.resizable()
.frame(width: 18, height: 18, alignment: .leading)
.padding([.top,.bottom],10)
Text(String("570"))
.textStyle(weight: .black, color: AllInColors.lightPurpleColor, size: 16)
.padding(.trailing, 18)
Spacer()
.frame(width: 70, height: 70)
.scaledToFit()
.cornerRadius(180)
.offset(x: 0, y: -55)
Text("1")
.frame(width: 28, height: 28)
.foregroundColor(.white)
.background(AllInColors.lightPurpleColor)
.cornerRadius(30)
.font(.system(size: 18))
.fontWeight(.bold)
.offset(x: 0, y: -23)
}
.frame(width: geometry.size.width * 0.43)
.background(AllInColors.underComponentBackgroundColor)
}
.frame(width: geometry.size.width * 0.43, height: 120)
.background(AllInColors.componentBackgroundColor)
.cornerRadius(41.5, corners: .topLeft)
.cornerRadius(8, corners: .topRight)
.cornerRadius(15, corners: [.bottomLeft, .bottomRight])
Image("defaultUserImage")
.resizable()
.frame(width: 70, height: 70)
.scaledToFit()
.cornerRadius(180)
.offset(x: 0, y: -55)
Text("1")
.frame(width: 28, height: 28)
.foregroundColor(.white)
.background(AllInColors.lightPurpleColor)
.cornerRadius(30)
.font(.system(size: 18))
.fontWeight(.bold)
.offset(x: 0, y: -23)
}
ZStack {
VStack(spacing: 0){
Spacer()
Text("Pseudo")
.fontWeight(.bold)
.padding(.bottom, 4)
.font(.system(size: 15))
.lineLimit(1)
Divider()
.background(AllInColors.lightGrey100Color)
HStack{
Spacer()
Image("allcoinIcon")
if viewModel.friends.indices.contains(1) {
ZStack {
VStack(spacing: 0) {
Spacer()
Text(viewModel.friends[1].username)
.fontWeight(.bold)
.padding(.bottom, 4)
.font(.system(size: 15))
.lineLimit(1)
Divider()
.background(AllInColors.lightGrey100Color)
HStack {
Spacer()
Image("allcoinIcon")
.resizable()
.frame(width: 18, height: 18, alignment: .leading)
.padding([.top, .bottom], 10)
Text(String(viewModel.friends[1].nbCoins))
.textStyle(weight: .black, color: AllInColors.lightPurpleColor, size: 16)
.padding(.trailing, 18)
Spacer()
}
.frame(width: geometry.size.width * 0.43)
.background(AllInColors.underComponentBackgroundColor)
}
.frame(width: geometry.size.width * 0.43, height: 90)
.background(AllInColors.componentBackgroundColor)
.cornerRadius(27.5, corners: .topRight)
.cornerRadius(8, corners: .topLeft)
.cornerRadius(15, corners: [.bottomLeft, .bottomRight])
Image("defaultUserImage")
.resizable()
.frame(width: 18, height: 18, alignment: .leading)
.padding([.top,.bottom],10)
Text(String("570"))
.textStyle(weight: .black, color: AllInColors.lightPurpleColor, size: 16)
.padding(.trailing, 18)
Spacer()
.frame(width: 50, height: 50)
.scaledToFit()
.cornerRadius(180)
.offset(x: 0, y: -50)
Text("2")
.frame(width: 20, height: 20)
.foregroundColor(.white)
.background(AllInColors.lightPurpleColor)
.cornerRadius(30)
.font(.system(size: 14))
.fontWeight(.bold)
.offset(x: 0, y: -28)
}
.frame(width: geometry.size.width * 0.43)
.background(AllInColors.underComponentBackgroundColor)
.padding(.top, 28)
}
.frame(width: geometry.size.width * 0.43, height: 90)
.background(AllInColors.componentBackgroundColor)
.cornerRadius(27.5, corners: .topRight)
.cornerRadius(8, corners: .topLeft)
.cornerRadius(15, corners: [.bottomLeft, .bottomRight])
Image("defaultUserImage")
.resizable()
.frame(width: 60, height: 60)
.scaledToFit()
.cornerRadius(180)
.offset(x: 0, y: -50)
Text("2")
.frame(width: 23, height: 23)
.foregroundColor(.white)
.background(AllInColors.lightPurpleColor)
.cornerRadius(30)
.font(.system(size: 15))
.fontWeight(.bold)
.offset(x: 0, y: -22)
}
.padding(.top, 28)
.padding([.leading, .trailing, .top], 20)
}
.padding([.leading,.trailing,.top],20)
ScrollView(showsIndicators: false){
VStack(spacing: 10) {
RankingRow(number: 3, image: "defaultUserImage", pseudo: "Lucas", allCoins: 541)
RankingRow(number: 4, image: "defaultUserImage", pseudo: "Arthur", allCoins: 542)
ScrollView(showsIndicators: false) {
ForEach(viewModel.friends.indices.dropFirst(2), id: \.self) { index in
let friend = viewModel.friends[index]
RankingRow(
number: index + 1,
image: "defaultUserImage",
pseudo: friend.username,
allCoins: friend.nbCoins
)
}
}.padding(.top, 10)
}
.padding(.top, 10)
Spacer()
}
.edgesIgnoringSafeArea(.bottom).background(AllInColors.backgroundColor)
}
}
}
struct RankingView_Previews: PreviewProvider {
static var previews: some View {
RankingView(showMenu: .constant(false))
}
}

@ -16,21 +16,30 @@ public struct BetApiManager: BetDataManager {
self.token = token
}
public func getBets(withIndex index: Int, withCount count: Int, completion: @escaping ([Bet]) -> Void) {
public func getBets(withIndex index: Int, withCount count: Int, filters: [BetFilter] = [], completion: @escaping ([Bet]) -> Void) {
let url = URL(string: allInApi + "bets/gets")!
var request = URLRequest(url: url)
request.httpMethod = "GET"
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
var bets: [Bet] = []
let filterStrings = filters.map { $0.rawValue }
let jsonDictionary: [String: Any] = ["filters": filterStrings]
do {
let jsonData = try JSONSerialization.data(withJSONObject: jsonDictionary, options: [])
request.httpBody = jsonData
} catch {
print("Error creating JSON data: \(error)")
return
}
URLSession.shared.dataTask(with: request) { data, response, error in
if let data = data {
print ("ALLIN : get bets")
do {
if let httpResponse = response as? HTTPURLResponse, let jsonArray = try JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]] {
var bets: [Bet] = []
for json in jsonArray {
if let bet = FactoryApiBet().toBet(from: json) {
bets.append(bet)
@ -45,6 +54,7 @@ public struct BetApiManager: BetDataManager {
}
}.resume()
}
public func getUsers(username: String) -> [User] {
return []

@ -73,8 +73,100 @@ public struct UserApiManager: UserDataManager {
}
}
public func getFriends() -> [User] {
fatalError("Not implemented yet")
public func addFriend(username: String, completion : @escaping (Int)-> ()) {
let url = URL(string: allInApi + "friends/add")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
let json: [String: Any] = ["username": username]
if let jsonData = try? JSONSerialization.data(withJSONObject: json, options: []){
URLSession.shared.uploadTask(with: request, from: jsonData) { data, response, error in
print ("ALLIN : Add friend")
if let httpResponse = response as? HTTPURLResponse {
print(httpResponse.statusCode)
completion(httpResponse.statusCode)
}
}.resume()
}
}
public func removeFriend(username: String, completion : @escaping (Int)-> ()) {
let url = URL(string: allInApi + "friends/delete")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
let json: [String: Any] = ["username": username]
if let jsonData = try? JSONSerialization.data(withJSONObject: json, options: []){
URLSession.shared.uploadTask(with: request, from: jsonData) { data, response, error in
print ("ALLIN : Remove friend")
if let httpResponse = response as? HTTPURLResponse {
print(httpResponse.statusCode)
completion(httpResponse.statusCode)
}
}.resume()
}
}
public func getFriends(completion: @escaping ([User]) -> Void) {
let url = URL(string: allInApi + "friends/gets")!
var request = URLRequest(url: url)
request.httpMethod = "GET"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
var users: [User] = []
URLSession.shared.dataTask(with: request) { data, response, error in
if let data = data {
print ("ALLIN : get friends")
do {
if let httpResponse = response as? HTTPURLResponse, let jsonArray = try JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]] {
print(jsonArray)
users = try JSONDecoder().decode([User].self, from: JSONSerialization.data(withJSONObject: jsonArray))
print(httpResponse.statusCode)
completion(users)
}
} catch {
print("Error parsing JSON: \(error)")
}
}
}.resume()
}
public func getUsers(withName name: String, completion: @escaping ([User]) -> Void) {
let url = URL(string: allInApi + "friends/search/" + name)!
var request = URLRequest(url: url)
request.httpMethod = "GET"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
var users: [User] = []
URLSession.shared.dataTask(with: request) { data, response, error in
if let data = data {
print ("ALLIN : get friends by search")
do {
if let httpResponse = response as? HTTPURLResponse, let jsonArray = try JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]] {
print(jsonArray)
users = try JSONDecoder().decode([User].self, from: JSONSerialization.data(withJSONObject: jsonArray))
print(httpResponse.statusCode)
completion(users)
}
} catch {
print("Error parsing JSON: \(error)")
}
}
}.resume()
}
public func getGifts(completion : @escaping (Int, Int)-> ()) {
@ -106,7 +198,7 @@ public struct UserApiManager: UserDataManager {
fatalError("Not implemented yet")
}
public func getCurrentBets(withIndex index: Int, withCount count: Int, completion: @escaping ([Bet]) -> Void) {
public func getCurrentBets(withIndex index: Int, withCount count: Int, completion: @escaping ([BetDetail]) -> Void) {
let url = URL(string: allInApi + "bets/current")!
var request = URLRequest(url: url)
@ -114,7 +206,7 @@ public struct UserApiManager: UserDataManager {
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
var bets: [Bet] = []
var bets: [BetDetail] = []
URLSession.shared.dataTask(with: request) { data, response, error in
if let data = data {
@ -123,7 +215,7 @@ public struct UserApiManager: UserDataManager {
if let httpResponse = response as? HTTPURLResponse, let jsonArray = try JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]] {
for json in jsonArray {
print(json)
if let bet = FactoryApiBet().toBet(from: json) {
if let bet = FactoryApiBet().toBetDetail(from: json) {
bets.append(bet)
}
}

@ -8,7 +8,7 @@
import Foundation
public protocol BetDataManager {
func getBets(withIndex index: Int, withCount count: Int, completion: @escaping ([Bet]) -> Void)
func getBets(withIndex index: Int, withCount count: Int, filters: [BetFilter], completion: @escaping ([Bet]) -> Void)
func getUsers(username: String) -> [User]
func getBet(withId id: String, completion: @escaping (BetDetail) -> Void)
}

@ -0,0 +1,16 @@
//
// BetFilter.swift
//
//
// Created by Lucas Delanier on 27/05/2024.
//
import Foundation
public enum BetFilter: String, Codable {
case inProgress = "IN_PROGRESS"
case isPublic = "PUBLIC"
case isInvitation = "INVITATION"
case isFinished = "FINISHED"
}

@ -0,0 +1,14 @@
//
// FriendStatus.swift
//
//
// Created by Emre on 28/05/2024.
//
import Foundation
public enum FriendStatus: String, Codable {
case friend = "FRIEND"
case requested = "REQUESTED"
case notFriend = "NOT_FRIEND"
}

@ -21,9 +21,21 @@ public struct Manager {
completion(status)
}
}
public func addFriend(withUsername username: String, completion : @escaping (Int)-> ()) {
userDataManager.addFriend(username: username) { status in
completion(status)
}
}
public func removeFriend(withUsername username: String, completion : @escaping (Int)-> ()) {
userDataManager.removeFriend(username: username) { status in
completion(status)
}
}
public func getBets(withIndex index: Int, withCount count: Int, completion: @escaping ([Bet]) -> Void) {
betDataManager.getBets(withIndex: index, withCount: count) { bets in
public func getBets(withIndex index: Int, withCount count: Int, filters: [BetFilter], completion: @escaping ([Bet]) -> Void) {
betDataManager.getBets(withIndex: index, withCount: count, filters: filters) { bets in
completion(bets)
}
}
@ -40,13 +52,25 @@ public struct Manager {
}
}
public func getFriends(completion: @escaping ([User]) -> Void) {
userDataManager.getFriends() { users in
completion(users)
}
}
public func getUsers(withName name: String, completion: @escaping ([User]) -> Void) {
userDataManager.getUsers(withName: name) { users in
completion(users)
}
}
public func getHistoricBets(withIndex index: Int, withCount count: Int, completion: @escaping ([Bet]) -> Void) {
userDataManager.getOldBets(withIndex: index, withCount: count) { bets in
completion(bets)
}
}
public func getCurrentBets(withIndex index: Int, withCount count: Int, completion: @escaping ([Bet]) -> Void) {
public func getCurrentBets(withIndex index: Int, withCount count: Int, completion: @escaping ([BetDetail]) -> Void) {
userDataManager.getCurrentBets(withIndex: index, withCount: count) { bets in
completion(bets)
}

@ -9,6 +9,7 @@ import Foundation
/// A subclass of Bet that represents a bet on a match between two teams.
public class MatchBet: Bet {
/// The name of the first team involved in the match.
public var nameTeam1: String

@ -8,7 +8,7 @@
import Foundation
/// A struct representing a user with details such as username, email, number of coins, and friends.
public struct User: Codable {
public struct User: Codable, Hashable {
/// The username of the user.
public private(set) var username: String
@ -19,6 +19,9 @@ public struct User: Codable {
/// The number of coins associated with the user.
public var nbCoins: Int
/// The friendship status with the main user.
public var friendStatus: FriendStatus?
/// Custom constructor to initialize a User instance.
///
/// - Parameters:
@ -30,4 +33,18 @@ public struct User: Codable {
self.email = email
self.nbCoins = nbCoins
}
/// Custom constructor to initialize a User instance.
///
/// - Parameters:
/// - username: The username of the user.
/// - email: The email address of the user.
/// - nbCoins: The number of coins associated with the user.
/// - status: The friendship status with the main user.
public init(username: String, email: String, nbCoins: Int, status: FriendStatus) {
self.username = username
self.email = email
self.nbCoins = nbCoins
self.friendStatus = status
}
}

@ -11,10 +11,13 @@ public protocol UserDataManager {
func getBets(withIndex index: Int, withCount count: Int) -> [Bet]
func getBetsOver(completion: @escaping ([BetDetail]) -> Void)
func addBet(bet: Bet, completion : @escaping (Int)-> ())
func getFriends() -> [User]
func addFriend(username: String, completion : @escaping (Int)-> ())
func removeFriend(username: String, completion : @escaping (Int)-> ())
func getFriends(completion: @escaping ([User]) -> Void)
func getUsers(withName name: String, completion: @escaping ([User]) -> Void)
func getGifts(completion : @escaping (Int, Int)-> ())
func getOldBets(withIndex index: Int, withCount count: Int, completion: @escaping ([Bet]) -> Void)
func getCurrentBets(withIndex index: Int, withCount count: Int, completion: @escaping ([Bet]) -> Void)
func getCurrentBets(withIndex index: Int, withCount count: Int, completion: @escaping ([BetDetail]) -> Void)
func addParticipation(withId id: String, withAnswer answer: String, andStake stake: Int, completion : @escaping (Int)-> ())
func addResponse(withIdBet id: String, andResponse responseBet: String)
}

@ -12,7 +12,7 @@ public struct BetStubManager: BetDataManager {
public init() {}
public func getBets(withIndex index: Int, withCount count: Int, completion: @escaping ([Bet]) -> Void) {
public func getBets(withIndex index: Int, withCount count: Int, filters: [BetFilter] = [], completion: @escaping ([Bet]) -> Void) {
completion(Stub.shared.bets)
}

Loading…
Cancel
Save