Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1 @@
disabled_rules:
- line_length
- trailing_whitespace
included:
- today-s-sound
- Tests
excluded:
- Pods
- Carthage
- DerivedData
reporter: "xcode"
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# ===== Config =====
SCHEME ?= today-s-sound
PROJECT ?= today-s-sound.xcodeproj
DEST ?= platform=iOS Simulator,name=iPhone 16 Pro
DEST ?= platform=iOS Simulator,name=iPhone 16 Pro,OS=18.6
CONFIG ?= Debug
SDK ?= iphonesimulator

Expand Down
56 changes: 54 additions & 2 deletions today-s-sound.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
objectVersion = 77;
objects = {

/* Begin PBXBuildFile section */
9ADF457E2E950B6100E8B5A2 /* CombineMoya in Frameworks */ = {isa = PBXBuildFile; productRef = 9ADF457D2E950B6100E8B5A2 /* CombineMoya */; };
9ADF45802E950B6100E8B5A2 /* Moya in Frameworks */ = {isa = PBXBuildFile; productRef = 9ADF457F2E950B6100E8B5A2 /* Moya */; };
9ADF45822E950B6100E8B5A2 /* ReactiveMoya in Frameworks */ = {isa = PBXBuildFile; productRef = 9ADF45812E950B6100E8B5A2 /* ReactiveMoya */; };
9ADF45842E950B6100E8B5A2 /* RxMoya in Frameworks */ = {isa = PBXBuildFile; productRef = 9ADF45832E950B6100E8B5A2 /* RxMoya */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
259FC9672E890D7F001152B9 /* today-s-sound.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "today-s-sound.app"; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -36,6 +43,10 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
9ADF45802E950B6100E8B5A2 /* Moya in Frameworks */,
9ADF457E2E950B6100E8B5A2 /* CombineMoya in Frameworks */,
9ADF45842E950B6100E8B5A2 /* RxMoya in Frameworks */,
9ADF45822E950B6100E8B5A2 /* ReactiveMoya in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -78,6 +89,10 @@
);
name = "today-s-sound";
packageProductDependencies = (
9ADF457D2E950B6100E8B5A2 /* CombineMoya */,
9ADF457F2E950B6100E8B5A2 /* Moya */,
9ADF45812E950B6100E8B5A2 /* ReactiveMoya */,
9ADF45832E950B6100E8B5A2 /* RxMoya */,
);
productName = "today-s-sound";
productReference = 259FC9672E890D7F001152B9 /* today-s-sound.app */;
Expand Down Expand Up @@ -107,6 +122,9 @@
);
mainGroup = 259FC95E2E890D7E001152B9;
minimizedProjectReferenceProxies = 1;
packageReferences = (
9ADF457C2E950ADB00E8B5A2 /* XCRemoteSwiftPackageReference "Moya" */,
);
preferredProjectObjectVersion = 77;
productRefGroup = 259FC9682E890D7F001152B9 /* Products */;
projectDirPath = "";
Expand Down Expand Up @@ -265,7 +283,7 @@
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = NO;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = "today-s-sound/Info.plist";
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
Expand Down Expand Up @@ -293,7 +311,7 @@
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = NO;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = "today-s-sound/Info.plist";
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
Expand Down Expand Up @@ -335,6 +353,40 @@
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */

