From 64c1ac33c93a43c3c39eccc272babc991f9b1fba Mon Sep 17 00:00:00 2001 From: LEEYOONJONG Date: Tue, 6 Dec 2022 17:12:33 +0900 Subject: [PATCH] =?UTF-8?q?#159=20#160=20feat:=20=EB=8B=A4=ED=81=AC?= =?UTF-8?q?=EB=AA=A8=EB=93=9C=20=EB=A1=9C=EC=A7=81=EC=9D=84=20DarkModeMana?= =?UTF-8?q?ger=EB=A1=9C=20=EC=9D=B4=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Segno/Segno.xcodeproj/project.pbxproj | 12 ++++++ .../Segno/Data/Manager/DarkModeManager.swift | 41 +++++++++++++++++++ .../Domain/UseCase/SettingsUseCase.swift | 20 ++------- .../ViewModel/SettingsViewModel.swift | 4 +- 4 files changed, 59 insertions(+), 18 deletions(-) create mode 100644 Segno/Segno/Data/Manager/DarkModeManager.swift diff --git a/Segno/Segno.xcodeproj/project.pbxproj b/Segno/Segno.xcodeproj/project.pbxproj index 6e7acd3..d6573f5 100644 --- a/Segno/Segno.xcodeproj/project.pbxproj +++ b/Segno/Segno.xcodeproj/project.pbxproj @@ -72,6 +72,7 @@ 7940FB31292E065100276EFC /* DiaryDetailEndpoint.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7940FB30292E065100276EFC /* DiaryDetailEndpoint.swift */; }; 7940FB33292E065F00276EFC /* DiaryDetailUseCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7940FB32292E065F00276EFC /* DiaryDetailUseCase.swift */; }; 79767E64293E2A1200E489DD /* DiaryDeleteEndpoint.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79767E63293E2A1200E489DD /* DiaryDeleteEndpoint.swift */; }; + 98003E0E293F20F6009FBC35 /* DarkModeManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 98003E0D293F20F6009FBC35 /* DarkModeManager.swift */; }; 9825F41B29377875005F2163 /* ChangeNicknameUseCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9825F41A29377875005F2163 /* ChangeNicknameUseCase.swift */; }; 9825F41D29377ACF005F2163 /* SettingsRepository.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9825F41C29377ACF005F2163 /* SettingsRepository.swift */; }; 982A2A472924AE74006F6ACD /* UserDefaultsKey.swift in Sources */ = {isa = PBXBuildFile; fileRef = 982A2A462924AE74006F6ACD /* UserDefaultsKey.swift */; }; @@ -162,6 +163,7 @@ 7940FB30292E065100276EFC /* DiaryDetailEndpoint.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DiaryDetailEndpoint.swift; sourceTree = ""; }; 7940FB32292E065F00276EFC /* DiaryDetailUseCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DiaryDetailUseCase.swift; sourceTree = ""; }; 79767E63293E2A1200E489DD /* DiaryDeleteEndpoint.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DiaryDeleteEndpoint.swift; sourceTree = ""; }; + 98003E0D293F20F6009FBC35 /* DarkModeManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DarkModeManager.swift; sourceTree = ""; }; 9825F41A29377875005F2163 /* ChangeNicknameUseCase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChangeNicknameUseCase.swift; sourceTree = ""; }; 9825F41C29377ACF005F2163 /* SettingsRepository.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsRepository.swift; sourceTree = ""; }; 982A2A462924AE74006F6ACD /* UserDefaultsKey.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserDefaultsKey.swift; sourceTree = ""; }; @@ -253,6 +255,7 @@ 4F317791291BEB490019BDFC /* Data */ = { isa = PBXGroup; children = ( + 98003E0C293F20E7009FBC35 /* Manager */, 4FCAC5C3292B792200BF9CDD /* Session */, 4F3177A0291BF1810019BDFC /* Repository */, 4F31779F291BF1780019BDFC /* Network */, @@ -477,6 +480,14 @@ path = Font; sourceTree = ""; }; + 98003E0C293F20E7009FBC35 /* Manager */ = { + isa = PBXGroup; + children = ( + 98003E0D293F20F6009FBC35 /* DarkModeManager.swift */, + ); + path = Manager; + sourceTree = ""; + }; 9894EAF629375272005F2B15 /* Utility */ = { isa = PBXGroup; children = ( @@ -592,6 +603,7 @@ 66A8CF6D29379A9900C17F84 /* UserDetailEndpoint.swift in Sources */, 4FEBFAAD291CF62E00E78139 /* DiaryDetail.swift in Sources */, 4FEBFAB1291CFB5500E78139 /* SHMediaItem+.swift in Sources */, + 98003E0E293F20F6009FBC35 /* DarkModeManager.swift in Sources */, 66A8CF692937945300C17F84 /* UserDetailUseCase.swift in Sources */, 988414AE2922235B007C9132 /* LocalUtilityRepository.swift in Sources */, 66A8CF612935F44100C17F84 /* MyPageViewModel.swift in Sources */, diff --git a/Segno/Segno/Data/Manager/DarkModeManager.swift b/Segno/Segno/Data/Manager/DarkModeManager.swift new file mode 100644 index 0000000..dc42f81 --- /dev/null +++ b/Segno/Segno/Data/Manager/DarkModeManager.swift @@ -0,0 +1,41 @@ +// +// DarkModeManager.swift +// Segno +// +// Created by YOONJONG on 2022/12/06. +// + +import UIKit + +import RxSwift + +class DarkModeManager { + static let shared = DarkModeManager() + + let repository: LocalUtilityRepository + + private init() { + self.repository = LocalUtilityRepositoryImpl() + } + + func getDarkMode() -> Int { + if let mode = repository.getUserDefaultsObject(forKey: .darkmode) as? Int { + debugPrint("SettingsUseCase - getDarkMode : 키가 있습니다 - \(mode)") + return mode + } else { + repository.setUserDefaults(DarkMode.system.rawValue, forKey: .darkmode) + debugPrint("SettingsUseCase - getDarkMode : 키가 없어 \(DarkMode.system.rawValue) 로 설정합니다.") + return DarkMode.system.rawValue + } + } + + func changeDarkMode(to mode: Int) { + repository.setUserDefaults(mode, forKey: .darkmode) + guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene, + let window = windowScene.windows.first else { return } + + window.overrideUserInterfaceStyle = UIUserInterfaceStyle(rawValue: mode) ?? .unspecified + debugPrint("SettingsUseCase - changeDarkMode : \(mode)로 설정") + + } +} diff --git a/Segno/Segno/Domain/UseCase/SettingsUseCase.swift b/Segno/Segno/Domain/UseCase/SettingsUseCase.swift index 31e92a1..4b0d34e 100644 --- a/Segno/Segno/Domain/UseCase/SettingsUseCase.swift +++ b/Segno/Segno/Domain/UseCase/SettingsUseCase.swift @@ -11,7 +11,7 @@ protocol SettingsUseCase { func getAutoPlayMode() -> Bool func changeAutoPlayMode(to mode: Bool) func getDarkMode() -> Int - func changeDarkMode(to mode: Int) -> Single + func changeDarkMode(to mode: Int) } final class SettingsUseCaseImpl: SettingsUseCase { @@ -39,22 +39,10 @@ final class SettingsUseCaseImpl: SettingsUseCase { } func getDarkMode() -> Int { - if let mode = repository.getUserDefaultsObject(forKey: .darkmode) as? Int { - debugPrint("SettingsUseCase - getDarkMode : 키가 있습니다 - \(mode)") - return mode - } else { - repository.setUserDefaults(DarkMode.system.rawValue, forKey: .darkmode) - debugPrint("SettingsUseCase - getDarkMode : 키가 없어 \(DarkMode.system.rawValue) 로 설정합니다.") - return DarkMode.system.rawValue - } + return DarkModeManager.shared.getDarkMode() } - func changeDarkMode(to mode: Int) -> Single { - repository.setUserDefaults(mode, forKey: .darkmode) - debugPrint("SettingsUseCase - changeDarkMode : \(mode)로 설정") - return Single.create { observer in - observer(.success(mode)) - return Disposables.create() - } + func changeDarkMode(to mode: Int) { + DarkModeManager.shared.changeDarkMode(to: mode) } } diff --git a/Segno/Segno/Presentation/ViewModel/SettingsViewModel.swift b/Segno/Segno/Presentation/ViewModel/SettingsViewModel.swift index 616f7f0..3bd0f7a 100644 --- a/Segno/Segno/Presentation/ViewModel/SettingsViewModel.swift +++ b/Segno/Segno/Presentation/ViewModel/SettingsViewModel.swift @@ -42,7 +42,7 @@ final class SettingsViewModel { return settingsUseCase.getDarkMode() } - func changeDarkMode(to mode: Int) -> Single { - return settingsUseCase.changeDarkMode(to: mode) + func changeDarkMode(to mode: Int) { + settingsUseCase.changeDarkMode(to: mode) } }