Skip to content

Commit c67b656

Browse files
authored
Merge pull request #1 from charlymr/master
Some fixed & a Locale extension I like
2 parents 4f28770 + 07364b9 commit c67b656

File tree

5 files changed

+55
-5
lines changed

5 files changed

+55
-5
lines changed

DJSwiftHelpers.xcodeproj/project.pbxproj

+11-1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@
9191
62DF50AD256E9AA800C88FF7 /* URLSession.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6206C950256D6FF8003F3A67 /* URLSession.swift */; };
9292
62DF50B1256E9AAA00C88FF7 /* UserDefaults.swift in Sources */ = {isa = PBXBuildFile; fileRef = 620F6A4D250BBCE900B10032 /* UserDefaults.swift */; };
9393
62DF50B5256E9AAD00C88FF7 /* Decodable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62D6CDE8255BFCCB00ABD216 /* Decodable.swift */; };
94+
62EC880526F8DACE00211A6A /* Locale.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62EC880426F8DACE00211A6A /* Locale.swift */; };
95+
62EC880626F8DACE00211A6A /* Locale.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62EC880426F8DACE00211A6A /* Locale.swift */; };
96+
62EC880726F8DACE00211A6A /* Locale.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62EC880426F8DACE00211A6A /* Locale.swift */; };
97+
62EC880826F8DACE00211A6A /* Locale.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62EC880426F8DACE00211A6A /* Locale.swift */; };
9498
/* End PBXBuildFile section */
9599

96100
/* Begin PBXFileReference section */
@@ -127,6 +131,7 @@
127131
62D6CDF4255C01F400ABD216 /* UILabel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UILabel.swift; sourceTree = "<group>"; };
128132
62DEDA482636D40A00771544 /* FileManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileManager.swift; sourceTree = "<group>"; };
129133
62DF505D256E99DB00C88FF7 /* DJSwiftHelpers.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = DJSwiftHelpers.framework; sourceTree = BUILT_PRODUCTS_DIR; };
134+
62EC880426F8DACE00211A6A /* Locale.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Locale.swift; sourceTree = "<group>"; };
130135
/* End PBXFileReference section */
131136

