Skip to content
Snippets Groups Projects
SettingsAdvancedController.swift 3.37 KiB
Newer Older
Bruno Muniz's avatar
Bruno Muniz committed
import UIKit
import Shared
import Combine
import DependencyInjection

public final class SettingsAdvancedController: UIViewController {
Bruno Muniz's avatar
Bruno Muniz committed
    @Dependency private var coordinator: SettingsCoordinating

    lazy private var screenView = SettingsAdvancedView()
Bruno Muniz's avatar
Bruno Muniz committed

    private var cancellables = Set<AnyCancellable>()
    private let viewModel = SettingsAdvancedViewModel()
Bruno Muniz's avatar
Bruno Muniz committed

    public override func loadView() {
Bruno Muniz's avatar
Bruno Muniz committed
        view = screenView
    }

    public override func viewWillAppear(_ animated: Bool) {
Bruno Muniz's avatar
Bruno Muniz committed
        super.viewWillAppear(animated)
        navigationItem.backButtonTitle = ""
Bruno Muniz's avatar
Bruno Muniz committed
        navigationController?.navigationBar
            .customize(backgroundColor: Asset.neutralWhite.color)
    }

    public override func viewDidLoad() {
Bruno Muniz's avatar
Bruno Muniz committed
        super.viewDidLoad()
        setupNavigationBar()
        setupBindings()

        viewModel.loadCachedSettings()
    }

    private func setupNavigationBar() {
        let title = UILabel()
        title.text = Localized.Settings.Advanced.title
        title.textColor = Asset.neutralActive.color
        title.font = Fonts.Mulish.semiBold.font(size: 18.0)

        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: title)
        navigationItem.leftItemsSupplementBackButton = true
Bruno Muniz's avatar
Bruno Muniz committed
    }

    private func setupBindings() {
        screenView.downloadLogsButton
Bruno Muniz's avatar
Bruno Muniz committed
            .publisher(for: .touchUpInside)
            .sink { [weak viewModel] in viewModel?.didTapDownloadLogs() }
            .store(in: &cancellables)

        screenView.logRecordingSwitcher.switcherView
Bruno Muniz's avatar
Bruno Muniz committed
            .publisher(for: .valueChanged)
            .sink { [weak viewModel] in viewModel?.didToggleRecordLogs() }
            .store(in: &cancellables)

Ahmed Shehata's avatar
Ahmed Shehata committed
        screenView.showUsernamesSwitcher.switcherView
            .publisher(for: .valueChanged)
            .sink { [weak viewModel] in viewModel?.didToggleShowUsernames() }
            .store(in: &cancellables)

        screenView.crashReportingSwitcher.switcherView
Bruno Muniz's avatar
Bruno Muniz committed
            .publisher(for: .valueChanged)
            .sink { [weak viewModel] in viewModel?.didToggleCrashReporting() }
            .store(in: &cancellables)

        screenView.reportingSwitcher.switcherView
            .publisher(for: .valueChanged)
Bruno Muniz's avatar
Bruno Muniz committed
            .compactMap { [weak screenView] _ in screenView?.reportingSwitcher.switcherView.isOn }
            .sink { [weak viewModel] isOn in viewModel?.didSetReporting(enabled: isOn) }
        viewModel.sharePublisher
            .receive(on: DispatchQueue.main)
            .sink { [unowned self] in coordinator.toActivityController(with: [$0], from: self) }
            .store(in: &cancellables)

        viewModel.state
            .removeDuplicates()
            .map(\.isReportingOptional)
            .sink { [unowned self] in screenView.reportingSwitcher.isHidden = !$0 }
            .store(in: &cancellables)

Bruno Muniz's avatar
Bruno Muniz committed
        viewModel.state
            .removeDuplicates()
            .sink { [unowned self] state in
                screenView.logRecordingSwitcher.switcherView.setOn(state.isRecordingLogs, animated: true)
                screenView.crashReportingSwitcher.switcherView.setOn(state.isCrashReporting, animated: true)
Ahmed Shehata's avatar
Ahmed Shehata committed
                screenView.showUsernamesSwitcher.switcherView.setOn(state.isShowingUsernames, animated: true)
                screenView.reportingSwitcher.switcherView.setOn(state.isReportingEnabled, animated: true)
Bruno Muniz's avatar
Bruno Muniz committed
            }.store(in: &cancellables)
    }
}