簡體   English   中英

正確使用@EnvironmentObject

[英]Using @EnvironmentObject properly

在我的應用程序從藍牙核心接收到新值后,我只是試圖讓我的更新值顯示在我的應用程序中。 我知道 class 中的值正在正確更新,因為我可以在打印語句中看到它。 我的問題是我的@EnvironmentObject 本身沒有在視圖中更新,所以它總是顯示“No Value Yet”

任何幫助將不勝感激 我是 IOS 的新手!

import SwiftUI
import CoreBluetooth
import UIKit

let weightScaleCBUUID = CBUUID(string: "0x181D")
let massPoundCharacteristicCBUUID = CBUUID(string: "0x27B8")


class HRMViewController: UIViewController, ObservableObject  {
    
    @IBOutlet weak var bodySensorLocationLabel: UILabel!
    var centralManager: CBCentralManager!
    var weightPeripheral: CBPeripheral!
    @Published var willWork: String = "No Value Yet"
    override func viewDidLoad() {
        super.viewDidLoad()
        
        centralManager = CBCentralManager(delegate: self, queue: nil)
        // Make the digits monospaces to avoid shifting when the numbers change
    }
    
}

extension HRMViewController: CBCentralManagerDelegate {
    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        switch central.state {
        case .unknown:
            print("central.state is .unknown")
        case .resetting:
            print("central.state is .resetting")
        case .unsupported:
            print("central.state is .unsupported")
        case .unauthorized:
            print("central.state is .unauthorized")
        case .poweredOff:
            print("central.state is .poweredOff")
        case .poweredOn:
            print("central.state is .poweredOn")
            centralManager.scanForPeripherals(withServices: [weightScaleCBUUID])
        @unknown default:
            print("Something is completely wrong here")
        }
    }
    
    func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral,
                        advertisementData: [String: Any], rssi RSSI: NSNumber) {
        print(peripheral)
        
        // copy the peripheral instance
        weightPeripheral = peripheral
        weightPeripheral.delegate = self
        
        centralManager.stopScan()
        centralManager.connect(weightPeripheral)
        
    }
    
    func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
        if peripheral == weightPeripheral {
            print("Connected!")
            weightPeripheral.discoverServices([weightScaleCBUUID])
        } else {
            print("Something went wrong in didConnect")
        }
        
    }
    
    
}

extension HRMViewController: CBPeripheralDelegate {
    func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
        guard let services = peripheral.services else { return }
        for service in services {
            print(service)
            peripheral.discoverCharacteristics([massPoundCharacteristicCBUUID], for: service)
            
        }
    }
    func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic,
                    error: Error?) {
        switch characteristic.uuid {
        case massPoundCharacteristicCBUUID:
            if let CurrWeightValue = String(bytes: characteristic.value![1...6], encoding: .utf8) {
//                print(CurrWeightValue)
                willWork = CurrWeightValue
                print(willWork)
                //        bodySensorLocationLabel!.text = CurrWeightValue
            } else {
                print("not a valid UTF-8 sequence")
            }
            
            //      let weightValue = weightHelper(from: characteristic)
            //      // print(weightValue)
            //      peripheral.setNotifyValue(true, for: characteristic)
            //      bodySensorLocationLabel.text = weightValue
        default:
            print("Unhandled Characteristic UUID: \(characteristic.uuid)")
        }
    }
    
    func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService,
                    error: Error?) {
        guard let characteristics = service.characteristics else { return }
        
        for characteristic in characteristics {
            if characteristic.properties.contains(.notify) {
                print("\(characteristic.uuid): properties contains .notify")
                peripheral.setNotifyValue(true, for: characteristic)
            }
        }
    }
    
}

struct HRMViewControllerRepresented: UIViewControllerRepresentable {
    
    var centralManager: CBCentralManager!
    var weightPeripheral: CBPeripheral!
    weak var bodySensorLocationLabel: UILabel!
    var willWork = ""
    func makeUIViewController(context: Context) -> HRMViewController {
        return HRMViewController()
    }
    
    func updateUIViewController(_ uiViewController: HRMViewController, context: Context) {
        uiViewController.centralManager = centralManager
        uiViewController.weightPeripheral = weightPeripheral
        uiViewController.bodySensorLocationLabel = bodySensorLocationLabel
        uiViewController.willWork = willWork
        uiViewController.viewDidLoad()
    }
}


struct SessionsView: View {
    @EnvironmentObject var userController: HRMViewController
    var body: some View {
        VStack{
            Text("Howdy Number \(userController.willWork)").padding()
            HRMViewControllerRepresented()
            
        }
            
        
        
    }
}

struct SessionsView_Previews: PreviewProvider {
    static var previews: some View {
        SessionsView().environmentObject(HRMViewController())
    }
}
struct ContentView: View {
    var body: some View {
        TabView {
            HomeView()
                .tabItem() {
                    Image("home-icon")
                        .renderingMode(.template)
                    Text("Home")
                }
            SessionsView().environmentObject(HRMViewController())
                .tabItem() {
                    Image("sessions-icon")
                        .renderingMode(.template)
                    Text("Sessions")
                }
            ReportsView()
                .tabItem() {
                    Image("reports-icon")
                        .renderingMode(.template)
                    Text("Reports")
                }
            SettingsView()
                .tabItem() {
                    Image("settings-icon")
                        .renderingMode(.template)
                    Text("Settings")
                }
        }
        
        
    }
}

事實證明,一旦我開始實施此協議,它就非常簡單: 將變量從 UIViewController 傳遞到 SwiftUI 查看

這是我使用 MVVM 的最終實現:

    class SharedViewModel: ObservableObject {
        @Published var myString = "---"
        @Published var centralManager: CBCentralManager!
        @Published var weightPeripheral: CBPeripheral!
    }
    
    //Not an extension
    struct UIKitViewController_UI: UIViewControllerRepresentable {
        typealias UIViewControllerType = WSViewController
        var sharedVM: SharedViewModel
        
        func makeUIViewController(context: Context) -> WSViewController {
            return WSViewController(vm: sharedVM)
        }
        
        
        func updateUIViewController(_ uiViewController: WSViewController, context: Context) {
            
        }
        
    }

//My bluetooth Class
class WSViewController: UIViewController {
    let sharedVM: SharedViewModel
    var weightPeripheral: CBPeripheral!
    
    
    
    init(vm: SharedViewModel) {
        self.sharedVM = vm
        super.init(nibName: nil, bundle: nil)
        Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { timer in
            self.methodThatChangeValueOfString()
        }
    }
    ...

// then used in the view struct like this
struct SessionsView: View {
    @StateObject var sharedVM: SharedViewModel = SharedViewModel()
    @EnvironmentObject var myWeights: StoredWeights

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM