Skip to content

Commit

Permalink
Add company settings
Browse files Browse the repository at this point in the history
  • Loading branch information
ecotopian committed May 16, 2024
1 parent 63e91f8 commit 1bda975
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 7 deletions.
4 changes: 4 additions & 0 deletions Client/Coordinators/Browser/BrowserCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ class BrowserCoordinator: BaseCoordinator,
}
self.privateViewController = privateHomepageController
}

func reloadHomePage() {
homepageViewController?.reloadView()
}

// MARK: - PrivateHomepageDelegate

Expand Down
3 changes: 3 additions & 0 deletions Client/Coordinators/Browser/BrowserDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@ protocol BrowserDelegate: AnyObject {

/// This is called the browser is ready to start navigating, ensuring we are in the required state to perform deeplinks
func browserHasLoaded()

/// Reload NTP
func reloadHomePage()
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import Common
import UIKit
import Shared
import Core

extension AppSettingsTableViewController {

Expand All @@ -23,6 +24,14 @@ extension AppSettingsTableViewController {
sections.insert(getSearchSection(), at: 1)
}

if User.shared.company != nil {
let company = CompanySetting(settings: self)
let about = AboutEcosiaForCompaniesSetting()

let section = SettingSection(title: NSAttributedString(string: "Ecosia for Companies"), children: [company, about])
sections.insert(section, at: 4)
}

if isDebugSectionEnabled {
sections.append(getEcosiaDebugSupportSection())
}
Expand Down
47 changes: 47 additions & 0 deletions Client/Ecosia/Settings/EcosiaSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,53 @@ final class QuickSearchSearchSetting: Setting {
}
}

final class CompanySetting: Setting {
let settings: SettingsTableViewController

init(settings: SettingsTableViewController) {
self.settings = settings
let companyName = User.shared.company?.name ?? ""
super.init(title: NSAttributedString(string: "Connected to \(companyName)", attributes: [NSAttributedString.Key.foregroundColor: UIColor.legacyTheme.tableView.rowText]))
}

override var status: NSAttributedString? {
return NSAttributedString(string: "Click to disconnect", attributes: [NSAttributedString.Key.foregroundColor: UIColor.legacyTheme.tableView.rowText])
}

override func onClick(_ navigationController: UINavigationController?) {
let alert = UIAlertController(title: "Disconnect from company", message: "Do you want to disconnect this iPhone from your company?", preferredStyle: .alert)
let cancel = UIAlertAction(title: "No", style: .cancel)
let ok = UIAlertAction(title: "Yes", style: .destructive) { _ in
User.shared.company = nil

DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(300)) {
// this will trigger a settings reload
self.settings.applyTheme()
NotificationCenter.default.post(name: .HomePanelPrefsChanged, object: nil)
}
// this will trigger the NTP to reload
}
alert.addAction(cancel)
alert.addAction(ok)
settings.present(alert, animated: true)
}

}

final class AboutEcosiaForCompaniesSetting: Setting {
override var title: NSAttributedString? {
return NSAttributedString(string: "About Ecosia for Companies", attributes: [NSAttributedString.Key.foregroundColor: UIColor.legacyTheme.tableView.rowText])
}

override var url: URL? {
return URL(string: "https://companies.ecosia.org")
}

override func onClick(_ navigationController: UINavigationController?) {
setUpAndPushSettingsContentViewController(navigationController, self.url)
}
}

extension Setting {
// Helper method to set up and push a SettingsContentViewController
func setUpAndPushSettingsContentViewController(_ navigationController: UINavigationController?, _ url: URL? = nil) {
Expand Down
24 changes: 19 additions & 5 deletions Client/Ecosia/UI/NTP/Logo/NTPLogoCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ final class NTPLogoCell: UICollectionViewCell, ReusableCell, Themeable {
static let bottomMargin: CGFloat = 6
static let width: CGFloat = 144

private weak var logo: UIImageView!
private weak var orgLogo: UIImageView!
private weak var logoStack: UIStackView!
private var logo: UIImageView!
private var orgLogo: UIImageView!
private var logoStack: UIStackView!
private var logoLabel: UILabel!

// MARK: - Themeable Properties

Expand All @@ -38,7 +39,7 @@ final class NTPLogoCell: UICollectionViewCell, ReusableCell, Themeable {
logoStack.translatesAutoresizingMaskIntoConstraints = false
logoStack.axis = .vertical
logoStack.distribution = .fill
logoStack.spacing = 12
logoStack.spacing = 16
self.logoStack = logoStack

let orgLogo = UIImageView(image: .init())
Expand All @@ -48,7 +49,15 @@ final class NTPLogoCell: UICollectionViewCell, ReusableCell, Themeable {
orgLogo.heightAnchor.constraint(equalToConstant: 20).isActive = true
self.orgLogo = orgLogo
logoStack.addArrangedSubview(orgLogo)


let logoLabel = UILabel()
logoLabel.translatesAutoresizingMaskIntoConstraints = false
logoLabel.text = "&"
logoLabel.textAlignment = .center
logoLabel.font = .preferredFont(forTextStyle: .subheadline)
logoLabel.adjustsFontForContentSizeCategory = true
logoStack.addArrangedSubview(logoLabel)
self.logoLabel = logoLabel

let logo = UIImageView(image: .init(named: "ecosiaLogoLaunch")?.withRenderingMode(.alwaysTemplate))
logo.translatesAutoresizingMaskIntoConstraints = false
Expand All @@ -75,17 +84,22 @@ final class NTPLogoCell: UICollectionViewCell, ReusableCell, Themeable {

func applyTheme() {
logo.tintColor = .legacyTheme.ecosia.primaryBrand
logoLabel.textColor = .legacyTheme.ecosia.secondaryText

if let company = User.shared.company {
let imageName = LegacyThemeManager.instance.current.isDark ? company.logoDark : company.logoLight
let baseURL = Environment.current.urlProvider.companiesBase.absoluteString
let finalURLString = baseURL + "/logos/" + imageName
let finalUrl = URL(string: finalURLString)
logoLabel.isHidden = false
orgLogo.isHidden = false
orgLogo.kf.setImage(with: finalUrl, options: [.processor(SVGImgProcessor())]) { result in
debugPrint(result)
}
} else {
orgLogo.image = nil
orgLogo.isHidden = true
logoLabel.isHidden = true
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1514,10 +1514,12 @@ class BrowserViewController: UIViewController,
@MainActor
func presentCompanyAlert(for company: Company) {
let alert = UIAlertController(title: "Congratulations!", message: "Your searches are now planting trees for \(company.name) ", preferredStyle: .alert)
alert.addAction(.init(title: "OK", style: .default))
alert.addAction(.init(title: "OK", style: .default, handler: { _ in
self.browserDelegate?.reloadHomePage()
self.openBlankNewTab(focusLocationField: false)
}))
present(alert, animated: true)

openBlankNewTab(focusLocationField: false)
}

func handle(url: URL?, isPrivate: Bool, options: Set<Route.SearchOptions>? = nil) {
Expand Down

0 comments on commit 1bda975

Please sign in to comment.