- 룰렛 설정 완료 API 적용
- 룰렛 설정 완료 시 신호전송
This commit is contained in:
		| @@ -9,11 +9,12 @@ import Foundation | ||||
|  | ||||
| struct LiveRoomChatRawMessage: Codable { | ||||
|     enum LiveRoomChatRawMessageType: String, Codable { | ||||
|         case DONATION, EDIT_ROOM_INFO, SET_MANAGER | ||||
|         case DONATION, EDIT_ROOM_INFO, SET_MANAGER, TOGGLE_ROULETTE | ||||
|     } | ||||
|      | ||||
|     let type: LiveRoomChatRawMessageType | ||||
|     let message: String | ||||
|     let can: Int | ||||
|     let donationMessage: String? | ||||
|     var isActiveRoulette: Bool? = nil | ||||
| } | ||||
|   | ||||
| @@ -686,7 +686,9 @@ struct LiveRoomView: View { | ||||
|             } | ||||
|              | ||||
|             if viewModel.isShowRouletteSettings { | ||||
|                 RouletteSettingsView(isShowing: $viewModel.isShowRouletteSettings) | ||||
|                 RouletteSettingsView(isShowing: $viewModel.isShowRouletteSettings) { isActiveRoulette in | ||||
|                     self.viewModel.setActiveRoulette(isActiveRoulette: isActiveRoulette) | ||||
|                 } | ||||
|             } | ||||
|              | ||||
|             if viewModel.isLoading && viewModel.liveRoomInfo == nil { | ||||
|   | ||||
| @@ -1328,6 +1328,20 @@ final class LiveRoomViewModel: NSObject, ObservableObject { | ||||
|             UserDefaults.set(jsonData, forKey: .noChatRoomList) | ||||
|         } | ||||
|     } | ||||
|      | ||||
|     func setActiveRoulette(isActiveRoulette: Bool) { | ||||
|         self.popupContent = isActiveRoulette ? "룰렛을 활성화 했습니다." : "룰렛을 비활성화 했습니다." | ||||
|         self.isShowPopup = true | ||||
|         self.agora.sendRawMessageToGroup( | ||||
|             rawMessage: LiveRoomChatRawMessage( | ||||
|                 type: .TOGGLE_ROULETTE, | ||||
|                 message: "", | ||||
|                 can: 0, | ||||
|                 donationMessage: "", | ||||
|                 isActiveRoulette: isActiveRoulette | ||||
|             ) | ||||
|         ) | ||||
|     } | ||||
| } | ||||
|  | ||||
| extension LiveRoomViewModel: AgoraRtcEngineDelegate { | ||||
|   | ||||
| @@ -0,0 +1,14 @@ | ||||
| // | ||||
| //  CreateOrUpdateRouletteRequest.swift | ||||
| //  SodaLive | ||||
| // | ||||
| //  Created by klaus on 2023/12/06. | ||||
| // | ||||
|  | ||||
| import Foundation | ||||
|  | ||||
| struct CreateOrUpdateRouletteRequest: Encodable { | ||||
|     let can: Int | ||||
|     let isActive: Bool | ||||
|     let items: [RouletteItem] | ||||
| } | ||||
| @@ -13,6 +13,7 @@ struct RouletteSettingsView: View { | ||||
|     @StateObject var viewModel = RouletteSettingsViewModel() | ||||
|      | ||||
|     @Binding var isShowing: Bool | ||||
|     let onComplete: (Bool) -> Void | ||||
|      | ||||
|     var body: some View { | ||||
|         GeometryReader { proxy in | ||||
| @@ -132,6 +133,10 @@ struct RouletteSettingsView: View { | ||||
|                             .background(Color(hex: "3bb9f1")) | ||||
|                             .cornerRadius(10) | ||||
|                             .onTapGesture { | ||||
|                                 viewModel.createOrUpdateRoulette { | ||||
|                                     onComplete($0) | ||||
|                                     isShowing = false | ||||
|                                 } | ||||
|                             } | ||||
|                     } | ||||
|                     .padding(13.3) | ||||
| @@ -180,6 +185,6 @@ struct RouletteSettingsView: View { | ||||
|  | ||||
| struct RouletteSettingsView_Previews: PreviewProvider { | ||||
|     static var previews: some View { | ||||
|         RouletteSettingsView(isShowing: .constant(true)) | ||||
|         RouletteSettingsView(isShowing: .constant(true)) { _ in } | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -147,4 +147,52 @@ final class RouletteSettingsViewModel: ObservableObject { | ||||
|         isLoading = false | ||||
|         isShowPreview = true | ||||
|     } | ||||
|      | ||||
|     func createOrUpdateRoulette(onSuccess: @escaping (Bool) -> Void) { | ||||
|         if !isLoading { | ||||
|             isLoading = true | ||||
|              | ||||
|             var items = [RouletteItem]() | ||||
|             for option in options { | ||||
|                 if option.title.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { | ||||
|                     isLoading = false | ||||
|                     errorMessage = "옵션은 빈칸일 수 없습니다." | ||||
|                     isShowErrorPopup = true | ||||
|                     return | ||||
|                 } | ||||
|                  | ||||
|                 items.append(RouletteItem(title: option.title, weight: option.weight)) | ||||
|             } | ||||
|              | ||||
|             let request = CreateOrUpdateRouletteRequest(can: can, isActive: isActive, items: items) | ||||
|             repository.createOrUpdateRoulette(request: request) | ||||
|                 .sink { result in | ||||
|                     switch result { | ||||
|                     case .finished: | ||||
|                         DEBUG_LOG("finish") | ||||
|                     case .failure(let error): | ||||
|                         ERROR_LOG(error.localizedDescription) | ||||
|                     } | ||||
|                 } receiveValue: { [unowned self] response in | ||||
|                     self.isLoading = false | ||||
|                     let responseData = response.data | ||||
|                      | ||||
|                     do { | ||||
|                         let jsonDecoder = JSONDecoder() | ||||
|                         let decoded = try jsonDecoder.decode(ApiResponseWithoutData.self, from: responseData) | ||||
|                          | ||||
|                         if decoded.success { | ||||
|                             onSuccess(isActive) | ||||
|                         } else { | ||||
|                             self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다." | ||||
|                             self.isShowErrorPopup = true | ||||
|                         } | ||||
|                     } catch { | ||||
|                         self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다." | ||||
|                         self.isShowErrorPopup = true | ||||
|                     } | ||||
|                 } | ||||
|                 .store(in: &subscription) | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -11,7 +11,7 @@ struct GetRouletteResponse: Decodable { | ||||
|     let items: [RouletteItem] | ||||
| } | ||||
|  | ||||
| struct RouletteItem: Decodable { | ||||
| struct RouletteItem: Codable { | ||||
|     let title: String | ||||
|     let weight: Int | ||||
| } | ||||
|   | ||||
| @@ -10,6 +10,7 @@ import Moya | ||||
|  | ||||
| enum RouletteApi { | ||||
|     case getRoulette(creatorId: Int) | ||||
|     case createOrUpdateRoulette(request: CreateOrUpdateRouletteRequest) | ||||
| } | ||||
|  | ||||
| extension RouletteApi: TargetType { | ||||
| @@ -19,7 +20,7 @@ extension RouletteApi: TargetType { | ||||
|      | ||||
|     var path: String { | ||||
|         switch self { | ||||
|         case .getRoulette: | ||||
|         case .getRoulette, .createOrUpdateRoulette: | ||||
|             return "/roulette" | ||||
|         } | ||||
|     } | ||||
| @@ -28,6 +29,9 @@ extension RouletteApi: TargetType { | ||||
|         switch self { | ||||
|         case .getRoulette: | ||||
|             return .get | ||||
|              | ||||
|         case .createOrUpdateRoulette: | ||||
|             return .post | ||||
|         } | ||||
|     } | ||||
|      | ||||
| @@ -42,6 +46,9 @@ extension RouletteApi: TargetType { | ||||
|                 parameters: parameters, | ||||
|                 encoding: URLEncoding.queryString | ||||
|             ) | ||||
|              | ||||
|         case .createOrUpdateRoulette(let request): | ||||
|             return .requestJSONEncodable(request) | ||||
|         } | ||||
|     } | ||||
|      | ||||
|   | ||||
| @@ -16,5 +16,9 @@ final class RouletteRepository { | ||||
|     func getRoulette(creatorId: Int) -> AnyPublisher<Response, MoyaError> { | ||||
|         return api.requestPublisher(.getRoulette(creatorId: creatorId)) | ||||
|     } | ||||
|      | ||||
|     func createOrUpdateRoulette(request: CreateOrUpdateRouletteRequest) -> AnyPublisher<Response, MoyaError> { | ||||
|         return api.requestPublisher(.createOrUpdateRoulette(request: request)) | ||||
|     } | ||||
| } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Yu Sung
					Yu Sung