Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions SiriusProject/SiriusProject/Networking/Endpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ enum Endpoint {
case sendTokenToServer(url: String = Constants.pushesPath, url1: String = Constants.registerPath, token: String)
case getHistoryNotifications(url: String = Constants.pushesPath, url1: String = Constants.historyPath, token: String)
case sendPushesToAll(url: String = Constants.pushesPath, url1: String = Constants.sendToAllPath, teamname: String, score: Int)
case sendTextPushesToAll(url: String = Constants.pushesPath, url1: String = Constants.sendToAllPath, text: String, title: String)

case deleteAllTeams(url: String = Constants.pushesPath)

Expand Down Expand Up @@ -74,6 +75,8 @@ enum Endpoint {
return url
case let .sendPushesToAll(url, url1, _, _):
return url + url1
case let .sendTextPushesToAll(url, url1, _, _):
return url + url1
}
}

Expand All @@ -88,7 +91,8 @@ enum Endpoint {
.sendTokenToServer,
.getHistoryNotifications,
.deleteAllTeams,
.sendPushesToAll:
.sendPushesToAll,
.sendTextPushesToAll:
return []
case let .enterTeam(_, name):
return [.init(name: "name", value: name)]
Expand Down Expand Up @@ -116,7 +120,8 @@ enum Endpoint {
case .enterTeam,
.addEvent,
.sendTokenToServer,
.sendPushesToAll:
.sendPushesToAll,
.sendTextPushesToAll:
return HTTP.Method.post.rawValue
case .updateTeamName,
.setTeamEventScore:
Expand All @@ -131,6 +136,7 @@ enum Endpoint {
case let .sendTokenToServer(_, _, token):
let jsonPost = try? JSONEncoder().encode(["token": token])
return jsonPost

case let .sendPushesToAll(_, _, teamname, score):
struct PushNotification: Encodable {
let recipients: [String]
Expand All @@ -149,6 +155,26 @@ enum Endpoint {
)

return try? JSONEncoder().encode(notification)

case let .sendTextPushesToAll(_, _, text, title):
struct PushNotification: Encodable {
let recipients: [String]
let title: String
let body: String
let sound: String
let destination: String
}

let notification = PushNotification(
recipients: [],
title: title,
body: text,
sound: "default",
destination: "notification"
)

return try? JSONEncoder().encode(notification)

default:
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,10 @@ struct FakeNetworkManager: NetworkManagerProtocol {
logging("Score: \(score)")
completion(true)
}

func sendTextPushesToAll(text: String, title: String, completion: @escaping (Bool) -> Void) {
logging("Title: \(title)")
logging("Text: \(text)")
completion(true)
}
}
17 changes: 17 additions & 0 deletions SiriusProject/SiriusProject/Networking/NetworkManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,21 @@ struct NetworkManager: NetworkManagerProtocol {
completion(response != nil)
}
}

func sendTextPushesToAll(text: String, title: String, completion: @escaping (Bool) -> Void) {
guard let request = Endpoint.sendTextPushesToAll(text: text, title: title).request else {
logging("Error: Failed to create request")
completion(false)
return
}

service.makeRequest(with: request, respModel: Data.self, logging: logging) { response, error in
if let error = error {
logging(error.localizedDescription)
completion(false)
return
}
completion(response != nil)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ protocol NetworkManagerProtocol {
func getHistoryNotifications(token: String, completion: @escaping ([Notification]) -> Void)
func deleteAllTeams(completion: @escaping (Bool) -> Void)
func sendPushesToAll(teamname: String, score: Int, completion: @escaping (Bool) -> Void)
func sendTextPushesToAll(text: String, title: String, completion: @escaping (Bool) -> Void)
}
6 changes: 4 additions & 2 deletions SiriusProject/SiriusProject/View/SendPushView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ struct SendPushView: View {
.bold()
Spacer()

TextFieldView(title: "Заголовок уведомления", text: $viewModel.pushTitle)

TextFieldView(title: "Текст уведомления", text: $viewModel.pushText)

Button {
viewModel.pushText
viewModel.sendPush()
} label: {
Text("sendpushbutton")
.foregroundStyle(.white)
Expand All @@ -34,7 +36,7 @@ struct SendPushView: View {
.cornerRadius(20)
.padding()
}
.disabled(viewModel.canSubmit)
.disabled(!viewModel.canSubmit)

Spacer()
}
Expand Down
9 changes: 7 additions & 2 deletions SiriusProject/SiriusProject/ViewModel/SendPushViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
import Foundation

class SendPushViewModel: ObservableObject {
@Published var pushTitle: String = ""
@Published var pushText: String = ""

var canSubmit: Bool {
return !pushText.isEmpty
return !pushText.isEmpty && !pushTitle.isEmpty
}

let networkManager: NetworkManagerProtocol
Expand All @@ -22,5 +23,9 @@ class SendPushViewModel: ObservableObject {
self.logging = logging
}

func sendPush() {}
func sendPush() {
networkManager.sendTextPushesToAll(text: pushText, title: pushTitle) { _ in }
pushText = ""
pushTitle = ""
}
}
Loading