Skip to content

Commit

Permalink
Certificates: Add a button to delete all certificates
Browse files Browse the repository at this point in the history
  • Loading branch information
AurelTyson committed Oct 30, 2020
1 parent a37c362 commit a6934eb
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 35 deletions.
130 changes: 110 additions & 20 deletions AttestationCovid/List/CertificateListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,138 @@
// Copyright © 2020 David Yang. All rights reserved.
//

import RxCocoa
import RxSwift
import UIKit

final class CertificateListViewController: UITableViewController {


// MARK: Outlets

@IBOutlet private weak var deleteAllCertificatesButton: UIBarButtonItem!

// MARK: Attributes

private var certificates: [URL] = []

private var documentDirectory: URL? {
return FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
}


private let disposeBag = DisposeBag()

// MARK: Viewcontroller lifecycle

override func viewDidLoad() {
super.viewDidLoad()

registerCells()

// Register cells
self.registerCells()

// Delete all certificates
self.deleteAllCertificatesButton.rx
.tap
.subscribe(onNext: { [weak self] _ in

self?.showDeleteAllCertificatesAlert()

})
.disposed(by: self.disposeBag)

}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

load {
tableView.reloadData()

// Reload data
self.load {

// Reload tableView
self.tableView.reloadData()

}

}


// MARK: Methods

private func registerCells() {
tableView.register(FileCell.self, forCellReuseIdentifier: FileCell.identifier)
}

private func load(completion: () -> Void) {
if let documentDirectory = documentDirectory, let files = try? FileManager.default.contentsOfDirectory(at: documentDirectory, includingPropertiesForKeys: nil, options: []) {
if let documentDirectory = documentDirectory,
let files = try? FileManager.default.contentsOfDirectory(at: documentDirectory, includingPropertiesForKeys: nil, options: []) {
self.certificates = files
completion()
}

}

private func removeCertificate(at index: Int) throws {
try FileManager.default.removeItem(at: self.certificates[index])
self.certificates.remove(at: index)
}

private func removeAllCertificates() throws {

// Foreach certificate index
try self.certificates.forEach { (url: URL) in

// Delete item at url
try FileManager.default.removeItem(at: url)

}

// Reload data
self.load() {

// Reload tableView
self.tableView.reloadData()

}

}

private func showDeleteAllCertificatesAlert() {

// Alert controller
let alertController = UIAlertController(title: NSLocalizedString("certificates.deleteall.alert.title", comment: ""),
message: NSLocalizedString("certificates.deleteall.alert.message", comment: ""),
preferredStyle: .alert)

// Cancel action
let cancelAction = UIAlertAction(title: NSLocalizedString("certificates.deleteall.alert.cancel", comment: ""),
style: .default,
handler: nil)
alertController.addAction(cancelAction)

// Delete action
let deleteAction = UIAlertAction(title: NSLocalizedString("certificates.deleteall.alert.delete", comment: ""),
style: .destructive,
handler: { [weak self] _ in

do {

// Remove all certificates
try self?.removeAllCertificates()

}
catch {

self?.showAlert(message: "Une erreur s'est produite. Impossible de supprimer les attestations")

}

})
alertController.addAction(deleteAction)

// Show alert
self.present(alertController,
animated: true,
completion: nil)

}

}

extension CertificateListViewController {
Expand Down Expand Up @@ -83,10 +180,3 @@ extension CertificateListViewController {
}
}
}

extension CertificateListViewController {
private func removeCertificate(at index: Int) throws {
try FileManager.default.removeItem(at: certificates[index])
certificates.remove(at: index)
}
}
5 changes: 5 additions & 0 deletions AttestationCovid/fr.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@
"about.sourceCode.github" = "Github";

"filename" = "Attestation_%@.pdf";

