Skip to content

Commit

Permalink
Migrate media-export from Legacy-Alert-Handler to new progress-handler
Browse files Browse the repository at this point in the history
  • Loading branch information
zeitschlag committed Jul 25, 2024
1 parent 548406f commit bb3bc05
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 32 deletions.
58 changes: 26 additions & 32 deletions deltachat-ios/Controller/Settings/ChatsAndMediaViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import UIKit
import DcCore
import Intents

internal final class ChatsAndMediaViewController: UITableViewController, LegacyProgressAlertHandler {
internal final class ChatsAndMediaViewController: UITableViewController {

private struct SectionConfigs {
let headerTitle: String?
Expand All @@ -21,14 +21,10 @@ internal final class ChatsAndMediaViewController: UITableViewController, LegacyP

private var dcContext: DcContext
internal let dcAccounts: DcAccounts

// MARK: - LegacyProgressAlertHandler
weak var progressAlert: UIAlertController?
var progressObserver: NSObjectProtocol?
var progressAlertHandler: ProgressAlertHandler?

// MARK: - cells


private lazy var blockedContactsCell: UITableViewCell = {
let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
cell.tag = CellTags.blockedContacts.rawValue
Expand Down Expand Up @@ -135,30 +131,6 @@ internal final class ChatsAndMediaViewController: UITableViewController, LegacyP
updateCells()
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
addProgressAlertListener(dcAccounts: dcAccounts, progressName: Event.importExportProgress) { [weak self] in
guard let self else { return }

self.progressAlert?.dismiss(animated: true) {
let alert = UIAlertController(
title: String.localized("backup_successful"),
message: String.localizedStringWithFormat(String.localized("backup_successful_explain_ios"), "\(String.localized("Files")) ➔ Delta Chat"),
preferredStyle: .alert)
alert.addAction(UIAlertAction(title: String.localized("ok"), style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
}

override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
let nc = NotificationCenter.default
if let progressObserver {
nc.removeObserver(progressObserver)
}
}

// MARK: - UITableViewDelegate + UITableViewDatasource
override func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
Expand Down Expand Up @@ -201,7 +173,23 @@ internal final class ChatsAndMediaViewController: UITableViewController, LegacyP

private func createBackup() {
let alert = UIAlertController(title: String.localized("pref_backup_export_explain"), message: nil, preferredStyle: .safeActionSheet)
alert.addAction(UIAlertAction(title: String.localized("pref_backup_export_start_button"), style: .default, handler: { _ in
alert.addAction(UIAlertAction(title: String.localized("pref_backup_export_start_button"), style: .default, handler: { [weak self] _ in
guard let self else { return }

let progressHandler = ProgressAlertHandler(dcAccounts: self.dcAccounts, notification: Event.importExportProgress) { [weak self] in
guard let self else { return }

let alert = UIAlertController(
title: String.localized("backup_successful"),
message: String.localizedStringWithFormat(String.localized("backup_successful_explain_ios"), "\(String.localized("Files")) ➔ Delta Chat"),
preferredStyle: .alert)
alert.addAction(UIAlertAction(title: String.localized("ok"), style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
progressHandler.dataSource = self

self.progressAlertHandler = progressHandler

self.dismiss(animated: true, completion: nil)
self.startImex(what: DC_IMEX_EXPORT_BACKUP)
}))
Expand All @@ -217,7 +205,7 @@ internal final class ChatsAndMediaViewController: UITableViewController, LegacyP
private func startImex(what: Int32, passphrase: String? = nil) {
let documents = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
if !documents.isEmpty {
showProgressAlert(title: String.localized("export_backup_desktop"), dcContext: dcContext)
progressAlertHandler?.showProgressAlert(title: String.localized("export_backup_desktop"), dcContext: dcContext)
DispatchQueue.main.async {
self.dcAccounts.stopIo()
self.dcContext.imex(what: what, directory: documents[0], passphrase: passphrase)
Expand Down Expand Up @@ -255,3 +243,9 @@ internal final class ChatsAndMediaViewController: UITableViewController, LegacyP
navigationController?.pushViewController(settingsAutodelOverviewController, animated: true)
}
}

extension ChatsAndMediaViewController: ProgressAlertHandlerDataSource {
func viewController() -> UIViewController {
self
}
}
1 change: 1 addition & 0 deletions deltachat-ios/Handler/LegacyProgressAlertHandler.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import UIKit
import DcCore

@available(*, deprecated, message: "Replace with ProgressAlertHandler-class")
protocol LegacyProgressAlertHandler: UIViewController {
var progressAlert: UIAlertController? { get set } // needs to be implemented as weak
var progressObserver: NSObjectProtocol? { get set } // set to nil in viewDidDisappear
Expand Down

0 comments on commit bb3bc05

Please sign in to comment.