File size: 4,437 Bytes
f0ccdab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import SwiftUI
import UIKit

struct RootView: View {
    @State private var tab: Tab = .device
    @StateObject private var bluetooth = BluetoothViewModel()
    @StateObject private var keyboard = KeyboardState()

    var body: some View {
        ZStack {
            Color.white.ignoresSafeArea()
            Group {
                switch tab {
                case .device: DeviceScreen(model: bluetooth)
                case .camera: CameraScreen(model: bluetooth)
                case .stream: StreamScreen(model: bluetooth)
                case .system: SystemScreen(model: bluetooth)
                case .console: ConsoleScreen(model: bluetooth)
                }
            }
            .environment(\.keyboardVisible, keyboard.isVisible)
            VStack {
                Spacer(minLength: 0)
                if !keyboard.isVisible {
                    TabBarView(active: $tab)
                        .padding(.horizontal, 12)
                        .padding(.bottom, 12)
                        .transition(.move(edge: .bottom).combined(with: .opacity))
                }
            }
            .frame(maxWidth: .infinity, maxHeight: .infinity)
            .ignoresSafeArea(.container, edges: .bottom)
        }
        .animation(.easeOut(duration: 0.18), value: keyboard.isVisible)
        .onAppear {
            bluetooth.setCameraTabForeground(tab == .camera)
        }
        .onChange(of: tab) { nextTab in
            bluetooth.setCameraTabForeground(nextTab == .camera)
        }
    }
}

final class KeyboardState: ObservableObject {
    @Published var isVisible = false
    private var observers: [NSObjectProtocol] = []

    init() {
        let center = NotificationCenter.default
        observers.append(center.addObserver(forName: UIResponder.keyboardWillShowNotification, object: nil, queue: .main) { [weak self] _ in
            self?.isVisible = true
        })
        observers.append(center.addObserver(forName: UIResponder.keyboardWillHideNotification, object: nil, queue: .main) { [weak self] _ in
            self?.isVisible = false
        })
    }

    deinit {
        observers.forEach(NotificationCenter.default.removeObserver)
    }
}

struct TabBarView: View {
    @Binding var active: Tab

    var body: some View {
        HStack(spacing: 4) {
            ForEach(Tab.allCases, id: \.self) { t in
                Button {
                    active = t
                } label: {
                    VStack(spacing: 4) {
                        Image(systemName: iconName(t))
                            .font(.system(size: 18, weight: .semibold))
                            .foregroundColor(active == t ? .white : AppColor.muted)
                        Text(t.label)
                            .font(.system(size: 11, weight: active == t ? .semibold : .medium))
                            .foregroundColor(active == t ? .white : AppColor.muted)
                    }
                    .padding(.vertical, 10)
                    .padding(.horizontal, 8)
                    .frame(minHeight: 58)
                    .frame(maxWidth: .infinity)
                    .background(
                        Group {
                            if active == t {
                                LinearGradient(colors: [Color(hex: 0x28473A), Color(hex: 0x1F3A2A)], startPoint: .top, endPoint: .bottom)
                            } else {
                                Color.clear
                            }
                        }
                    )
                    .clipShape(RoundedRectangle(cornerRadius: 22))
                }
            }
        }
        .padding(8)
        .background(Color.white.opacity(0.85))
        .overlay(
            RoundedRectangle(cornerRadius: 30)
                .stroke(Color.white.opacity(0.8), lineWidth: 1)
        )
        .clipShape(RoundedRectangle(cornerRadius: 30))
        .shadow(color: Color(hex: 0x0F2A1D).opacity(0.16), radius: 44, x: 0, y: 14)
    }

    private func iconName(_ t: Tab) -> String {
        switch t {
        case .device: return "bolt.fill"
        case .camera: return "camera"
        case .stream: return "dot.radiowaves.left.and.right"
        case .system: return "square.grid.2x2"
        case .console: return "chevron.left.forwardslash.chevron.right"
        }
    }
}