Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Taking out zoom in out logic into a handler
Browse files Browse the repository at this point in the history
  • Loading branch information
soner-yuksel committed Nov 10, 2023
1 parent 381a2b6 commit acee281
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 134 deletions.
14 changes: 8 additions & 6 deletions Sources/Brave/Frontend/Browser/BrowserViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,8 @@ public class BrowserViewController: UIViewController {
self?.tabManager.allTabs.forEach({
guard let url = $0.webView?.url else { return }
let zoomLevel = self?.privateBrowsingManager.isPrivateBrowsing == true ? 1.0 : Domain.getPersistedDomain(for: url)?.zoom_level?.doubleValue ?? Preferences.General.defaultPageZoomLevel.value
$0.webView?.setValue(zoomLevel, forKey: PageZoomView.propertyName)

$0.webView?.setValue(zoomLevel, forKey: PageZoomHandler.propertyName)
})
}

Expand Down Expand Up @@ -2285,9 +2286,10 @@ public class BrowserViewController: UIViewController {
return
}

guard let webView = tabManager.selectedTab?.webView else { return }
let pageZoomBar = UIHostingController(rootView: PageZoomView(webView: webView, isPrivateBrowsing: privateBrowsingManager.isPrivateBrowsing))

guard let selectTab = tabManager.selectedTab else { return }
let zoomHandler = PageZoomHandler(tab: selectTab, isPrivateBrowsing: privateBrowsingManager.isPrivateBrowsing)
let pageZoomBar = UIHostingController(rootView: PageZoomView(zoomHandler: zoomHandler))

pageZoomBar.rootView.dismiss = { [weak self] in
guard let self = self else { return }
pageZoomBar.view.removeFromSuperview()
Expand Down Expand Up @@ -2324,7 +2326,7 @@ public class BrowserViewController: UIViewController {
let domain = Domain.getPersistedDomain(for: currentURL)

let zoomLevel = privateBrowsingManager.isPrivateBrowsing ? 1.0 : domain?.zoom_level?.doubleValue ?? Preferences.General.defaultPageZoomLevel.value
tab.webView?.setValue(zoomLevel, forKey: PageZoomView.propertyName)
tab.webView?.setValue(zoomLevel, forKey: PageZoomHandler.propertyName)
}
}

