Skip to content

Commit

Permalink
Merge pull request #21 from vespinola/20-create-package-detail-sheet
Browse files Browse the repository at this point in the history
20 create package detail sheet
  • Loading branch information
vespinola authored Jun 23, 2024
2 parents 39596cd + 53c6e6d commit 9393bdd
Show file tree
Hide file tree
Showing 31 changed files with 499 additions and 194 deletions.
68 changes: 51 additions & 17 deletions mobile-courier-app.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

50 changes: 50 additions & 0 deletions mobile-courier-app/App/AppDIContainer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// AppDIContainer.swift
// mobile-courier-app
//
// Created by Vladimir Espinola on 2024-06-21.
//

import Foundation

final class AppDIContainer: DIContainerProtocol {
private var services: [String: Any] = [:]

init() {
// Repositories
register(AuthRepository(), for: AuthRepositoryProtocol.self)
register(AddressRespository(), for: AddressRepositoryProtocol.self)
register(PackagesRepository(), for: PackagesRepositoryProtocol.self)

// ViewModels
register(
LoginViewModel(authRepository: resolve(AuthRepositoryProtocol.self)),
for: LoginViewModel.self
)
register(
HomeViewModel(addressesRepository: resolve(AddressRepositoryProtocol.self)),
for: HomeViewModel.self
)
register(
WithdrawnPackagesViewModel(packagesRepository: resolve(PackagesRepositoryProtocol.self)),
for: WithdrawnPackagesViewModel.self
)
register(
PackagesForWithdrawalViewModel(packagesRepository: resolve(PackagesRepositoryProtocol.self)),
for: PackagesForWithdrawalViewModel.self
)
}

func register<T>(_ service: T, for type: T.Type) {
let key = String(describing: type)
services[key] = service
}

func resolve<T>(_ type: T.Type) -> T {
let key = String(describing: type)
guard let service = services[key] as? T else {
fatalError("Service for \(type) not found")
}
return service
}
}
13 changes: 13 additions & 0 deletions mobile-courier-app/App/DIContainerProtocol.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// DIContainerProtocol.swift
// mobile-courier-app
//
// Created by Vladimir Espinola on 2024-06-21.
//

import Foundation

protocol DIContainerProtocol {
func register<T>(_ service: T, for type: T.Type)
func resolve<T>(_ type: T.Type) -> T
}
26 changes: 26 additions & 0 deletions mobile-courier-app/App/MobileCourierApp.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// mobile_courier_appApp.swift
// mobile-courier-app
//
// Created by Vladimir Espinola on 2024-05-04.
//

import SwiftUI

