Skip to content

Commit 12d753c

Browse files
author
Mohammed Rokon Uddin
authored
Merge pull request #5 from monstar-lab-oss/feature/utilities
2 parents 7770b1c + 02cb786 commit 12d753c

File tree

9 files changed

+119
-37
lines changed

9 files changed

+119
-37
lines changed

{{cookiecutter.app_name}}/NetworkPlatform/Package.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ let package = Package(
1313

1414
dependencies: [
1515
.package(path: "../Domain"),
16+
.package(path: "../Utilities"),
1617
.package(
1718
url: "https://github.com/Moya/Moya.git",
1819
from: "15.0.0"
@@ -37,6 +38,7 @@ let package = Package(
3738
dependencies: [
3839
"BuildConfiguration",
3940
.product(name: "Domain", package: "Domain"),
41+
.product(name: "Utilities", package: "Utilities"),
4042
.product(name: "Moya", package: "Moya"),
4143
.product(name: "CombineMoya", package: "Moya")
4244
]

{{cookiecutter.app_name}}/NetworkPlatform/Sources/NetworkPlatform/Network/Networking.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Combine
1111
import CombineMoya
1212
import Foundation
1313
import Moya
14+
import Utilities
1415

1516
class OnlineProvider<Target> where Target: Moya.TargetType {
1617
private let online: AnyPublisher<Bool, Never>
@@ -52,16 +53,14 @@ class OnlineProvider<Target> where Target: Moya.TargetType {
5253
// TODO: investigate
5354
.mapError { $0 as! MoyaError }
5455
.handleEvents(receiveOutput: { response in
55-
print(response.statusCode)
56+
Logger.info("Status Code: \(response.statusCode)")
5657
}, receiveCompletion: { completion in
57-
print(completion)
5858
switch completion {
5959
case .finished:
60-
break
60+
Logger.success("Request Finished")
6161
case let .failure(error):
62-
print(error.localizedDescription)
6362
// TODO: handle network error
64-
// self.networkPopup(error.localizedDescription)
63+
Logger.error(error.localizedDescription)
6564
}
6665
})
6766
.receive(on: DispatchQueue.main)
@@ -79,7 +78,7 @@ class OnlineProvider<Target> where Target: Moya.TargetType {
7978
// TODO: Update new tokens
8079
break
8180
case let .failure(error):
82-
print("error: \(error)")
81+
Logger.error(error.localizedDescription)
8382
// TODO: delete existing token and logout from app
8483
}
8584

{{cookiecutter.app_name}}/NetworkPlatform/Sources/NetworkPlatform/Network/NetworkingType.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Alamofire
1010
import Combine
1111
import Foundation
1212
import Moya
13+
import Utilities
1314

1415
protocol ProductAPIType {
1516
var addXAuth: Bool { get }
@@ -24,11 +25,10 @@ protocol NetworkingType {
2425
}
2526

2627
extension NetworkingType {
27-
static func endpointsClosure<T>(_: String? = nil) -> (T) -> Endpoint where T: TargetType, T: ProductAPIType
28-
{
28+
static func endpointsClosure<T>(_: String? = nil) -> (T) -> Endpoint where T: TargetType, T: ProductAPIType {
2929
return { target in
3030
let endpoint = MoyaProvider.defaultEndpointMapping(for: target)
31-
print(endpoint.url)
31+
Logger.info("Endpoint URL: \(endpoint.url)")
3232
// Sign all non-XApp, non-XAuth token requests
3333
return endpoint
3434
}
@@ -52,8 +52,7 @@ extension NetworkingType {
5252
request.httpShouldHandleCookies = false
5353
closure(.success(request))
5454
} catch {
55-
// TODO: print error
56-
// logError(error.localizedDescription)
55+
Logger.error(error.localizedDescription)
5756
}
5857
}
5958
}

{{cookiecutter.app_name}}/NetworkPlatform/Sources/NetworkPlatform/Utility/VerbosePlugin.swift

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,41 @@
66
// Copyright © {% now 'utc', '%Y' %} {{cookiecutter.company_name}}. All rights reserved.
77
//
88

9-
import Combine
109
import Foundation
10+
import Combine
1111
import Moya
12+
import Utilities
1213

1314
struct VerbosePlugin: PluginType {
1415
let verbose: Bool
15-
16+
1617
func prepare(_ request: URLRequest, target _: TargetType) -> URLRequest {
17-
#if DEBUG
18-
if let body = request.httpBody,
19-
let str = String(data: body, encoding: .utf8)
20-
{
21-
if verbose {
22-
print("request to send: \(str))")
23-
}
18+
#if DEBUG
19+
if let body = request.httpBody,
20+
let string = String(data: body, encoding: .utf8) {
21+
if verbose {
22+
Logger.info("Request Body: \(string))")
2423
}
25-
#endif
24+
}
25+
#endif
2626
return request
2727
}
28-
28+
2929
func didReceive(_ result: Result<Response, MoyaError>, target _: TargetType) {
30-
#if DEBUG
31-
switch result {
32-
case let .success(body):
33-
if verbose {
34-
print("Response:")
35-
if let json = try? JSONSerialization.jsonObject(with: body.data, options: .mutableContainers)
36-
{
37-
print(json)
38-
} else {
39-
let response = String(data: body.data, encoding: .utf8)!
40-
print(response)
41-
}
30+
#if DEBUG
31+
switch result {
32+
case let .success(body):
33+
if verbose {
34+
if let json = try? JSONSerialization.jsonObject(with: body.data, options: .mutableContainers) {
35+
Logger.info("Response: \(json))")
36+
} else {
37+
let response = String(data: body.data, encoding: .utf8)!
38+
Logger.info("Response: \(response))")
4239
}
43-
case .failure:
44-
break
4540
}
46-
#endif
41+
case .failure:
42+
break
43+
}
44+
#endif
4745
}
4846
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
/*.xcodeproj
5+
xcuserdata/
6+
DerivedData/
7+
.swiftpm/config/registries.json
8+
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
9+
.netrc
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// swift-tools-version: 5.7
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "Utilities",
8+
products: [
9+
.library(
10+
name: "Utilities",
11+
targets: ["Utilities"]),
12+
],
13+
dependencies: [
14+
15+
],
16+
targets: [
17+
.target(
18+
name: "Utilities",
19+
dependencies: [])
20+
]
21+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Utilities
2+
3+
A description of this package.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//
2+
// VerbosePlugin.swift
3+
// NetworkPlatform
4+
//
5+
// Created by {{ cookiecutter.creator }} on {% now 'utc', '%d/%m/%Y' %}.
6+
// Copyright © {% now 'utc', '%Y' %} {{cookiecutter.company_name}}. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
public enum Logger {
12+
13+
private static let dateFormatter: DateFormatter = {
14+
let formatter = DateFormatter()
15+
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
16+
return formatter
17+
}()
18+
19+
public static func info(_ messages: Any?..., file: String = #file, function: String = #function, line: Int = #line) {
20+
printMessage(messages, state: "ℹ️ INFO", file: file, function: function, line: line)
21+
}
22+
23+
public static func error(_ messages: Any?..., file: String = #file, function: String = #function, line: Int = #line) {
24+
printMessage(messages, state: "💥 ERROR", file: file, function: function, line: line)
25+
}
26+
27+
public static func warning(_ messages: Any?..., file: String = #file, function: String = #function, line: Int = #line) {
28+
printMessage(messages, state: "⚠️ WARNING", file: file, function: function, line: line)
29+
}
30+
31+
public static func success(_ messages: Any?..., file: String = #file, function: String = #function, line: Int = #line) {
32+
printMessage(messages, state: "✅ SUCCESS", file: file, function: function, line: line)
33+
}
34+
35+
private static func printMessage(_ messages: Any?..., state: String, file: String, function: String, line: Int) {
36+
#if RELEASE
37+
return
38+
#endif
39+
40+
let dateString = dateFormatter.string(from: Date())
41+
print("\(dateString) - \(state) \(sourceFileName(file)).\(function):\(line)", messages)
42+
43+
}
44+
45+
private static func sourceFileName(_ filePath: String) -> String {
46+
let components = filePath.components(separatedBy: "/")
47+
return components.isEmpty ? "" : (components.last ?? "")
48+
}
49+
}

{{cookiecutter.app_name}}/{{cookiecutter.app_name}}.xcodeproj/project.pbxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
2F7B0E70273AD7AD00C68B1F /* {{cookiecutter.app_name}}UITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "{{cookiecutter.app_name}}UITests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
6363
2F7B0E74273AD7AD00C68B1F /* __cookiecutter_app_name__UITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = __cookiecutter_app_name__UITests.swift; sourceTree = "<group>"; };
6464
2F7B0E76273AD7AD00C68B1F /* __cookiecutter_app_name__UITestsLaunchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = __cookiecutter_app_name__UITestsLaunchTests.swift; sourceTree = "<group>"; };
65+
2F9DFD462990EA1800611A47 /* Utilities */ = {isa = PBXFileReference; lastKnownFileType = wrapper; path = Utilities; sourceTree = "<group>"; };
6566
6782D5572912526F0015FCBB /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
6667
6782D558291252700015FCBB /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
6768
67B2B5BD29127D3A00B2E0AC /* Staging.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = Staging.xcconfig; sourceTree = "<group>"; };
@@ -115,6 +116,7 @@
115116
2F7B0E4D273AD7AC00C68B1F = {
116117
isa = PBXGroup;
117118
children = (
119+
2F9DFD462990EA1800611A47 /* Utilities */,
118120
2F28C5F82942162600B7E601 /* NetworkPlatform */,
119121
2F4989C3298E8F470007A245 /* Domain */,
120122
2F4989C4298E8F6F0007A245 /* Commons */,

0 commit comments

Comments
 (0)