BlueWallet/ios/BlueWalletWatch Extension/InterfaceController.swift

82 lines
2.5 KiB
Swift
Raw Normal View History

2019-05-02 16:33:03 -04:00
//
// InterfaceController.swift
// BlueWalletWatch Extension
//
// Created by Marcos Rodriguez on 3/6/19.
//
import WatchKit
import WatchConnectivity
import Foundation
2024-01-25 00:39:01 -04:00
class InterfaceController: WKInterfaceController, WCSessionDelegate {
2019-05-02 16:33:03 -04:00
@IBOutlet weak var walletsTable: WKInterfaceTable!
@IBOutlet weak var noWalletsAvailableLabel: WKInterfaceLabel!
2023-10-26 23:45:55 -04:00
override func awake(withContext context: Any?) {
2024-01-25 00:39:01 -04:00
setupSession()
2023-10-26 23:45:55 -04:00
}
2019-05-02 16:33:03 -04:00
override func willActivate() {
super.willActivate()
2024-01-25 00:39:01 -04:00
updateUI()
NotificationCenter.default.addObserver(self, selector: #selector(updateUI), name: WatchDataSource.NotificationName.dataUpdated, object: nil)
}
private func setupSession() {
guard WCSession.isSupported() else { return }
WCSession.default.delegate = self
WCSession.default.activate()
}
private func processContextData(_ context: Any?) {
guard let contextUnwrapped = context as? [String: Any] else { return }
WatchDataSource.shared.processData(data: contextUnwrapped)
2019-05-02 16:33:03 -04:00
}
2024-01-25 00:39:01 -04:00
@objc private func updateUI() {
let wallets = WatchDataSource.shared.wallets
let isEmpty = wallets.isEmpty
noWalletsAvailableLabel.setHidden(!isEmpty)
walletsTable.setHidden(isEmpty)
2019-05-02 16:33:03 -04:00
2024-01-25 00:39:01 -04:00
if isEmpty { return }
walletsTable.setNumberOfRows(wallets.count, withRowType: WalletInformation.identifier)
for index in 0..<wallets.count {
updateRow(at: index, with: wallets[index])
2019-05-02 16:33:03 -04:00
}
}
2024-01-25 00:39:01 -04:00
private func updateRow(at index: Int, with wallet: Wallet) {
guard let controller = walletsTable.rowController(at: index) as? WalletInformation else { return }
controller.configure(with: wallet)
2019-05-02 16:33:03 -04:00
}
2024-01-25 00:39:01 -04:00
override func contextForSegue(withIdentifier segueIdentifier: String, in table: WKInterfaceTable, rowIndex: Int) -> Any? {
return rowIndex
}
2023-10-26 23:45:55 -04:00
2023-10-30 12:38:12 -04:00
func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {
2023-10-26 23:45:55 -04:00
WatchDataSource.shared.processData(data: applicationContext)
}
2024-06-15 13:05:54 -04:00
2023-10-30 12:38:12 -04:00
2023-10-26 23:45:55 -04:00
func session(_ session: WCSession, didReceiveUserInfo userInfo: [String : Any] = [:]) {
WatchDataSource.shared.processData(data: userInfo)
}
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
if activationState == .activated {
2024-06-15 13:05:54 -04:00
WatchDataSource.shared.loadKeychainData()
2023-10-26 23:45:55 -04:00
}
}
2023-10-30 12:38:12 -04:00
func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {
WatchDataSource.shared.processData(data: message)
}
2019-05-02 16:33:03 -04:00
}