Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate AppStorage to protocol oriented architecture. #69

Merged
merged 3 commits into from
Aug 31, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ struct FieldsView: View {
type: .discovery,
fontSize: 90, screenWidth: proxy.size.width)
)
.id(UUID())
.padding(.horizontal, -6)

case .unknown:
Expand Down
8 changes: 4 additions & 4 deletions Core/Core.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
07460FE3294B72D700F70538 /* Notification.swift in Sources */ = {isa = PBXBuildFile; fileRef = 07460FE2294B72D700F70538 /* Notification.swift */; };
076F297F2A1F80C800967E7D /* Pagination.swift in Sources */ = {isa = PBXBuildFile; fileRef = 076F297E2A1F80C800967E7D /* Pagination.swift */; };
0770DE1928D0847D006D8A5D /* BaseRouter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0770DE1828D0847D006D8A5D /* BaseRouter.swift */; };
0770DE2528D08FBA006D8A5D /* AppStorage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0770DE2428D08FBA006D8A5D /* AppStorage.swift */; };
0770DE2528D08FBA006D8A5D /* CoreStorage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0770DE2428D08FBA006D8A5D /* CoreStorage.swift */; };
0770DE2A28D0929E006D8A5D /* HTTPTask.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0770DE2928D0929E006D8A5D /* HTTPTask.swift */; };
0770DE2C28D092B3006D8A5D /* NetworkLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0770DE2B28D092B3006D8A5D /* NetworkLogger.swift */; };
0770DE2E28D09743006D8A5D /* API.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0770DE2D28D09743006D8A5D /* API.swift */; };
Expand Down Expand Up @@ -221,7 +221,7 @@
076F297E2A1F80C800967E7D /* Pagination.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Pagination.swift; sourceTree = "<group>"; };
0770DE0828D07831006D8A5D /* Core.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Core.framework; sourceTree = BUILT_PRODUCTS_DIR; };
0770DE1828D0847D006D8A5D /* BaseRouter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BaseRouter.swift; sourceTree = "<group>"; };
0770DE2428D08FBA006D8A5D /* AppStorage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppStorage.swift; sourceTree = "<group>"; };
0770DE2428D08FBA006D8A5D /* CoreStorage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoreStorage.swift; sourceTree = "<group>"; };
0770DE2928D0929E006D8A5D /* HTTPTask.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HTTPTask.swift; sourceTree = "<group>"; };
0770DE2B28D092B3006D8A5D /* NetworkLogger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkLogger.swift; sourceTree = "<group>"; };
0770DE2D28D09743006D8A5D /* API.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = API.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -381,7 +381,7 @@
02CF46C92954A42100A698EE /* Persistence */,
0236961728F9A21600EEF206 /* Repository */,
0727877528D2383C002E9142 /* Model */,
0770DE2428D08FBA006D8A5D /* AppStorage.swift */,
0770DE2428D08FBA006D8A5D /* CoreStorage.swift */,
02512FEF299533DE0024D438 /* CoreDataHandlerProtocol.swift */,
);
path = Data;
Expand Down Expand Up @@ -793,7 +793,7 @@
021D925728DCF12900ACC565 /* AlertView.swift in Sources */,
027BD3A82909474200392132 /* KeyboardAvoidingViewController.swift in Sources */,
0770DE7B28D0C78C006D8A5D /* Theme.swift in Sources */,
0770DE2528D08FBA006D8A5D /* AppStorage.swift in Sources */,
0770DE2528D08FBA006D8A5D /* CoreStorage.swift in Sources */,
020306CC2932C0C4000949EA /* PickerView.swift in Sources */,
027BD3C52909707700392132 /* Shake.swift in Sources */,
027BD39C2908810C00392132 /* RegisterUser.swift in Sources */,
Expand Down
17 changes: 17 additions & 0 deletions Core/Core/Data/CoreStorage.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// CoreStorage.swift
// Core
//
// Created by Vladimir Chekyrta on 13.09.2022.
//

import Foundation