132137
/* Begin PBXFrameworksBuildPhase section */
@@ -201,9 +206,11 @@
201206
62754BA525E691E200F2F489 /* Codable.swift */,
202207
6220B4C225ADDDC40064B9EB /* Collection.swift */,
203208
62D6CDEB255BFDA900ABD216 /* Date.swift */,
209+
62D6CDE8255BFCCB00ABD216 /* Decodable.swift */,
204210
625380812594CE9B00C58B2F /* Double.swift */,
205211
627631A6255D512F005C09B1 /* DJSwiftHelpers.swift */,
206212
62DEDA482636D40A00771544 /* FileManager.swift */,
213+
62EC880426F8DACE00211A6A /* Locale.swift */,
207214
620F6A34250BB78200B10032 /* String.swift */,
208215
620F6A36250BB78200B10032 /* UIColor.swift */,
209216
620F6A47250BBC2F00B10032 /* UIDevice.swift */,
@@ -217,7 +224,6 @@
217224
62D6CDEE255BFFA300ABD216 /* URLResponse.swift */,
218225
6206C950256D6FF8003F3A67 /* URLSession.swift */,
219226
620F6A4D250BBCE900B10032 /* UserDefaults.swift */,
220-
62D6CDE8255BFCCB00ABD216 /* Decodable.swift */,
221227
);
222228
name = Common;
223229
path = Sources/Common;
@@ -437,6 +443,7 @@
437443
6206C951256D6FF8003F3A67 /* URLSession.swift in Sources */,
438444
620F6A37250BB78200B10032 /* Array.swift in Sources */,
439445
620F6A38250BB78200B10032 /* UIStackView.swift in Sources */,
446+
62EC880526F8DACE00211A6A /* Locale.swift in Sources */,
440447
620F6A4E250BBCE900B10032 /* UserDefaults.swift in Sources */,
441448
62C4485225A2208F0014F44B /* UIFont.swift in Sources */,
442449
620F6A3B250BB78200B10032 /* UIColor.swift in Sources */,
@@ -485,6 +492,7 @@
485492
621136ED255D5CAC003F8FBF /* DJSwiftHelpers.swift in Sources */,
486493
621B3C2E255D467000A5E77F /* UIViewController.swift in Sources */,
487494
621B3C2F255D467000A5E77F /* UIImage.swift in Sources */,
495+
62EC880626F8DACE00211A6A /* Locale.swift in Sources */,
488496
);
489497
runOnlyForDeploymentPostprocessing = 0;
490498
};
@@ -499,6 +507,7 @@
499507
6266549F25F2767000696303 /* DJSwiftHelpers.swift in Sources */,
500508
6266549725F2767000696303 /* URLSession.swift in Sources */,
501509
6266549E25F2767000696303 /* URLResponse.swift in Sources */,
510+
62EC880826F8DACE00211A6A /* Locale.swift in Sources */,
502511
6266549B25F2767000696303 /* Collection.swift in Sources */,
503512
6266549125F2767000696303 /* Codable.swift in Sources */,
504513
626654A025F2767000696303 /* UserDefaults.swift in Sources */,
@@ -525,6 +534,7 @@
525534
62DF5078256E9A8900C88FF7 /* Array.swift in Sources */,
526535
62DF5081256E9A9000C88FF7 /* String.swift in Sources */,
527536
62018FE925DEB40F002B71A5 /* AsyncOperation.swift in Sources */,
537+
62EC880726F8DACE00211A6A /* Locale.swift in Sources */,
528538
62DF50A5256E9AA500C88FF7 /* URLRequest.swift in Sources */,
529539
62DF50B5256E9AAD00C88FF7 /* Decodable.swift in Sources */,
530540
62C4485425A2208F0014F44B /* UIFont.swift in Sources */,

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,14 @@ Parse a `json` file into the specified object type
149149
readJSONFromFile<T: Decodable>(fileURL: URL, type: T.Type) -> T?
150150
```
151151

152+
### Locale
153+
154+
Return the emoji flag for the given Locale
155+
156+
```swift
157+
var emojiFlag: String
158+
```
159+
152160
### String
153161
Truncate a string by removing all characters at the `position`
154162

Sources/Common/Locale.swift

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// Locale.swift
3+
// DJSwiftHelpers
4+
//
5+
// Created by Denis Martin-Bruillot on 17/09/2021.
6+
// Copyright © 2021 Darren Jones. All rights reserved.
7+
8+
import Foundation
9+
10+
public
11+
extension Locale {
12+
13+
/**
14+
Return the emoji flag for the given Locale
15+
*/
16+
var emojiFlag: String {
17+
let base : UInt32 = 127397
18+
var s = ""
19+
let country = regionCode?.uppercased() ?? languageCode ?? "US"
20+
for v in country.unicodeScalars {
21+
s.unicodeScalars.append(UnicodeScalar(base + v.value)!)
22+
}
23+
return String(s)
24+
}
25+
26+
}

Sources/Non_Extension/UIApplication.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ extension UIApplication {
1616
/**
1717
Returns the top most ViewController regardless if embeded in a navigation stack or not
1818
*/
19-
class func getTopViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
19+
class func getTopViewController(base: UIViewController? = UIWindow.key?.rootViewController) -> UIViewController? {
2020

2121
if let nav = base as? UINavigationController {
2222
return getTopViewController(base: nav.visibleViewController)
@@ -51,15 +51,15 @@ extension UIApplication {
5151
class func dismisstoRoot(animated:Bool, completion:(() -> Void)? = nil) {
5252

5353
// On iOS 12, calling dismiss when there is nothing to dismiss does not call the completion block, so we have to see if we are already at base level
54-
guard self.shared.keyWindow?.rootViewController?.presentedViewController != nil else {
54+
guard UIWindow.key?.rootViewController?.presentedViewController != nil else {
5555

5656
// rootViewController is on top, so nothing to dismiss
5757
completion?()
5858
return
5959
}
6060

6161
// We have views on top of the rootViewController
62-
self.shared.keyWindow?.rootViewController?.dismiss(animated: animated, completion: completion)
62+
UIWindow.key?.rootViewController?.dismiss(animated: animated, completion: completion)
6363
}
6464
}
6565
#endif // !IS_EXTENSION

Sources/Non_Extension/UIWindow.swift

+7-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ extension UIWindow {
1818
*/
1919
static var key: UIWindow? {
2020
if #available(iOS 13, *) {
21-
return UIApplication.shared.windows.first { $0.isKeyWindow }
21+
return UIApplication.shared.windows.first { $0.isKeyWindow } ??
22+
UIApplication.shared.connectedScenes
23+
.filter({$0.activationState == .foregroundActive})
24+
.map({$0 as? UIWindowScene})
25+
.compactMap({$0})
26+
.first?.windows
27+
.filter({ $0.isKeyWindow }).first
2228
} else {
2329
return UIApplication.shared.keyWindow
2430
}

0 commit comments

Comments
 (0)