Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

[IB2-43] Imported Connect feature

  • Loading branch information
mahmoud-adam85 authored and naira-cliqz committed Apr 12, 2018
1 parent 0f26e7c commit b664886263e6a5a26963246193bb70713c0e64bf
@@ -45,7 +45,12 @@
19DE1F671EC13B6400428B8C /* LeanplumIntegration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19DE1F661EC13B6400428B8C /* LeanplumIntegration.swift */; };
1E3CBC152057D83700898B05 /* CliqzAppSettingsTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E3CBC142057D83700898B05 /* CliqzAppSettingsTableViewController.swift */; };
1E3CBC1D205806F000898B05 /* DateExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E3CBC1C205806F000898B05 /* DateExtension.swift */; };
1E3FBF59207763FE00FAE3FD /* BrowserViewController+Connect.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E3FBF58207763FE00FAE3FD /* BrowserViewController+Connect.swift */; };
1E4CFBE7207380AD00B26E85 /* ConnectTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E4CFBE6207380AD00B26E85 /* ConnectTableViewController.swift */; };
1E4CFBF92073A5BD00B26E85 /* AddConnectionViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E4CFBF82073A5BD00B26E85 /* AddConnectionViewController.swift */; };
1E4CFBFB2073A5CC00B26E85 /* EditConnectionViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E4CFBFA2073A5CC00B26E85 /* EditConnectionViewController.swift */; };
1E7B7688206A8AC000FD0BA5 /* CliqzTabToolbar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E7B7687206A8AC000FD0BA5 /* CliqzTabToolbar.swift */; };
1E7B76AF206B9CB500FD0BA5 /* ConnectDataSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E7B76AE206B9CB500FD0BA5 /* ConnectDataSource.swift */; };
1EAFE4A3205934C8006A36B4 /* CliqzAppSettingsOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E3CBC162058057700898B05 /* CliqzAppSettingsOptions.swift */; };
1EAFE4F0205BDEC0006A36B4 /* cliqz.json in Resources */ = {isa = PBXBuildFile; fileRef = 1EAFE4EF205BDEC0006A36B4 /* cliqz.json */; };
1EAFE524205FBBBC006A36B4 /* CliqzLaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 1EAFE515205FBBBC006A36B4 /* CliqzLaunchScreen.xib */; };
@@ -1492,7 +1497,12 @@
1E3CBC142057D83700898B05 /* CliqzAppSettingsTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CliqzAppSettingsTableViewController.swift; sourceTree = "<group>"; };
1E3CBC162058057700898B05 /* CliqzAppSettingsOptions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CliqzAppSettingsOptions.swift; sourceTree = "<group>"; };
1E3CBC1C205806F000898B05 /* DateExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DateExtension.swift; sourceTree = "<group>"; };
1E3FBF58207763FE00FAE3FD /* BrowserViewController+Connect.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "BrowserViewController+Connect.swift"; sourceTree = "<group>"; };
1E4CFBE6207380AD00B26E85 /* ConnectTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConnectTableViewController.swift; sourceTree = "<group>"; };
1E4CFBF82073A5BD00B26E85 /* AddConnectionViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddConnectionViewController.swift; sourceTree = "<group>"; };
1E4CFBFA2073A5CC00B26E85 /* EditConnectionViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditConnectionViewController.swift; sourceTree = "<group>"; };
1E7B7687206A8AC000FD0BA5 /* CliqzTabToolbar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CliqzTabToolbar.swift; sourceTree = "<group>"; };
1E7B76AE206B9CB500FD0BA5 /* ConnectDataSource.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConnectDataSource.swift; sourceTree = "<group>"; };
1EAFE4EF205BDEC0006A36B4 /* cliqz.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = cliqz.json; path = "node_modules/browser-core/build/config/cliqz.json"; sourceTree = SOURCE_ROOT; };
1EAFE515205FBBBC006A36B4 /* CliqzLaunchScreen.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = CliqzLaunchScreen.xib; sourceTree = "<group>"; };
1EAFE55A205FFE21006A36B4 /* LocalResourceSetting.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LocalResourceSetting.swift; sourceTree = "<group>"; };
@@ -2561,6 +2571,17 @@
path = Settings;
sourceTree = "<group>";
};
1E7B769F206B9C4900FD0BA5 /* Connect */ = {
isa = PBXGroup;
children = (
1E7B76AE206B9CB500FD0BA5 /* ConnectDataSource.swift */,
1E4CFBE6207380AD00B26E85 /* ConnectTableViewController.swift */,
1E4CFBF82073A5BD00B26E85 /* AddConnectionViewController.swift */,
1E4CFBFA2073A5CC00B26E85 /* EditConnectionViewController.swift */,
);
path = Connect;
sourceTree = "<group>";
};
1EAFE54B205FFD9E006A36B4 /* Base Classes */ = {
isa = PBXGroup;
children = (
@@ -3216,13 +3237,15 @@
4F30F52620517C210049E4F6 /* SearchViewDelegate.swift */,
4FD19FC4205A9F7700547DD5 /* NativeContextMenu.swift */,
1EEA36EC2067DD6F003B6AD5 /* VideoDownloader.swift */,
1E3FBF58207763FE00FAE3FD /* BrowserViewController+Connect.swift */,
);
path = BrowserViewController;
sourceTree = "<group>";
};
4F8F3C0D202CB39F001C1A0F /* Cliqz */ = {
isa = PBXGroup;
children = (
1E7B769F206B9C4900FD0BA5 /* Connect */,
4FF07D712063C754001385A8 /* MyOffrz */,
AF6FA95E20652C1100D7F9F0 /* Privacy */,
AFE484C32056836400554B2E /* URLBar */,
@@ -4910,7 +4933,7 @@
LastSwiftMigration = 0700;
LastSwiftUpdateCheck = 0830;
LastUpgradeCheck = 0800;
ORGANIZATIONNAME = Mozilla;
ORGANIZATIONNAME = Cliqz;
TargetAttributes = {
2827315D1ABC9BE600AA1954 = {
CreatedOnToolsVersion = 6.2;
@@ -6191,6 +6214,7 @@
AF53D3BC206A346B0012A11F /* TabExtension.swift in Sources */,
1EAFE563205FFE21006A36B4 /* ShowCliqzPageSetting.swift in Sources */,
E689C6FA1E0C6E98008BAADB /* FxAContentViewController.swift in Sources */,
1E4CFBF92073A5BD00B26E85 /* AddConnectionViewController.swift in Sources */,
C4E3983D1D21F1E7004E89BA /* TopTabsViews.swift in Sources */,
3B0943811D6CC4FC004F24E1 /* FilledPageControl.swift in Sources */,
FA9293D41D6580E100AC8D33 /* QRCodeViewController.swift in Sources */,
@@ -6204,6 +6228,7 @@
0BF1B7E31AC60DEA00A7B407 /* InsetButton.swift in Sources */,
D0C95EF6201A55A800E4E51C /* BrowserViewController+UIDropInteractionDelegate.swift in Sources */,
D31CF65C1CC1959A001D0BD0 /* PrivilegedRequest.swift in Sources */,
1E3FBF59207763FE00FAE3FD /* BrowserViewController+Connect.swift in Sources */,
D8D33A7D1FBD080300A20A28 /* SnapKitExtensions.swift in Sources */,
4FF07D862063C99E001385A8 /* OffrzViewController.swift in Sources */,
E650755C1E37F747006961AC /* Swizzling.m in Sources */,
@@ -6220,12 +6245,14 @@
4FF07D8B2063CA46001385A8 /* OffrzDataService.swift in Sources */,
0B1C05D71A798B1F004C78B0 /* UIImageViewAligned.m in Sources */,
0BB5B30B1AC0AD1F0052877D /* LoginsHelper.swift in Sources */,
1E4CFBE7207380AD00B26E85 /* ConnectTableViewController.swift in Sources */,
1EEA36D72063F87C003B6AD5 /* URLExtension.swift in Sources */,
E69E06C91C76198000D0F926 /* AuthenticationManagerConstants.swift in Sources */,
392ED7E61D0AEFEF009D9B62 /* HomePageAccessors.swift in Sources */,
AF6FA99220652D4600D7F9F0 /* TrieNode.swift in Sources */,
4F30F5152051670C0049E4F6 /* SubscriptionModule.swift in Sources */,
4FD19FC5205A9F7700547DD5 /* NativeContextMenu.swift in Sources */,
1E4CFBFB2073A5CC00B26E85 /* EditConnectionViewController.swift in Sources */,
7BA8D1C71BA037F500C8AE9E /* OpenInHelper.swift in Sources */,
E4B423BE1AB9FE6A007E66C8 /* ReaderModeCache.swift in Sources */,
4F30F5172051670C0049E4F6 /* Telemetry.swift in Sources */,
@@ -6353,6 +6380,7 @@
E633E2DA1C21EAF8001FFF6C /* LoginDetailViewController.swift in Sources */,
59A68B280D62462B85CF57A4 /* HistoryPanel.swift in Sources */,
1EAFE56C205FFE82006A36B4 /* LimitMobileDataUsageTableViewController.swift in Sources */,
1E7B76AF206B9CB500FD0BA5 /* ConnectDataSource.swift in Sources */,
19DE1F671EC13B6400428B8C /* LeanplumIntegration.swift in Sources */,
E65D89181C8647420006EA35 /* AppAuthenticator.swift in Sources */,
1EEA36D52063E105003B6AD5 /* FileManagerExtension.swift in Sources */,
"$(PROJECT_DIR)/Cliqz/Third-Party/KKDomain/Carthage/Build/Mac",
"$(PROJECT_DIR)/Cliqz/Third-Party/KKDomain/Carthage/Build/tvOS",
"$(PROJECT_DIR)/Cliqz/Third-Party/KKDomain/Carthage/Build/watchOS",
"$(PROJECT_DIR)/node_modules/react-native-webrtc/ios",
);
HEADER_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)/Cliqz/Third-Party/KKDomain/Carthage/Build/Mac",
"$(PROJECT_DIR)/Cliqz/Third-Party/KKDomain/Carthage/Build/tvOS",
"$(PROJECT_DIR)/Cliqz/Third-Party/KKDomain/Carthage/Build/watchOS",
"$(PROJECT_DIR)/node_modules/react-native-webrtc/ios",
);
HEADER_SEARCH_PATHS = (
"$(inherited)",
@@ -349,6 +349,9 @@ class BrowserViewController: UIViewController {
urlBar = CliqzURLBar()
NotificationCenter.default.addObserver(self, selector: #selector(urlBarDidPressPageCliqzOptions), name: URLBarDidPressPageOptionsNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(urlBarDidPressVideoDownload), name: URLBarDidPressVideoDownloadNotification, object: nil)
// Cliqz: Add observers for Connection features
NotificationCenter.default.addObserver(self, selector: #selector(openTabViaConnect), name: SendTabNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(downloadVideoViaConnect), name: DownloadVideoNotification, object: nil)

urlBar.translatesAutoresizingMaskIntoConstraints = false
urlBar.delegate = self
@@ -0,0 +1,195 @@
//
// AddConnectionViewController.swift
// Client
//
// Created by Mahmoud Adam on 4/3/18.
// Copyright © 2018 Cliqz. All rights reserved.
//
import UIKit
import AVFoundation

class AddConnectionViewController: UIViewController {

fileprivate var dataSource: ConnectDataSource
fileprivate var captureSession:AVCaptureSession?
fileprivate var videoPreviewLayer:AVCaptureVideoPreviewLayer?
fileprivate var qrCodeFrameView:UIView?
fileprivate let instructionsLabel = UILabel()
fileprivate var qrCodeScannerReady = true
fileprivate var qrCodeScanned = false
fileprivate let supportedBarCodes = [AVMetadataObjectTypeQRCode]

init(_ dataSource: ConnectDataSource) {
self.dataSource = dataSource
super.init(nibName: nil, bundle: nil)
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func viewDidLoad() {
super.viewDidLoad()
self.title = NSLocalizedString("Scan QR-Code", tableName: "Cliqz", comment: "[Settings -> Connect] Scan QR-Code")
configureInstructionLabel()
configureScanningView()
}

override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {
adjustVideoPreviewLayerFrame()
}


// MARK: - Private Helpers
private func configureInstructionLabel() {
let attachment = NSTextAttachment()
attachment.image = UIImage(named: "connectDesktopMenu")
let attachmentString = NSAttributedString(attachment: attachment)

let attributedString = NSMutableAttributedString(string: NSLocalizedString("Go to ", tableName: "Cliqz", comment: "[Connect] instructions text part#1"))
attributedString.append(attachmentString)
attributedString.append(NSAttributedString(string: NSLocalizedString(" and select connect to scan the QR-Code", tableName: "Cliqz", comment: "[Connect] instructions text part#2")))

instructionsLabel.attributedText = attributedString
instructionsLabel.numberOfLines = 2
self.view.addSubview(instructionsLabel)

let margin = 15
instructionsLabel.snp.makeConstraints { make in
make.leading.equalTo(self.view).offset(margin)
make.trailing.equalTo(self.view).offset(-1 * margin)
make.bottom.equalTo(self.view).offset(-2 * margin)
}
}

private func configureScanningView() {
// Get an instance of the AVCaptureDevice class to initialize a device object and provide the video
// as the media type parameter.
let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)

do {
// Get an instance of the AVCaptureDeviceInput class using the previous device object.
let input = try AVCaptureDeviceInput(device: captureDevice)

// Initialize the captureSession object.
captureSession = AVCaptureSession()
// Set the input device on the capture session.
captureSession?.addInput(input)

// Initialize a AVCaptureMetadataOutput object and set it as the output device to the capture session.
let captureMetadataOutput = AVCaptureMetadataOutput()
captureSession?.addOutput(captureMetadataOutput)

// Set delegate and use the default dispatch queue to execute the call back
captureMetadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)

// Detect all the supported bar code
captureMetadataOutput.metadataObjectTypes = supportedBarCodes

// Initialize the video preview layer and add it as a sublayer to the viewPreview view's layer.
videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
adjustVideoPreviewLayerFrame()
view.layer.addSublayer(videoPreviewLayer!)

// Start video capture
captureSession?.startRunning()

// Initialize QR Code Frame to highlight the QR code
qrCodeFrameView = UIView()

if let qrCodeFrameView = qrCodeFrameView {
qrCodeFrameView.layer.borderColor = UIColor.green.cgColor
qrCodeFrameView.layer.borderWidth = 2
view.addSubview(qrCodeFrameView)
view.bringSubview(toFront: qrCodeFrameView)
}

} catch {
showAllowCameraAccessAlert()
return
}
}

private func showAllowCameraAccessAlert() {
let settingsOptionTitle = NSLocalizedString("Settings", tableName: "Cliqz", comment: "Settings option for turning on Camera access")
let message = NSLocalizedString("Cliqz Browser does not have access to your camera. To enable access, tap ‘Settings’ and turn on camera.", tableName: "Cliqz", comment: "[Connect] Alert message for turning on Camera access when Scanning QR Code")
let settingsAction = UIAlertAction(title: settingsOptionTitle, style: .default) { (_) -> Void in
if let settingsUrl = URL(string: UIApplicationOpenSettingsURLString) {
UIApplication.shared.open(settingsUrl, options: [:])
}
}

let title = NSLocalizedString("Allow Camera Access", tableName: "Cliqz", comment: "[Connect] Alert title for turning on Camera access when scanning QRCode")
let alertController = UIAlertController (title: title, message: message, preferredStyle: .alert)

let notNowOptionTitle = NSLocalizedString("Not Now", tableName: "Cliqz", comment: "Not now option for turning on Camera access")
let cancelAction = UIAlertAction(title: notNowOptionTitle, style: .default, handler: nil)

alertController.addAction(cancelAction)
alertController.addAction(settingsAction)

present(alertController, animated: true, completion: nil)

}

private func adjustVideoPreviewLayerFrame() {
let width = view.frame.width
let height = view.frame.height * 0.8

videoPreviewLayer?.frame = CGRect(x: 0, y: 0, width: width, height: height)

switch UIDevice.current.orientation {
case .portrait:
videoPreviewLayer?.connection.videoOrientation = .portrait
case .landscapeLeft:
videoPreviewLayer?.connection.videoOrientation = .landscapeRight
case .landscapeRight:
videoPreviewLayer?.connection.videoOrientation = .landscapeLeft
default:
videoPreviewLayer?.connection.videoOrientation = .portrait
}
}
}

extension AddConnectionViewController : AVCaptureMetadataOutputObjectsDelegate {
//MARK: - AVCaptureMetadataOutputObjectsDelegate
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
guard qrCodeScannerReady == true else {
return
}
// Check if the metadataObjects array is not nil and it contains at least one object.
if metadataObjects == nil || metadataObjects.count == 0 {
qrCodeFrameView?.frame = CGRect.zero
return
}

// Get the metadata object.
let metadataObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject

// Here we use filter method to check if the type of metadataObj is supported
// Instead of hardcoding the AVMetadataObjectTypeQRCode, we check if the type
// can be found in the array of supported bar codes.
if supportedBarCodes.contains(metadataObj.type) {
// If the found metadata is equal to the QR code metadata then update the status label's text and set the bounds
let barCodeObject = videoPreviewLayer?.transformedMetadataObject(for: metadataObj)
qrCodeFrameView?.frame = barCodeObject!.bounds

if let qrCode = metadataObj.stringValue {
qrCodeScannerReady = false
self.processScannedCode(qrCode)
}
}
}

private func processScannedCode(_ qrCode: String) {
dataSource.qrcodeScanned(qrCode)
qrCodeScanned = true

// give user some time to notice the action, as scanning is done very quickly
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: { [weak self] in
self?.navigationController?.popViewController(animated: true)
})
}
}

0 comments on commit b664886

Please sign in to comment.