1// Copyright 2020-present 650 Industries. All rights reserved.
2
3#import "EXUpdatesDatabaseManager.h"
4#import <EXUpdates/EXUpdatesUtils.h>
5
6NS_ASSUME_NONNULL_BEGIN
7
8@interface EXUpdatesDatabaseManager ()
9
10@property (nonatomic, strong) NSURL *updatesDirectory;
11@property (nonatomic, strong) EXUpdatesDatabase *database;
12@property (nonatomic, assign) BOOL isDatabaseOpen;
13@property (nonatomic, strong, nullable) NSError *error;
14
15@end
16
17@implementation EXUpdatesDatabaseManager
18
19- (instancetype)init
20{
21  if (self = [super init]) {
22    _database = [[EXUpdatesDatabase alloc] init];
23    _isDatabaseOpen = NO;
24  }
25  return self;
26}
27
28- (NSURL *)updatesDirectory
29{
30  if (!_updatesDirectory) {
31    NSError *fsError;
32    _updatesDirectory = [EXUpdatesUtils initializeUpdatesDirectoryWithError:&fsError];
33    if (fsError) {
34      _error = fsError;
35    }
36  }
37  return _updatesDirectory;
38}
39
40- (BOOL)openDatabase
41{
42  if (!self.updatesDirectory) {
43    return NO;
44  }
45
46  __block BOOL success = NO;
47  __block NSError *dbError;
48  dispatch_sync(self.database.databaseQueue, ^{
49    success = [self.database openDatabaseInDirectory:self.updatesDirectory withError:&dbError];
50  });
51
52  if (dbError) {
53    _error = dbError;
54  }
55  _isDatabaseOpen = success;
56
57  return success;
58}
59
60@end
61
62NS_ASSUME_NONNULL_END
63