|
| 1 | +// |
| 2 | +// ViewController.swift |
| 3 | +// TrackerApp |
| 4 | +// |
| 5 | +// Created by ezen on 08/08/2024. |
| 6 | +// |
| 7 | + |
| 8 | +import UIKit |
| 9 | +import SwiftUI |
| 10 | + |
| 11 | +class MainViewController: UIViewController { |
| 12 | + var vm: MainViewModel! |
| 13 | + |
| 14 | + override func viewDidLoad() { |
| 15 | + super.viewDidLoad() |
| 16 | + |
| 17 | + self.vm = MainViewModel(host: self) |
| 18 | + |
| 19 | + let _view = MainView(vm: self.vm) |
| 20 | + |
| 21 | + let _vc = UIHostingController(rootView: _view) |
| 22 | + _vc.view.translatesAutoresizingMaskIntoConstraints = false |
| 23 | + _vc.view.backgroundColor = .clear |
| 24 | + |
| 25 | + addChild(_vc) |
| 26 | + view.addSubview(_vc.view) |
| 27 | + _vc.didMove(toParent: self) |
| 28 | + |
| 29 | + NSLayoutConstraint.activate([ |
| 30 | + NSLayoutConstraint(item: _vc.view!, attribute: .top, relatedBy: .equal, toItem: view.safeAreaLayoutGuide, attribute: .top, multiplier: 1, constant: 0), |
| 31 | + NSLayoutConstraint(item: _vc.view!, attribute: .bottom, relatedBy: .equal, toItem: view.safeAreaLayoutGuide, attribute: .bottom, multiplier: 1, constant: 0), |
| 32 | + NSLayoutConstraint(item: _vc.view!, attribute: .leading, relatedBy: .equal, toItem: view.safeAreaLayoutGuide, attribute: .leading, multiplier: 1, constant: 0), |
| 33 | + NSLayoutConstraint(item: _vc.view!, attribute: .trailing, relatedBy: .equal, toItem: view.safeAreaLayoutGuide, attribute: .trailing, multiplier: 1, constant: 0) |
| 34 | + ]) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +struct MainView: View { |
| 39 | + @StateObject var vm: MainViewModel |
| 40 | + |
| 41 | + var body: some View { |
| 42 | + ScrollView(showsIndicators: false) { |
| 43 | + LazyVStack(spacing: 15) { |
| 44 | + HStack(spacing: 3) { |
| 45 | + Text("Updates") |
| 46 | + .font(.headline) |
| 47 | + Rectangle() |
| 48 | + .fill(vm.isConnected ? .green : .red) |
| 49 | + .frame(width: 6, height: 6) |
| 50 | + .cornerRadius(3) |
| 51 | + .offset(CGSize(width: 0.0, height: -5.0)) |
| 52 | + Spacer() |
| 53 | + if vm.isConnected { |
| 54 | + Button("Connected") { |
| 55 | + // |
| 56 | + } |
| 57 | + .buttonStyle(BorderedButtonStyle()) |
| 58 | + .accentColor(.green) |
| 59 | + } else { |
| 60 | + Button("Connect") { |
| 61 | + vm.onConnect() |
| 62 | + } |
| 63 | + .buttonStyle(BorderedButtonStyle()) |
| 64 | + } |
| 65 | + } |
| 66 | + .frame(maxWidth: .infinity, alignment: .leading) |
| 67 | + .padding(15) |
| 68 | + .background(Color.white.opacity(0.1)) |
| 69 | + .cornerRadius(10) |
| 70 | + |
| 71 | + ForEach(Array(vm.data.enumerated()), id: \.0) { _, item in |
| 72 | + Text(item) |
| 73 | + .frame(maxWidth: .infinity, alignment: .leading) |
| 74 | + .padding(15) |
| 75 | + .background(Color.white.opacity(0.05)) |
| 76 | + .cornerRadius(10) |
| 77 | + } |
| 78 | + .padding(.top, 15) |
| 79 | + |
| 80 | + Spacer() |
| 81 | + } |
| 82 | + .frame(maxWidth: .infinity) |
| 83 | + .padding(15) |
| 84 | + .padding(.bottom, 100) |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +#Preview { |
| 90 | + MainView(vm: MainViewModel(host: UIViewController())) |
| 91 | +} |
0 commit comments