Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use support dir instead of document dir #746

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions hive_flutter/lib/hive_flutter.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
library hive_flutter;

import 'dart:async';
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
Expand Down
73 changes: 68 additions & 5 deletions hive_flutter/lib/src/hive_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,77 @@ part of hive_flutter;

/// Flutter extensions for Hive.
extension HiveX on HiveInterface {
/// Initializes Hive with the path from [getApplicationDocumentsDirectory].
/// Initializes Hive with the path from either
/// [getApplicationDocumentsDirectory] (default) or
/// [getApplicationSupportDirectory] ([useSupportDir] = true).
///
/// You can provide a [subDir] where the boxes should be stored.
Future<void> initFlutter([String? subDir]) async {
/// You can provide a [subDir] where the boxes should be stored. if
/// [migrateFromDocuments] is set to true, Boxes in (documents dir + subdir)
/// will be copied over to (support dir + subdir)
///
/// [useSupportDir] cannot be false when [migrateFromDocuments] is true, and
/// vice versa.
Future<void> initFlutter({
bool useSupportDir = false,
bool migrateFromDocuments = false,
String? subDir,
}) async {
// We don't want the developer to migrate from documents and then try to
// init Hive with the documents dir.
assert(useSupportDir == false && migrateFromDocuments == true);

WidgetsFlutterBinding.ensureInitialized();
if (kIsWeb) return;

var appDir = await getApplicationDocumentsDirectory();
init(path_helper.join(appDir.path, subDir));
// Get the directory that Hive uses to stor DB files (dir + subdir).
final supportDir = useSupportDir
? await getApplicationSupportDirectory()
: await getApplicationDocumentsDirectory();

final appDir = path_helper.join(supportDir.path, subDir);

if (migrateFromDocuments) {
// If the app dir doesn't exist, create it. This is usually handled later
// on in Hive but the directory needs to exist to copy old DB files over.
if (!await Directory(appDir).exists()) {
await Directory(appDir).create();
}

// Get the old location that Hive used to store files.
final documentsDir = await getApplicationDocumentsDirectory();
final oldLocationDir =
Directory(path_helper.join(documentsDir.path, subDir));

// If the old location exists, we start looking for old DB files to copy.
if (await oldLocationDir.exists()) {
// Make an array to store files to copy.
final filesToCopy = <File>[];

await oldLocationDir.list().forEach((event) {
final extension = path_helper.extension(event.path);

// If the item is a file and has an extension of .hive, add it and the
// matching .lock file to the copy array.
if (event is File && extension == '.hive') {
filesToCopy.add(File(event.path));

// We do this instead of just copying all .lock files since .lock
// can commonly be used for non-Hive purposes.
filesToCopy
.add(File('${path_helper.withoutExtension(event.path)}.lock'));
}
});

// Make copy/delete futures for every file and wait for them
// asynchronously. Files are copied to the new appDir.
await Future.wait(filesToCopy.map(
(e) => e
.copy(path_helper.join(appDir, path_helper.basename(e.path)))
.then((_) => e.delete()),
));
}
}

init(appDir);
}
}