Эх сурвалжийг харах

use core generated svg based QR-code

cyberta 3 жил өмнө
parent
commit
f0c8edb8b6

+ 10 - 1
DcCore/DcCore/DC/Wrapper.swift

@@ -288,7 +288,7 @@ public class DcContext {
         dc_marknoticed_chat(self.contextPointer, UInt32(chatId))
     }
 
-    public func getSecurejoinQr (chatId: Int) -> String? {
+    public func getSecurejoinQr(chatId: Int) -> String? {
         if let cString = dc_get_securejoin_qr(self.contextPointer, UInt32(chatId)) {
             let swiftString = String(cString: cString)
             dc_str_unref(cString)
@@ -297,6 +297,15 @@ public class DcContext {
         return nil
     }
 
+    public func getSecurejoinQrSVG(chatId: Int) -> String? {
+        if let cString = dc_get_securejoin_qr_svg(self.contextPointer, UInt32(chatId)) {
+            let swiftString = String(cString: cString)
+            dc_str_unref(cString)
+            return swiftString
+        }
+        return nil
+    }
+
     public func joinSecurejoin (qrCode: String) -> Int {
         return Int(dc_join_securejoin(contextPointer, qrCode))
     }

+ 3 - 1
deltachat-ios/AppDelegate.swift

@@ -7,6 +7,7 @@ import DcCore
 import DBDebugToolkit
 import SDWebImageWebPCoder
 import Intents
+import SDWebImageSVGKitPlugin
 
 let logger = SwiftyBeaver.self
 
@@ -117,7 +118,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
 
         let webPCoder = SDImageWebPCoder.shared
         SDImageCodersManager.shared.addCoder(webPCoder)
-
+        let svgCoder = SDImageSVGKCoder.shared
+        SDImageCodersManager.shared.addCoder(svgCoder)
         return true
     }
 

+ 17 - 5
deltachat-ios/Controller/QrViewController.swift

@@ -1,6 +1,7 @@
 import Foundation
 import UIKit
 import DcCore
+import SDWebImageSVGKitPlugin
 
 class QrViewController: UIViewController {
 
@@ -16,7 +17,8 @@ class QrViewController: UIViewController {
 
     private lazy var qrContentView: QrViewContentView = {
         let qrCode = dcContext.getSecurejoinQr(chatId: chatId)
-        let view = QrViewContentView(qrCode: qrCode, hint: qrCodeHint)
+        let svg = dcContext.getSecurejoinQrSVG(chatId: chatId)
+        let view = QrViewContentView(svg: svg, qrCode: qrCode, hint: qrCodeHint)
         view.translatesAutoresizingMaskIntoConstraints = false
         return view
     }()
@@ -24,7 +26,8 @@ class QrViewController: UIViewController {
     var qrCodeHint: String {
         willSet {
             let qrCode = dcContext.getSecurejoinQr(chatId: chatId)
-            qrContentView.update(qrCode: qrCode, hint: newValue)
+            let svg = dcContext.getSecurejoinQrSVG(chatId: chatId)
+            qrContentView.update(svg: svg, qrCode: qrCode, hint: newValue)
         }
     }
     private let chatId: Int
@@ -118,9 +121,9 @@ class QrViewContentView: UIView {
         return container.heightAnchor.constraint(greaterThanOrEqualToConstant: 0)
     }()
 
-    init(qrCode: String?, hint: String) {
+    init(svg: String?, qrCode: String?, hint: String) {
         super.init(frame: .zero)
-        update(qrCode: qrCode, hint: hint)
+        update(svg: svg, qrCode: qrCode, hint: hint)
         setupSubviews()
     }
 
@@ -129,7 +132,15 @@ class QrViewContentView: UIView {
     }
 
     // MARK: - update
-    func update(qrCode: String?, hint: String?) {
+    func update(svg: String?, qrCode: String?, hint: String?) {
+        if let svg = svg {
+            let svgData = svg.data(using: .utf8)
+            let image = SDImageSVGKCoder.shared.decodedImage(with: svgData, options: [:])
+            qrCodeView.imageView.image = image
+            hintLabel.isHidden = true
+            return
+        }
+   
         if let qrCode = qrCode {
             qrCodeView.generateCode(
                 qrCode,
@@ -137,6 +148,7 @@ class QrViewContentView: UIView {
                 backgroundColor: .white
             )
         }
+        hintLabel.isHidden = false
         hintLabel.text = hint
     }