diff --git a/SiriusProject/SiriusProject/Networking/Endpoint.swift b/SiriusProject/SiriusProject/Networking/Endpoint.swift index 4b3d7cb..c704e9e 100644 --- a/SiriusProject/SiriusProject/Networking/Endpoint.swift +++ b/SiriusProject/SiriusProject/Networking/Endpoint.swift @@ -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) @@ -74,6 +75,8 @@ enum Endpoint { return url case let .sendPushesToAll(url, url1, _, _): return url + url1 + case let .sendTextPushesToAll(url, url1, _, _): + return url + url1 } } @@ -88,7 +91,8 @@ enum Endpoint { .sendTokenToServer, .getHistoryNotifications, .deleteAllTeams, - .sendPushesToAll: + .sendPushesToAll, + .sendTextPushesToAll: return [] case let .enterTeam(_, name): return [.init(name: "name", value: name)] @@ -116,7 +120,8 @@ enum Endpoint { case .enterTeam, .addEvent, .sendTokenToServer, - .sendPushesToAll: + .sendPushesToAll, + .sendTextPushesToAll: return HTTP.Method.post.rawValue case .updateTeamName, .setTeamEventScore: @@ -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] @@ -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 } diff --git a/SiriusProject/SiriusProject/Networking/FakeNetworkManager.swift b/SiriusProject/SiriusProject/Networking/FakeNetworkManager.swift index f2a783c..9e5e262 100644 --- a/SiriusProject/SiriusProject/Networking/FakeNetworkManager.swift +++ b/SiriusProject/SiriusProject/Networking/FakeNetworkManager.swift @@ -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) + } } diff --git a/SiriusProject/SiriusProject/Networking/NetworkManager.swift b/SiriusProject/SiriusProject/Networking/NetworkManager.swift index 9d9eba6..7c70f1e 100644 --- a/SiriusProject/SiriusProject/Networking/NetworkManager.swift +++ b/SiriusProject/SiriusProject/Networking/NetworkManager.swift @@ -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) + } + } } diff --git a/SiriusProject/SiriusProject/Networking/NetworkManagerProtocol.swift b/SiriusProject/SiriusProject/Networking/NetworkManagerProtocol.swift index c442053..99cc5c5 100644 --- a/SiriusProject/SiriusProject/Networking/NetworkManagerProtocol.swift +++ b/SiriusProject/SiriusProject/Networking/NetworkManagerProtocol.swift @@ -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) } diff --git a/SiriusProject/SiriusProject/View/SendPushView.swift b/SiriusProject/SiriusProject/View/SendPushView.swift index adefbe6..f364bc3 100644 --- a/SiriusProject/SiriusProject/View/SendPushView.swift +++ b/SiriusProject/SiriusProject/View/SendPushView.swift @@ -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) @@ -34,7 +36,7 @@ struct SendPushView: View { .cornerRadius(20) .padding() } - .disabled(viewModel.canSubmit) + .disabled(!viewModel.canSubmit) Spacer() } diff --git a/SiriusProject/SiriusProject/ViewModel/SendPushViewModel.swift b/SiriusProject/SiriusProject/ViewModel/SendPushViewModel.swift index 9c56304..20f130d 100644 --- a/SiriusProject/SiriusProject/ViewModel/SendPushViewModel.swift +++ b/SiriusProject/SiriusProject/ViewModel/SendPushViewModel.swift @@ -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 @@ -22,5 +23,9 @@ class SendPushViewModel: ObservableObject { self.logging = logging } - func sendPush() {} + func sendPush() { + networkManager.sendTextPushesToAll(text: pushText, title: pushTitle) { _ in } + pushText = "" + pushTitle = "" + } }