public protocol CoreStorage {
var accessToken: String? {get set}
var refreshToken: String? {get set}
var cookiesDate: String? {get set}
var user: DataLayer.User? {get set}
var userSettings: UserSettings? {get set}
func clear()
}
4 changes: 2 additions & 2 deletions Core/Core/Data/Repository/AuthRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public protocol AuthRepositoryProtocol {
public class AuthRepository: AuthRepositoryProtocol {

private let api: API
private let appStorage: AppStorage
private var appStorage: CoreStorage
private let config: Config

public init(api: API, appStorage: AppStorage, config: Config) {
public init(api: API, appStorage: CoreStorage, config: Config) {
self.api = api
self.appStorage = appStorage
self.config = config
Expand Down
4 changes: 2 additions & 2 deletions Core/Core/Network/DownloadManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ public protocol DownloadManagerProtocol {
public class DownloadManager: DownloadManagerProtocol {

private let persistence: CorePersistenceProtocol
private let appStorage: Core.AppStorage
private let appStorage: CoreStorage
private let connectivity: ConnectivityProtocol
private var downloadRequest: DownloadRequest?
private var currentDownload: DownloadData?
private var isDownloadingInProgress: Bool = false

public init(
persistence: CorePersistenceProtocol,
appStorage: Core.AppStorage,
appStorage: CoreStorage,
connectivity: ConnectivityProtocol
) {
self.persistence = persistence
Expand Down
14 changes: 7 additions & 7 deletions Core/Core/Network/RequestInterceptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import Alamofire
final public class RequestInterceptor: Alamofire.RequestInterceptor {

private let config: Config
private let appStorage: AppStorage
private var storage: CoreStorage

public init(config: Config, appStorage: AppStorage) {
public init(config: Config, storage: CoreStorage) {
self.config = config
self.appStorage = appStorage
self.storage = storage
}

private let lock = NSLock()
Expand All @@ -34,7 +34,7 @@ final public class RequestInterceptor: Alamofire.RequestInterceptor {
var urlRequest = urlRequest

// Set the Authorization header value using the access token.
if let token = appStorage.accessToken {
if let token = storage.accessToken {
urlRequest.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
}

Expand All @@ -57,7 +57,7 @@ final public class RequestInterceptor: Alamofire.RequestInterceptor {
return completion(.doNotRetry)
}

guard let token = appStorage.refreshToken else {
guard let token = storage.refreshToken else {
return completion(.doNotRetryWithError(error))
}

Expand Down Expand Up @@ -117,8 +117,8 @@ final public class RequestInterceptor: Alamofire.RequestInterceptor {
refreshToken.count > 0 else {
return completion(false)
}
self.appStorage.accessToken = accessToken
self.appStorage.refreshToken = refreshToken
self.storage.accessToken = accessToken
self.storage.refreshToken = refreshToken
completion(true)
} catch {
completion(false)
Expand Down
4 changes: 2 additions & 2 deletions Course/Course/Data/CourseRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public protocol CourseRepositoryProtocol {
public class CourseRepository: CourseRepositoryProtocol {

private let api: API
private let appStorage: AppStorage
private let appStorage: CoreStorage
private let config: Config
private let persistence: CoursePersistenceProtocol

public init(api: API,
appStorage: AppStorage,
appStorage: CoreStorage,
config: Config,
persistence: CoursePersistenceProtocol) {
self.api = api
Expand Down
8 changes: 4 additions & 4 deletions Dashboard/Dashboard/Data/DashboardRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ public protocol DashboardRepositoryProtocol {
public class DashboardRepository: DashboardRepositoryProtocol {

private let api: API
private let appStorage: AppStorage
private let storage: CoreStorage
private let config: Config
private let persistence: DashboardPersistenceProtocol

public init(api: API, appStorage: AppStorage, config: Config, persistence: DashboardPersistenceProtocol) {
public init(api: API, storage: CoreStorage, config: Config, persistence: DashboardPersistenceProtocol) {
self.api = api
self.appStorage = appStorage
self.storage = storage
self.config = config
self.persistence = persistence
}

public func getMyCourses(page: Int) async throws -> [CourseItem] {
let result = try await api.requestData(
DashboardEndpoint.getMyCourses(username: appStorage.user?.username ?? "", page: page)
DashboardEndpoint.getMyCourses(username: storage.user?.username ?? "", page: page)
)
.mapResponse(DataLayer.CourseEnrollments.self)
.domain(baseURL: config.baseURL.absoluteString)
Expand Down
4 changes: 2 additions & 2 deletions Discovery/Discovery/Data/DiscoveryRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ public protocol DiscoveryRepositoryProtocol {
public class DiscoveryRepository: DiscoveryRepositoryProtocol {

private let api: API
private let appStorage: AppStorage
private let appStorage: CoreStorage
private let config: Config
private let persistence: DiscoveryPersistenceProtocol

public init(api: API,
appStorage: AppStorage,
appStorage: CoreStorage,
config: Config,
persistence: DiscoveryPersistenceProtocol) {
self.api = api
Expand Down
4 changes: 1 addition & 3 deletions Discussion/Discussion/Data/Model/Data_CreatedComment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ public extension DataLayer {
public extension DataLayer.CreatedComment {
var domain: Post {
Post(authorName: author ?? DiscussionLocalization.anonymous,
authorAvatar: profileImage.imageURLSmall?.addingPercentEncoding(
withAllowedCharacters: .urlHostAllowed
) ?? "",
authorAvatar: profileImage.imageURLSmall ?? "",
postDate: Date(iso8601: createdAt),
postTitle: "",
postBodyHtml: renderedBody,
Expand Down
4 changes: 2 additions & 2 deletions Discussion/Discussion/Data/Network/DiscussionRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ public protocol DiscussionRepositoryProtocol {
public class DiscussionRepository: DiscussionRepositoryProtocol {

private let api: API
private let appStorage: AppStorage
private let appStorage: CoreStorage
private let config: Config
private let router: DiscussionRouter

public init(api: API, appStorage: AppStorage, config: Config, router: DiscussionRouter) {
public init(api: API, appStorage: CoreStorage, config: Config, router: DiscussionRouter) {
self.api = api
self.appStorage = appStorage
self.config = config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class BaseResponsesViewModel {
public var totalPages = 1
@Published public var itemsCount = 0
public var fetchInProgress = false

var errorMessage: String? {
didSet {
withAnimation {
Expand All @@ -44,19 +44,16 @@ public class BaseResponsesViewModel {
internal let interactor: DiscussionInteractorProtocol
internal let router: DiscussionRouter
internal let config: Config
internal let storage: Core.AppStorage

internal let addPostSubject = CurrentValueSubject<Post?, Never>(nil)

init(interactor: DiscussionInteractorProtocol,
router: DiscussionRouter,
config: Config,
storage: Core.AppStorage
init(
interactor: DiscussionInteractorProtocol,
router: DiscussionRouter,
config: Config
) {
self.interactor = interactor
self.router = router
self.config = config
self.storage = storage
}

@MainActor
Expand Down Expand Up @@ -137,7 +134,6 @@ public class BaseResponsesViewModel {

func addNewPost(_ post: Post) {
var newPostWithAvatar = post
newPostWithAvatar.authorAvatar = storage.userProfile?.profileImage?.imageURLLarge ?? ""
postComments?.comments.append(newPostWithAvatar)
itemsCount += 1
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import SwiftUI
import Core
import Kingfisher
import Combine

public struct ResponsesView: View {
Expand Down Expand Up @@ -208,7 +207,6 @@ struct ResponsesView_Previews: PreviewProvider {
interactor: DiscussionInteractor(repository: DiscussionRepositoryMock()),
router: DiscussionRouterMock(),
config: ConfigMock(),
storage: .mock,
threadStateSubject: .init(nil)
)
let post = Post(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ public class ResponsesViewModel: BaseResponsesViewModel, ObservableObject {
interactor: DiscussionInteractorProtocol,
router: DiscussionRouter,
config: Config,
storage: Core.AppStorage,
threadStateSubject: CurrentValueSubject<ThreadPostState?, Never>
) {
self.threadStateSubject = threadStateSubject
super.init(interactor: interactor, router: router, config: config, storage: storage)
super.init(interactor: interactor, router: router, config: config)
}

func generateCommentsResponses(comments: [UserComment], parentComment: Post) -> Post? {
Expand Down
Loading