Commit 21b1ac4e authored by Jose Blaya's avatar Jose Blaya
Browse files

Quick connect tile

parent da150014
This diff is collapsed.
......@@ -16,8 +16,11 @@ private let log = SwiftyBeaver.self
class DashboardViewController: AutolayoutViewController {
private struct Cells {
static let tile = "IPTileCell"
static let tileCellClass = "IPTileCollectionViewCell"
static let ipTile = "IPTileCell"
static let ipTileCellClass = "IPTileCollectionViewCell"
static let quickConnectTile = "QuickConnectTileCell"
static let quickConnectCellClass = "QuickConnectTileCollectionViewCell"
}
@IBOutlet private weak var viewContent: UIView!
......@@ -43,9 +46,12 @@ class DashboardViewController: AutolayoutViewController {
override func viewDidLoad() {
super.viewDidLoad()
collectionView.register(UINib(nibName: Cells.tileCellClass,
collectionView.register(UINib(nibName: Cells.ipTileCellClass,
bundle: nil),
forCellWithReuseIdentifier: Cells.ipTile)
collectionView.register(UINib(nibName: Cells.quickConnectCellClass,
bundle: nil),
forCellWithReuseIdentifier: Cells.tile)
forCellWithReuseIdentifier: Cells.quickConnectTile)
collectionView.backgroundColor = .clear
navigationItem.leftBarButtonItem = UIBarButtonItem(
......@@ -177,6 +183,9 @@ class DashboardViewController: AutolayoutViewController {
@IBAction func vpnButtonClicked(_ sender: Any?) {
if !toggleConnection.isOn {
Client.providers.vpnProvider.connect(nil)
NotificationCenter.default.post(name: .PIAServerHasBeenUpdated,
object: self,
userInfo: nil)
} else {
Client.providers.vpnProvider.disconnect(nil)
}
......@@ -469,8 +478,14 @@ extension DashboardViewController: UICollectionViewDelegateFlowLayout {
extension DashboardViewController: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Cells.tile, for: indexPath) as! IPTileCollectionViewCell
return cell
switch indexPath.row {
case AvailableTiles.quickConnect.rawValue:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Cells.quickConnectTile, for: indexPath) as! QuickConnectTileCollectionViewCell
return cell
default:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Cells.ipTile, for: indexPath) as! IPTileCollectionViewCell
return cell
}
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
......@@ -478,7 +493,7 @@ extension DashboardViewController: UICollectionViewDelegate, UICollectionViewDat
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 21//AvailableTiles.countCases()
return AvailableTiles.countCases()
}
}
{
"images" : [
{
"idiom" : "universal",
"filename" : "heart.pdf"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
\ No newline at end of file
......@@ -82,6 +82,9 @@ class RegionCell: UITableViewCell, Restylable {
self.isFavorite = !self.isFavorite
self.isFavorite ? self.server.favorite() : self.server.unfavorite()
self.animateFavoriteImage()
NotificationCenter.default.post(name: .PIAServerHasBeenUpdated,
object: self,
userInfo: nil)
}
private func animateFavoriteImage() {
......
......@@ -180,6 +180,10 @@ class RegionsViewController: AutolayoutViewController {
NotificationCenter.default.post(name: .PIAThemeDidChange,
object: self,
userInfo: nil)
NotificationCenter.default.post(name: .PIAServerHasBeenUpdated,
object: self,
userInfo: nil)
default:
break
}
......
......@@ -55,3 +55,14 @@ extension UIImageView {
self.image = image.withRenderingMode(.alwaysOriginal)
}
}
extension UIButton {
func setImage(fromServer server: Server) {
let imageName = "flag-\(server.country.lowercased())"
guard let image = UIImage(named: imageName) else {
af_setImage(for: .normal, url: server.flagURL, placeholderImage: Asset.Flags.flagUniversal.image)
return
}
self.setImage(image.withRenderingMode(.alwaysOriginal), for: [])
}
}
//
// ServerButton.swift
// PIA VPN
//
// Created by Jose Antonio Blaya Garcia on 10/01/2019.
// Copyright © 2019 London Trust Media. All rights reserved.
//
import UIKit
import PIALibrary
class ServerButton: UIButton {
var server: Server?
}
......@@ -49,6 +49,7 @@ enum Asset {
static let vpnButton = ImageAsset(name: "vpn-button")
}
enum Global {
static let favoriteGreen = ImageAsset(name: "favorite-green")
static let favoriteSelected = ImageAsset(name: "favorite-selected")
static let favoriteUnselectedDark = ImageAsset(name: "favorite-unselected-dark")
static let favoriteUnselected = ImageAsset(name: "favorite-unselected")
......@@ -331,6 +332,7 @@ enum Asset {
]
static let allImages: [ImageAsset] = [
Piax.Dashboard.vpnButton,
Piax.Global.favoriteGreen,
Piax.Global.favoriteSelected,
Piax.Global.favoriteUnselectedDark,
Piax.Global.favoriteUnselected,
......
......@@ -486,6 +486,15 @@ internal enum L10n {
internal static let selectRegion = L10n.tr("Localizable", "shortcuts.select_region")
}
internal enum Tiles {
internal enum Quick {
internal enum Connect {
/// Quick connect
internal static let title = L10n.tr("Localizable", "tiles.quick.connect.title")
}
}
}
internal enum VpnPermission {
/// PIA
internal static let title = L10n.tr("Localizable", "vpn_permission.title")
......
......@@ -9,6 +9,6 @@
import Foundation
enum AvailableTiles: Int, EnumsBuilder {
case regions
case quickConnect
case ip
}
......@@ -11,9 +11,4 @@ import PIALibrary
class IPTileCollectionViewCell: UICollectionViewCell {
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
}
//
// QuickConnectTile.swift
// PIA VPN
//
// Created by Jose Antonio Blaya Garcia on 10/01/2019.
// Copyright © 2019 London Trust Media. All rights reserved.
//
import UIKit
import PIALibrary
class QuickConnectTile: UIView, Tileable {
var detailViewAction: Func!
var view: UIView!
var detailSegueIdentifier: String!
@IBOutlet private weak var tileTitle: UILabel!
@IBOutlet private weak var stackView: UIStackView!
override init(frame: CGRect) {
super.init(frame: frame)
self.xibSetup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.xibSetup()
self.setupView()
}
deinit {
NotificationCenter.default.removeObserver(self)
}
private func setupView() {
let nc = NotificationCenter.default
nc.addObserver(self, selector: #selector(viewShouldRestyle), name: .PIAThemeDidChange, object: nil)
nc.addObserver(self, selector: #selector(updateQuickConnectList), name: .PIAServerHasBeenUpdated, object: nil)
viewShouldRestyle()
self.detailViewAction = {}
self.tileTitle.text = L10n.Tiles.Quick.Connect.title.uppercased()
updateQuickConnectList()
}
@objc private func viewShouldRestyle() {
tileTitle.style(style: TextStyle.textStyle21)
Theme.current.applyLightBackground(self)
}
@objc private func updateQuickConnectList() {
let historicalServers = Client.providers.serverProvider.historicalServers
for containerView in stackView.subviews {
if let button = containerView.subviews.first as? ServerButton,
let favoriteImage = containerView.subviews.last as? UIImageView {
button.setImage(Asset.Flags.flagUniversal.image, for: .normal)
button.alpha = 0.1
button.imageView?.contentMode = .scaleAspectFit
button.isUserInteractionEnabled = false
favoriteImage.isHidden = true
}
}
for (index, server) in historicalServers.enumerated().reversed() {
let buttonIndex = historicalServers.count - (index + 1)
let view = stackView.subviews[buttonIndex]
if let button = view.subviews.first as? ServerButton,
let favoriteImage = view.subviews.last as? UIImageView {
button.alpha = 1
button.setImage(fromServer: server)
button.imageView?.contentMode = .scaleAspectFit
button.isUserInteractionEnabled = true
button.server = server
favoriteImage.isHidden = !AppPreferences.shared.favoriteServerIdentifiers.contains(server.identifier)
}
}
}
@IBAction private func connectToServer(_ sender: ServerButton) {
if let server = sender.server {
let currentServer = Client.preferences.displayedServer
guard (server.identifier != currentServer.identifier) else {
return
}
Client.preferences.displayedServer = server
NotificationCenter.default.post(name: .PIAThemeDidChange,
object: self,
userInfo: nil)
NotificationCenter.default.post(name: .PIAServerHasBeenUpdated,
object: self,
userInfo: nil)
}
}
}
This diff is collapsed.
//
// QuickConnectTileCollectionViewCell.swift
// PIA VPN
//
// Created by Jose Antonio Blaya Garcia on 10/01/2019.
// Copyright © 2019 London Trust Media. All rights reserved.
//
import UIKit
class QuickConnectTileCollectionViewCell: UICollectionViewCell {
}
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14460.20"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<collectionViewCell opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" reuseIdentifier="QuickConnectTileCell" id="gTV-IL-0wX" customClass="QuickConnectTileCollectionViewCell" customModule="PIA_VPN" customModuleProvider="target">
<rect key="frame" x="0.0" y="0.0" width="50" height="50"/>
<autoresizingMask key="autoresizingMask"/>
<view key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center">
<rect key="frame" x="0.0" y="0.0" width="50" height="50"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="SxC-qL-YYL" customClass="QuickConnectTile" customModule="PIA_VPN" customModuleProvider="target">
<rect key="frame" x="0.0" y="0.0" width="50" height="50"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</view>
</subviews>
</view>
<constraints>
<constraint firstAttribute="trailing" secondItem="SxC-qL-YYL" secondAttribute="trailing" id="0B7-2f-MDz"/>
<constraint firstItem="SxC-qL-YYL" firstAttribute="leading" secondItem="gTV-IL-0wX" secondAttribute="leading" id="1KW-MX-2mZ"/>
<constraint firstAttribute="bottom" secondItem="SxC-qL-YYL" secondAttribute="bottom" id="giD-g3-4HN"/>
<constraint firstItem="SxC-qL-YYL" firstAttribute="top" secondItem="gTV-IL-0wX" secondAttribute="top" id="kvJ-pe-ZkO"/>
</constraints>
<viewLayoutGuide key="safeArea" id="ZTg-uK-7eu"/>
</collectionViewCell>
</objects>
</document>
......@@ -25,28 +25,30 @@ final class TileFlowLayout: UICollectionViewFlowLayout {
let lineWidth = self.minimumLineSpacing
var decorationAttributes: [UICollectionViewLayoutAttributes] = []
for layoutAttribute in layoutAttributes {
for (index, layoutAttribute) in layoutAttributes.enumerated() {
//bottom
let separatorAttribute = UICollectionViewLayoutAttributes(forDecorationViewOfKind: separatorDecorationViewBottom,
with: layoutAttribute.indexPath)
let cellFrame = layoutAttribute.frame
separatorAttribute.frame = CGRect(x: cellFrame.origin.x,
y: cellFrame.origin.y + cellFrame.size.height,
y: cellFrame.origin.y + cellFrame.size.height - lineWidth,
width: cellFrame.size.width,
height: lineWidth)
separatorAttribute.zIndex = Int.max
decorationAttributes.append(separatorAttribute)
//top
let separatorAttributeTop = UICollectionViewLayoutAttributes(forDecorationViewOfKind: separatorDecorationViewTop,
with: layoutAttribute.indexPath)
let cellFrameTop = layoutAttribute.frame
separatorAttributeTop.frame = CGRect(x: cellFrameTop.origin.x,
y: cellFrame.origin.y,
width: cellFrameTop.size.width,
height: lineWidth)
separatorAttributeTop.zIndex = Int.max - 1
decorationAttributes.append(separatorAttributeTop)
if layoutAttributes.count == 1 || index == 0 {
let separatorAttributeTop = UICollectionViewLayoutAttributes(forDecorationViewOfKind: separatorDecorationViewTop,
with: layoutAttribute.indexPath)
let cellFrameTop = layoutAttribute.frame
separatorAttributeTop.frame = CGRect(x: cellFrameTop.origin.x,
y: cellFrame.origin.y,
width: cellFrameTop.size.width,
height: lineWidth)
separatorAttributeTop.zIndex = Int.max - 1
decorationAttributes.append(separatorAttributeTop)
}
}
......
......@@ -209,3 +209,4 @@
"region.filter.favorites" = "Favorites";
// TILES
"tiles.quick.connect.title" = "Quick connect";
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment