-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
97fbe6a
commit 3a3da43
Showing
42 changed files
with
1,016 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
{ | ||
"files.associations": { | ||
"ostream": "cpp" | ||
} | ||
}, | ||
"java.compile.nullAnalysis.mode": "disabled" | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// This source code is a part of Project Violet. | ||
// Copyright (C) 2020-2023. violet-team. Licensed under the Apache-2.0 License. | ||
|
||
import 'dart:io'; | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:path_provider/path_provider.dart'; | ||
import 'package:sqflite_common_ffi/sqflite_ffi.dart'; | ||
import 'package:sqflite/sqflite.dart'; | ||
import 'package:flutter/services.dart'; | ||
|
||
class SettingsManager { | ||
String? dbPath; | ||
Database? db; | ||
static SettingsManager? _instance; | ||
|
||
SettingsManager({this.dbPath}); | ||
|
||
static SettingsManager create(String dbPath) { | ||
return SettingsManager(dbPath: dbPath); | ||
} | ||
|
||
@protected | ||
@mustCallSuper | ||
void dispose() async { | ||
print('close: ${dbPath!}'); | ||
if (db != null) db!.close(); | ||
} | ||
|
||
static Future<SettingsManager> getInstance() async { | ||
if (_instance == null) { | ||
var home = ''; | ||
if(Platform.isLinux){ | ||
Platform.environment.forEach((key, value) => { | ||
if(key == 'HOME'){ | ||
home = value | ||
} | ||
}); | ||
} | ||
var dbPath = (Platform.isLinux) | ||
? '${home}/.violet/settings.db' | ||
: ''; | ||
if(!(await File(dbPath).exists())){ | ||
await File(dbPath).create(recursive: true); | ||
final sharedLibraryPath = 'assets/db/null.db'; | ||
final sharedLibraryContent = await rootBundle.load(sharedLibraryPath); | ||
|
||
final libraryFile = File('${dbPath}'); | ||
final createdFile = await libraryFile.create(); | ||
final openFile = await createdFile.open(mode: FileMode.write); | ||
final writtenFile = | ||
await openFile.writeFrom(Uint8List.view(sharedLibraryContent.buffer)); | ||
await writtenFile.close(); | ||
|
||
|
||
} | ||
_instance = create(dbPath); | ||
await _instance!.open(); | ||
} | ||
return _instance!; | ||
} | ||
|
||
static Future<void> reloadInstance() async { | ||
var home = ''; | ||
if(Platform.isLinux){ | ||
Platform.environment.forEach((key, value) => { | ||
if(key == 'HOME'){ | ||
home = value | ||
} | ||
}); | ||
} | ||
var dbPath = (Platform.isLinux) | ||
? '${home}/.violet/settings.db' | ||
: ''; | ||
_instance = create(dbPath); | ||
} | ||
|
||
Future open() async { | ||
db ??= await openDatabase(dbPath!); | ||
} | ||
|
||
Future checkOpen() async { | ||
if(db != null){ | ||
if (!db!.isOpen) db = await openDatabase(dbPath!); | ||
} else if(db == null){ | ||
db = await openDatabase(dbPath!); | ||
} | ||
} | ||
|
||
Future<List<Map<String, dynamic>>> query(String str) async { | ||
List<Map<String, dynamic>> result = []; | ||
await checkOpen(); | ||
result = await db!.rawQuery(str); | ||
return result; | ||
} | ||
|
||
Future<void> execute(String str) async { | ||
await checkOpen(); | ||
await db!.execute(str); | ||
} | ||
|
||
Future<int> insert(String name, Map<String, dynamic> wh) async { | ||
int result = -1; | ||
await checkOpen(); | ||
result = await db!.insert(name, wh); | ||
return result; | ||
} | ||
|
||
Future<void> update(String name, Map<String, dynamic> wh, String where, | ||
List<dynamic> args) async { | ||
await checkOpen(); | ||
await db!.update(name, wh, where: where, whereArgs: args); | ||
} | ||
|
||
Future<void> swap(String name, String key, String what, int key1, int key2, | ||
int s1, int s2) async { | ||
await checkOpen(); | ||
await db!.transaction((txn) async { | ||
await txn.rawUpdate('UPDATE $name SET $what=? WHERE $key=?', [s2, key1]); | ||
await txn.rawUpdate('UPDATE $name SET $what=? WHERE $key=?', [s1, key2]); | ||
}); | ||
} | ||
|
||
Future<void> delete(String name, String where, List<dynamic> args) async { | ||
await checkOpen(); | ||
await db!.delete(name, where: where, whereArgs: args); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.