Skip to content

Commit

Permalink
Name refactor to align with the Repository pattern (35)
Browse files Browse the repository at this point in the history
  • Loading branch information
billp committed Mar 27, 2023
1 parent 713d7b1 commit 3c62386
Show file tree
Hide file tree
Showing 112 changed files with 541 additions and 617 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ jobs:
- name: List available Xcode versions
run: ls /Applications | grep Xcode
- name: Select Xcode
run: sudo xcode-select -switch /Applications/Xcode_14.1.app && /usr/bin/xcodebuild -version
run: sudo xcode-select -switch /Applications/Xcode_14.2.app && /usr/bin/xcodebuild -version
- name: Run unit tests
run: xcodebuild test -scheme TermiNetworkTests -destination 'platform=iOS Simulator,name=iPhone 14 Pro,OS=16.1'
run: xcodebuild test -scheme TermiNetworkTests -destination 'platform=iOS Simulator,name=iPhone 14 Pro,OS=16.2'
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1.2.1

2 changes: 1 addition & 1 deletion Examples/Communication/DemoEnvironment.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// DemoEnvironment.swift
//
// Copyright © 2018-2022 Vassilis Panagiotopoulos. All rights reserved.
// Copyright © 2018-2023 Vassilis Panagiotopoulos. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in the
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// GlobalNetworkErrorHandler.swift
//
// Copyright © 2018-2022 Vassilis Panagiotopoulos. All rights reserved.
// Copyright © 2018-2023 Vassilis Panagiotopoulos. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in the
Expand Down
2 changes: 1 addition & 1 deletion Examples/Communication/Middleware/CryptoMiddleware.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// CryptoMiddleware.swift
//
// Copyright © 2018-2022 Vassilis Panagiotopoulos. All rights reserved.
// Copyright © 2018-2023 Vassilis Panagiotopoulos. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in the
Expand Down
2 changes: 1 addition & 1 deletion Examples/Communication/Models/Domain/City.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// City.swift
//
// Copyright © 2018-2022 Vassilis Panagiotopoulos. All rights reserved.
// Copyright © 2018-2023 Vassilis Panagiotopoulos. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in the
Expand Down
2 changes: 1 addition & 1 deletion Examples/Communication/Models/Domain/EncryptedModel.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// EncryptedModel.swift
//
// Copyright © 2018-2022 Vassilis Panagiotopoulos. All rights reserved.
// Copyright © 2018-2023 Vassilis Panagiotopoulos. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in the
Expand Down
2 changes: 1 addition & 1 deletion Examples/Communication/Models/Domain/FileResponse.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// FileResponse.swift
//
// Copyright © 2018-2022 Vassilis Panagiotopoulos. All rights reserved.
// Copyright © 2018-2023 Vassilis Panagiotopoulos. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in the
Expand Down
2 changes: 1 addition & 1 deletion Examples/Communication/Models/Rest/RSCity.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RSCity.swift
//
// Copyright © 2018-2022 Vassilis Panagiotopoulos. All rights reserved.
// Copyright © 2018-2023 Vassilis Panagiotopoulos. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in the
Expand Down
2 changes: 1 addition & 1 deletion Examples/Communication/Models/Rest/RSEncryptedModel.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RSEncryptedModel.swift
//
// Copyright © 2018-2022 Vassilis Panagiotopoulos. All rights reserved.
// Copyright © 2018-2023 Vassilis Panagiotopoulos. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in the
Expand Down
2 changes: 1 addition & 1 deletion Examples/Communication/Models/Rest/RSFileResponse.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RSFileResponse.swift
//
// Copyright © 2018-2022 Vassilis Panagiotopoulos. All rights reserved.
// Copyright © 2018-2023 Vassilis Panagiotopoulos. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in the
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// CityRoute.swift
// CitiesRepository.swift
//
// Copyright © 2018-2022 Vassilis Panagiotopoulos. All rights reserved.
// Copyright © 2018-2023 Vassilis Panagiotopoulos. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in the
Expand All @@ -20,40 +20,35 @@
import Foundation
import TermiNetwork