"certificates.deleteall.alert.title" = "Tout supprimer";
"certificates.deleteall.alert.message" = "Supprimer l'ensemble des certificats ?";
"certificates.deleteall.alert.cancel" = "Annuler";
"certificates.deleteall.alert.delete" = "Supprimer";
44 changes: 29 additions & 15 deletions AttestationCovid/fr.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="16096" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="6qy-Aj-721">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="17156" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="6qy-Aj-721">
<device id="retina6_1" orientation="portrait" appearance="light"/>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="16086"/>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="17126"/>
<capability name="System colors in document resources" minToolsVersion="11.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
Expand Down Expand Up @@ -49,14 +51,14 @@
<tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="28" sectionFooterHeight="28" id="QWC-AX-hZA">
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<view key="tableFooterView" contentMode="scaleToFill" id="P9s-yS-WHr">
<rect key="frame" x="0.0" y="166.5" width="414" height="76"/>
<rect key="frame" x="0.0" y="167" width="414" height="76"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="fq3-l1-eUV">
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="fq3-l1-eUV">
<rect key="frame" x="16" y="16" width="382" height="44"/>
<color key="backgroundColor" systemColor="systemBlueColor" red="0.0" green="0.47843137250000001" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<color key="backgroundColor" systemColor="systemBlueColor"/>
<constraints>
<constraint firstAttribute="height" constant="44" id="FPu-FK-krg"/>
</constraints>
Expand All @@ -68,7 +70,7 @@
</connections>
</button>
</subviews>
<color key="backgroundColor" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<constraints>
<constraint firstItem="fq3-l1-eUV" firstAttribute="top" secondItem="P9s-yS-WHr" secondAttribute="top" constant="16" id="6GE-6c-SbN"/>
<constraint firstAttribute="trailing" secondItem="fq3-l1-eUV" secondAttribute="trailing" constant="16" id="M1t-o0-9qc"/>
Expand All @@ -78,20 +80,20 @@
</view>
<prototypes>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" indentationWidth="10" reuseIdentifier="KeyValueCellIdentifier" id="aLd-X3-aMY" customClass="KeyValueCell" customModule="Covid_Attestation" customModuleProvider="target">
<rect key="frame" x="0.0" y="28" width="414" height="43"/>
<rect key="frame" x="0.0" y="28" width="414" height="43.5"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="aLd-X3-aMY" id="JYk-cN-cAq">
<rect key="frame" x="0.0" y="0.0" width="414" height="43"/>
<rect key="frame" x="0.0" y="0.0" width="414" height="43.5"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<textField opaque="NO" contentMode="scaleToFill" horizontalCompressionResistancePriority="751" verticalCompressionResistancePriority="751" contentHorizontalAlignment="left" contentVerticalAlignment="center" placeholder="Value" textAlignment="right" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="Lb5-mI-3wK">
<rect key="frame" x="356" y="0.0" width="42" height="43"/>
<rect key="frame" x="356" y="0.0" width="42" height="43.5"/>
<color key="textColor" red="0.0" green="0.47843137250000001" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<textInputTraits key="textInputTraits"/>
</textField>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Name" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="BC0-Ev-DYl">
<rect key="frame" x="16" y="11" width="45" height="21"/>
<rect key="frame" x="16" y="11" width="45" height="21.5"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
Expand All @@ -113,7 +115,7 @@
</connections>
</tableViewCell>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" indentationWidth="10" reuseIdentifier="CheckCellIdentifier" id="P7u-9w-v3c" customClass="CheckCell" customModule="Covid_Attestation" customModuleProvider="target">
<rect key="frame" x="0.0" y="71" width="414" height="67.5"/>
<rect key="frame" x="0.0" y="71.5" width="414" height="67.5"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="P7u-9w-v3c" id="kJF-tC-dJk">
<rect key="frame" x="0.0" y="0.0" width="414" height="67.5"/>
Expand Down Expand Up @@ -189,14 +191,19 @@ lakdsjlfkd</string>
<tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="28" sectionFooterHeight="28" id="ZdU-C5-s5j">
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<connections>
<outlet property="dataSource" destination="Mbc-RY-Xt0" id="nM3-Mx-ZUS"/>
<outlet property="delegate" destination="Mbc-RY-Xt0" id="JwZ-pR-idk"/>
</connections>
</tableView>
<tabBarItem key="tabBarItem" title="Mes attestations" id="Ce2-1U-2Dc"/>
<navigationItem key="navigationItem" title="Liste des attestations" id="cmN-HB-yXG"/>
<navigationItem key="navigationItem" title="Liste des attestations" id="cmN-HB-yXG">
<barButtonItem key="rightBarButtonItem" title="Item" image="trash" catalog="system" id="bPf-FP-ok8"/>
</navigationItem>
<connections>
<outlet property="deleteAllCertificatesButton" destination="bPf-FP-ok8" id="8fh-A8-fNf"/>
</connections>
</tableViewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="08w-4K-nB9" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
</objects>
Expand Down Expand Up @@ -242,7 +249,14 @@ lakdsjlfkd</string>
<image name="checkmark.circle.fill" catalog="system" width="128" height="121"/>
<image name="circle" catalog="system" width="128" height="121"/>
<image name="list.dash" catalog="system" width="128" height="85"/>
<image name="plus.rectangle.fill.on.rectangle.fill" catalog="system" width="128" height="98"/>
<image name="plus.rectangle.fill.on.rectangle.fill" catalog="system" width="128" height="96"/>
<image name="questionmark.circle" catalog="system" width="128" height="121"/>
<image name="trash" catalog="system" width="121" height="128"/>
<systemColor name="systemBackgroundColor">
<color white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</systemColor>
<systemColor name="systemBlueColor">
<color red="0.0" green="0.47843137254901963" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</systemColor>
</resources>
</document>

0 comments on commit a6934eb

Please sign in to comment.