Skip to content

Commit

Permalink
Remove FEATURE_RN_FRIENDS flag
Browse files Browse the repository at this point in the history
  • Loading branch information
peng-u-0807 committed Sep 7, 2023
1 parent 1155703 commit 0dfddd2
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 224 deletions.
4 changes: 2 additions & 2 deletions SNUTT-2022/SNUTT.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -2013,7 +2013,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "match Development com.wafflestudio.snutt.dev";
"PROVISIONING_PROFILE_SPECIFIER[sdk=iphoneos*]" = "match Development com.wafflestudio.snutt.dev";
SWIFT_ACTIVE_COMPILATION_CONDITIONS = "DEBUG FEATURE_RN_FRIENDS";
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_STRICT_CONCURRENCY = targeted;
SWIFT_VERSION = 5.0;
Expand Down Expand Up @@ -2057,7 +2057,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "match Development com.wafflestudio.snutt.dev";
"PROVISIONING_PROFILE_SPECIFIER[sdk=iphoneos*]" = "match Development com.wafflestudio.snutt.dev";
SWIFT_ACTIVE_COMPILATION_CONDITIONS = FEATURE_RN_FRIENDS;
SWIFT_ACTIVE_COMPILATION_CONDITIONS = "";
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_STRICT_CONCURRENCY = targeted;
SWIFT_VERSION = 5.0;
Expand Down
82 changes: 26 additions & 56 deletions SNUTT-2022/SNUTT/AppState/AppEnvironment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ extension AppEnvironment {
let popupService: PopupServiceProtocol
let etcService: EtcServiceProtocol
let vacancyService: VacancyServiceProtocol
#if FEATURE_RN_FRIENDS
let friendsService: FriendsServiceProtocol
#endif
let friendsService: FriendsServiceProtocol
}
}

Expand Down Expand Up @@ -135,33 +133,19 @@ extension AppEnvironment {
let popupService = PopupService(appState: appState, webRepositories: webRepositories, localRepositories: localRepositories)
let etcService = EtcService(appState: appState, webRepositories: webRepositories)
let vacancyService = VacancyService(appState: appState, webRepositories: webRepositories, localRepositories: localRepositories)
#if FEATURE_RN_FRIENDS
let friendsService = FriendsService(appState: appState, webRepositories: webRepositories, localRepositories: localRepositories)
return .init(timetableService: timetableService,
userService: userService,
lectureService: lectureService,
searchService: searchService,
globalUIService: globalUIService,
courseBookService: courseBookService,
authService: authService,
notificationService: notificationService,
popupService: popupService,
etcService: etcService,
vacancyService: vacancyService,
friendsService: friendsService)
#else
return .init(timetableService: timetableService,
userService: userService,
lectureService: lectureService,
searchService: searchService,
globalUIService: globalUIService,
courseBookService: courseBookService,
authService: authService,
notificationService: notificationService,
popupService: popupService,
etcService: etcService,
vacancyService: vacancyService)
#endif
let friendsService = FriendsService(appState: appState, webRepositories: webRepositories, localRepositories: localRepositories)
return .init(timetableService: timetableService,
userService: userService,
lectureService: lectureService,
searchService: searchService,
globalUIService: globalUIService,
courseBookService: courseBookService,
authService: authService,
notificationService: notificationService,
popupService: popupService,
etcService: etcService,
vacancyService: vacancyService,
friendsService: friendsService)
}
}

