Skip to content

Commit ff49a36

Browse files
authored
[Refactor/#44] sceneWidth, sceneHeight를 EnvironmentKey로 사용할 수 있도록 변경한다 (#48)
1 parent 1aa65e6 commit ff49a36

5 files changed

Lines changed: 29 additions & 23 deletions

File tree

DevLog/UI/Extension/EnvironmentValues+.swift

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,18 @@
88
import SwiftUI
99

1010
extension EnvironmentValues {
11-
1211
var safeAreaInsets: EdgeInsets {
1312
self[SafeAreaInsetsKey.self]
1413
}
1514

15+
var sceneWidth: CGFloat {
16+
self[SceneWidthKey.self]
17+
}
18+
19+
var sceneHeight: CGFloat {
20+
self[SceneHeightKey.self]
21+
}
22+
1623
private struct SafeAreaInsetsKey: EnvironmentKey {
1724
static var defaultValue: EdgeInsets {
1825
guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
@@ -22,6 +29,24 @@ extension EnvironmentValues {
2229
return window.safeAreaInsets.insets
2330
}
2431
}
32+
33+
private struct SceneWidthKey: EnvironmentKey {
34+
static var defaultValue: CGFloat {
35+
guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene else {
36+
return UIScreen.main.bounds.width
37+
}
38+
return windowScene.screen.bounds.width
39+
}
40+
}
41+
42+
private struct SceneHeightKey: EnvironmentKey {
43+
static var defaultValue: CGFloat {
44+
guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene else {
45+
return UIScreen.main.bounds.height
46+
}
47+
return windowScene.screen.bounds.height
48+
}
49+
}
2550
}
2651

2752
extension UIEdgeInsets {

DevLog/UI/Extension/View+.swift

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,6 @@
88
import SwiftUI
99

1010
extension View {
11-
var sceneWidth: CGFloat {
12-
guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene
13-
else { return UIScreen.main.bounds.width }
14-
15-
return windowScene.screen.bounds.width
16-
}
17-
18-
var sceneHeight: CGFloat {
19-
guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene
20-
else { return UIScreen.main.bounds.height }
21-
22-
return windowScene.screen.bounds.height
23-
}
24-
25-
var safeAreaInsets: UIEdgeInsets {
26-
guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
27-
let window = windowScene.windows.first
28-
else { return UIEdgeInsets.zero }
29-
30-
return window.safeAreaInsets
31-
}
32-
3311
@ViewBuilder
3412
func adaptiveButtonStyle(_ color: Color? = nil) -> some View {
3513
if #available(iOS 26.0, *), color == nil {

DevLog/UI/Home/HomeView.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import SwiftUI
99

1010
struct HomeView: View {
1111
@Environment(\.diContainer) var container: any DIContainer
12+
@Environment(\.sceneWidth) var sceneWidth: CGFloat
1213
@StateObject private var router = NavigationRouter()
1314
@StateObject var viewModel: HomeViewModel
1415

DevLog/UI/Home/TodoEditorView.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import SwiftUI
1111

1212
struct TodoEditorView: View {
1313
@StateObject var viewModel: TodoEditorViewModel
14+
@Environment(\.safeAreaInsets) private var safeAreaInsets
1415
@Environment(\.dismiss) private var dismiss
1516
@FocusState private var field: Field?
1617
@State private var showDueDatePicker: Bool = false

DevLog/UI/Login/LoginView.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import SwiftUI
1010
struct LoginView: View {
1111
@StateObject var viewModel: LoginViewModel
1212
@Environment(\.colorScheme) var colorScheme
13+
@Environment(\.sceneWidth) var sceneWidth
1314

1415
var body: some View {
1516
ZStack {

0 commit comments

Comments
 (0)