/* Begin XCRemoteSwiftPackageReference section */
9ADF457C2E950ADB00E8B5A2 /* XCRemoteSwiftPackageReference "Moya" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/Moya/Moya";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 15.0.3;
};
};
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
9ADF457D2E950B6100E8B5A2 /* CombineMoya */ = {
isa = XCSwiftPackageProductDependency;
package = 9ADF457C2E950ADB00E8B5A2 /* XCRemoteSwiftPackageReference "Moya" */;
productName = CombineMoya;
};
9ADF457F2E950B6100E8B5A2 /* Moya */ = {
isa = XCSwiftPackageProductDependency;
package = 9ADF457C2E950ADB00E8B5A2 /* XCRemoteSwiftPackageReference "Moya" */;
productName = Moya;
};
9ADF45812E950B6100E8B5A2 /* ReactiveMoya */ = {
isa = XCSwiftPackageProductDependency;
package = 9ADF457C2E950ADB00E8B5A2 /* XCRemoteSwiftPackageReference "Moya" */;
productName = ReactiveMoya;
};
9ADF45832E950B6100E8B5A2 /* RxMoya */ = {
isa = XCSwiftPackageProductDependency;
package = 9ADF457C2E950ADB00E8B5A2 /* XCRemoteSwiftPackageReference "Moya" */;
productName = RxMoya;
};
/* End XCSwiftPackageProductDependency section */
};
rootObject = 259FC95F2E890D7E001152B9 /* Project object */;
}
2 changes: 1 addition & 1 deletion today-s-sound/App/TodaySSoundApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import SwiftUI
struct TodaySSoundApp: App {
var body: some Scene {
WindowGroup {
HomeView()
MainView()
}
}
}
12 changes: 12 additions & 0 deletions today-s-sound/Core/Auth/UserSession.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Foundation

final class UserSession: ObservableObject {
@Published var accessToken: String = ""
@Published var refreshToken: String = ""
@Published var autoLogin: Bool = true

func clear() {
accessToken = ""
refreshToken = ""
}
}
13 changes: 13 additions & 0 deletions today-s-sound/Core/Network/APITargetType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Foundation
import Moya

protocol APITargetType: TargetType {}

extension APITargetType {
var baseURL: URL {
guard let url = URL(string: Config.baseURL) else {
fatalError("Invalid Base URL")
}
return url
}
}
11 changes: 11 additions & 0 deletions today-s-sound/Core/Network/Config.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Foundation

enum Config {
static var baseURL: String {
#if DEBUG
return "https://dev-your-api-url.com"
#else
return "https://your-api-url.com"
#endif
}
}
9 changes: 9 additions & 0 deletions today-s-sound/Core/Network/NetworkError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Foundation

enum NetworkError: Error {
case invalidURL
case requestFailed(Error)
case decodingFailed(Error)
case serverError(statusCode: Int)
case unknown
}
108 changes: 108 additions & 0 deletions today-s-sound/Core/Network/Provider/AuthInterceptor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import Alamofire
import Foundation
import Moya

final class AuthInterceptor: RequestInterceptor {
private let userSession: UserSession

private lazy var refreshProvider: MoyaProvider<AuthAPITarget> = {
let session = Session(configuration: .default) // no interceptor
return MoyaProvider<AuthAPITarget>(session: session)
}()

private var isRefreshing = false
private var waiting: [(RetryResult) -> Void] = []
private let lock = NSLock()

init(userSession: UserSession) { self.userSession = userSession }

func adapt(_ urlRequest: URLRequest,
for session: Session,
completion: @escaping (Result<URLRequest, Error>) -> Void)
{
var req = urlRequest
let path = req.url?.path ?? ""

let bypass: Set<String> = [
"/api/auth/login",
"/api/auth/refresh"
]

if bypass.contains(path) {
if req.value(forHTTPHeaderField: "Authorization") != nil {
req.setValue(nil, forHTTPHeaderField: "Authorization")
}
return completion(.success(req))
}

if !userSession.accessToken.isEmpty {
req.setValue("Bearer \(userSession.accessToken)", forHTTPHeaderField: "Authorization")
}
completion(.success(req))
}

func retry(_ request: Request,
for session: Session,
dueTo error: Error,
completion: @escaping (RetryResult) -> Void)
{
let path = request.request?.url?.path ?? "nil"
let status = request.response?.statusCode ?? -1

if path == "/api/auth/refresh" {
completion(.doNotRetry); return
}

guard status == 401 || status == 403 || status == 419 else {
completion(.doNotRetry); return
}

guard userSession.autoLogin, !userSession.refreshToken.isEmpty else {
DispatchQueue.main.async { self.userSession.clear() }
completion(.doNotRetry)
return
}

lock.lock()
if isRefreshing {
waiting.append(completion)
lock.unlock()
return
}
isRefreshing = true
waiting.append(completion)
lock.unlock()

refreshProvider.request(.refresh(refreshToken: userSession.refreshToken)) { [weak self] result in
guard let self else { return }
var queuedResult: RetryResult = .doNotRetry

switch result {
case let .success(res):
if (200 ..< 300).contains(res.statusCode),
let dto = try? JSONDecoder().decode(RefreshResponseDTO.self, from: res.data),
dto.isSuccess, let data = dto.data
{
DispatchQueue.main.async {
self.userSession.accessToken = data.accessToken
self.userSession.refreshToken = data.refreshToken
}
queuedResult = .retry
} else {
DispatchQueue.main.async { self.userSession.clear() }
}

case .failure:
DispatchQueue.main.async { self.userSession.clear() }
}

lock.lock()
let queued = waiting
waiting.removeAll()
isRefreshing = false
lock.unlock()

queued.forEach { $0(queuedResult) }
}
}
}
13 changes: 13 additions & 0 deletions today-s-sound/Core/Network/Provider/NetworkKit.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Alamofire
import Foundation
import Moya