Expand All @@ -179,32 +163,18 @@ extension EnvironmentValues {
#if DEBUG
extension AppEnvironment.Services {
@MainActor static func preview(appState: AppState) -> Self {
#if FEATURE_RN_FRIENDS
.init(timetableService: FakeTimetableService(),
userService: FakeUserService(),
lectureService: FakeLectureService(),
searchService: FakeSearchService(),
globalUIService: GlobalUIService(appState: appState, localRepositories: .init(userDefaultsRepository: UserDefaultsRepository(storage: .preview)), webRepositories: nil),
courseBookService: FakeCourseBookService(),
authService: FakeAuthService(),
notificationService: FakeNotificationService(),
popupService: FakePopupService(),
etcService: FakeEtcService(),
vacancyService: FakeVacancyService(),
friendsService: FakeFriendsService())
#else
.init(timetableService: FakeTimetableService(),
userService: FakeUserService(),
lectureService: FakeLectureService(),
searchService: FakeSearchService(),
globalUIService: GlobalUIService(appState: appState, localRepositories: .init(userDefaultsRepository: UserDefaultsRepository(storage: .preview)), webRepositories: nil),
courseBookService: FakeCourseBookService(),
authService: FakeAuthService(),
notificationService: FakeNotificationService(),
popupService: FakePopupService(),
etcService: FakeEtcService(),
vacancyService: FakeVacancyService())
#endif
.init(timetableService: FakeTimetableService(),
userService: FakeUserService(),
lectureService: FakeLectureService(),
searchService: FakeSearchService(),
globalUIService: GlobalUIService(appState: appState, localRepositories: .init(userDefaultsRepository: UserDefaultsRepository(storage: .preview)), webRepositories: nil),
courseBookService: FakeCourseBookService(),
authService: FakeAuthService(),
notificationService: FakeNotificationService(),
popupService: FakePopupService(),
etcService: FakeEtcService(),
vacancyService: FakeVacancyService(),
friendsService: FakeFriendsService())
}
}
#endif
157 changes: 77 additions & 80 deletions SNUTT-2022/SNUTT/Services/FriendsService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,109 +5,106 @@
// Created by user on 2023/08/11.
//

#if FEATURE_RN_FRIENDS
import Alamofire
import Foundation
import Alamofire
import Foundation

@MainActor
protocol FriendsServiceProtocol: Sendable {
@discardableResult func fetchReactNativeBundleUrl() async throws -> URL
}
@MainActor
protocol FriendsServiceProtocol: Sendable {
@discardableResult func fetchReactNativeBundleUrl() async throws -> URL
}

struct FriendsService: FriendsServiceProtocol, ConfigsProvidable {
var appState: AppState
var webRepositories: AppEnvironment.WebRepositories
var localRepositories: AppEnvironment.LocalRepositories
struct FriendsService: FriendsServiceProtocol, ConfigsProvidable {
var appState: AppState
var webRepositories: AppEnvironment.WebRepositories
var localRepositories: AppEnvironment.LocalRepositories

private let rnBundlePath = "ReactNativeBundles"
private let rnBundlePath = "ReactNativeBundles"

private var vacancyRepository: any VacancyRepositoryProtocol {
webRepositories.vacancyRepository
}
private var vacancyRepository: any VacancyRepositoryProtocol {
webRepositories.vacancyRepository
}

private var configRepository: any ConfigRepositoryProtocol {
webRepositories.configRepository
}
private var configRepository: any ConfigRepositoryProtocol {
webRepositories.configRepository
}

private var userDefaultsRepository: any UserDefaultsRepositoryProtocol {
localRepositories.userDefaultsRepository
}
private var userDefaultsRepository: any UserDefaultsRepositoryProtocol {
localRepositories.userDefaultsRepository
}

// MARK: RN Bundle Management
// MARK: RN Bundle Management

// TODO: refactor with modularization; move to infra layer
// TODO: refactor with modularization; move to infra layer

func fetchReactNativeBundleUrl() async throws -> URL {
let configsDto = try await fetchConfigs()
guard let remoteUrlString = configsDto.reactNativeBundleFriends?.iosBundleSrc,
let remoteUrl = URL(string: remoteUrlString)
else {
throw STError(.INVALID_RN_BUNDLE)
}
func fetchReactNativeBundleUrl() async throws -> URL {
let configsDto = try await fetchConfigs()
guard let remoteUrlString = configsDto.reactNativeBundleFriends?.iosBundleSrc,
let remoteUrl = URL(string: remoteUrlString)
else {
throw STError(.INVALID_RN_BUNDLE)
}

let localUrl = constructLocalCacheUrl(from: remoteUrl)
if FileManager.default.fileExists(atPath: localUrl.path) {
return localUrl
}
clearReactNativeBundlesCache() // always maintain the latest bundle file
let downloadUrl = try await downloadFile(from: remoteUrl)
return downloadUrl
let localUrl = constructLocalCacheUrl(from: remoteUrl)
if FileManager.default.fileExists(atPath: localUrl.path) {
return localUrl
}
clearReactNativeBundlesCache() // always maintain the latest bundle file
let downloadUrl = try await downloadFile(from: remoteUrl)
return downloadUrl
}

private var bundlesURL: URL {
guard let cacheURL = try? FileManager.default.url(for: .cachesDirectory, in: .userDomainMask, appropriateFor: nil, create: true) else {
return FileManager.default.temporaryDirectory
}
return cacheURL.appendingPathComponent(rnBundlePath)
private var bundlesURL: URL {
guard let cacheURL = try? FileManager.default.url(for: .cachesDirectory, in: .userDomainMask, appropriateFor: nil, create: true) else {
return FileManager.default.temporaryDirectory
}
return cacheURL.appendingPathComponent(rnBundlePath)
}

private func constructLocalCacheUrl(from remoteUrl: URL) -> URL {
var bundlesURL = self.bundlesURL
for component in remoteUrl.pathComponents.dropFirst() {
bundlesURL = bundlesURL.appendingPathComponent(component)
}
return bundlesURL
private func constructLocalCacheUrl(from remoteUrl: URL) -> URL {
var bundlesURL = self.bundlesURL
for component in remoteUrl.pathComponents.dropFirst() {
bundlesURL = bundlesURL.appendingPathComponent(component)
}
return bundlesURL
}

private func clearReactNativeBundlesCache() {
do {
let fileURLs = try FileManager.default.contentsOfDirectory(at: bundlesURL,
includingPropertiesForKeys: nil,
options: .skipsHiddenFiles)
for fileURL in fileURLs {
try FileManager.default.removeItem(at: fileURL)
}
} catch {
// pass
private func clearReactNativeBundlesCache() {
do {
let fileURLs = try FileManager.default.contentsOfDirectory(at: bundlesURL,
includingPropertiesForKeys: nil,
options: .skipsHiddenFiles)
for fileURL in fileURLs {
try FileManager.default.removeItem(at: fileURL)
}
} catch {
// pass
}
}

func downloadFile(from remoteUrl: URL) async throws -> URL {
return try await withCheckedThrowingContinuation { continuation in
let destinationBlock: DownloadRequest.Destination = { _, _ in
let cacheURL = constructLocalCacheUrl(from: remoteUrl)
return (cacheURL, [.removePreviousFile, .createIntermediateDirectories])
}
AF.download(remoteUrl, to: destinationBlock).response { response in
switch response.result {
case let .success(url):
guard let url else {
continuation.resume(throwing: STError(.INVALID_RN_BUNDLE))
return
}
continuation.resume(returning: url)
case let .failure(error):
continuation.resume(throwing: error)
func downloadFile(from remoteUrl: URL) async throws -> URL {
return try await withCheckedThrowingContinuation { continuation in
let destinationBlock: DownloadRequest.Destination = { _, _ in
let cacheURL = constructLocalCacheUrl(from: remoteUrl)
return (cacheURL, [.removePreviousFile, .createIntermediateDirectories])
}
AF.download(remoteUrl, to: destinationBlock).response { response in
switch response.result {
case let .success(url):
guard let url else {
continuation.resume(throwing: STError(.INVALID_RN_BUNDLE))
return
}
continuation.resume(returning: url)
case let .failure(error):
continuation.resume(throwing: error)
}
}
}
}
}

struct FakeFriendsService: FriendsServiceProtocol {
func fetchReactNativeBundleUrl() async throws -> URL {
throw STError(.INVALID_RN_BUNDLE)
}
struct FakeFriendsService: FriendsServiceProtocol {
func fetchReactNativeBundleUrl() async throws -> URL {
throw STError(.INVALID_RN_BUNDLE)
}

#endif
}
33 changes: 15 additions & 18 deletions SNUTT-2022/SNUTT/ViewModels/FriendsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,23 @@
// Created by 박신홍 on 2023/07/15.
//

#if FEATURE_RN_FRIENDS
import Foundation
import Foundation

class FriendsViewModel: BaseViewModel, ObservableObject {
override init(container: DIContainer) {
super.init(container: container)
}
class FriendsViewModel: BaseViewModel, ObservableObject {
override init(container: DIContainer) {
super.init(container: container)
}

var accessToken: String? {
appState.user.accessToken
}
var accessToken: String? {
appState.user.accessToken
}

func fetchReactNativeBundleUrl() async -> URL? {
do {
return try await services.friendsService.fetchReactNativeBundleUrl()
} catch {
services.globalUIService.presentErrorAlert(error: error)
}
return nil
func fetchReactNativeBundleUrl() async -> URL? {
do {
return try await services.friendsService.fetchReactNativeBundleUrl()
} catch {
services.globalUIService.presentErrorAlert(error: error)
}
return nil
}

#endif
}
6 changes: 2 additions & 4 deletions SNUTT-2022/SNUTT/Views/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ import UIKit
///
/// See [here](https://www.raywenderlich.com/20201639-firebase-cloud-messaging-for-ios-push-notifications) for more information about FCM configuration.
class AppDelegate: NSObject, UIApplicationDelegate {
#if FEATURE_RN_FRIENDS
/// `ReactNativeKit` and `ReactNativeDevKit` requires this property to be declared.
var window: UIWindow?
#endif
/// `ReactNativeKit` and `ReactNativeDevKit` requires this property to be declared.
var window: UIWindow?

var firebaseConfigName: String {
#if DEBUG
Expand Down
Loading

0 comments on commit 0dfddd2

Please sign in to comment.