QRCodeDisplayView.swift 2.2 KB

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