Skip to content

Commit

Permalink
Modify RecentHistoryProvider to open FileModificationHistory box
Browse files Browse the repository at this point in the history
  • Loading branch information
predatorx7 committed Nov 22, 2020
1 parent 69cb06a commit 5eba6c7
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 40 deletions.
1 change: 1 addition & 0 deletions org.purplegraphite.code/lib/src/common/strings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class StorageBoxNames {
static const _base = 'org.purplegraphite.code';
static const HISTORY = '$_base-history';
static const THEME_SETTINGS = '$_base-themesettings';
static const FILE_MODIFICATION_HISTORY = '$_base-fileModificationHistory';
}

const _legalese = """BSD 3-Clause License
Expand Down
24 changes: 18 additions & 6 deletions org.purplegraphite.code/lib/src/models/hive/history.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@ part 'history.g.dart';
class FileModificationHistory extends HiveObject {
@HiveField(1)
String absolutePath;

@HiveField(2)
DateTime lastModified;
DateTime _lastModified;

DateTime get lastModified => _lastModified;

void updateLastModified() {
_lastModified = DateTime.now();
}

@HiveField(3)
double scrollOffset;
@HiveField(4)
Expand All @@ -17,22 +25,26 @@ class FileModificationHistory extends HiveObject {

@HiveType(typeId: 5)
class History extends HiveObject {
History(this.absolutePathOfEntity) : super();

@HiveField(1)
String workspacePath;
final String absolutePathOfEntity;

@HiveField(2)
FileModificationHistory lastModifiedFileDetails;

@HiveField(3)
DateTime lastModified;
DateTime _lastModified;

DateTime get lastModified => _lastModified;

void setlatestModified() {
lastModified = DateTime.now();
void _updateLastModified() {
_lastModified = DateTime.now();
}

@override
Future<void> save() {
setlatestModified();
_updateLastModified();
return super.save();
}
}
15 changes: 8 additions & 7 deletions org.purplegraphite.code/lib/src/models/hive/history.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions org.purplegraphite.code/lib/src/models/hive/repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,26 @@ class Repository<M> {
/// Initiates Hive for flutter and returns a Hive [Box] wrapped with [Repository].
///
/// Registers M's [adapter] and opens [M] box of name [boxName]. (Creates if doesn't exist)
static Future<Repository<M>> get<M>(
String boxName, TypeAdapter<M> adapter) async {
///
/// Either register Type adapter with [register] or provide the TypeAdapter as the parameter in [adapter]
static Future<Repository<M>> get<M>(String boxName,
[TypeAdapter<M> adapter]) async {
if (!_hadHiveInitialized) {
await Hive.initFlutter();
_hadHiveInitialized = true;
}

Hive.registerAdapter<M>(adapter);
register<M>(adapter);
final Box box = await Hive.openBox<M>(boxName);
return Repository<M>(boxName, adapter, box);
}

static void register<T>(TypeAdapter<T> adapter) {
final _isRegistered = Hive.isAdapterRegistered(adapter.typeId);
if (_isRegistered) return;
Hive.registerAdapter<T>(adapter);
}

/// Check if box is open
bool isBoxOpen() {
return box?.isOpen ?? false;
Expand Down
53 changes: 30 additions & 23 deletions org.purplegraphite.code/lib/src/models/provider/history.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,50 @@ class RecentHistoryProvider extends ChangeNotifier {
}

Repository<History> _history;
Repository<FileModificationHistory> _fileModificationHistory;

bool get hasHistory {
return !(_history?.isRepositoryEmpty ?? true);
}

bool get isInitialized => _history != null;

/// Sets up & Initializes preferences.
Future _setup() async {
_history = await Repository.get<History>(
StorageBoxNames.HISTORY, HistoryAdapter());
Repository.register<FileModificationHistory>(
FileModificationHistoryAdapter());
Repository.register<History>(HistoryAdapter());
_fileModificationHistory =
await Repository.get(StorageBoxNames.FILE_MODIFICATION_HISTORY);
_history = await Repository.get<History>(StorageBoxNames.HISTORY);

notifyListeners();
// _themeSettingsR.listenStream(_onThemeChange); // No current use
}

History searchFor(String path) {
for (History i in _history.iterable()) {
if (i.workspacePath == path) {
return i;
}
}

return null;
}

void add(String path) {

History get(String path) {
final _result = _history.box.get(path);
return _result;
}

void remove() {

}

void update() {

void add(
String path,
String lastModifiedFilePath,
int lastModifiedFileCursorOffset,
double lastModifiedFileScrollOffset,
) async {
final _entry = History(path);
final _lastModifiedFileDetails = FileModificationHistory();
_lastModifiedFileDetails.absolutePath = lastModifiedFilePath;
_lastModifiedFileDetails.cursorOffset = lastModifiedFileCursorOffset;
_lastModifiedFileDetails.scrollOffset = lastModifiedFileScrollOffset;
_lastModifiedFileDetails.updateLastModified();
await _fileModificationHistory.box
.put(lastModifiedFilePath, _lastModifiedFileDetails);
_entry.lastModifiedFileDetails = _lastModifiedFileDetails;
await _history.box.put(path, _entry);
}

void get() {

void update(History history) async {
await _history.box.put(history.absolutePathOfEntity, history);
}
}
2 changes: 1 addition & 1 deletion org.purplegraphite.code/lib/src/models/provider/theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class ThemeProvider with ChangeNotifier {
StorageBoxNames.THEME_SETTINGS, ThemeSettingsAdapter());
if (_themeSettingsR.isRepositoryEmpty) {
print("Creating theme settings for the first time");
_themeSettingsR.box.add(ThemeSettings.manual(0, ThemeMode.system));
await _themeSettingsR.box.add(ThemeSettings.manual(0, ThemeMode.system));
}
_themeChoice = _themeSettingsR.first.themeChoice;
_themeMode = _themeSettingsR.first.themeMode;
Expand Down

0 comments on commit 5eba6c7

Please sign in to comment.