@main
struct MobileCourierApp: App {
@StateObject var coordinator = Coordinator(diContainer: AppDIContainer())
@StateObject var appData = AppData.shared

init() {
UITabBar.appearance().backgroundColor = UIColor.lightGray.withAlphaComponent(0.3)
}

var body: some Scene {
WindowGroup {
CoordinatorRootView()
.environmentObject(coordinator)
.environmentObject(appData)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
struct AddressRespository: AddressRepositoryProtocol {
private var apiRequestClient: APIRequestClientProtocol

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
struct AuthRepository: AuthRepositoryProtocol {
private var apiRequestClient: APIRequestClientProtocol

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

Expand All @@ -20,8 +20,6 @@ struct AuthRepository: AuthRepositoryProtocol {
decoder: JSONDecoder()
)

// let model = LoginEntity(username: "[email protected]", accessToken: "eyJhbGciOiJIUzI1NiJ9.eyJwcmluY2lwYWwiOiJINHNJQUFBQUFBQUFBSlZTejJzVFFSUitpU2tWaXRnV0ZFUXFIdXBOTmpZaE5DR1gybEtsc0NSaXpLV0NaYkw3c2s0N083UE96S2FiaThTTFBYZ1ExSUxneFlNbjZYXC9peVQ5QUVPODk5K3FidE0ybVhvckx3aTV2dnYxK3ZUMDZoaG1qb1JwcHhvWHhFcEZHWEhvbTBWeEdCb05VY3p2MFVvTTZSRHRHUEI0RHV6U0IwNnRRaElJUFJSNWFXUFIzMllDVkJaTlJ1ZDNieGNBMk13MFZwYU16eHI1bU1lNHJ2ZWROdUFPbDhZSkFUbDFZSzhMc05peXdJRkNwdEMwbE43T0Vhd3kzWVQ2ZitTclljNk1iQVoyZ3RKd0pNdzJkUmNsNkFrTWY1bGhxWHlwUzVXZ3NYRDgxbTFvdXloMjBUUit1SnN3WWN2ZFBrbzUxMXQyNXN5a3B3U3Q0RGFVc0tkQkYzZDF6VU1cL3hlQnRLQ0VyTmxUVExYUm1ya1BlNUV5ZiswZEtIbisrXC9qTHBGQU9ya1wvdVhmNVBOYjZ6RDY4ZUxrenJqb1FtRGg1cFQxSE5iTUVuS3prRE1cLzAraVVmMzErOHZIdytPRDVGVkoyaUVmXC92NFwvbGgyZk5EVGRVbkRETnJKcmFFZEh1bDl3N2thOWZUbjYraGFIWDRYRWlrUDRvYVRHY1NPVEVGTGVrbFRqdjI4SzFwMjFcL2M2ZlYzbkhQVG1iaGJyOGZybFRweGxWV2J6UjZyRjZyWTZOU3FmYnJxMkd0aGc4czNCNElGdktZYXc5TndxVVNiQzJLS1JtWmlTZDl1VVY3VzJRalFyMzQ1K3Uza3pjSGRkTGZncGtCRXluU3h1WnpYQ3VOZTZqZkhoMHV6WDM2XC9XNmNIOERcL1wvaGNLbEMyeFN3TUFBQT09Iiwic3ViIjoidmxhZGltaXIuZXNwaW5vbGFAZ21haWwuY29tIiwiZXhwIjoxNzI0NTQxOTQ3LCJpYXQiOjE3MTg0OTM5NDcsInJvbGVzIjpbIlJPTEVfTk9fUk9MRVMiXX0._SSqulgIqcM0GabBT7oSULFsWEgiFGQKDGDpVKdFy3U")
//
AppData.shared.updateToken(model.accessToken)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
struct PackagesRepository: PackagesRepositoryProtocol {
private var apiRequestClient: APIRequestClientProtocol

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ extension PackagesModel {
.map {
GroupedPackageEntity(
embarqueCodigo: $0.key,
paquetes: Set($0.value.map { $0.asEntity() })
paquetes: $0.value.map { $0.asEntity() }
)
}
}
Expand Down
11 changes: 8 additions & 3 deletions mobile-courier-app/Domain/Entities/AddressesEntity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@

import Foundation

struct AddressesEntity: Codable {
final class AddressesEntity: ObservableObject {
let enviosAereos: EnviosEntity
let viaMaritima: EnviosEntity

init(enviosAereos: EnviosEntity, viaMaritima: EnviosEntity) {
self.enviosAereos = enviosAereos
self.viaMaritima = viaMaritima
}
}

struct EnviosEntity: Codable {
struct EnviosEntity {
let ciudad: String
var cliente: ClienteEntity?
let direccion: String
Expand All @@ -21,7 +26,7 @@ struct EnviosEntity: Codable {
let telefono: String
}

struct ClienteEntity: Codable {
struct ClienteEntity {
let id: Int
let autorizaEmail: Int
let ciautorizado: String
Expand Down
37 changes: 29 additions & 8 deletions mobile-courier-app/Domain/Entities/PackageEntity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct GroupedPackageEntity: Identifiable {
var id: Int { embarqueCodigo }

var embarqueCodigo: Int
var paquetes: Set<PackageEntity>
var paquetes: [PackageEntity]

var totalCost: Decimal {
let thousand: Decimal = 1000
Expand All @@ -42,14 +42,8 @@ struct GroupedPackageEntity: Identifiable {
}

var formattedTotalCost: String {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.minimumFractionDigits = .zero
formatter.allowsFloats = false
formatter.groupingSeparator = "."

let formattedValue = NSDecimalNumber(decimal: totalCost)
return formatter.string(from: formattedValue) ?? "0"
return numberFormatter.string(from: formattedValue) ?? "0"
}

var formattedDate: String {
Expand Down Expand Up @@ -95,6 +89,15 @@ struct GroupedPackageEntity: Identifiable {

return status.caseInsensitiveCompare("C") == .orderedSame ? .inconsistent : .unknown
}

private var numberFormatter: NumberFormatter {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.minimumFractionDigits = .zero
formatter.allowsFloats = false
formatter.groupingSeparator = "."
return formatter
}
}

extension Array where Element == GroupedPackageEntity {
Expand Down Expand Up @@ -123,4 +126,22 @@ extension PackageEntity {
var guaraniesCost: Decimal {
tarifaPrecioCli * cotizacion * paquetePeso
}

var formattedCost: String {
let thousand: Decimal = 1000
let divided = guaraniesCost / thousand
let rounded = NSDecimalNumber(decimal: divided).rounding(accordingToBehavior: nil)
let multiplied = rounded.multiplying(by: thousand as NSDecimalNumber) as Decimal
let formattedValue = NSDecimalNumber(decimal: multiplied)
return numberFormatter.string(from: formattedValue) ?? "0"
}

private var numberFormatter: NumberFormatter {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.minimumFractionDigits = .zero
formatter.allowsFloats = false
formatter.groupingSeparator = "."
return formatter
}
}
17 changes: 0 additions & 17 deletions mobile-courier-app/MobileCourierApp.swift

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,20 @@

import SwiftUI

struct MainView: View {
@StateObject private var coordinator = Coordinator()
struct CoordinatorRootView: View {
@EnvironmentObject private var coordinator: Coordinator
@EnvironmentObject private var appData: AppData

var body: some View {
NavigationStack(path: $coordinator.path) {
coordinator.build(page: .login)
.navigationDestination(for: Page.self) { page in
coordinator.build(page: page)
}
.sheet(item: $coordinator.sheet) { sheet in
coordinator.build(sheet: sheet)
}
}
.environmentObject(coordinator)
.environmentObject(AppData.shared)
}
}

#Preview {
MainView()
CoordinatorRootView()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// GroupedPackageStyleModifier.swift
// mobile-courier-app
//
// Created by Vladimir Espinola on 2024-06-22.
//

import SwiftUI

struct GroupedPackageStyleModifier: ViewModifier {
func body(content: Content) -> some View {
content
.listRowBackground(EmptyView())
.listRowSeparator(.hidden)
.listRowSpacing(.zero)
.listRowInsets(.none)
}
}

extension View {
func groupedPackageRowStyle() -> some View {
self.modifier(GroupedPackageStyleModifier())
}
}
37 changes: 29 additions & 8 deletions mobile-courier-app/Presentation/Home/HomeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,57 @@ import SwiftUI

struct HomeView: View {
@EnvironmentObject var coordinator: Coordinator
@ObservedObject var viewModel: HomeViewModel

init() {
UITabBar.appearance().backgroundColor = UIColor.lightGray.withAlphaComponent(0.3)
}
@State var selectedTab = 0

var body: some View {
HeaderView()

TabView {
TabView(selection: $selectedTab) {
coordinator.build(page: .packagesForWithdrawl)
.tabItem {
Label("Home", systemImage: "house")
}
.tag(0)

coordinator.build(page: .withdrawnPackages)
.tabItem {
Label("Withdrawn", systemImage: "bag")
}
coordinator.build(page: .profile)
.tag(1)

getProfileChildView(addresses: viewModel.addresses)
.tabItem {
Label("Profile", systemImage: "person")
}
.tag(2)
}
.navigationTitle("")
.toolbar(.hidden)
.sheet(item: $coordinator.sheet) { sheet in
coordinator.build(sheet: sheet)
}
.toast(message: $viewModel.toastMessage)
.onAppear {
Task {
await viewModel.getAddresses()
}
}
}

@ViewBuilder
private func getProfileChildView(addresses: AddressesEntity?) -> some View {
if let addresses = addresses {
coordinator.build(page: .profile)
.environmentObject(addresses)
} else {
Text("Loading...")
}
}
}

#Preview {
HomeView()
.environmentObject(Coordinator())
.environmentObject(AppData())
HomeView(viewModel: .previewInstance())
.environmentObject(Coordinator(diContainer: AppDIContainer()))
}
Loading

0 comments on commit 9393bdd

Please sign in to comment.