Skip to content
This repository has been archived by the owner on Jul 8, 2023. It is now read-only.

Updated and fixed issue-6, revised completion to Swift5 #9

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions Example/PrivacyKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
16D7A0BD23D429290073A805 /* PrivacyKitTopVCTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 16D7A0BB23D428D40073A805 /* PrivacyKitTopVCTests.swift */; };
16D7A0BF23D42AC80073A805 /* PrivacyKitBottomVCTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 16D7A0BE23D42AC80073A805 /* PrivacyKitBottomVCTests.swift */; };
16D7A0C123D42AD50073A805 /* PrivacyKitPrivacyVCTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 16D7A0C023D42AD50073A805 /* PrivacyKitPrivacyVCTests.swift */; };
16F740A523DB63F9005005BC /* PrivacyKitDelegateCompTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 16F740A323DB63C9005005BC /* PrivacyKitDelegateCompTests.swift */; };
607FACD61AFB9204008FA782 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 607FACD51AFB9204008FA782 /* AppDelegate.swift */; };
607FACD81AFB9204008FA782 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 607FACD71AFB9204008FA782 /* ViewController.swift */; };
607FACDB1AFB9204008FA782 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 607FACD91AFB9204008FA782 /* Main.storyboard */; };
Expand Down Expand Up @@ -47,6 +48,7 @@
16D7A0BB23D428D40073A805 /* PrivacyKitTopVCTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrivacyKitTopVCTests.swift; sourceTree = "<group>"; };
16D7A0BE23D42AC80073A805 /* PrivacyKitBottomVCTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrivacyKitBottomVCTests.swift; sourceTree = "<group>"; };
16D7A0C023D42AD50073A805 /* PrivacyKitPrivacyVCTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrivacyKitPrivacyVCTests.swift; sourceTree = "<group>"; };
16F740A323DB63C9005005BC /* PrivacyKitDelegateCompTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrivacyKitDelegateCompTests.swift; sourceTree = "<group>"; };
1C4EFF76C324FFD5F142D5F5 /* Pods_PrivacyKit_Example.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_PrivacyKit_Example.framework; sourceTree = BUILT_PRODUCTS_DIR; };
22A839033D7259A64A7EE4C2 /* Pods-PrivacyKit_Example.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-PrivacyKit_Example.debug.xcconfig"; path = "Target Support Files/Pods-PrivacyKit_Example/Pods-PrivacyKit_Example.debug.xcconfig"; sourceTree = "<group>"; };
43DBA13BBFBC97D64AA7C997 /* PrivacyKit.podspec */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = PrivacyKit.podspec; path = ../PrivacyKit.podspec; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.ruby; };
Expand Down Expand Up @@ -142,6 +144,7 @@
16A6356D23D195B700FD7195 /* PrivacyKitStateTests.swift */,
16A6356A23D1949400FD7195 /* PrivacyKitDescriptionTests.swift */,
16489DF323D28E5B00DE414D /* PrivacyKitDelegateTests.swift */,
16F740A323DB63C9005005BC /* PrivacyKitDelegateCompTests.swift */,
16D7A0BB23D428D40073A805 /* PrivacyKitTopVCTests.swift */,
16D7A0BE23D42AC80073A805 /* PrivacyKitBottomVCTests.swift */,
16D7A0C023D42AD50073A805 /* PrivacyKitPrivacyVCTests.swift */,
Expand Down Expand Up @@ -424,6 +427,7 @@
16A6356C23D194BB00FD7195 /* PrivacyKitDescriptionTests.swift in Sources */,
16D7A0BF23D42AC80073A805 /* PrivacyKitBottomVCTests.swift in Sources */,
16489DF523D28F1300DE414D /* PrivacyKitDelegateTests.swift in Sources */,
16F740A523DB63F9005005BC /* PrivacyKitDelegateCompTests.swift in Sources */,
607FACEC1AFB9204008FA782 /* PrivacyKitBasicsTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
59 changes: 35 additions & 24 deletions Example/PrivacyKit/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,13 @@ class ViewController: UIViewController, PrivacyKitDelegate {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

self.requirePrivacy(.bottom) { (accepted, denied) in
if denied {
print("PrivacyKit: completed with denied.")
self.requirePrivacy(.bottom) { result in
switch result {
case .success(let privacyAccepted):
print("PrivacyKit: privacy was accepted? \(privacyAccepted)")
case .failure(let error):
print("PrivacyKit: Error: \(error)")
}

if accepted {
print("PrivacyKit: completed with accepted.")
}

print("PrivacyKit: ViewDidAppear Privacy Completion State: \(PrivacyKit.shared.getState())")
self.updateLabels()
}
}
Expand Down Expand Up @@ -202,8 +199,13 @@ class ViewController: UIViewController, PrivacyKitDelegate {
self.reset()

print("PrivacyKit: Presenting Top Notice")
requirePrivacy(.top) { (accepted, denied) in
print("Top Privacy Notice completed with results... accepted: \(accepted), denied: \(denied)")
requirePrivacy(.top) { result in
switch result {
case .success(let privacyAccepted):
print("PrivacyKit.Top: privacy was accepted? \(privacyAccepted)")
case .failure(let error):
print("PrivacyKit.Top: Error: \(error)")
}
self.updateLabels()
}
}
Expand All @@ -212,40 +214,49 @@ class ViewController: UIViewController, PrivacyKitDelegate {
self.reset()

print("PrivacyKit: Presenting Bottom Notice")
requirePrivacy(.bottom) { (accepted, denied) in
print("Bottom Privacy Notice completed with results... accepted: \(accepted), denied: \(denied)")
requirePrivacy(.bottom) { result in
switch result {
case .success(let privacyAccepted):
print("PrivacyKit.Bottom: privacy was accepted? \(privacyAccepted)")
case .failure(let error):
print("PrivacyKit.Bottom: Error: \(error)")
}
self.updateLabels()
}
}

@objc func doAlert() {
self.reset()

print("PrivacyKit: Presenting Alert Notice")
requirePrivacy(.alert) { (accepted, denied) in
print("Alert Privacy Notice completed with results... accepted: \(accepted), denied: \(denied)")
requirePrivacy(.alert) { result in
switch result {
case .success(let privacyAccepted):
print("PrivacyKit.Alert: privacy was accepted? \(privacyAccepted)")
case .failure(let error):
print("PrivacyKit.Alert: Error: \(error)")
}
self.updateLabels()
}
}

@objc func doCustom() {
self.reset()

print("PrivacyKit: Presenting Custom Notice")
requirePrivacy(privacyViewController: CustomNoticeVC()) { (accepted, denied) in
print("Custom Privacy Notice completed with results... accepted: \(accepted), denied: \(denied)")
requirePrivacy(privacyViewController: CustomNoticeVC()) { result in
switch result {
case .success(let privacyAccepted):
print("PrivacyKit.Custom: privacy was accepted? \(privacyAccepted)")
case .failure(let error):
print("PrivacyKit.Custom: Error: \(error)")
}
self.updateLabels()
}
}

@objc func reset() {
print("PrivacyKit: Reset")

PrivacyKit.shared.setPrivacy(accepted: false)
PrivacyKit.shared.setPrivacy(denied: false)
PrivacyKit.shared.setPrivacyRead(read: false)
PrivacyKit.shared.setTermsRead(read: false)

PrivacyKit.shared.resetState()
self.updateLabels()
}
}
39 changes: 39 additions & 0 deletions Example/Tests/PrivacyKitDelegateCompTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// PrivacyKitDelegateCompTests.swift
// PrivacyKit_Example
//
// Created by Jacob Fielding on 1/24/20.
// Copyright © 2020 CocoaPods. All rights reserved.
//

import Quick
import Nimble
@testable import PrivacyKit

class TestPrivacyKitDelegateCompletion: QuickSpec {
override func spec() {
it("runs completion when accepted") {
let delegateVC = TestDelegateVC()
let noticeVC = TestNoticeVC()

var success: Bool = false

delegateVC.requirePrivacy(privacyViewController: noticeVC) { _ in
success = true
}

noticeVC.acceptPrivacy()

expect(success).toEventually(beTrue())
}

it("doesn't run completion when no accept") {
let delegateVC = TestDelegateVC()
let noticeVC = TestNoticeVC()

delegateVC.requirePrivacy(privacyViewController: noticeVC) { _ in
fail("closure shouldn't run")
}
}
}
}
40 changes: 0 additions & 40 deletions Example/Tests/PrivacyKitStateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,45 +34,5 @@ class TestPrivacyKitStateExtras: QuickSpec {
expect(newState["privacyAccepted"] as? Bool).to(equal(false))
expect(newState["privacyDenied"] as? Bool).to(equal(false))
}

it("sets privacy accepted boolean") {
PrivacyKit.shared.resetState()

PrivacyKit.shared.setPrivacy(accepted: true)
expect(PrivacyKit.shared.privacyAccepted()).to(equal(true))

PrivacyKit.shared.setPrivacy(accepted: false)
expect(PrivacyKit.shared.privacyAccepted()).to(equal(false))
}

it("sets privacy denied boolean") {
PrivacyKit.shared.resetState()

PrivacyKit.shared.setPrivacy(denied: true)
expect(PrivacyKit.shared.privacyDenied()).to(equal(true))

PrivacyKit.shared.setPrivacy(denied: false)
expect(PrivacyKit.shared.privacyDenied()).to(equal(false))
}

it("sets privacy read boolean") {
PrivacyKit.shared.resetState()

PrivacyKit.shared.setPrivacyRead(read: true)
expect(PrivacyKit.shared.privacyTapped()).to(equal(true))

PrivacyKit.shared.setPrivacyRead(read: false)
expect(PrivacyKit.shared.privacyTapped()).to(equal(false))
}

it("sets terms read boolean") {
PrivacyKit.shared.resetState()

PrivacyKit.shared.setTermsRead(read: true)
expect(PrivacyKit.shared.termsTapped()).to(equal(true))

PrivacyKit.shared.setTermsRead(read: false)
expect(PrivacyKit.shared.termsTapped()).to(equal(false))
}
}
}
18 changes: 8 additions & 10 deletions PrivacyKit/Classes/PrivacyKitDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Foundation

public typealias PrivacyAccepted = Bool
public typealias PrivacyDenied = Bool
public typealias PrivacyCompletion = ((PrivacyAccepted, PrivacyDenied) -> Void)
public typealias PrivacyCompletion = (Result<PrivacyAccepted, Error>) -> Void

public protocol PrivacyKitDelegate {
func requirePrivacy(_ viewType: PrivacyNoticeType, _ completion: PrivacyCompletion?)
Expand Down Expand Up @@ -42,13 +42,13 @@ extension PrivacyKitDelegate where Self: UIViewController {
/// If the View Controller is using the default accept and deny `@objc` functions.
public func requirePrivacy(privacyViewController: PrivacyNoticeVC, _ completion: PrivacyCompletion? = nil) {
if PrivacyKit.shared.privacyModel.privacyAccepted == false {
self.presentPrivacyNotice(viewController: privacyViewController, completion)
self.presentPrivacyNotice(viewController: privacyViewController, completion: completion)
}
}

private func presentPrivacyNotice(_ viewType: PrivacyNoticeType = .bottom, _ completion: PrivacyCompletion? = nil) {
var viewController: PrivacyNoticeVC?

switch viewType {
case .alert:
return presentAlert(completion: completion)
Expand All @@ -58,31 +58,29 @@ extension PrivacyKitDelegate where Self: UIViewController {
viewController = BottomNoticeVC()
}

presentPrivacyNotice(viewController: viewController!)
presentPrivacyNotice(viewController: viewController!, completion: completion)
}

private func presentPrivacyNotice(viewController: PrivacyNoticeVC, _ completion: PrivacyCompletion? = nil) {
private func presentPrivacyNotice(viewController: PrivacyNoticeVC, completion: PrivacyCompletion?) {
viewController.modalTransitionStyle = .crossDissolve
viewController.modalPresentationStyle = .overCurrentContext
viewController.privacyCompletion = completion

self.present(viewController, animated: true, completion: nil)
}

private func presentAlert(completion: PrivacyCompletion? = nil) {
private func presentAlert(completion: PrivacyCompletion?) {
let alert = UIAlertController()

alert.title = PrivacyKit.shared.privacyNoticeTitle
alert.message = PrivacyKit.shared.descriptionAttributed!.string

let acceptAction = UIAlertAction(title: "Accept Privacy", style: .default) { (_) in
PrivacyKit.shared.acceptPrivacy()
completion?(PrivacyKit.shared.privacyAccepted(), PrivacyKit.shared.privacyDenied())
PrivacyKit.shared.acceptPrivacy(completion)
}

let denyAction = UIAlertAction(title: "Deny Privacy", style: .destructive) { (_) in
PrivacyKit.shared.denyPrivacy()
completion?(PrivacyKit.shared.privacyAccepted(), PrivacyKit.shared.privacyDenied())
PrivacyKit.shared.denyPrivacy(completion)
}

alert.addAction(acceptAction)
Expand Down
5 changes: 1 addition & 4 deletions PrivacyKit/Classes/Protocols/PrivacyKitErrors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,5 @@
import Foundation

enum PrivacyKitError: Error {
case noPrivacyPolicyLink
case noTermsOfServiceLink
case noPrivacyPolicyTextMatched
case noTermsOfServiceLinkMatched
case privacyDenied
}
46 changes: 20 additions & 26 deletions PrivacyKit/Classes/Protocols/PrivacyKitState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,12 @@
import Foundation

public protocol PrivacyKitState {
func acceptPrivacy()
func denyPrivacy()

func setPrivacy(accepted: Bool)
func setPrivacy(denied: Bool)
func acceptPrivacy(_ completion: PrivacyCompletion?)
func denyPrivacy(_ completion: PrivacyCompletion?)

func tapPrivacyPolicy()
func tapTermsOfService()

func setPrivacyRead(read: Bool)
func setTermsRead(read: Bool)

func privacyAccepted() -> Bool
func privacyDenied() -> Bool

Expand All @@ -31,42 +25,42 @@ public protocol PrivacyKitState {
}

public extension PrivacyKitState where Self: PrivacyKit {
func acceptPrivacy() {
privacyModel.privacyAccepted = true
privacyModel.save()
func acceptPrivacy(_ completion: PrivacyCompletion? = nil) {
let accepted = true
self.setPrivacy(accepted: accepted)
completion?(.success(accepted))
}

func denyPrivacy() {
privacyModel.privacyDenied = true
privacyModel.save()
func denyPrivacy(_ completion: PrivacyCompletion? = nil) {
let denied = true
self.setPrivacy(denied: denied)
completion?(.failure(PrivacyKitError.privacyDenied))
}

func setPrivacy(accepted: Bool) {
privacyModel.privacyAccepted = accepted
privacyModel.save()
func tapPrivacyPolicy() {
self.setPrivacyRead(read: true)
}

func setPrivacy(denied: Bool) {
privacyModel.privacyDenied = denied
privacyModel.save()
func tapTermsOfService() {
self.setTermsRead(read: true)
}

func tapPrivacyPolicy() {
privacyModel.privacyPolicyRead = true
private func setPrivacy(accepted: Bool) {
privacyModel.privacyAccepted = accepted
privacyModel.save()
}

func tapTermsOfService() {
privacyModel.termsOfServiceRead = true
private func setPrivacy(denied: Bool) {
privacyModel.privacyDenied = denied
privacyModel.save()
}

func setPrivacyRead(read: Bool) {
private func setPrivacyRead(read: Bool) {
privacyModel.privacyPolicyRead = read
privacyModel.save()
}

func setTermsRead(read: Bool) {
private func setTermsRead(read: Bool) {
privacyModel.termsOfServiceRead = read
privacyModel.save()
}
Expand Down
Loading