Compare commits

...

2 Commits

2 changed files with 35 additions and 13 deletions

View File

@ -51,7 +51,10 @@ enum CompositionAttachmentData {
func getData(completion: @escaping (_ data: Data, _ mimeType: String) -> Void) { func getData(completion: @escaping (_ data: Data, _ mimeType: String) -> Void) {
switch self { switch self {
case let .image(image): case let .image(image):
completion(image.pngData()!, "image/png") // Export as JPEG instead of PNG, otherweise photos straight from the camera are too large
// for Mastodon in its default configuration (max of 10MB).
// The quality of 0.8 was chosen completely arbitrarily, it may need to be tuned in the future.
completion(image.jpegData(compressionQuality: 0.8)!, "image/jpeg")
case let .asset(asset): case let .asset(asset):
if asset.mediaType == .image { if asset.mediaType == .image {
let options = PHImageRequestOptions() let options = PHImageRequestOptions()

View File

@ -14,11 +14,17 @@ class ProfileViewController: UIPageViewController {
weak var mastodonController: MastodonController! weak var mastodonController: MastodonController!
// todo: does this still need to be settable? // This property is optional because MyProfileViewController may not have the user's account ID
var accountID: String! { // when first constructed. It should never be set to nil.
var accountID: String? {
willSet {
if newValue == nil {
fatalError("Do not set ProfileViewController.accountID to nil")
}
}
didSet { didSet {
updateAccountUI()
pageControllers.forEach { $0.accountID = accountID } pageControllers.forEach { $0.accountID = accountID }
loadAccount()
} }
} }
@ -50,7 +56,9 @@ class ProfileViewController: UIPageViewController {
} }
deinit { deinit {
mastodonController.persistentContainer.account(for: accountID)?.decrementReferenceCount() if let accountID = accountID {
mastodonController.persistentContainer.account(for: accountID)?.decrementReferenceCount()
}
} }
override func viewDidLoad() { override func viewDidLoad() {
@ -84,8 +92,12 @@ class ProfileViewController: UIPageViewController {
.receive(on: DispatchQueue.main) .receive(on: DispatchQueue.main)
.sink { [weak self] (_) in self?.updateAccountUI() } .sink { [weak self] (_) in self?.updateAccountUI() }
loadAccount()
}
private func loadAccount() {
guard let accountID = accountID else { return }
if mastodonController.persistentContainer.account(for: accountID) != nil { if mastodonController.persistentContainer.account(for: accountID) != nil {
headerView.updateUI(for: accountID)
updateAccountUI() updateAccountUI()
} else { } else {
let req = Client.getAccount(id: accountID) let req = Client.getAccount(id: accountID)
@ -95,10 +107,6 @@ class ProfileViewController: UIPageViewController {
self.mastodonController.persistentContainer.addOrUpdate(account: account, incrementReferenceCount: true) { (account) in self.mastodonController.persistentContainer.addOrUpdate(account: account, incrementReferenceCount: true) { (account) in
DispatchQueue.main.async { DispatchQueue.main.async {
self.updateAccountUI() self.updateAccountUI()
self.headerView.updateUI(for: self.accountID)
self.pageControllers.forEach {
$0.updateUI(account: account)
}
} }
} }
} }
@ -106,8 +114,17 @@ class ProfileViewController: UIPageViewController {
} }
private func updateAccountUI() { private func updateAccountUI() {
guard let account = mastodonController.persistentContainer.account(for: accountID) else { return } guard let accountID = accountID,
let account = mastodonController.persistentContainer.account(for: accountID) else {
return
}
// Optionally invoke updateUI on headerView because viewDidLoad may not have been called yet
headerView?.updateUI(for: accountID)
navigationItem.title = account.displayNameWithoutCustomEmoji navigationItem.title = account.displayNameWithoutCustomEmoji
pageControllers.forEach {
$0.updateUI(account: account)
}
} }
private func selectPage(at index: Int, animated: Bool, completion: ((Bool) -> Void)? = nil) { private func selectPage(at index: Int, animated: Bool, completion: ((Bool) -> Void)? = nil) {
@ -177,13 +194,15 @@ class ProfileViewController: UIPageViewController {
// MARK: Interaction // MARK: Interaction
@objc private func composeMentioning() { @objc private func composeMentioning() {
if let account = mastodonController.persistentContainer.account(for: accountID) { if let accountID = accountID,
let account = mastodonController.persistentContainer.account(for: accountID) {
compose(mentioningAcct: account.acct) compose(mentioningAcct: account.acct)
} }
} }
private func composeDirectMentioning() { private func composeDirectMentioning() {
if let account = mastodonController.persistentContainer.account(for: accountID) { if let accountID = accountID,
let account = mastodonController.persistentContainer.account(for: accountID) {
let draft = mastodonController.createDraft(mentioningAcct: account.acct) let draft = mastodonController.createDraft(mentioningAcct: account.acct)
draft.visibility = .direct draft.visibility = .direct
compose(editing: draft) compose(editing: draft)