Skip to content

Commit 06e1f37

Browse files
deleted rechability swift, also added rechability for check if device connected to network, edited RequestDispatcher, Constants
1 parent 1b8420b commit 06e1f37

File tree

7 files changed

+140
-61
lines changed

7 files changed

+140
-61
lines changed

MyDictionary_App_Swift.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@
318318
22CA088E265167620052D169 /* MDReloadDataProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22CA088D265167620052D169 /* MDReloadDataProtocol.swift */; };
319319
22CD45F026F090DD0060978D /* MDCounterPasswordTextFieldWithToolBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22CD45EF26F090DD0060978D /* MDCounterPasswordTextFieldWithToolBar.swift */; };
320320
22CD45F226F090F20060978D /* MDCounterTextFieldWithToolBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22CD45F126F090F20060978D /* MDCounterTextFieldWithToolBar.swift */; };
321+
22D54B6C270EE06100DEE843 /* Reachability.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22D54B6B270EE06100DEE843 /* Reachability.swift */; };
321322
22D6D01027082EBC002CA1B2 /* MDProgressHUDHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22D6D00F27082EBC002CA1B2 /* MDProgressHUDHelper.swift */; };
322323
22DB2F7726C3BE7900C1AADA /* CourseListModule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22DB2F7026C3BE7900C1AADA /* CourseListModule.swift */; };
323324
22DB2F7826C3BE7900C1AADA /* CourseListPresenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22DB2F7126C3BE7900C1AADA /* CourseListPresenter.swift */; };
@@ -720,6 +721,7 @@
720721
22CA088D265167620052D169 /* MDReloadDataProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MDReloadDataProtocol.swift; sourceTree = "<group>"; };
721722
22CD45EF26F090DD0060978D /* MDCounterPasswordTextFieldWithToolBar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MDCounterPasswordTextFieldWithToolBar.swift; sourceTree = "<group>"; };
722723
22CD45F126F090F20060978D /* MDCounterTextFieldWithToolBar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MDCounterTextFieldWithToolBar.swift; sourceTree = "<group>"; };
724+
22D54B6B270EE06100DEE843 /* Reachability.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Reachability.swift; sourceTree = "<group>"; };
723725
22D6D00F27082EBC002CA1B2 /* MDProgressHUDHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MDProgressHUDHelper.swift; sourceTree = "<group>"; };
724726
22DB2F7026C3BE7900C1AADA /* CourseListModule.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CourseListModule.swift; sourceTree = "<group>"; };
725727
22DB2F7126C3BE7900C1AADA /* CourseListPresenter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CourseListPresenter.swift; sourceTree = "<group>"; };
@@ -1354,6 +1356,7 @@
13541356
226FEE7E266B735B0044A5F9 /* Common */ = {
13551357
isa = PBXGroup;
13561358
children = (
1359+
22D54B6A270EE05900DEE843 /* Reachability */,
13571360
22D6D00E27082EAC002CA1B2 /* MDProgressHUDHelper */,
13581361
22B5C62E27059E5C00185954 /* DelegatesImplementation */,
13591362
229572B926FF1FAD0092817B /* MDBridge */,
@@ -2109,6 +2112,14 @@
21092112
path = UIResources;
21102113
sourceTree = "<group>";
21112114
};
2115+
22D54B6A270EE05900DEE843 /* Reachability */ = {
2116+
isa = PBXGroup;
2117+
children = (
2118+
22D54B6B270EE06100DEE843 /* Reachability.swift */,
2119+
);
2120+
path = Reachability;
2121+
sourceTree = "<group>";
2122+
};
21122123
22D6D00E27082EAC002CA1B2 /* MDProgressHUDHelper */ = {
21132124
isa = PBXGroup;
21142125
children = (
@@ -2742,6 +2753,7 @@
27422753
22DB2F7B26C3BE7900C1AADA /* CourseListInteractor.swift in Sources */,
27432754
220793BB26C65965003D434B /* MDEnvironment.swift in Sources */,
27442755
2291DADA27019DA900216B64 /* AddWordDataManager.swift in Sources */,
2756+
22D54B6C270EE06100DEE843 /* Reachability.swift in Sources */,
27452757
22E691B626CE5E5200641DFA /* MDStorageResult.swift in Sources */,
27462758
2289CEE42678CC34007254C3 /* MDUpdateWordMemoryStorageOperation.swift in Sources */,
27472759
22EDF02826C7E8B900D19DA1 /* CDJWTResponseEntityAttributeName.swift in Sources */,

MyDictionary_App_Swift/Common/Constants/MDConstants.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
//
77

88
import UIKit
9-
import Reachability
109

1110
struct MDConstants {
1211

@@ -395,4 +394,20 @@ struct MDConstants {
395394

396395
}
397396

397+
struct MDNetworkActivityIndicator {
398+
399+
static func show() {
400+
DispatchQueue.main.async {
401+
UIApplication.shared.isNetworkActivityIndicatorVisible = true
402+
}
403+
}
404+
405+
static func hide() {
406+
DispatchQueue.main.async {
407+
UIApplication.shared.isNetworkActivityIndicatorVisible = false
408+
}
409+
}
410+
411+
}
412+
398413
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//
2+
// Reachability.swift
3+
// MyDictionary_App_Swift
4+
//
5+
// Created by Dmytro Chumakov on 07.10.2021.
6+
//
7+
8+
import SystemConfiguration
9+
10+
public class Reachability {
11+
12+
var isConnectedToNetwork: Bool {
13+
14+
var zeroAddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))
15+
zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress))
16+
zeroAddress.sin_family = sa_family_t(AF_INET)
17+
18+
let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) {
19+
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) {zeroSockAddress in
20+
SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress)
21+
}
22+
}
23+
24+
var flags: SCNetworkReachabilityFlags = SCNetworkReachabilityFlags(rawValue: 0)
25+
if SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) == false {
26+
return false
27+
}
28+
29+
/* Only Working for WIFI
30+
let isReachable = flags == .reachable
31+
let needsConnection = flags == .connectionRequired
32+
33+
return isReachable && !needsConnection
34+
*/
35+
36+
// Working for Cellular and WIFI
37+
let isReachable = (flags.rawValue & UInt32(kSCNetworkFlagsReachable)) != 0
38+
let needsConnection = (flags.rawValue & UInt32(kSCNetworkFlagsConnectionRequired)) != 0
39+
let ret = (isReachable && !needsConnection)
40+
41+
return ret
42+
43+
}
44+
45+
}

