Skip to content

Commit

Permalink
Use class DefaultPathProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
TaYaKi71751 committed Dec 12, 2023
1 parent b468a3d commit 197c663
Show file tree
Hide file tree
Showing 19 changed files with 84 additions and 383 deletions.
28 changes: 4 additions & 24 deletions lib/checker/checker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:path_provider/path_provider.dart';
import 'package:sqflite/sqflite.dart';
import 'package:violet/database/query.dart';
import 'package:violet/log/log.dart';
import 'package:violet/settings/path.dart';

class VioletChecker {
static Future<bool> checkArticleDatabase() async {
Expand All @@ -18,17 +19,8 @@ class VioletChecker {
// 0. get database path
//
var dbPath = Platform.isAndroid
? '${(await getApplicationDocumentsDirectory()).path}/data/data.db'
: '${await getDatabasesPath()}/data.db';
if(Platform.isLinux){
var home = '';
Platform.environment.forEach((key, value) {
if(key == 'HOME'){
home = value;
}
});
dbPath = '${home}/.violet/data.db';
}
? '${(await DefaultPathProvider.getBaseDirectory())}/data/data.db'
: '${(await DefaultPathProvider.getBaseDirectory())}/data.db';

//
// 1. check file exists
Expand Down Expand Up @@ -106,19 +98,7 @@ class VioletChecker {
//
// 0. get database path
//
var dir;
if(Platform.isAndroid || Platform.isIOS){
var d = await getApplicationDocumentsDirectory();
dir = d.path;
} else if(Platform.isLinux){
var home = '';
Platform.environment.forEach((key, value) {
if(key == 'HOME'){
home = value;
}
});
dir = '${home}/.violet';
}
var dir = DefaultPathProvider.getBaseDirectory();
var dbPath = '${dir}/user.db';

//
Expand Down
31 changes: 4 additions & 27 deletions lib/component/hitomi/hitomi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:violet/algorithm/distance.dart';
import 'package:violet/component/hitomi/displayed_tag.dart';
import 'package:violet/component/hitomi/tag_translate.dart';
import 'package:violet/script/script_manager.dart';
import 'package:violet/settings/path.dart';
import 'package:violet/settings/settings.dart';
import 'package:violet/variables.dart';

Expand All @@ -27,18 +28,7 @@ class HitomiManager {
static int? getArticleCount(String classification, String name) {
if (tagmap == null) {
final subdir = Platform.isAndroid ? '/data' : '';
late final path;
if(Platform.isAndroid || Platform.isIOS){
path = File('${Variables.applicationDocumentsDirectory}$subdir/index.json');
} else if(Platform.isLinux){
var home = '';
Platform.environment.forEach((key, value) {
if(key == 'HOME'){
home = value;
}
});
path = File('${home}/.violet$subdir/index.json');
}
final path = File('${Variables.applicationDocumentsDirectory}$subdir/index.json');
final text = path.readAsStringSync();
tagmap = jsonDecode(text);
}
Expand Down Expand Up @@ -101,21 +91,8 @@ class HitomiManager {
tagmap = jsonDecode(await file.readAsString());
} else {
final subdir = Platform.isAndroid ? '/data' : '';
late final directory;
late final path;
if(Platform.isAndroid || Platform.isIOS){
directory = await getApplicationDocumentsDirectory();
path = File('${directory.path}$subdir/index.json');
} else if(Platform.isLinux){
var home = '';
Platform.environment.forEach((key, value) {
if(key == 'HOME'){
home = value;
}
});
directory = '${home}/.violet';
path = File('${directory}$subdir/index.json');
}
final directory = (await (DefaultPathProvider.getDocumentsDirectory()));
final path = File('${directory}$subdir/index.json');
final text = path.readAsStringSync();
tagmap = jsonDecode(text);
}
Expand Down
15 changes: 2 additions & 13 deletions lib/component/hitomi/indexs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:tuple/tuple.dart';
import 'package:violet/algorithm/distance.dart';
import 'package:violet/component/hitomi/tag_translate.dart';
import 'package:violet/log/log.dart';
import 'package:violet/settings/path.dart';

// This is used for estimation similiar Aritst/Group/Uplaoder with each others.
class HitomiIndexs {
Expand Down Expand Up @@ -40,19 +41,7 @@ class HitomiIndexs {
static late Map<String, dynamic> relatedTag;

static Future<void> init() async {
late final directory;
if(Platform.isAndroid || Platform.isIOS){
var d = await getApplicationDocumentsDirectory();
directory = d.path;
} else if(Platform.isLinux){
var home = '';
Platform.environment.forEach((key, value) {
if(key == 'HOME'){
home = value;
}
});
directory = '${home}/.violet';
}
final directory = await DefaultPathProvider.getDocumentsDirectory();
final subdir = Platform.isAndroid ? '/data' : '';

// No data on first run.
Expand Down
18 changes: 3 additions & 15 deletions lib/component/hitomi/series_finder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,13 @@ import 'package:tuple/tuple.dart';
import 'package:violet/component/hitomi/hitomi.dart';
import 'package:violet/component/hitomi/title_cluster.dart';
import 'package:violet/database/query.dart';
import 'package:violet/settings/path.dart';

class SeriesFinder {
static Future<void> doFind1() async {
final subdir = Platform.isAndroid ? '/data' : '';
late final directory;
late final path;
if(Platform.isAndroid || Platform.isIOS){
directory = await getApplicationDocumentsDirectory();
path = File('${directory.path}$subdir/index.json');
} else if(Platform.isLinux){
var home = '';
Platform.environment.forEach((key, value) {
if(key == 'HOME'){
home = value;
}
});
directory = '${home}/.violet';
path = '${directory}$subdir/index.json';
}
final directory = await DefaultPathProvider.getDocumentsDirectory();
final path = File('${directory}$subdir/index.json');
final text = path.readAsStringSync();
final tagmap = jsonDecode(text);
final artists = tagmap['artist'] as Map<String, dynamic>;
Expand Down
17 changes: 4 additions & 13 deletions lib/database/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:flutter/material.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sqflite/sqflite.dart';
import 'package:violet/settings/path.dart';

class DataBaseManager {
String? dbPath;
Expand All @@ -33,19 +34,9 @@ class DataBaseManager {
if (Platform.environment.containsKey('FLUTTER_TEST')) {
dbPath = join(Directory.current.path, 'test/db/data.db');
} else {
if(Platform.isAndroid || Platform.isIOS){
dbPath = Platform.isAndroid
? '${(await getApplicationDocumentsDirectory()).path}/data/data.db'
: '${await getDatabasesPath()}/data.db';
} else if(Platform.isLinux){
var home = '';
Platform.environment.forEach((key, value) {
if(key == 'HOME'){
home = value;
}
});
dbPath = '${home}/.violet/data.db';
}
dbPath = Platform.isAndroid
? '${(await DefaultPathProvider.getBaseDirectory())}/data/data.db'
: '${(await DefaultPathProvider.getBaseDirectory())}/data.db';
}
_instance = create(dbPath);
await _instance!.open();
Expand Down
15 changes: 3 additions & 12 deletions lib/database/user/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,15 @@ import 'dart:io';

import 'package:path_provider/path_provider.dart';
import 'package:violet/database/database.dart';
import 'package:violet/settings/path.dart';

class CommonUserDatabase extends DataBaseManager {
static DataBaseManager? _instance;

static Future<DataBaseManager> getInstance() async {
if (_instance == null) {
var home = '';
if(Platform.isAndroid || Platform.isIOS){
var dir = await getApplicationDocumentsDirectory();
_instance = DataBaseManager.create('${dir.path}/user.db');
} else if(Platform.isLinux){
Platform.environment.forEach((key, value) => {
if(key == 'HOME'){
home = value
}
});
_instance = DataBaseManager.create('${home}/.violet/user.db');
}
var dir = await DefaultPathProvider.getDocumentsDirectory();
_instance = DataBaseManager.create('${dir}/user.db');
await _instance!.open();
}
return _instance!;
Expand Down
31 changes: 3 additions & 28 deletions lib/log/act_log.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:crypto/crypto.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:synchronized/synchronized.dart' as sync;
import 'package:violet/settings/path.dart';

enum ActLogType {
none,
Expand Down Expand Up @@ -109,19 +110,7 @@ class ActLogger {
if (Platform.environment.containsKey('FLUTTER_TEST')) {
logFile = File('act-log.txt');
} else {
late final dir;
if(Platform.isAndroid || Platform.isIOS){
var d = await getApplicationDocumentsDirectory();
dir = d.path;
} else if(Platform.isLinux){
var home = '';
Platform.environment.forEach((key, value) {
if(key == 'HOME'){
home = value;
}
});
dir = '${home}/.violet';
}
final dir = await DefaultPathProvider.getDocumentsDirectory();
logFile = File(join(dir, 'act-log.txt'));
}

Expand Down Expand Up @@ -150,21 +139,7 @@ class ActLogger {
}

static Future<void> exportLog() async {
late final ext;
if(Platform.isAndroid || Platform.isIOS){
var d = Platform.isIOS
? await getApplicationSupportDirectory()
: await getExternalStorageDirectory();
ext = d!.path;
} else if(Platform.isLinux){
var home = '';
Platform.environment.forEach((key, value) {
if(key == 'HOME'){
key = value;
}
});
ext = '${home}/.violet';
}
final ext = await DefaultPathProvider.getExportDirectory();
final extpath = '${ext}/act-log.txt';
await logFile.copy(extpath);
}
Expand Down
22 changes: 5 additions & 17 deletions lib/log/log.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:intl/intl.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:synchronized/synchronized.dart' as sync;
import 'package:violet/settings/path.dart';

class LogEvent {
DateTime? dateTime;
Expand Down Expand Up @@ -48,19 +49,8 @@ class Logger {
if (Platform.environment.containsKey('FLUTTER_TEST')) {
logFile = File('log.txt');
} else {
if(Platform.isAndroid || Platform.isIOS){
final dir = await getApplicationDocumentsDirectory();
logFile = File(join(dir.path, 'log.txt'));
} else if(Platform.isLinux){
var home = '';
Platform.environment.forEach((key, value) {
if(key == 'HOME'){
home = value;
}
});
final dir = '${home}/.violet';
logFile = File(join(dir, 'log.txt'));
}
final dir = await DefaultPathProvider.getDocumentsDirectory();
logFile = File(join(dir, 'log.txt'));
}

if (!await logFile.exists()) {
Expand Down Expand Up @@ -121,12 +111,10 @@ class Logger {
}

static Future<void> exportLog() async {
final ext = Platform.isIOS
? await getApplicationSupportDirectory()
: await getExternalStorageDirectory();
final ext = await DefaultPathProvider.getExportDirectory();
// final dateFormat = DateFormat("yyyy-MM-dd HH:mm:ss");
// final extpath = '${ext.path}/log-${dateFormat.format(DateTime.now())}.log';
final extpath = '${ext!.path}/log.txt';
final extpath = '${ext}/log.txt';
await logFile.copy(extpath);
}
}
Loading

0 comments on commit 197c663

Please sign in to comment.