fix(content): 성인 콘텐츠 설정 동기화와 국가별 인증 분기를 적용한다

This commit is contained in:
Yu Sung
2026-03-27 17:34:02 +09:00
parent 44daabdcae
commit 1d120b58bd
24 changed files with 1029 additions and 133 deletions

View File

@@ -10,113 +10,138 @@ import SwiftUI
struct ContentSettingsView: View {
@StateObject var viewModel = ContentSettingsViewModel()
@ObservedObject private var appState = AppState.shared
var body: some View {
ZStack {
Color.black.ignoresSafeArea()
VStack(spacing: 0) {
DetailNavigationBar(title: "콘텐츠 보기 설정") {
if AppState.shared.isRestartApp {
AppState.shared.setAppStep(step: .splash)
} else {
AppState.shared.back()
}
}
ScrollView(.vertical) {
VStack(spacing: 0) {
HStack(spacing: 0) {
Text("민감한 콘텐츠 보기")
.appFont(size: 15, weight: .bold)
.foregroundColor(Color(hex: "eeeeee"))
Spacer()
Image(viewModel.isAdultContentVisible ? "btn_toggle_on_big" : "btn_toggle_off_big")
.resizable()
.frame(width: 44, height: 27)
.onTapGesture {
viewModel.isAdultContentVisible.toggle()
}
BaseView(isLoading: $viewModel.isLoading) {
ZStack {
Color.black.ignoresSafeArea()
VStack(spacing: 0) {
DetailNavigationBar(title: "콘텐츠 보기 설정") {
if AppState.shared.isRestartApp {
AppState.shared.setAppStep(step: .splash)
} else {
AppState.shared.back()
}
.frame(height: 50)
if viewModel.isAdultContentVisible {
Rectangle()
.frame(height: 1)
.foregroundColor(Color.gray90.opacity(0.3))
}
ScrollView(.vertical) {
VStack(spacing: 0) {
HStack(spacing: 0) {
HStack(spacing: 13.3) {
Image(
viewModel.adultContentPreference == .ALL ?
"btn_radio_select_selected" :
"btn_radio_select_normal"
)
.resizable()
.frame(width: 20, height: 20)
Text("전체")
.appFont(size: 13.3, weight: .medium)
.foregroundColor(Color.grayee)
}
.contentShape(Rectangle())
.onTapGesture {
viewModel.adultContentPreference = .ALL
}
Text("민감한 콘텐츠 보기")
.appFont(size: 15, weight: .bold)
.foregroundColor(Color(hex: "eeeeee"))
Spacer()
HStack(spacing: 13.3) {
Image(
viewModel.adultContentPreference == .MALE ?
"btn_radio_select_selected" :
"btn_radio_select_normal"
)
Image(viewModel.isAdultContentVisible ? "btn_toggle_on_big" : "btn_toggle_off_big")
.resizable()
.frame(width: 20, height: 20)
Text("남성향")
.appFont(size: 13.3, weight: .medium)
.foregroundColor(Color.grayee)
}
.contentShape(Rectangle())
.onTapGesture {
viewModel.adultContentPreference = .MALE
}
Spacer()
HStack(spacing: 13.3) {
Image(
viewModel.adultContentPreference == .FEMALE ?
"btn_radio_select_selected" :
"btn_radio_select_normal"
)
.resizable()
.frame(width: 20, height: 20)
Text("여성향")
.appFont(size: 13.3, weight: .medium)
.foregroundColor(Color.grayee)
}
.contentShape(Rectangle())
.onTapGesture {
viewModel.adultContentPreference = .FEMALE
}
Spacer()
.frame(width: 44, height: 27)
.onTapGesture {
viewModel.handleAdultContentToggleTap()
}
}
.frame(height: 50)
if viewModel.isAdultContentVisible {
Rectangle()
.frame(height: 1)
.foregroundColor(Color.gray90.opacity(0.3))
HStack(spacing: 0) {
HStack(spacing: 13.3) {
Image(
viewModel.adultContentPreference == .ALL ?
"btn_radio_select_selected" :
"btn_radio_select_normal"
)
.resizable()
.frame(width: 20, height: 20)
Text("전체")
.appFont(size: 13.3, weight: .medium)
.foregroundColor(Color.grayee)
}
.contentShape(Rectangle())
.onTapGesture {
viewModel.adultContentPreference = .ALL
}
Spacer()
HStack(spacing: 13.3) {
Image(
viewModel.adultContentPreference == .MALE ?
"btn_radio_select_selected" :
"btn_radio_select_normal"
)
.resizable()
.frame(width: 20, height: 20)
Text("남성향")
.appFont(size: 13.3, weight: .medium)
.foregroundColor(Color.grayee)
}
.contentShape(Rectangle())
.onTapGesture {
viewModel.adultContentPreference = .MALE
}
Spacer()
HStack(spacing: 13.3) {
Image(
viewModel.adultContentPreference == .FEMALE ?
"btn_radio_select_selected" :
"btn_radio_select_normal"
)
.resizable()
.frame(width: 20, height: 20)
Text("여성향")
.appFont(size: 13.3, weight: .medium)
.foregroundColor(Color.grayee)
}
.contentShape(Rectangle())
.onTapGesture {
viewModel.adultContentPreference = .FEMALE
}
Spacer()
}
.frame(height: 50)
}
}
.padding(.vertical, 6.7)
.padding(.horizontal, 13.3)
.background(Color.gray22)
.cornerRadius(10)
.padding(.top, 13.3)
.padding(.horizontal, 13.3)
}
.padding(.vertical, 6.7)
.padding(.horizontal, 13.3)
.background(Color.gray22)
.cornerRadius(10)
.padding(.top, 13.3)
.padding(.horizontal, 13.3)
}
if viewModel.isShowAdultContentAgeCheckDialog {
SodaDialog(
title: I18n.Settings.adultContentAgeCheckTitle,
desc: I18n.Settings.adultContentAgeCheckDesc,
confirmButtonTitle: I18n.Common.yes,
confirmButtonAction: {
viewModel.confirmAdultContentAgeCheck()
},
cancelButtonTitle: I18n.Common.no,
cancelButtonAction: {
viewModel.cancelAdultContentAgeCheck()
}
)
}
}
}
.sodaToast(isPresented: $viewModel.isShowPopup, message: viewModel.errorMessage, autohideIn: 2)
.onAppear {
if let pendingGuideMessage = appState.consumePendingContentSettingsGuideMessage() {
viewModel.errorMessage = pendingGuideMessage
viewModel.isShowPopup = true
}
}
}

View File

@@ -6,28 +6,183 @@
//
import Foundation
import Combine
final class ContentSettingsViewModel: ObservableObject {
@Published var isAdultContentVisible = UserDefaults.isAdultContentVisible() {
didSet {
if oldValue != isAdultContentVisible {
UserDefaults.set(isAdultContentVisible, forKey: .isAdultContentVisible)
AppState.shared.isRestartApp = true
if !isAdultContentVisible {
adultContentPreference = .ALL
UserDefaults.set(ContentType.ALL.rawValue, forKey: .contentPreference)
private let userRepository = UserRepository()
private var subscription = Set<AnyCancellable>()
private let contentPreferenceSubject = PassthroughSubject<ContentPreferenceState, Never>()
private var latestRequestToken = UUID()
private var isApplyingServerState = false
private var lastSyncedState: ContentPreferenceState
@Published var isLoading = false
@Published var errorMessage = ""
@Published var isShowPopup = false
@Published var isShowAdultContentAgeCheckDialog = false
@Published var isAdultContentVisible: Bool
@Published var adultContentPreference: ContentType
init() {
let isAdultContentVisible = UserDefaults.isAdultContentVisible()
let contentPreference = ContentType(rawValue: UserDefaults.string(forKey: .contentPreference)) ?? .ALL
let initialState = ContentPreferenceState(
isAdultContentVisible: isAdultContentVisible,
contentType: isAdultContentVisible ? contentPreference : .ALL
)
_isAdultContentVisible = Published(initialValue: isAdultContentVisible)
_adultContentPreference = Published(initialValue: isAdultContentVisible ? contentPreference : .ALL)
lastSyncedState = initialState
bindContentPreference()
}
private func bindContentPreference() {
$isAdultContentVisible
.dropFirst()
.removeDuplicates()
.sink { [weak self] isAdultContentVisible in
guard let self = self else { return }
if self.isApplyingServerState { return }
if !isAdultContentVisible && self.adultContentPreference != .ALL {
self.adultContentPreference = .ALL
}
}
.store(in: &subscription)
Publishers.CombineLatest($isAdultContentVisible, $adultContentPreference)
.map { isAdultContentVisible, adultContentPreference in
ContentPreferenceState(
isAdultContentVisible: isAdultContentVisible,
contentType: isAdultContentVisible ? adultContentPreference : .ALL
)
}
.removeDuplicates()
.dropFirst()
.sink { [weak self] state in
guard let self = self else { return }
if self.isApplyingServerState { return }
self.applyLocalState(state)
self.contentPreferenceSubject.send(state)
}
.store(in: &subscription)
contentPreferenceSubject
.removeDuplicates()
.debounce(for: .seconds(0.3), scheduler: RunLoop.main)
.sink { [weak self] state in
self?.updateContentPreference(state: state)
}
.store(in: &subscription)
}
private func updateContentPreference(state: ContentPreferenceState) {
let request = makeUpdateContentPreferenceRequest(from: lastSyncedState, to: state)
if request.isEmpty {
return
}
let requestToken = UUID()
latestRequestToken = requestToken
isLoading = true
userRepository
.updateContentPreference(
request: request
)
.sink { [weak self] result in
guard let self = self else { return }
guard self.latestRequestToken == requestToken else { return }
switch result {
case .finished:
DEBUG_LOG("finish")
case .failure(let error):
ERROR_LOG(error.localizedDescription)
self.errorMessage = I18n.Common.commonError
self.isShowPopup = true
self.isLoading = false
}
} receiveValue: { [weak self] response in
guard let self = self else { return }
guard self.latestRequestToken == requestToken else { return }
do {
let decoded = try JSONDecoder().decode(ApiResponse<UpdateContentPreferenceResponse>.self, from: response.data)
if let data = decoded.data, decoded.success {
let serverState = ContentPreferenceState(
isAdultContentVisible: data.isAdultContentVisible,
contentType: data.isAdultContentVisible ? data.contentType : .ALL
)
self.applyServerState(serverState)
} else {
self.errorMessage = decoded.message ?? I18n.Common.commonError
self.isShowPopup = true
}
} catch {
self.errorMessage = I18n.Common.commonError
self.isShowPopup = true
}
self.isLoading = false
}
.store(in: &subscription)
}
private func applyLocalState(_ state: ContentPreferenceState) {
UserDefaults.set(state.isAdultContentVisible, forKey: .isAdultContentVisible)
UserDefaults.set(state.contentType.rawValue, forKey: .contentPreference)
AppState.shared.isRestartApp = true
}
private func applyServerState(_ state: ContentPreferenceState) {
isApplyingServerState = true
isAdultContentVisible = state.isAdultContentVisible
adultContentPreference = state.contentType
applyLocalState(state)
lastSyncedState = state
isApplyingServerState = false
}
private func makeUpdateContentPreferenceRequest(from previousState: ContentPreferenceState, to currentState: ContentPreferenceState) -> UpdateContentPreferenceRequest {
let isAdultContentVisible = previousState.isAdultContentVisible != currentState.isAdultContentVisible
? currentState.isAdultContentVisible
: nil
let contentType = previousState.contentType != currentState.contentType
? currentState.contentType
: nil
return UpdateContentPreferenceRequest(
isAdultContentVisible: isAdultContentVisible,
contentType: contentType
)
}
func handleAdultContentToggleTap() {
if isAdultContentVisible {
isAdultContentVisible = false
} else {
isShowAdultContentAgeCheckDialog = true
}
}
@Published var adultContentPreference = ContentType(rawValue: UserDefaults.string(forKey: .contentPreference)) ?? ContentType.ALL {
didSet {
if oldValue != adultContentPreference {
UserDefaults.set(adultContentPreference.rawValue, forKey: .contentPreference)
AppState.shared.isRestartApp = true
}
}
func confirmAdultContentAgeCheck() {
isShowAdultContentAgeCheckDialog = false
isAdultContentVisible = true
}
func cancelAdultContentAgeCheck() {
isShowAdultContentAgeCheckDialog = false
}
}
private struct ContentPreferenceState: Equatable {
let isAdultContentVisible: Bool
let contentType: ContentType
}

View File

@@ -0,0 +1,10 @@
import Foundation
struct UpdateContentPreferenceRequest: Encodable {
let isAdultContentVisible: Bool?
let contentType: ContentType?
var isEmpty: Bool {
return isAdultContentVisible == nil && contentType == nil
}
}

View File

@@ -0,0 +1,24 @@
import Foundation
struct UpdateContentPreferenceResponse: Decodable {
let isAdultContentVisible: Bool
let contentType: ContentType
enum CodingKeys: String, CodingKey {
case isAdultContentVisible
case contentType
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
isAdultContentVisible = try container.decodeIfPresent(Bool.self, forKey: .isAdultContentVisible) ?? true
let rawContentType =
try container
.decodeIfPresent(String.self, forKey: .contentType)?
.trimmingCharacters(in: .whitespacesAndNewlines)
.uppercased()
contentType = ContentType(rawValue: rawContentType ?? "") ?? .ALL
}
}