QRCodeDisplayView.swift 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // LoopFollow
  2. // QRCodeDisplayView.swift
  3. import SwiftUI
  4. import UIKit
  5. struct QRCodeDisplayView: View {
  6. let qrCodeString: String
  7. let size: CGSize
  8. let foregroundColor: UIColor
  9. let backgroundColor: UIColor
  10. @State private var qrCodeImage: UIImage?
  11. init(
  12. qrCodeString: String,
  13. size: CGSize = CGSize(width: 250, height: 250),
  14. foregroundColor: UIColor = .black,
  15. backgroundColor: UIColor = .white
  16. ) {
  17. self.qrCodeString = qrCodeString
  18. self.size = size
  19. self.foregroundColor = foregroundColor
  20. self.backgroundColor = backgroundColor
  21. }
  22. var body: some View {
  23. VStack(spacing: 16) {
  24. if let qrCodeImage = qrCodeImage {
  25. Image(uiImage: qrCodeImage)
  26. .resizable()
  27. .aspectRatio(contentMode: .fit)
  28. .frame(width: size.width, height: size.height)
  29. .cornerRadius(12)
  30. .shadow(radius: 4)
  31. } else {
  32. RoundedRectangle(cornerRadius: 12)
  33. .fill(Color.gray.opacity(0.3))
  34. .frame(width: size.width, height: size.height)
  35. .overlay(
  36. ProgressView()
  37. .scaleEffect(1.5)
  38. )
  39. }
  40. Text("Scan this QR code with another LoopFollow app to import remote command settings")
  41. .font(.caption)
  42. .foregroundColor(.secondary)
  43. .multilineTextAlignment(.center)
  44. .padding(.horizontal, 20)
  45. }
  46. .onAppear {
  47. generateQRCode()
  48. }
  49. }
  50. private func generateQRCode() {
  51. DispatchQueue.global(qos: .userInitiated).async {
  52. let image = QRCodeGenerator.generateQRCode(
  53. from: qrCodeString,
  54. size: size,
  55. foregroundColor: foregroundColor,
  56. backgroundColor: backgroundColor
  57. )
  58. DispatchQueue.main.async {
  59. self.qrCodeImage = image
  60. }
  61. }
  62. }
  63. }
  64. #Preview {
  65. QRCodeDisplayView(qrCodeString: "https://example.com/test")
  66. .padding()
  67. }