Skip to content

Commit 86bffd6

Browse files
author
Nikita Mikheev
committed
implement functionality
1 parent 1db3e3b commit 86bffd6

File tree

5 files changed

+116
-20
lines changed

5 files changed

+116
-20
lines changed

Package.swift

+4-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ let package = Package(
99
// Products define the executables and libraries a package produces, and make them visible to other packages.
1010
.library(
1111
name: "WindowInstanceManager",
12-
targets: ["WindowInstanceManager"]),
12+
targets: ["WindowInstanceManager"]
13+
),
1314
],
1415
dependencies: [
1516
// Dependencies declare other packages that this package depends on.
@@ -20,9 +21,7 @@ let package = Package(
2021
// Targets can depend on other targets in this package, and on products in packages this package depends on.
2122
.target(
2223
name: "WindowInstanceManager",
23-
dependencies: []),
24-
.testTarget(
25-
name: "WindowInstanceManagerTests",
26-
dependencies: ["WindowInstanceManager"]),
24+
dependencies: []
25+
)
2726
]
2827
)

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# WindowInstanceManager
22

3-
A description of this package.
3+
`WindowInstanceManager` is an utility wich helps to manage multiple windows for a single app.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// Window.swift
3+
// Created by Nikita Mikheev on 26.02.2022.
4+
//
5+
6+
import UIKit
7+
8+
protocol WindowDelegate: AnyObject {
9+
func close(window: Window)
10+
}
11+
12+
final class Window {
13+
// MARK: Delegate
14+
weak var delegate: WindowDelegate?
15+
16+
// MARK: Properties
17+
let id = UUID()
18+
19+
private(set) var uiWindow: UIWindow?
20+
private(set) weak var resignedKeyWidnow: UIWindow!
21+
22+
// MARK: Initializers
23+
init(
24+
application: UIApplication,
25+
rootController: UIViewController
26+
) {
27+
guard let applicationWidnow = application.keyWindow else {
28+
assertionFailure("Application does not have a keyWindow!")
29+
return
30+
}
31+
resignedKeyWidnow = applicationWidnow
32+
33+
uiWindow = UIWindow(frame: UIScreen.main.bounds)
34+
uiWindow!.rootViewController = rootController
35+
}
36+
}
37+
38+
// MARK: - WindowControlInterface
39+
extension Window: WindowControlInterface {
40+
func close() {
41+
delegate?.close(window: self)
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,71 @@
1-
public struct WindowInstanceManager {
2-
public private(set) var text = "Hello, World!"
1+
import UIKit
32

4-
public init() {
3+
public protocol WindowControlInterface {
4+
var uiWindow: UIWindow? { get }
5+
func close()
6+
}
7+
8+
public protocol WindowInstanceManaging {
9+
func presentNew(withRoot controller: UIViewController) -> WindowControlInterface
10+
}
11+
12+
public final class WindowInstanceManager {
13+
// MARK: Singleton
14+
public static func shared(for application: UIApplication) -> WindowInstanceManaging {
15+
guard let instance = instance else {
16+
instance = WindowInstanceManager(application: application)
17+
return instance!
18+
}
19+
20+
return instance
21+
}
22+
private static var instance: WindowInstanceManager?
23+
24+
// MARK: Properties
25+
private let application: UIApplication
26+
private var instances = [UUID: Window]()
27+
28+
// MARK: Initializers
29+
private init(
30+
application: UIApplication
31+
) {
32+
self.application = application
33+
}
34+
}
35+
36+
// MARK: - WindowInstanceManaging
37+
extension WindowInstanceManager: WindowInstanceManaging {
38+
public func presentNew(withRoot controller: UIViewController) -> WindowControlInterface {
39+
let window = Window(
40+
application: application,
41+
rootController: controller
42+
)
43+
44+
instances[window.id] = window
45+
window.delegate = self
46+
47+
window.uiWindow?.makeKeyAndVisible()
48+
49+
return window
50+
}
51+
}
52+
53+
// MARK: - WindowDelegate
54+
extension WindowInstanceManager: WindowDelegate {
55+
func close(window: Window) {
56+
guard let uiWindow = window.uiWindow else {
57+
return
58+
}
59+
60+
if let resigned = window.resignedKeyWidnow {
61+
resigned.makeKeyAndVisible()
62+
} else {
63+
application.windows.last?.makeKeyAndVisible()
64+
}
65+
66+
uiWindow.resignKey()
67+
uiWindow.isHidden = true
68+
69+
instances[window.id] = nil
570
}
671
}

Tests/WindowInstanceManagerTests/WindowInstanceManagerTests.swift

-11
This file was deleted.

0 commit comments

Comments
 (0)