Skip to content

Commit

Permalink
Add "reopen tweaks" button to Floating Tweak Window
Browse files Browse the repository at this point in the history
Previously, once you were in the "floating tweak window" state, you couldn't get right back into SwiftTweaks' main interface.

Now there's a new button on the floating tweak window that pops you back into SwiftTweaks' interface.
  • Loading branch information
bryanjclark committed Nov 2, 2018
1 parent e765cdc commit c1901a6
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 46 deletions.
25 changes: 23 additions & 2 deletions SwiftTweaks/FloatingTweakGroupViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import UIKit
internal protocol FloatingTweaksWindowPresenter {
func presentFloatingTweaksUI(forTweakGroup tweakGroup: TweakGroup)
func dismissFloatingTweaksUI()
func resumeDisplayingMainTweaksInterface()
}

// MARK: - FloatingTweakGroupViewController
Expand Down Expand Up @@ -85,7 +86,7 @@ internal final class FloatingTweakGroupViewController: UIViewController {
internal static let margins: CGFloat = 5
private static let minimizedWidth: CGFloat = 30

private static let closeButtonSize = CGSize(width: 42, height: 32)
private static let navBarButtonSize = CGSize(width: 42, height: 32)
private static let navBarHeight: CGFloat = 32
private static let windowCornerRadius: CGFloat = 5

Expand Down Expand Up @@ -116,6 +117,14 @@ internal final class FloatingTweakGroupViewController: UIViewController {
return button
}()

private let reopenButton: UIButton = {
let button = UIButton()
let buttonImage = UIImage(swiftTweaksImage: .floatingReopenButton).withRenderingMode(.alwaysTemplate)
button.setImage(buttonImage.imageTintedWithColor(AppTheme.Colors.controlTinted), for: UIControl.State())
button.setImage(buttonImage.imageTintedWithColor(AppTheme.Colors.controlTintedPressed), for: .highlighted)
return button
}()

fileprivate let tableView: UITableView = {
let tableView = UITableView(frame: .zero, style: .plain)
tableView.backgroundColor = .clear
Expand Down Expand Up @@ -152,7 +161,9 @@ internal final class FloatingTweakGroupViewController: UIViewController {

// The "fake nav bar"
closeButton.addTarget(self, action: #selector(self.closeButtonTapped), for: .touchUpInside)
reopenButton.addTarget(self, action: #selector(self.reopenButtonTapped), for: .touchUpInside)
navBar.addSubview(closeButton)
navBar.addSubview(reopenButton)
navBar.addSubview(titleLabel)
view.addSubview(navBar)

Expand Down Expand Up @@ -192,7 +203,13 @@ internal final class FloatingTweakGroupViewController: UIViewController {
return mask
}()

closeButton.frame = CGRect(origin: .zero, size: FloatingTweakGroupViewController.closeButtonSize)
closeButton.frame = CGRect(origin: .zero, size: FloatingTweakGroupViewController.navBarButtonSize)
reopenButton.sizeToFit()
reopenButton.frame = CGRect(
origin: CGPoint(
x: navBar.bounds.width - FloatingTweakGroupViewController.navBarButtonSize.width,
y: 0),
size: FloatingTweakGroupViewController.navBarButtonSize)
titleLabel.frame = CGRect(
origin: CGPoint(
x: closeButton.frame.width,
Expand All @@ -215,6 +232,10 @@ internal final class FloatingTweakGroupViewController: UIViewController {

// MARK: Actions

@objc private func reopenButtonTapped() {
self.presenter.resumeDisplayingMainTweaksInterface()
}

@objc private func closeButtonTapped() {
presenter.dismissFloatingTweaksUI()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "floating-ui-open-tweaks.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "floating-ui-open-tweaks@2x.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "floating-ui-open-tweaks@3x.png",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
94 changes: 50 additions & 44 deletions SwiftTweaks/TweakWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -173,57 +173,56 @@ extension TweakWindow: FloatingTweaksWindowPresenter {

/// Presents a floating TweakGroup over your app's UI, so you don't have to hop in and out of the full-modal Tweak UI.
internal func presentFloatingTweaksUI(forTweakGroup tweakGroup: TweakGroup) {
if (floatingTweakGroupUIWindow == nil) {
let window = HitTransparentWindow()
window.frame = UIScreen.main.bounds
window.backgroundColor = UIColor.clear

var originY = window.frame.size.height - FloatingTweakGroupViewController.height - FloatingTweakGroupViewController.margins
if #available(iOS 11.0, *) {
originY = originY - self.safeAreaInsets.bottom
}
guard floatingTweakGroupUIWindow == nil else { return }

let floatingTweakGroupFrame = CGRect(
origin: CGPoint(
x: FloatingTweakGroupViewController.margins,
y: originY
),
size: CGSize(
width: window.frame.size.width - FloatingTweakGroupViewController.margins*2,
height: FloatingTweakGroupViewController.height
)
)
let window = HitTransparentWindow()
window.frame = UIScreen.main.bounds
window.backgroundColor = UIColor.clear

let floatingTweaksVC = FloatingTweakGroupViewController(frame: floatingTweakGroupFrame, tweakStore: tweakStore, presenter: self)
floatingTweaksVC.tweakGroup = tweakGroup
window.rootViewController = floatingTweaksVC
window.addSubview(floatingTweaksVC.view)

window.alpha = 0
let initialWindowFrame = window.frame.offsetBy(dx: 0, dy: floatingTweaksVC.view.bounds.height)
let destinationWindowFrame = window.frame
window.makeKeyAndVisible()
floatingTweakGroupUIWindow = window

window.frame = initialWindowFrame
UIView.animate(
withDuration: TweakWindow.presentationDuration,
delay: 0,
usingSpringWithDamping: TweakWindow.presentationDamping,
initialSpringVelocity: TweakWindow.presentationVelocity,
options: .beginFromCurrentState,
animations: {
window.frame = destinationWindowFrame
window.alpha = 1
},
completion: nil
)
var originY = window.frame.size.height - FloatingTweakGroupViewController.height - FloatingTweakGroupViewController.margins
if #available(iOS 11.0, *) {
originY = originY - self.safeAreaInsets.bottom
}

let floatingTweakGroupFrame = CGRect(
origin: CGPoint(
x: FloatingTweakGroupViewController.margins,
y: originY
),
size: CGSize(
width: window.frame.size.width - FloatingTweakGroupViewController.margins*2,
height: FloatingTweakGroupViewController.height
)
)

let floatingTweaksVC = FloatingTweakGroupViewController(frame: floatingTweakGroupFrame, tweakStore: tweakStore, presenter: self)
floatingTweaksVC.tweakGroup = tweakGroup
window.rootViewController = floatingTweaksVC
window.addSubview(floatingTweaksVC.view)

window.alpha = 0
let initialWindowFrame = window.frame.offsetBy(dx: 0, dy: floatingTweaksVC.view.bounds.height)
let destinationWindowFrame = window.frame
window.makeKeyAndVisible()
floatingTweakGroupUIWindow = window

window.frame = initialWindowFrame
UIView.animate(
withDuration: TweakWindow.presentationDuration,
delay: 0,
usingSpringWithDamping: TweakWindow.presentationDamping,
initialSpringVelocity: TweakWindow.presentationVelocity,
options: .beginFromCurrentState,
animations: {
window.frame = destinationWindowFrame
window.alpha = 1
},
completion: nil
)
}

/// Dismisses the floating TweakGroup
func dismissFloatingTweaksUI() {

guard let floatingTweakGroupUIWindow = floatingTweakGroupUIWindow else { return }

UIView.animate(
Expand All @@ -240,4 +239,11 @@ extension TweakWindow: FloatingTweaksWindowPresenter {
}
)
}

func resumeDisplayingMainTweaksInterface() {
guard floatingTweakGroupUIWindow != nil else { return }

self.dismissFloatingTweaksUI()
self.presentTweaks()
}
}
1 change: 1 addition & 0 deletions SwiftTweaks/UIImage+SwiftTweaks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ internal extension UIImage {
case disclosureIndicator = "disclosure-indicator"
case floatingPlusButton = "floating-plus-button"
case floatingCloseButton = "floating-ui-close"
case floatingReopenButton = "floating-ui-open-tweaks"
case floatingMinimizedArrow = "floating-ui-minimized-arrow"
}

Expand Down

0 comments on commit c1901a6

Please sign in to comment.