Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
wojciech-kulik committed Jul 15, 2019
1 parent 933e8fd commit 2e62bc0
Show file tree
Hide file tree
Showing 18 changed files with 66 additions and 79 deletions.
12 changes: 4 additions & 8 deletions MVVMC.xcodeproj/project.pbxproj
Expand Up @@ -33,7 +33,6 @@
16A1E61322DA40C40003F0DF /* Container+Services.swift in Sources */ = {isa = PBXBuildFile; fileRef = 16A1E5A222DA40C30003F0DF /* Container+Services.swift */; };
16A1E61422DA40C40003F0DF /* Container+Coordinators.swift in Sources */ = {isa = PBXBuildFile; fileRef = 16A1E5A322DA40C30003F0DF /* Container+Coordinators.swift */; };
16A1E61722DA40C40003F0DF /* Storyboarded.swift in Sources */ = {isa = PBXBuildFile; fileRef = 16A1E5A722DA40C30003F0DF /* Storyboarded.swift */; };
16A1E61822DA40C40003F0DF /* ApiRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 16A1E5A822DA40C30003F0DF /* ApiRequest.swift */; };
16A1E61922DA40C40003F0DF /* translations.json in Resources */ = {isa = PBXBuildFile; fileRef = 16A1E5AA22DA40C30003F0DF /* translations.json */; };
16A1E61A22DA40C40003F0DF /* AppCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 16A1E5AC22DA40C30003F0DF /* AppCoordinator.swift */; };
16A1E61B22DA40C40003F0DF /* DrawerMenuCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 16A1E5AD22DA40C30003F0DF /* DrawerMenuCoordinator.swift */; };
Expand Down Expand Up @@ -64,7 +63,7 @@
16A1E64622DA40C40003F0DF /* ApiError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 16A1E5E722DA40C40003F0DF /* ApiError.swift */; };
16A1E64722DA40C40003F0DF /* HttpStatusCode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 16A1E5E822DA40C40003F0DF /* HttpStatusCode.swift */; };
16A1E64822DA40C40003F0DF /* Token.swift in Sources */ = {isa = PBXBuildFile; fileRef = 16A1E5E922DA40C40003F0DF /* Token.swift */; };
16A1E64922DA40C40003F0DF /* BaseApiRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 16A1E5EA22DA40C40003F0DF /* BaseApiRequest.swift */; };
16A1E64922DA40C40003F0DF /* ApiRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 16A1E5EA22DA40C40003F0DF /* ApiRequest.swift */; };
16DD9CD622DA4756007D9D2F /* RxCocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 16DD9CCF22DA4756007D9D2F /* RxCocoa.framework */; };
16DD9CD922DA4756007D9D2F /* SideMenu.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 16DD9CD222DA4756007D9D2F /* SideMenu.framework */; };
16DD9CDA22DA4756007D9D2F /* RxRelay.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 16DD9CD322DA4756007D9D2F /* RxRelay.framework */; };
Expand Down Expand Up @@ -150,7 +149,6 @@
16A1E5A222DA40C30003F0DF /* Container+Services.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Container+Services.swift"; sourceTree = "<group>"; };
16A1E5A322DA40C30003F0DF /* Container+Coordinators.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Container+Coordinators.swift"; sourceTree = "<group>"; };
16A1E5A722DA40C30003F0DF /* Storyboarded.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Storyboarded.swift; sourceTree = "<group>"; };
16A1E5A822DA40C30003F0DF /* ApiRequest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ApiRequest.swift; sourceTree = "<group>"; };
16A1E5AA22DA40C30003F0DF /* translations.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = translations.json; sourceTree = "<group>"; };
16A1E5AC22DA40C30003F0DF /* AppCoordinator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppCoordinator.swift; sourceTree = "<group>"; };
16A1E5AD22DA40C30003F0DF /* DrawerMenuCoordinator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DrawerMenuCoordinator.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -181,7 +179,7 @@
16A1E5E722DA40C40003F0DF /* ApiError.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ApiError.swift; sourceTree = "<group>"; };
16A1E5E822DA40C40003F0DF /* HttpStatusCode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HttpStatusCode.swift; sourceTree = "<group>"; };
16A1E5E922DA40C40003F0DF /* Token.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Token.swift; sourceTree = "<group>"; };
16A1E5EA22DA40C40003F0DF /* BaseApiRequest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BaseApiRequest.swift; sourceTree = "<group>"; };
16A1E5EA22DA40C40003F0DF /* ApiRequest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ApiRequest.swift; sourceTree = "<group>"; };
16DD9CCF22DA4756007D9D2F /* RxCocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = RxCocoa.framework; path = Carthage/Build/iOS/RxCocoa.framework; sourceTree = "<group>"; };
16DD9CD022DA4756007D9D2F /* ReSwift.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ReSwift.framework; path = Carthage/Build/iOS/ReSwift.framework; sourceTree = "<group>"; };
16DD9CD122DA4756007D9D2F /* RxBlocking.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = RxBlocking.framework; path = Carthage/Build/iOS/RxBlocking.framework; sourceTree = "<group>"; };
Expand Down Expand Up @@ -505,10 +503,9 @@
16A1E5E622DA40C40003F0DF /* Models */ = {
isa = PBXGroup;
children = (
16A1E5A822DA40C30003F0DF /* ApiRequest.swift */,
16A1E5EA22DA40C40003F0DF /* ApiRequest.swift */,
16DD9CE322DA47D3007D9D2F /* ApiResponse.swift */,
16A1E5E722DA40C40003F0DF /* ApiError.swift */,
16A1E5EA22DA40C40003F0DF /* BaseApiRequest.swift */,
16DD9CE422DA47D3007D9D2F /* HttpMethod.swift */,
16A1E5E822DA40C40003F0DF /* HttpStatusCode.swift */,
);
Expand Down Expand Up @@ -745,7 +742,7 @@
16A1E61A22DA40C40003F0DF /* AppCoordinator.swift in Sources */,
16A1E60E22DA40C40003F0DF /* SignInViewModel.swift in Sources */,
16DD9D1F22DB32F2007D9D2F /* SettingsViewModel.swift in Sources */,
16A1E64922DA40C40003F0DF /* BaseApiRequest.swift in Sources */,
16A1E64922DA40C40003F0DF /* ApiRequest.swift in Sources */,
16DD9D1122DA8615007D9D2F /* SetNameViewModel.swift in Sources */,
16DD9D0F22DA8565007D9D2F /* SetOptionsViewController.swift in Sources */,
16A1E5EF22DA40C40003F0DF /* Json.swift in Sources */,
Expand All @@ -772,7 +769,6 @@
16A1E61122DA40C40003F0DF /* Container+ViewModels.swift in Sources */,
16A1E64222DA40C40003F0DF /* AppStoryboard.swift in Sources */,
16A1E61722DA40C40003F0DF /* Storyboarded.swift in Sources */,
16A1E61822DA40C40003F0DF /* ApiRequest.swift in Sources */,
16A1E62F22DA40C40003F0DF /* SessionService.swift in Sources */,
16A1E60222DA40C40003F0DF /* LocalizedLabel.swift in Sources */,
16A1E60322DA40C40003F0DF /* FancyButton.swift in Sources */,
Expand Down
8 changes: 4 additions & 4 deletions MVVMC/Models/SettingKey.swift
@@ -1,7 +1,7 @@
import Foundation

public struct SettingKey {
public static let session = "session"
public static let translations = "translations"
public static let onBoardingData = "onBoardingData"
struct SettingKey {
static let session = "session"
static let translations = "translations"
static let onBoardingData = "onBoardingData"
}
2 changes: 1 addition & 1 deletion MVVMC/Networking/ApiEndpoints/GeneralEndpoints.swift
Expand Up @@ -2,7 +2,7 @@ import Foundation

enum GeneralEndpoints {

class FetchTranslations: BaseApiRequest<TranslationsResponse> {
class FetchTranslations: ApiRequest<TranslationsResponse> {
init() {
super.init(resource: "translations")
}
Expand Down
6 changes: 3 additions & 3 deletions MVVMC/Networking/ApiEndpoints/SessionEndpoints.swift
Expand Up @@ -2,22 +2,22 @@ import Foundation

enum SessionEndpoints {

class SignIn: BaseApiRequest<SignInResponse> {
class SignIn: ApiRequest<SignInResponse> {
init(credentials: Credentials) {
super.init(resource: "login",
method: .post,
json: SignInRequest(username: credentials.username, password: credentials.password).toJson())
}
}

class SignOut: BaseApiRequest<VoidResponse> {
class SignOut: ApiRequest<VoidResponse> {
init() {
super.init(resource: "logout",
method: .post)
}
}

class FetchMe: BaseApiRequest<MeResponse> {
class FetchMe: ApiRequest<MeResponse> {
init() {
super.init(resource: "me")
}
Expand Down
2 changes: 1 addition & 1 deletion MVVMC/Networking/ApiEndpoints/TasksEndpoints.swift
Expand Up @@ -2,7 +2,7 @@ import Foundation

enum TasksEndpoints {

class FetchTasks: BaseApiRequest<[String]> {
class FetchTasks: ApiRequest<[String]> {
init() {
super.init(resource: "tasks")
}
Expand Down
12 changes: 6 additions & 6 deletions MVVMC/Networking/BackendRestClient.swift
Expand Up @@ -16,7 +16,7 @@ class BackendRestClient {
self.alertDispatcher = alertDispatcher
}

func request<T:Codable>(_ request: ApiRequest) -> Single<T>{
func request<T:Codable>(_ request: ApiRequest<T>) -> Single<T>{

return Single.create { single in
self.httpClient.set(headers: self.getHeaders())
Expand All @@ -32,21 +32,21 @@ class BackendRestClient {
}
}

private func validate<T:Codable>(response: ApiResponse, for request: ApiRequest, single: Single<T>.SingleObserver) {
private func validate<T:Codable>(response: ApiResponse, for request: ApiRequest<T>, single: Single<T>.SingleObserver) {
response.print()

guard response.success && response.statusCode == request.expectedCode else {
Logger.error("Unsuccessful request", error: response.error)
let error = ApiError.requestFailed(statusCode: response.statusCode, response: response.data)
dispatchError(request: request, error: error)
self.dispatch(error: error)
single(.error(error))
return
}

guard let parsedResponse = response.data?.toObject(T.self) else {
Logger.info("Could not parse response")
Logger.error("Could not parse response")
let error = ApiError.requestFailed(statusCode: response.statusCode, response: response.data)
dispatchError(request: request, error: error)
self.dispatch(error: error)
single(.error(error))
return
}
Expand All @@ -62,7 +62,7 @@ class BackendRestClient {
return headers
}

private func dispatchError(request: ApiRequest, error: ApiError) {
private func dispatch(error: ApiError) {
let message = self.getMessage(error: error)
self.alertDispatcher.dispatch(error: message)
}
Expand Down
3 changes: 1 addition & 2 deletions MVVMC/Networking/Models/ApiError.swift
@@ -1,6 +1,5 @@
import Foundation

public enum ApiError: Error {
enum ApiError: Error {
case requestFailed(statusCode: Int?, response: Data?)
case sessionExpired
}
25 changes: 20 additions & 5 deletions MVVMC/Networking/Models/ApiRequest.swift
@@ -1,8 +1,23 @@
import Foundation

protocol ApiRequest {
var method: HttpMethod { get }
var resource: String { get }
var json: Data? { get }
var expectedCode: Int { get }
class ApiRequest<T:Codable> {

let method: HttpMethod
let resource: String
let expectedCode: Int
let form: [String:String]?
let json: Data?

init(resource: String,
method: HttpMethod = .get,
expectedCode: Int = 200,
form: [String:String]? = nil,
json: Data? = nil) {

self.resource = resource
self.method = method
self.expectedCode = expectedCode
self.form = form
self.json = json
}
}
18 changes: 10 additions & 8 deletions MVVMC/Networking/Models/ApiResponse.swift
@@ -1,16 +1,18 @@
import Foundation

public struct ApiResponse {
public let success: Bool
public let statusCode: Int?
struct VoidResponse: Codable { }

struct ApiResponse {
let success: Bool
let statusCode: Int?

public let requestUrl: String
public let method: HttpMethod
let requestUrl: String
let method: HttpMethod

public let data: Data?
public let error: Error?
let data: Data?
let error: Error?

public init(success: Bool, statusCode: Int?, requestUrl: String, method: HttpMethod, data: Data?, error: Error?) {
init(success: Bool, statusCode: Int?, requestUrl: String, method: HttpMethod, data: Data?, error: Error?) {
self.success = success
self.statusCode = statusCode
self.requestUrl = requestUrl
Expand Down
25 changes: 0 additions & 25 deletions MVVMC/Networking/Models/BaseApiRequest.swift

This file was deleted.

2 changes: 1 addition & 1 deletion MVVMC/Networking/Models/HttpMethod.swift
@@ -1,6 +1,6 @@
import Foundation

public enum HttpMethod: String {
enum HttpMethod: String {
case post = "POST"
case get = "GET"
case delete = "DELETE"
Expand Down
2 changes: 1 addition & 1 deletion MVVMC/Networking/Models/HttpStatusCode.swift
@@ -1,6 +1,6 @@
import Foundation

public enum HttpStatusCode: Int {
enum HttpStatusCode: Int {
case success = 200
case forbidden = 403
}
2 changes: 1 addition & 1 deletion MVVMC/Protocols/DataManager.swift
@@ -1,6 +1,6 @@
import Foundation

public protocol DataManager {
protocol DataManager {

func get<T>(key: String, type: T.Type) -> T? where T : Codable

Expand Down
2 changes: 1 addition & 1 deletion MVVMC/Protocols/HttpClient.swift
@@ -1,6 +1,6 @@
import Foundation

public protocol HttpClient {
protocol HttpClient {

func set(headers: [String:String])

Expand Down
8 changes: 4 additions & 4 deletions MVVMC/Services/SessionService.swift
Expand Up @@ -41,8 +41,8 @@ class SessionService {
}

func signIn(credentials: Credentials) -> Completable {
let signIn = self.restClient.request(SessionEndpoints.SignIn(credentials: credentials)) as Single<SignInResponse>
let fetchMe = self.restClient.request(SessionEndpoints.FetchMe()) as Single<MeResponse>
let signIn = self.restClient.request(SessionEndpoints.SignIn(credentials: credentials))
let fetchMe = self.restClient.request(SessionEndpoints.FetchMe())

return self.translationsService.fetchTranslations()
.andThen(signIn)
Expand All @@ -53,15 +53,15 @@ class SessionService {
}

func signOut() -> Completable {
let signOut = self.restClient.request(SessionEndpoints.SignOut()) as Single<VoidResponse>
let signOut = self.restClient.request(SessionEndpoints.SignOut())

return signOut
.do(onSuccess: { [weak self] _ in self?.removeSession() })
.asCompletable()
}

func refreshProfile() -> Single<MeResponse> {
let fetchMe = self.restClient.request(SessionEndpoints.FetchMe()) as Single<MeResponse>
let fetchMe = self.restClient.request(SessionEndpoints.FetchMe())

return fetchMe
.do(onSuccess: { [weak self] in self?.updateProfile(data: $0) })
Expand Down
2 changes: 1 addition & 1 deletion MVVMC/Services/TranslationsService.swift
Expand Up @@ -23,7 +23,7 @@ class TranslationsService {
}

func fetchTranslations() -> Completable {
let request = self.restClient.request(GeneralEndpoints.FetchTranslations()) as Single<TranslationsResponse>
let request = self.restClient.request(GeneralEndpoints.FetchTranslations())

return request
.do(onSuccess: { [weak self] response in
Expand Down
4 changes: 2 additions & 2 deletions MVVMC/Utils/FileUtils.swift
@@ -1,8 +1,8 @@
import Foundation

public struct FileUtils {
struct FileUtils {

public static func loadTextFile(with name: String, ofType type: String) -> String? {
static func loadTextFile(with name: String, ofType type: String) -> String? {
guard let path = Bundle.main.path(forResource: name, ofType: type) else { return nil }

do {
Expand Down
10 changes: 5 additions & 5 deletions MVVMC/Utils/Logger.swift
@@ -1,6 +1,6 @@
import Foundation

public class Logger {
class Logger {
enum LogLevel: Int {
case none
case error
Expand All @@ -10,22 +10,22 @@ public class Logger {

static var logLevel = LogLevel.debug

public static func debug(_ message: String) {
static func debug(_ message: String) {
guard logLevel.rawValue >= LogLevel.debug.rawValue else { return }
log("\(getDate()) | DEBUG | \(message)")
}

public static func info(_ message: String) {
static func info(_ message: String) {
guard logLevel.rawValue >= LogLevel.info.rawValue else { return }
log("\(getDate()) | INFO | \(message)")
}

public static func error(_ message: String) {
static func error(_ message: String) {
guard logLevel.rawValue >= LogLevel.error.rawValue else { return }
log("\(getDate()) | ERROR | \(message)")
}

public static func error(_ message: String, error: Error?) {
static func error(_ message: String, error: Error?) {
guard logLevel.rawValue >= LogLevel.error.rawValue else { return }
if error != nil {
log("\(getDate()) | ERROR | \(message)\n\(error!)")
Expand Down

0 comments on commit 2e62bc0

Please sign in to comment.