enum CityRoute: RouteProtocol {
enum CitiesRepository: EndpointProtocol {
case cities
case city(id: Int)
case thumb(city: City)
case image(city: City)
case pinning(configuration: Configuration)

func configure() -> RouteConfiguration {
func configure() -> EndpointConfiguration {
switch self {
case .cities:
return RouteConfiguration(method: .get,
path: .path(["cities"]),
mockFilePath:
.path(["Cities", "cities.json"]))
return .init(method: .get,
path: .path(["cities"]),
mockFilePath: .path(["Cities", "cities.json"]))
case .city(let id):
return RouteConfiguration(method: .get,
path: .path(["city", String(id)]),
mockFilePath:
.path(["Cities", "Details", String(format: "%i.json", id)]))
return .init(method: .get,
path: .path(["city", String(id)]),
mockFilePath: .path(["Cities", "Details", String(format: "%i.json", id)]))
case .thumb(let city):
return RouteConfiguration(method: .get,
path: .path([city.thumb ?? ""]),
mockFilePath:
.path(["Cities", "Thumbs", String(format: "%i.jpg", city.cityID)]))
return .init(method: .get,
path: .path([city.thumb ?? ""]),
mockFilePath: .path(["Cities", "Thumbs", String(format: "%i.jpg", city.cityID)]))
case .image(let city):
return RouteConfiguration(method: .get,
path: .path([city.image ?? ""]),
mockFilePath:
.path(["Cities", "Images", String(format: "%i.jpg", city.cityID)]))
return .init(method: .get,
path: .path([city.image ?? ""]),
mockFilePath: .path(["Cities", "Images", String(format: "%i.jpg", city.cityID)]))
case .pinning(let configuration):
return RouteConfiguration(
method: .get,
path: .path(["cities"]),
configuration: configuration
return .init(method: .get,
path: .path(["cities"]),
configuration: configuration
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// MiscRoute.swift
// MiscRepository.swift
//
// Copyright © 2018-2022 Vassilis Panagiotopoulos. All rights reserved.
// Copyright © 2018-2023 Vassilis Panagiotopoulos. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in the
Expand All @@ -20,20 +20,20 @@
import Foundation
import TermiNetwork

enum MiscRoute: RouteProtocol {
enum MiscRepository: EndpointProtocol {
case testEncryptParams(param: String)
case upload(fileUrl: URL)

func configure() -> RouteConfiguration {
func configure() -> EndpointConfiguration {
switch self {
case .testEncryptParams(let value):
return RouteConfiguration(
return EndpointConfiguration(
method: .post,
path: .path(["test_encrypt_params"]),
params: ["value": value]
)
case .upload(fileUrl: let fileUrl):
return RouteConfiguration(
return EndpointConfiguration(
method: .post,
path: .path(["file_upload"]),
params: ["file": MultipartFormDataPartType.url(fileUrl)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// CitiesTransformer.swift
//
// Copyright © 2018-2022 Vassilis Panagiotopoulos. All rights reserved.
// Copyright © 2018-2023 Vassilis Panagiotopoulos. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in the
Expand Down
2 changes: 1 addition & 1 deletion Examples/Communication/Transformers/CityTransformer.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// CityTransformer.swift
//
// Copyright © 2018-2022 Vassilis Panagiotopoulos. All rights reserved.
// Copyright © 2018-2023 Vassilis Panagiotopoulos. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in the
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// EncryptedModelTransformer.swift
//
// Copyright © 2018-2022 Vassilis Panagiotopoulos. All rights reserved.
// Copyright © 2018-2023 Vassilis Panagiotopoulos. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in the
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// FileUploadTransformer.swift
//
// Copyright © 2018-2022 Vassilis Panagiotopoulos. All rights reserved.
// Copyright © 2018-2023 Vassilis Panagiotopoulos. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in the
Expand Down
2 changes: 1 addition & 1 deletion Examples/Screens/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// AppDelegate.swift
//
// Copyright © 2018-2022 Vassilis Panagiotopoulos. All rights reserved.
// Copyright © 2018-2023 Vassilis Panagiotopoulos. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in the
Expand Down
26 changes: 6 additions & 20 deletions Examples/Screens/City Explorer/CityExplorerDetails.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// CityExplorerDetails.swift
//
// Copyright © 2018-2022 Vassilis Panagiotopoulos. All rights reserved.
// Copyright © 2018-2023 Vassilis Panagiotopoulos. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in the
Expand Down Expand Up @@ -46,13 +46,12 @@ struct CityExplorerDetails: View {
}
}
.navigationTitle(viewModel.city.name)
.onAppear(perform: viewModel.onAppear)
.onDisappear(perform: viewModel.onDisappear)
.task(viewModel.loadCity)
}
}

extension CityExplorerDetails {
class ViewModel: ObservableObject {
@MainActor class ViewModel: ObservableObject {
var fetchCityDetailsTask: Task<(), Never>?
@Published var city: City
var cityFetched: Bool = false
Expand All @@ -64,21 +63,8 @@ extension CityExplorerDetails {
self.usesMockData = usesMockData
}

func onAppear() {
guard fetchCityDetailsTask == nil else {
return
}
fetchCityDetailsTask = Task {
await loadCity()
}
}

func onDisappear() {
fetchCityDetailsTask?.cancel()
}

@MainActor func loadCity() async {
let request = Router<CityRoute>().request(for: .city(id: city.cityID))
@Sendable func loadCity() async {
let request = Client<CitiesRepository>().request(for: .city(id: city.cityID))

do {
city = try await request.asyncUpload(using: CityTransformer.self)
Expand Down Expand Up @@ -114,7 +100,7 @@ struct CityDetailsEntry: View {
}

var thumbView: some View {
TermiNetwork.Image(request: Router<CityRoute>().request(for: .image(city: city)))
TermiNetwork.Image(request: Client<CitiesRepository>().request(for: .image(city: city)))
.aspectRatio(contentMode: .fill)
}
}
30 changes: 8 additions & 22 deletions Examples/Screens/City Explorer/CityExplorerView.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// CityExplorerView.swift
//
// Copyright © 2018-2022 Vassilis Panagiotopoulos. All rights reserved.
// Copyright © 2018-2023 Vassilis Panagiotopoulos. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in the
Expand Down Expand Up @@ -45,12 +45,7 @@ struct CityExplorerView: View {
}
}
.navigationTitle("City Explorer")
.onAppear { [unowned viewModel] in
viewModel.onAppear()
}
.onDisappear { [unowned viewModel] in
viewModel.onDissapear()
}
.task(viewModel.loadCities)
}
}

Expand All @@ -74,7 +69,7 @@ struct CityRow: View {

@ViewBuilder
var thumbView: some View {
let request = Router<CityRoute>().request(for: .thumb(city: city))
let request = Client<CitiesRepository>().request(for: .thumb(city: city))

ZStack {
if !imageLoaded {
Expand All @@ -94,7 +89,7 @@ struct CityRow: View {

extension CityExplorerView {
@MainActor class ViewModel: ObservableObject {
private var fetchCitiesTask: Task<(), Never>?
private var initialRequestCompleted: Bool = false

@Published var cities: [City] = []
@Published var errorMessage: String?
Expand All @@ -106,24 +101,15 @@ extension CityExplorerView {
Environment.current.configuration?.mockDataEnabled = usesMockData
}

func onAppear() {
guard fetchCitiesTask == nil else {
@Sendable func loadCities() async {
guard !initialRequestCompleted else {
return
}
self.fetchCitiesTask = Task {
await loadCities()
}
}

func onDissapear() {
fetchCitiesTask?.cancel()
}

func loadCities() async {
do {
cities = try await Router<CityRoute>()
cities = try await Client<CitiesRepository>()
.request(for: .cities)
.async(using: CitiesTransformer.self)
initialRequestCompleted = true
} catch let error {
if let error = error as? TNError {
self.errorMessage = error.localizedDescription
Expand Down
2 changes: 1 addition & 1 deletion Examples/Screens/ContentView.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ContentView.swift
//
// Copyright © 2018-2022 Vassilis Panagiotopoulos. All rights reserved.
// Copyright © 2018-2023 Vassilis Panagiotopoulos. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in the
Expand Down
6 changes: 3 additions & 3 deletions Examples/Screens/DemoApp.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// DemoApp.swift
//
// Copyright © 2018-2022 Vassilis Panagiotopoulos. All rights reserved.
// Copyright © 2018-2023 Vassilis Panagiotopoulos. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in the
Expand Down Expand Up @@ -31,12 +31,12 @@ struct DemoApp: Identifiable {
[
DemoApp(
name: "City Explorer",
description: "Router, Transformers, Codables",
description: "Repository, Transformers, Codables",
destination: AnyView(CityExplorerView(viewModel: .init(usesMockData: false)))
),
DemoApp(
name: "City Explorer - Offline Mode",
description: "Router, Transformers, Codables, Mock Data",
description: "Repository, Transformers, Codables, Mock Data",
destination: AnyView(CityExplorerView(viewModel: .init(usesMockData: true)))
),
DemoApp(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// EncryptedCommunicationView.swift
//
// Copyright © 2018-2022 Vassilis Panagiotopoulos. All rights reserved.
// Copyright © 2018-2023 Vassilis Panagiotopoulos. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in the
Expand Down Expand Up @@ -67,7 +67,7 @@ struct EncryptedCommunicationView: View {
func startRequest() {
responseString = "fetching..."

Router<MiscRoute>(configuration: configuration)
Client<MiscRepository>(configuration: configuration)
.request(for: .testEncryptParams(param: text))
.success(transformer: EncryptedModelTransformer.self) { model in
responseString = model.text
Expand Down
Loading

0 comments on commit 3c62386

Please sign in to comment.