Skip to content

Commit

Permalink
Merge pull request #7 from vespinola/5-add-dashboard-screen
Browse files Browse the repository at this point in the history
5 add dashboard screen
  • Loading branch information
vespinola authored Jun 16, 2024
2 parents 8d721a9 + abc8cb0 commit e99e4fb
Show file tree
Hide file tree
Showing 38 changed files with 959 additions and 107 deletions.
18 changes: 15 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
name: CI

on:
issue_comment:
types: [created]
push:
branches:
- main
- develop
- "feature/*"
pull_request:
branches:
- main
- develop
- "feature/*"
workflow_dispatch:

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
test:
if: ${{ github.event.issue.pull_request != null && github.event.comment.body == '/build' }}
runs-on: macos-latest

steps:
Expand Down
114 changes: 111 additions & 3 deletions mobile-courier-app.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"images" : [
{
"filename" : "shortLogo.pdf",
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Binary file not shown.
15 changes: 15 additions & 0 deletions mobile-courier-app/Data/AuthData.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// AuthData.swift
// mobile-courier-app
//
// Created by Vladimir Espinola on 2024-05-29.
//

import Foundation

final class AuthData {
static var shared: AuthData = .init()
var loginData: LoginEntity?

private init() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// AddressRespository.swift
// mobile-courier-app
//
// Created by Vladimir Espinola on 2024-06-06.
//

import Foundation

struct AddressRespository: AddressRepositoryProtocol {
private var apiRequestClient: APIRequestClientProtocol

init(apiRequestClient: APIRequestClientProtocol) {
self.apiRequestClient = apiRequestClient
}

func getAddress() async throws -> AddressesEntity {
let model: AddressesModel = try await apiRequestClient.performRequest(
endpoint: AddressEndpoints.addresses,
decoder: JSONDecoder()
)

AppData.shared.setUsername(model.enviosAereos.cliente.clienteNombre)

return model.asEntity()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ struct AuthRepository: AuthRepositoryProtocol {
self.apiRequestClient = apiRequestClient
}

func performLogin(email: String, password: String) async throws -> LoginEntity {
func performLogin(email: String, password: String) async throws {
let model: LoginModel = try await apiRequestClient.performRequest(
endpoint: AuthEndpoints.login(email: email, password: password),
decoder: JSONDecoder()
)

return model.asEntity()
AppData.shared.updateToken(model.accessToken)
}
}
22 changes: 2 additions & 20 deletions mobile-courier-app/Data/Networking/APIRequestClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,13 @@ final class APIRequestClient: NSObject, APIRequestClientProtocol {
guard let urlRequest = try? endpoint.asRequest() else {
throw APIErrorMessage.invalidRequest
}

do {
//
// #if DEBUG
// endpoint.mockFile =
// #endif


let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = 10
configuration.timeoutIntervalForResource = 10
Expand All @@ -57,8 +56,7 @@ final class APIRequestClient: NSObject, APIRequestClientProtocol {
decoder.keyDecodingStrategy = .convertFromSnakeCase
let decodedData = try decoder.decode(T.self, from: data)
return decodedData
}
catch {
} catch {
if (error as NSError).code == NSURLErrorTimedOut {
throw APIErrorMessage.timeout
}
Expand All @@ -67,19 +65,3 @@ final class APIRequestClient: NSObject, APIRequestClientProtocol {
}
}
}

fileprivate class CustomSessionDelegate: NSObject, URLSessionDelegate {
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
// Check if the challenge is a server trust challenge
guard challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust,
let serverTrust = challenge.protectionSpace.serverTrust else {
// If it's not a server trust challenge, perform default handling
completionHandler(.performDefaultHandling, nil)
return
}

// Bypass SSL certificate validation by always trusting the server
let credential = URLCredential(trust: serverTrust)
completionHandler(.useCredential, credential)
}
}
29 changes: 29 additions & 0 deletions mobile-courier-app/Data/Networking/AppData.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// AppData.swift
// mobile-courier-app
//
// Created by Vladimir Espinola on 2024-06-05.
//

import Foundation

final class AppData: ObservableObject {
var token: String?
@Published var username: String?

static var shared: AppData = .init()

func setUsername(_ newUsername: String?) {
DispatchQueue.main.async { [weak self] in
self?.username = newUsername
}
}

func updateToken(_ newToken: String?) {
token = newToken
}

func getToken() -> String? {
token
}
}
44 changes: 44 additions & 0 deletions mobile-courier-app/Data/Networking/CustomSessionDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// CustomSessionDelegate.swift
// mobile-courier-app
//
// Created by Vladimir Espinola on 2024-06-12.
//

import Foundation

final class CustomSessionDelegate: NSObject, URLSessionDataDelegate {
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
do {
let jsonObject = try JSONSerialization.jsonObject(with: data, options: [])
let jsonData = try JSONSerialization.data(withJSONObject: jsonObject, options: [.prettyPrinted])

if let jsonString = String(data: jsonData, encoding: .utf8) {
let urlString = dataTask.currentRequest?.url?.absoluteString ?? "Unknown URL"
print("Data received from '\(urlString)' ➡️ \n\(jsonString)")
}
} catch {
print("Failed to parse JSON: \(error.localizedDescription)")
}
}

func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
if let error = error {
print("Error: \(error.localizedDescription)")
} else {
print("Request completed successfully.")
}
}
}

extension CustomSessionDelegate {
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
guard challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust,
let serverTrust = challenge.protectionSpace.serverTrust else {
completionHandler(.performDefaultHandling, nil)
return
}
let credential = URLCredential(trust: serverTrust)
completionHandler(.useCredential, credential)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// AddressEndpoints.swift
// mobile-courier-app
//
// Created by Vladimir Espinola on 2024-06-05.
//

import Foundation

enum AddressEndpoints {
case addresses
}

extension AddressEndpoints: Endpoint {
var mockFile: String? {
""
}

var requestType: RequestType {
.get
}

var path: String {
"/frontliner-middleware/api/direccion"
}

var body: [AnyHashable: Any]? {
nil
}

var queryParams: [String: String]? {
nil
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ extension AuthEndpoints: Endpoint {
return .get
}
}

var path: String {
switch self {
case .login(email: _, password: _):
Expand All @@ -37,23 +37,23 @@ extension AuthEndpoints: Endpoint {
return ""
}
}
var body: [AnyHashable : Any]? {

var body: [AnyHashable: Any]? {
switch self {
case .login(email: let email, password: let pass):
return [
"username" : email,
"password" : pass
"username": email,
"password": pass
]
default:
return nil
}
}

var queryParams: [String : String]? {
var queryParams: [String: String]? {
nil
}

var authToken: String? {
nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ extension Endpoint {

return internalHeaders
}

var authToken: String? {
guard let token = AppData.shared.getToken() else { return nil }
return "Bearer \(token)"
}
}

extension Endpoint {
Expand All @@ -63,6 +68,10 @@ extension Endpoint {
urlRequest.httpBody = try serializer.data(withJSONObject: body)
}

if let authToken = authToken {
urlRequest.allHTTPHeaderFields?["authorization"] = authToken
}

return urlRequest
}
}
Loading

0 comments on commit e99e4fb

Please sign in to comment.