enum NetworkKit {
static func provider<T: TargetType>(userSession: UserSession,
plugins: [PluginType] = []) -> MoyaProvider<T>
{
let interceptor = AuthInterceptor(userSession: userSession)
let session = Session(interceptor: interceptor)
return MoyaProvider<T>(session: session, plugins: plugins)
}
}
42 changes: 42 additions & 0 deletions today-s-sound/Core/Network/Service/APIService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import Combine
import CombineMoya
import Foundation
import Moya

protocol APIServiceType {
func request<T: Decodable>(_ target: some TargetType) -> AnyPublisher<T, NetworkError>
func createAnonymous(deviceSecret: String) -> AnyPublisher<AnonymousUserResponse, NetworkError>
}

class APIService: APIServiceType {
private let anonymousProvider: MoyaProvider<AnonymousAPI>

init(userSession: UserSession = UserSession()) {
anonymousProvider = NetworkKit.provider(userSession: userSession)
}

func request<T: Decodable>(_ target: some TargetType) -> AnyPublisher<T, NetworkError> {
Fail<T, NetworkError>(error: .requestFailed(NSError(domain: "NotImplemented", code: -1))).eraseToAnyPublisher()
}

func createAnonymous(deviceSecret: String) -> AnyPublisher<AnonymousUserResponse, NetworkError> {
anonymousProvider.requestPublisher(.createAnonymous(deviceSecret: deviceSecret))
.tryMap { response -> Data in
guard (200 ... 299).contains(response.statusCode) else {
throw NetworkError.serverError(statusCode: response.statusCode)
}
return response.data
}
.decode(type: AnonymousUserResponse.self, decoder: JSONDecoder())
.mapError { error -> NetworkError in
if let networkError = error as? NetworkError {
return networkError
} else if error is DecodingError {
return .decodingFailed(error)
} else {
return .requestFailed(error)
}
}
.eraseToAnyPublisher()
}
}
26 changes: 26 additions & 0 deletions today-s-sound/Core/Network/Targets/AnonymousAPI.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import Foundation
import Moya

enum AnonymousAPI {
case createAnonymous(deviceSecret: String)
}

extension AnonymousAPI: APITargetType {
var path: String {
switch self {
case .createAnonymous:
"/api/users/anonymous"
}
}

var method: Moya.Method { .post }

var task: Task {
switch self {
case let .createAnonymous(deviceSecret):
.requestParameters(parameters: ["deviceSecret": deviceSecret], encoding: JSONEncoding.default)
}
}

var headers: [String: String]? { ["Content-Type": "application/json"] }
}
Loading
Loading