-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add InstantOnboardingViewController, copied from SelfProfileViewContr…
…oller
- Loading branch information
Showing
3 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
deltachat-ios/Controller/AccountSetup/InstantOnboardingViewController.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import UIKit | ||
import DcCore | ||
|
||
class InstantOnboardingViewController: UITableViewController, MediaPickerDelegate { | ||
|
||
private struct SectionConfigs { | ||
let headerTitle: String? | ||
let footerTitle: String? | ||
let cells: [UITableViewCell] | ||
} | ||
|
||
private let dcContext: DcContext | ||
private let dcAccounts: DcAccounts | ||
|
||
private lazy var mediaPicker: MediaPicker? = { | ||
let mediaPicker = MediaPicker(dcContext: dcContext, navigationController: navigationController) | ||
mediaPicker.delegate = self | ||
return mediaPicker | ||
}() | ||
|
||
private lazy var avatarSelectionCell: AvatarSelectionCell = { | ||
return AvatarSelectionCell(image: dcContext.getSelfAvatarImage()) | ||
}() | ||
|
||
private lazy var nameCell: TextFieldCell = { | ||
let cell = TextFieldCell(description: String.localized("pref_your_name"), placeholder: String.localized("pref_your_name")) | ||
cell.setText(text: dcContext.displayname) | ||
cell.textFieldDelegate = self | ||
cell.textField.returnKeyType = .default | ||
return cell | ||
}() | ||
|
||
private lazy var sections: [SectionConfigs] = { | ||
let nameSection = SectionConfigs( | ||
headerTitle: nil, | ||
footerTitle: String.localized("set_name_and_avatar_explain"), | ||
cells: [nameCell, avatarSelectionCell] | ||
) | ||
return [nameSection] | ||
}() | ||
|
||
init(dcAccounts: DcAccounts) { | ||
self.dcAccounts = dcAccounts | ||
self.dcContext = dcAccounts.getSelected() | ||
super.init(style: .grouped) | ||
hidesBottomBarWhenPushed = true | ||
} | ||
|
||
required init?(coder aDecoder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
title = String.localized("pref_profile_info_headline") | ||
avatarSelectionCell.onAvatarTapped = { [weak self] in | ||
self?.onAvatarTapped() | ||
} | ||
tableView.rowHeight = UITableView.automaticDimension | ||
} | ||
|
||
override func viewWillDisappear(_ animated: Bool) { | ||
dcContext.displayname = nameCell.getText() | ||
} | ||
|
||
// MARK: - Table view data source | ||
override func numberOfSections(in tableView: UITableView) -> Int { | ||
return sections.count | ||
} | ||
|
||
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | ||
return sections[section].cells.count | ||
} | ||
|
||
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | ||
return sections[indexPath.section].cells[indexPath.row] | ||
} | ||
|
||
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { | ||
return sections[section].headerTitle | ||
} | ||
|
||
override func tableView(_: UITableView, titleForFooterInSection section: Int) -> String? { | ||
return sections[section].footerTitle | ||
} | ||
|
||
// MARK: - actions | ||
private func galleryButtonPressed(_ action: UIAlertAction) { | ||
mediaPicker?.showPhotoGallery() | ||
} | ||
|
||
private func cameraButtonPressed(_ action: UIAlertAction) { | ||
mediaPicker?.showCamera(allowCropping: true, supportedMediaTypes: .photo) | ||
} | ||
|
||
private func deleteProfileIconPressed(_ action: UIAlertAction) { | ||
dcContext.selfavatar = nil | ||
updateAvatarCell() | ||
} | ||
|
||
private func onAvatarTapped() { | ||
let alert = UIAlertController(title: String.localized("pref_profile_photo"), message: nil, preferredStyle: .safeActionSheet) | ||
alert.addAction(PhotoPickerAlertAction(title: String.localized("camera"), style: .default, handler: cameraButtonPressed(_:))) | ||
alert.addAction(PhotoPickerAlertAction(title: String.localized("gallery"), style: .default, handler: galleryButtonPressed(_:))) | ||
if dcContext.getSelfAvatarImage() != nil { | ||
alert.addAction(UIAlertAction(title: String.localized("delete"), style: .destructive, handler: deleteProfileIconPressed(_:))) | ||
} | ||
alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil)) | ||
|
||
self.present(alert, animated: true, completion: nil) | ||
} | ||
|
||
func onImageSelected(image: UIImage) { | ||
AvatarHelper.saveSelfAvatarImage(dcContext: dcContext, image: image) | ||
updateAvatarCell() | ||
} | ||
|
||
private func updateAvatarCell() { | ||
self.avatarSelectionCell.setAvatar(image: dcContext.getSelfAvatarImage()) | ||
} | ||
|
||
} | ||
|
||
|
||
extension InstantOnboardingViewController: UITextFieldDelegate { | ||
|
||
func textFieldShouldReturn(_ textField: UITextField) -> Bool { | ||
textField.resignFirstResponder() | ||
return true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters