Return to conversation after deleting long text

This commit is contained in:
Michael Kirk 2019-02-26 17:19:18 -07:00
parent 05cc3c00f2
commit b11308b2f8
3 changed files with 87 additions and 7 deletions

View file

@ -141,6 +141,7 @@ typedef enum : NSUInteger {
ConversationViewLayoutDelegate,
ConversationViewCellDelegate,
ConversationInputTextViewDelegate,
LongTextViewDelegate,
MessageActionsDelegate,
MessageDetailViewDelegate,
MenuActionsViewControllerDelegate,
@ -1956,6 +1957,14 @@ typedef enum : NSUInteger {
[self.navigationController popToViewController:self animated:YES];
}
#pragma mark - LongTextViewDelegate
- (void)longTextViewMessageWasDeleted:(LongTextViewController *)longTextViewController
{
OWSLogInfo(@"");
[self.navigationController popToViewController:self animated:YES];
}
#pragma mark - MenuActionsViewControllerDelegate
- (void)menuActionsDidHide:(MenuActionsViewController *)menuActionsViewController
@ -2256,8 +2265,9 @@ typedef enum : NSUInteger {
OWSAssertDebug(conversationItem);
OWSAssertDebug([conversationItem.interaction isKindOfClass:[TSMessage class]]);
LongTextViewController *view = [[LongTextViewController alloc] initWithViewItem:conversationItem];
[self.navigationController pushViewController:view animated:YES];
LongTextViewController *viewController = [[LongTextViewController alloc] initWithViewItem:conversationItem];
viewController.delegate = self;
[self.navigationController pushViewController:viewController animated:YES];
}
- (void)didTapContactShareViewItem:(id<ConversationViewItem>)conversationItem

View file

@ -1,15 +1,30 @@
//
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
//
import Foundation
import SignalServiceKit
import SignalMessaging
@objc
public protocol LongTextViewDelegate {
@objc
func longTextViewMessageWasDeleted(_ longTextViewController: LongTextViewController)
}
@objc
public class LongTextViewController: OWSViewController {
// MARK: Properties
// MARK: - Dependencies
var uiDatabaseConnection: YapDatabaseConnection {
return OWSPrimaryStorage.shared().uiDatabaseConnection
}
// MARK: - Properties
@objc
weak var delegate: LongTextViewDelegate?
let viewItem: ConversationViewItem
@ -55,6 +70,54 @@ public class LongTextViewController: OWSViewController {
createViews()
self.messageTextView.contentOffset = CGPoint(x: 0, y: self.messageTextView.contentInset.top)
NotificationCenter.default.addObserver(self,
selector: #selector(uiDatabaseDidUpdate),
name: .OWSUIDatabaseConnectionDidUpdate,
object: OWSPrimaryStorage.shared().dbNotificationObject)
}
// MARK: - DB
@objc internal func uiDatabaseDidUpdate(notification: NSNotification) {
AssertIsOnMainThread()
guard let notifications = notification.userInfo?[OWSUIDatabaseConnectionNotificationsKey] as? [Notification] else {
owsFailDebug("notifications was unexpectedly nil")
return
}
guard let uniqueId = self.viewItem.interaction.uniqueId else {
Logger.error("Message is missing uniqueId.")
return
}
guard self.uiDatabaseConnection.hasChange(forKey: uniqueId,
inCollection: TSInteraction.collection(),
in: notifications) else {
Logger.debug("No relevant changes.")
return
}
do {
try uiDatabaseConnection.read { transaction in
guard TSInteraction.fetch(uniqueId: uniqueId, transaction: transaction) != nil else {
Logger.error("Message was deleted")
throw LongTextViewError.messageWasDeleted
}
}
} catch LongTextViewError.messageWasDeleted {
DispatchQueue.main.async {
self.delegate?.longTextViewMessageWasDeleted(self)
}
} catch {
owsFailDebug("unexpected error: \(error)")
}
}
enum LongTextViewError: Error {
case messageWasDeleted
}
// MARK: - Create Views

View file

@ -90,7 +90,7 @@ class MessageDetailViewController: OWSViewController, MediaGalleryDataSourceDele
contactShareViewHelper.delegate = self
do {
try updateDBConnectionAndMessageToLatest()
try updateMessageToLatest()
} catch DetailViewError.messageWasDeleted {
self.delegate?.detailViewMessageWasDeleted(self)
} catch {
@ -543,7 +543,7 @@ class MessageDetailViewController: OWSViewController, MediaGalleryDataSourceDele
}
// This method should be called after self.databaseConnection.beginLongLivedReadTransaction().
private func updateDBConnectionAndMessageToLatest() throws {
private func updateMessageToLatest() throws {
AssertIsOnMainThread()
@ -589,7 +589,7 @@ class MessageDetailViewController: OWSViewController, MediaGalleryDataSourceDele
}
do {
try updateDBConnectionAndMessageToLatest()
try updateMessageToLatest()
} catch DetailViewError.messageWasDeleted {
DispatchQueue.main.async {
self.delegate?.detailViewMessageWasDeleted(self)
@ -723,6 +723,7 @@ class MessageDetailViewController: OWSViewController, MediaGalleryDataSourceDele
}
let viewController = LongTextViewController(viewItem: viewItem)
viewController.delegate = self
navigationController.pushViewController(viewController, animated: true)
}
@ -789,3 +790,9 @@ class MessageDetailViewController: OWSViewController, MediaGalleryDataSourceDele
self.dismiss(animated: true)
}
}
extension MessageDetailViewController: LongTextViewDelegate {
func longTextViewMessageWasDeleted(_ longTextViewController: LongTextViewController) {
self.delegate?.detailViewMessageWasDeleted(self)
}
}