Meshtastic-Apple/MeshtasticClient/Views/Settings/ShareChannel.swift

96 lines
2.4 KiB
Swift
Raw Normal View History

2022-04-11 15:58:11 -07:00
//
// ShareChannel.swift
2022-04-11 15:58:11 -07:00
// MeshtasticClient
//
// Created by Garth Vander Houwen on 4/8/22.
//
import SwiftUI
import CoreData
2022-04-19 22:56:59 -07:00
import CoreImage.CIFilterBuiltins
2022-04-11 15:58:11 -07:00
2022-04-19 22:56:59 -07:00
struct QrCodeImage {
let context = CIContext()
func generateQRCode(from text: String) -> UIImage {
var qrImage = UIImage(systemName: "xmark.circle") ?? UIImage()
let data = Data(text.utf8)
let filter = CIFilter.qrCodeGenerator()
filter.setValue(data, forKey: "inputMessage")
let transform = CGAffineTransform(scaleX: 20, y: 20)
2022-04-19 22:56:59 -07:00
if let outputImage = filter.outputImage?.transformed(by: transform) {
if let image = context.createCGImage(
outputImage,
from: outputImage.extent) {
qrImage = UIImage(cgImage: image)
}
}
return qrImage
}
}
2022-04-11 15:58:11 -07:00
struct ShareChannel: View {
@Environment(\.managedObjectContext) var context
@EnvironmentObject var bleManager: BLEManager
@EnvironmentObject var userSettings: UserSettings
2022-04-19 22:56:59 -07:00
let channelSet = ChannelSet()
2022-04-11 15:58:11 -07:00
@State private var text = "https://www.meshtastic.org/e/#"
2022-04-19 22:56:59 -07:00
var qrCodeImage = QrCodeImage()
2022-04-11 15:58:11 -07:00
var body: some View {
2022-04-19 22:56:59 -07:00
VStack {
2022-04-11 15:58:11 -07:00
GeometryReader { bounds in
2022-04-19 22:56:59 -07:00
let smallest = min(bounds.size.width, bounds.size.height)
2022-04-11 15:58:11 -07:00
ScrollView {
2022-04-19 22:56:59 -07:00
2022-04-11 15:58:11 -07:00
VStack {
2022-04-19 22:56:59 -07:00
Text("Scan the QR code below with the Apple or Android device you would like to share with your channel settings with.")
.fixedSize(horizontal: false, vertical: true)
.font(.callout)
.padding()
Spacer()
2022-04-11 15:58:11 -07:00
2022-04-19 22:56:59 -07:00
let image = qrCodeImage.generateQRCode(from: text)
Image(uiImage: image)
.resizable()
.scaledToFit()
.frame(
2022-04-11 15:58:11 -07:00
minWidth: smallest * 0.9,
maxWidth: smallest * 0.9,
minHeight: smallest * 0.9,
maxHeight: smallest * 0.9,
2022-04-19 22:56:59 -07:00
alignment: .center
2022-04-11 15:58:11 -07:00
)
Spacer()
2022-04-19 22:56:59 -07:00
Text("Channel Name (Long/Slow)").font(.title)
Spacer()
2022-04-11 15:58:11 -07:00
}
2022-04-19 22:56:59 -07:00
.frame(width: bounds.size.width, height: bounds.size.height)
2022-04-11 15:58:11 -07:00
}
2022-04-19 22:56:59 -07:00
}
.navigationTitle("Share Channel")
.navigationBarTitleDisplayMode(.automatic)
.navigationBarItems(trailing:
2022-04-11 15:58:11 -07:00
2022-04-19 22:56:59 -07:00
ZStack {
2022-04-11 15:58:11 -07:00
ConnectedDevice(bluetoothOn: bleManager.isSwitchedOn, deviceConnected: bleManager.connectedPeripheral != nil, name: (bleManager.connectedPeripheral != nil) ? bleManager.connectedPeripheral.lastFourCode : "????")
2022-04-19 22:56:59 -07:00
})
.onAppear {
2022-04-11 15:58:11 -07:00
2022-04-19 22:56:59 -07:00
self.bleManager.context = context
}
2022-04-11 15:58:11 -07:00
}
.navigationViewStyle(StackNavigationViewStyle())
}
}