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