MyDictionary_App_Swift/Core/Network/MDRequestDispatcher.swift

Lines changed: 64 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
//
77

88
import Foundation
9-
import UIKit
10-
import Reachability
119

1210
protocol MDRequestDispatcherProtocol {
1311
init(reachability: Reachability, environment: MDEnvironmentProtocol, networkSession: MDNetworkSessionProtocol)
@@ -42,35 +40,57 @@ final class MDRequestDispatcher: MDRequestDispatcherProtocol {
4240
extension MDRequestDispatcher {
4341

4442
func execute(endpoint: MDEndpoint, completion: @escaping (MDResponseOperationResult) -> Void) -> URLSessionTask? {
43+
4544
// Check Internet Connection
46-
guard (isReachable) else {
47-
DispatchQueue.main.async {
48-
completion(.error(MDAPIError.noInternetConnection, nil))
49-
}
45+
guard (reachability.isConnectedToNetwork) else {
46+
47+
//
48+
completion(.error(MDAPIError.noInternetConnection, nil))
49+
//
5050
return nil
51+
//
52+
5153
}
52-
// Show Indicator //
53-
showNetworkActivityIndicator()
54+
55+
// Show Indicator
56+
MDConstants.MDNetworkActivityIndicator.show()
57+
5458
// Create a URL request.
5559
guard let urlRequest = endpoint.urlRequest(with: environment) else {
56-
// Hide Indicator //
57-
hideNetworkActivityIndicator()
58-
// -------------- //
60+
61+
// Hide Indicator
62+
MDConstants.MDNetworkActivityIndicator.hide()
63+
//
5964
completion(.error(MDAPIError.badRequest, nil))
65+
//
6066
return nil
67+
//
68+
6169
}
6270

6371
// Create a URLSessionTask to execute the URLRequest.
6472
var task: URLSessionTask?
73+
6574
switch endpoint.requestType {
75+
6676
case .data:
67-
task = networkSession.dataTask(with: urlRequest, completionHandler: { (data, urlResponse, error) in
68-
self.handleJsonTaskResponse(data: data, urlResponse: urlResponse, error: error, completion: completion)
69-
// Hide Indicator //
70-
self.hideNetworkActivityIndicator()
71-
// -------------- //
77+
78+
//
79+
task = networkSession.dataTask(with: urlRequest, completionHandler: { [unowned self] (data, urlResponse, error) in
80+
81+
//
82+
handleJsonTaskResponse(data: data, urlResponse: urlResponse, error: error, completion: completion)
83+
//
84+
85+
// Hide Indicator
86+
MDConstants.MDNetworkActivityIndicator.hide()
87+
//
88+
7289
})
90+
91+
//
7392
break
93+
//
7494
}
7595

7696
// Start the task.
@@ -84,30 +104,48 @@ extension MDRequestDispatcher {
84104
fileprivate extension MDRequestDispatcher {
85105

86106
func handleJsonTaskResponse(data: Data?, urlResponse: URLResponse?, error: Error?, completion: @escaping (MDResponseOperationResult) -> Void) {
107+
87108
// Check if the response is valid.
88109
guard let urlResponse = urlResponse as? HTTPURLResponse else {
89-
DispatchQueue.main.async {
90-
completion(MDResponseOperationResult.error(MDAPIError.invalidResponse, nil))
91-
}
110+
completion(MDResponseOperationResult.error(MDAPIError.invalidResponse, nil))
92111
return
93112
}
113+
94114
// Verify the HTTP status code.
95115
let result = verify(data: data, urlResponse: urlResponse, error: error)
116+
96117
switch result {
118+
97119
case .success(let data):
98-
DispatchQueue.main.async {
99-
completion(MDResponseOperationResult.data(data, urlResponse))
100-
}
120+
121+
//
122+
completion(MDResponseOperationResult.data(data, urlResponse))
123+
//
124+
125+
//
126+
break
127+
//
128+
101129
case .failure(let error):
102-
DispatchQueue.main.async {
103-
completion(MDResponseOperationResult.error(error, urlResponse))
104-
}
130+
131+
//
132+
completion(MDResponseOperationResult.error(error, urlResponse))
133+
//
134+
135+
//
136+
break
137+
//
138+
105139
}
140+
106141
}
107142

108143
func verify(data: Data?, urlResponse: HTTPURLResponse, error: Error?) -> Result<Data?, Error> {
144+
109145
guard let statusCode = MDAPIStatusCode.init(rawValue: urlResponse.statusCode) else { return .failure(MDAPIError.unknown) }
146+
110147
switch statusCode {
148+
111149
case .ok:
112150
return .success(data)
113151
case .badRequest:
@@ -125,32 +163,7 @@ fileprivate extension MDRequestDispatcher {
125163
case .internalServerError:
126164
return .failure(MDAPIError.internalServerError)
127165
}
128-
}
129-
130-
}
131-
132-
// MARK: - Network Activity Indicator
133-
fileprivate extension MDRequestDispatcher {
134-
135-
func showNetworkActivityIndicator() {
136-
DispatchQueue.main.async {
137-
UIApplication.shared.isNetworkActivityIndicatorVisible = true
138-
}
139-
}
140-
141-
func hideNetworkActivityIndicator() {
142-
DispatchQueue.main.async {
143-
UIApplication.shared.isNetworkActivityIndicatorVisible = false
144-
}
145-
}
146-
147-
}
148-
149-
// MARK: - Check Internet Connection
150-
fileprivate extension MDRequestDispatcher {
151-
152-
var isReachable: Bool {
153-
return (reachability.connection == .cellular || reachability.connection == .wifi)
166+
154167
}
155168

156169
}

MyDictionary_App_Swift/MDAppDependencies.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import UIKit
99
import Firebase
10-
import Reachability
1110

1211
protocol MDAppDependenciesProtocol {
1312

@@ -94,7 +93,7 @@ extension MDAppDependencies {
9493

9594
func configureDependencies() {
9695

97-
guard let reachability = try? Reachability.init() else { fatalError("Impossible initialize Reachability Service") }
96+
let reachability = Reachability.init()
9897
self.reachability = reachability
9998
//
10099
let coreDataStack: MDCoreDataStack = .init()

Podfile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ target 'MyDictionary_App_Swift' do
88
# Pods for MyDictionary_App_Swift
99

1010
pod 'Firebase/Analytics'
11-
pod 'Firebase/Crashlytics'
12-
pod 'ReachabilitySwift'
11+
pod 'Firebase/Crashlytics'
1312
pod 'MBProgressHUD'
1413
pod 'MGSwipeTableCell'
1514
pod 'KMPlaceholderTextView'

Podfile.lock

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,13 @@ PODS:
100100
- nanopb/decode (2.30908.0)
101101
- nanopb/encode (2.30908.0)
102102
- PromisesObjC (2.0.0)
103-
- ReachabilitySwift (5.0.0)
104103

105104
DEPENDENCIES:
106105
- Firebase/Analytics
107106
- Firebase/Crashlytics
108107
- KMPlaceholderTextView
109108
- MBProgressHUD
110109
- MGSwipeTableCell
111-
- ReachabilitySwift
112110

113111
SPEC REPOS:
114112
trunk:
@@ -126,7 +124,6 @@ SPEC REPOS:
126124
- MGSwipeTableCell
127125
- nanopb
128126
- PromisesObjC
129-
- ReachabilitySwift
130127

131128
SPEC CHECKSUMS:
132129
Firebase: 629510f1a9ddb235f3a7c5c8ceb23ba887f0f814
@@ -143,8 +140,7 @@ SPEC CHECKSUMS:
143140
MGSwipeTableCell: b804e4e450dee439c42250be90bd50458bf67fce
144141
nanopb: a0ba3315591a9ae0a16a309ee504766e90db0c96
145142
PromisesObjC: 68159ce6952d93e17b2dfe273b8c40907db5ba58
146-
ReachabilitySwift: 985039c6f7b23a1da463388634119492ff86c825
147143

148-
PODFILE CHECKSUM: a43bf092276dadc13e3075491ae3d835b52d9536
144+
PODFILE CHECKSUM: 31299d7410c451ae26ca3a3a7dee5dc9d0e7543d
149145

150146
COCOAPODS: 1.11.2

0 commit comments

Comments
 (0)