Expand Down Expand Up @@ -3127,7 +3129,7 @@ extension BrowserViewController: PreferencesObserver {
tabManager.allTabs.forEach({
guard let url = $0.webView?.url else { return }
let zoomLevel = $0.isPrivate ? 1.0 : Domain.getPersistedDomain(for: url)?.zoom_level?.doubleValue ?? Preferences.General.defaultPageZoomLevel.value
$0.webView?.setValue(zoomLevel, forKey: PageZoomView.propertyName)
$0.webView?.setValue(zoomLevel, forKey: PageZoomHandler.propertyName)
})
case Preferences.Shields.httpsEverywhere.key:
tabManager.reset()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ import Preferences

extension BrowserViewController {

private enum PageZoomChangeStatus {
case increment, decrement
}

// MARK: Actions

@objc private func reloadTabKeyCommand() {
Expand Down Expand Up @@ -204,48 +200,12 @@ extension BrowserViewController {
changeZoomLevel(.decrement)
}

private func changeZoomLevel(_ status: PageZoomChangeStatus) {
guard let webView = tabManager.selectedTab?.webView else { return }

let steps = [0.5, 0.75, 0.85,
1.0, 1.15, 1.25,
1.50, 1.75, 2.00,
2.50, 3.0]

var currentValue: Double
let propertyName = "viewScale"
private func changeZoomLevel(_ status: PageZoomHandler.ChangeStatus) {
guard let selectTab = tabManager.selectedTab else { return }
let zoomHandler = PageZoomHandler(
tab: selectTab, isPrivateBrowsing: privateBrowsingManager.isPrivateBrowsing)

// Fetch the current value for zoom
if let url = webView.url, let domain = Domain.getPersistedDomain(for: url) {
currentValue = domain.zoom_level?.doubleValue ?? Preferences.General.defaultPageZoomLevel.value
} else {
currentValue = webView.value(forKey: propertyName) as? Double ?? Preferences.General.defaultPageZoomLevel.value
}

switch status {
case .increment:
guard let index = steps.firstIndex(of: currentValue),
index + 1 < steps.count else { return }

currentValue = steps[index + 1]
case .decrement:
guard let index = PageZoomView.steps.firstIndex(of: currentValue),
index - 1 >= 0 else { return }
currentValue = PageZoomView.steps[index - 1]
}

// Setting the value
guard let url = webView.url else { return }
webView.setValue(currentValue, forKey: propertyName)

// Do NOT store the changes in the Domain if private domain
if !privateBrowsingManager.isPrivateBrowsing {
let domain = Domain.getPersistedDomain(for: url)?.then {
$0.zoom_level = currentValue == $0.zoom_level?.doubleValue ? nil : NSNumber(value: currentValue)
}

try? domain?.managedObjectContext?.save()
}
zoomHandler.changeZoomLevel(status)
}

// MARK: KeyCommands
Expand Down
84 changes: 84 additions & 0 deletions Sources/Brave/Frontend/Browser/PageZoom/PageZoomHandler.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright 2023 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

import Foundation
import Shared
import Data
import Preferences

class PageZoomHandler: ObservableObject {

enum ChangeStatus {
case increment, decrement
}

private weak var webView: BraveWebView?
let isPrivateBrowsing: Bool

static let steps = [0.5, 0.75, 0.85,
1.0, 1.15, 1.25,
1.50, 1.75, 2.00,
2.50, 3.0]
static let propertyName = "viewScale"
@Published var currentValue: Double = 1.0

required init(tab: Tab, isPrivateBrowsing: Bool) {
self.webView = tab.webView
self.isPrivateBrowsing = isPrivateBrowsing

// Private Browsing on Safari iOS always defaults to 100%, and isn't persistently saved.
if isPrivateBrowsing {
currentValue = 1.0
return
}

if let webView = webView {
// Fetch the current value for zoom
if let url = webView.url, let domain = Domain.getPersistedDomain(for: url) {
currentValue = domain.zoom_level?.doubleValue ?? Preferences.General.defaultPageZoomLevel.value
} else {
currentValue = webView.value(forKey: Self.propertyName) as? Double ?? Preferences.General.defaultPageZoomLevel.value
}
}
}

func changeZoomLevel(_ status: ChangeStatus) {
switch status {
case .increment:
guard let index = Self.steps.firstIndex(of: currentValue),
index + 1 < Self.steps.count else { return }

currentValue = Self.steps[index + 1]
case .decrement:
guard let index = Self.steps.firstIndex(of: currentValue),
index - 1 >= 0 else { return }
currentValue = Self.steps[index - 1]
}

// Setting the value
storeChanges()
}

func reset() {
currentValue = Preferences.General.defaultPageZoomLevel.value
storeChanges()
}

private func storeChanges() {
guard let webView = webView,
let url = webView.url else { return }

webView.setValue(currentValue, forKey: PageZoomHandler.propertyName)

// Do NOT store the changes in the Domain
if !isPrivateBrowsing {
let domain = Domain.getPersistedDomain(for: url)?.then {
$0.zoom_level = currentValue == $0.zoom_level?.doubleValue ? nil : NSNumber(value: currentValue)
}

try? domain?.managedObjectContext?.save()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ struct PageZoomSettingsView: View {
.font(.subheadline)
.foregroundColor(Color(.secondaryBraveLabel))
) {
ForEach(PageZoomView.steps, id: \.self) { step in
ForEach(PageZoomHandler.steps, id: \.self) { step in
Button(action: {
resetDefaultZoomLevel(zoomLevel: step)
}, label: {
Expand Down
112 changes: 30 additions & 82 deletions Sources/Brave/Frontend/Browser/PageZoom/PageZoomView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ private struct ZoomView: View {
var maxValue: Double
@Binding var value: Double

var onDecrement: () -> Void
var onReset: () -> Void
var onIncrement: () -> Void
var onDecrement: ()
var onReset: ()
var onIncrement: ()

var body: some View {
HStack(spacing: 0.0) {
Button(action: onDecrement) {
Button(action: {
onDecrement
}) {
Image(systemName: "minus")
.font(.system(.footnote).weight(.medium))
.foregroundColor(value == minValue ? .accentColor : Color(UIColor.braveLabel))
Expand All @@ -45,7 +47,9 @@ private struct ZoomView: View {

Divider()

Button(action: onIncrement) {
Button(action: {
onIncrement
}) {
Image(systemName: "plus")
.font(.system(.footnote).weight(.medium))
.foregroundColor(value == maxValue ? .accentColor : Color(UIColor.braveLabel))
Expand All @@ -60,7 +64,9 @@ private struct ZoomView: View {
}

private var resetZoomButton: some View {
Button(action: onReset) {
Button(action: {
onReset
}) {
Text(NSNumber(value: value), formatter: PageZoomView.percentFormatter)
.font(.system(.footnote).weight(.medium))
.foregroundColor((value == (isPrivateBrowsing ? 1.0 : Preferences.General.defaultPageZoomLevel.value)) ? .accentColor : Color(UIColor.braveLabel))
Expand All @@ -75,11 +81,10 @@ private struct ZoomView: View {
struct PageZoomView: View {
@Environment(\.managedObjectContext) private var context

private var webView: WKWebView?
@ObservedObject private var zoomHandler: PageZoomHandler
private let isPrivateBrowsing: Bool
@State private var minValue = 0.5
@State private var maxValue = 3.0
@State private var currentValue: Double

public static let percentFormatter = NumberFormatter().then {
$0.numberStyle = .percent
Expand All @@ -89,35 +94,14 @@ struct PageZoomView: View {
$0.minimumFractionDigits = 0
}

public static let propertyName = "viewScale"
public static let notificationName = Notification.Name(rawValue: "com.brave.pagezoom-change")

public static let steps = [0.5, 0.75, 0.85,
1.0, 1.15, 1.25,
1.50, 1.75, 2.00,
2.50, 3.0]

var dismiss: (() -> Void)?

init(webView: WKWebView?, isPrivateBrowsing: Bool) {
self.webView = webView
self.isPrivateBrowsing = isPrivateBrowsing

// Private Browsing on Safari iOS always defaults to 100%, and isn't persistently saved.
if isPrivateBrowsing {
_currentValue = State(initialValue: 1.0)
return
}
init(zoomHandler: PageZoomHandler) {
self.zoomHandler = zoomHandler
self.isPrivateBrowsing = zoomHandler.isPrivateBrowsing

// We never re-init, so
// it is okay to initialize state here.
if let url = webView?.url,
let domain = Domain.getPersistedDomain(for: url) {

_currentValue = State(initialValue: domain.zoom_level?.doubleValue ?? Preferences.General.defaultPageZoomLevel.value)
} else {
_currentValue = State(initialValue: webView?.value(forKey: PageZoomView.propertyName) as? Double ?? Preferences.General.defaultPageZoomLevel.value)
}
}

var body: some View {
Expand All @@ -127,17 +111,15 @@ struct PageZoomView: View {
Text(Strings.PageZoom.zoomViewText)
.font(.system(.subheadline))
.frame(maxWidth: .infinity, alignment: .leading)

ZoomView(
isPrivateBrowsing: isPrivateBrowsing,
minValue: minValue,
maxValue: maxValue,
value: $currentValue,
onDecrement: decrement,
onReset: reset,
onIncrement: increment)
.frame(maxWidth: .infinity)

value: $zoomHandler.currentValue,
onDecrement: zoomHandler.changeZoomLevel(.decrement),
onReset: zoomHandler.reset(),
onIncrement: zoomHandler.changeZoomLevel(.increment))
.frame(maxWidth: .infinity)
Button {
dismiss?()
} label: {
Expand All @@ -152,48 +134,14 @@ struct PageZoomView: View {
}
.background(Color(UIColor.braveBackground))
}

private func storeChanges() {
guard let webView = webView,
let url = webView.url else { return }

webView.setValue(currentValue, forKey: PageZoomView.propertyName)

// Do NOT store the changes in the Domain
if !isPrivateBrowsing {
let domain = Domain.getPersistedDomain(for: url)?.then {
$0.zoom_level = currentValue == $0.zoom_level?.doubleValue ? nil : NSNumber(value: currentValue)
}

try? domain?.managedObjectContext?.save()
}
}

private func increment() {
guard let index = PageZoomView.steps.firstIndex(of: currentValue),
index + 1 < PageZoomView.steps.count else { return }
currentValue = PageZoomView.steps[index + 1]
storeChanges()
}

private func reset() {
currentValue = Preferences.General.defaultPageZoomLevel.value
storeChanges()
}

private func decrement() {
guard let index = PageZoomView.steps.firstIndex(of: currentValue),
index - 1 >= 0 else { return }
currentValue = PageZoomView.steps[index - 1]
storeChanges()
}
}

#if DEBUG
struct PageZoomView_Previews: PreviewProvider {
static var previews: some View {
PageZoomView(webView: nil, isPrivateBrowsing: false)
.previewLayout(PreviewLayout.sizeThatFits)
}
}
#endif
//
//#if DEBUG
//struct PageZoomView_Previews: PreviewProvider {
// static var previews: some View {
// PageZoomView(zoomHandler: nil)
// .previewLayout(PreviewLayout.sizeThatFits)
// }
//}
//#endif

0 comments on commit acee281

Please sign in to comment.