2018-01-26 00:14:22 +01:00
|
|
|
//
|
|
|
|
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2018-01-30 21:05:04 +01:00
|
|
|
#import "OWSMessageUtils.h"
|
2018-01-26 00:14:22 +01:00
|
|
|
#import "AppContext.h"
|
2018-04-03 22:46:19 +02:00
|
|
|
#import "MIMETypeUtil.h"
|
|
|
|
#import "OWSMessageSender.h"
|
2018-03-05 15:30:58 +01:00
|
|
|
#import "OWSPrimaryStorage.h"
|
2018-02-07 18:44:09 +01:00
|
|
|
#import "TSAccountManager.h"
|
|
|
|
#import "TSAttachment.h"
|
|
|
|
#import "TSAttachmentStream.h"
|
2018-01-26 00:14:22 +01:00
|
|
|
#import "TSDatabaseView.h"
|
2018-02-07 18:44:09 +01:00
|
|
|
#import "TSIncomingMessage.h"
|
|
|
|
#import "TSMessage.h"
|
|
|
|
#import "TSOutgoingMessage.h"
|
|
|
|
#import "TSQuotedMessage.h"
|
2018-01-30 21:05:04 +01:00
|
|
|
#import "TSThread.h"
|
2018-02-07 18:44:09 +01:00
|
|
|
#import "UIImage+OWS.h"
|
2018-01-26 00:14:22 +01:00
|
|
|
#import <YapDatabase/YapDatabase.h>
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
2018-01-30 21:05:04 +01:00
|
|
|
@interface OWSMessageUtils ()
|
2018-01-26 00:14:22 +01:00
|
|
|
|
|
|
|
@property (nonatomic, readonly) YapDatabaseConnection *dbConnection;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
2018-01-30 21:05:04 +01:00
|
|
|
@implementation OWSMessageUtils
|
2018-01-26 00:14:22 +01:00
|
|
|
|
|
|
|
+ (instancetype)sharedManager
|
|
|
|
{
|
2018-01-30 21:05:04 +01:00
|
|
|
static OWSMessageUtils *sharedMyManager = nil;
|
2018-01-26 00:14:22 +01:00
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
sharedMyManager = [[self alloc] initDefault];
|
|
|
|
});
|
|
|
|
return sharedMyManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)initDefault
|
|
|
|
{
|
2018-03-05 15:30:58 +01:00
|
|
|
OWSPrimaryStorage *primaryStorage = [OWSPrimaryStorage sharedManager];
|
2018-01-26 00:14:22 +01:00
|
|
|
|
2018-03-05 15:30:58 +01:00
|
|
|
return [self initWithPrimaryStorage:primaryStorage];
|
2018-01-26 00:14:22 +01:00
|
|
|
}
|
|
|
|
|
2018-03-05 15:30:58 +01:00
|
|
|
- (instancetype)initWithPrimaryStorage:(OWSPrimaryStorage *)primaryStorage
|
2018-01-26 00:14:22 +01:00
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
|
|
|
|
if (!self) {
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2018-03-05 15:30:58 +01:00
|
|
|
_dbConnection = primaryStorage.newDatabaseConnection;
|
2018-01-26 00:14:22 +01:00
|
|
|
|
|
|
|
OWSSingletonAssert();
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSUInteger)unreadMessagesCount
|
|
|
|
{
|
|
|
|
__block NSUInteger numberOfItems;
|
|
|
|
[self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
|
|
|
|
numberOfItems = [[transaction ext:TSUnreadDatabaseViewExtensionName] numberOfItemsInAllGroups];
|
|
|
|
}];
|
|
|
|
|
|
|
|
return numberOfItems;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSUInteger)unreadMessagesCountExcept:(TSThread *)thread
|
|
|
|
{
|
|
|
|
__block NSUInteger numberOfItems;
|
|
|
|
[self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
|
|
|
|
id databaseView = [transaction ext:TSUnreadDatabaseViewExtensionName];
|
|
|
|
OWSAssert(databaseView);
|
|
|
|
numberOfItems = ([databaseView numberOfItemsInAllGroups] - [databaseView numberOfItemsInGroup:thread.uniqueId]);
|
|
|
|
}];
|
|
|
|
|
|
|
|
return numberOfItems;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)updateApplicationBadgeCount
|
|
|
|
{
|
|
|
|
if (!CurrentAppContext().isMainApp) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSUInteger numberOfItems = [self unreadMessagesCount];
|
|
|
|
[CurrentAppContext() setMainAppBadgeNumber:numberOfItems];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|