Skip to content

Commit

Permalink
Add linux
Browse files Browse the repository at this point in the history
  • Loading branch information
TaYaKi71751 committed Dec 10, 2023
1 parent 97fbe6a commit 3a3da43
Show file tree
Hide file tree
Showing 42 changed files with 1,016 additions and 140 deletions.
16 changes: 8 additions & 8 deletions .metadata
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled.
# This file should be version controlled and should not be manually edited.

version:
revision: 18a827f3933c19f51862dde3fa472197683249d6
channel: stable
revision: "7f20e5d18ce4cb80c621533090a7c5113f5bdc52"
channel: "stable"

project_type: app

# Tracks metadata for the flutter migrate command
migration:
platforms:
- platform: root
create_revision: 18a827f3933c19f51862dde3fa472197683249d6
base_revision: 18a827f3933c19f51862dde3fa472197683249d6
- platform: windows
create_revision: 18a827f3933c19f51862dde3fa472197683249d6
base_revision: 18a827f3933c19f51862dde3fa472197683249d6
create_revision: 7f20e5d18ce4cb80c621533090a7c5113f5bdc52
base_revision: 7f20e5d18ce4cb80c621533090a7c5113f5bdc52
- platform: linux
create_revision: 7f20e5d18ce4cb80c621533090a7c5113f5bdc52
base_revision: 7f20e5d18ce4cb80c621533090a7c5113f5bdc52

# User provided section

Expand Down
3 changes: 2 additions & 1 deletion .vscode/settings.json
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 added assets/db/null.db
Binary file not shown.
13 changes: 7 additions & 6 deletions lib/component/eh/eh_headers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@
import 'package:http/http.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:violet/network/wrapper.dart' as http;
import 'package:violet/settings/settings.dart';

class EHSession {
static EHSession? tryLogin(String id, String pass) {
return null;
}

static Future<String> requestString(String url) async {
final prefs = await SharedPreferences.getInstance();
var cookie = prefs.getString('eh_cookies');
final prefs = await MultiPreferences.getInstance();
var cookie = await prefs.getString('eh_cookies');
return (await http.get(url, headers: {'Cookie': cookie ?? ''})).body;
}

static Future<String?> requestRedirect(String url) async {
final prefs = await SharedPreferences.getInstance();
var cookie = prefs.getString('eh_cookies');
final prefs = await MultiPreferences.getInstance();
var cookie = await prefs.getString('eh_cookies');
Request req = Request('Get', Uri.parse(url))..followRedirects = false;
req.headers['Cookie'] = cookie ?? '';
Client baseClient = Client();
Expand All @@ -27,8 +28,8 @@ class EHSession {
}

static Future<String> postComment(String url, String content) async {
final prefs = await SharedPreferences.getInstance();
var cookie = prefs.getString('eh_cookies');
final prefs = await MultiPreferences.getInstance();
var cookie = await prefs.getString('eh_cookies');
return (await http.post(url,
headers: {'Cookie': cookie ?? ''},
body: 'commenttext_new=${Uri.encodeFull(content)}'))
Expand Down
4 changes: 2 additions & 2 deletions lib/component/eh/eh_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ class EHentaiImageProvider extends VioletImageProvider {

@override
Future<Map<String, String>> getHeader(int page) async {
final prefs = await SharedPreferences.getInstance();
var cookie = prefs.getString('eh_cookies');
final prefs = await MultiPreferences.getInstance();
var cookie = await prefs.getString('eh_cookies');
return {'Cookie': cookie ?? ''};
}

Expand Down
2 changes: 1 addition & 1 deletion lib/component/hentai.dart
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ class HentaiManager {
'https://e${exh ? 'x' : '-'}hentai.org/?page=$page&f_cats=993&f_search=$search&advsearch=1&f_sname=on&f_stags=on&f_sh=on&f_spf=&f_spt=';

final cookie =
(await SharedPreferences.getInstance()).getString('eh_cookies') ?? '';
(await (await MultiPreferences.getInstance()).getString('eh_cookies')) ?? '';
final html =
(await http.get(url, headers: {'Cookie': '$cookie;sl=dm_2'})).body;

Expand Down
6 changes: 6 additions & 0 deletions lib/database/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,29 @@ class DataBaseManager {
List<Map<String, dynamic>> result = [];
await checkOpen();
result = await db!.rawQuery(str);
await db!.close();
return result;
}

Future<void> execute(String str) async {
await checkOpen();
await db!.execute(str);
await db!.close();
}

Future<int> insert(String name, Map<String, dynamic> wh) async {
int result = -1;
await checkOpen();
result = await db!.insert(name, wh);
await db!.close();
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);
await db!.close();
}

Future<void> swap(String name, String key, String what, int key1, int key2,
Expand All @@ -91,11 +95,13 @@ class DataBaseManager {
await txn.rawUpdate('UPDATE $name SET $what=? WHERE $key=?', [s2, key1]);
await txn.rawUpdate('UPDATE $name SET $what=? WHERE $key=?', [s1, key2]);
});
await db!.close();
}

Future<void> delete(String name, String where, List<dynamic> args) async {
await checkOpen();
await db!.delete(name, where: where, whereArgs: args);
await db!.close();
}

Future<bool> test() async {
Expand Down
128 changes: 128 additions & 0 deletions lib/database/user/settings.dart
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);
}
}
5 changes: 3 additions & 2 deletions lib/downloader/isolate_downloader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:dio/dio.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:violet/component/downloadable.dart';
import 'package:violet/log/log.dart';
import 'package:violet/settings/settings.dart';

part './isolate/core.dart';

Expand Down Expand Up @@ -84,8 +85,8 @@ class IsolateDownloader {
}

Future<void> _initThreadCount() async {
final prefs = await SharedPreferences.getInstance();
var tc = prefs.getInt('thread_count');
final prefs = await MultiPreferences.getInstance();
var tc = await prefs.getInt('thread_count');
if (tc == null) {
tc = 4;
await prefs.setInt('thread_count', 4);
Expand Down
1 change: 1 addition & 0 deletions lib/log/log.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class Logger {

if (!Platform.environment.containsKey('FLUTTER_TEST')) {
await lock.synchronized(() async {
await init();
await logFile.writeAsString('[${DateTime.now().toUtc()}] $msg\n',
mode: FileMode.append);
});
Expand Down
27 changes: 19 additions & 8 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

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

import 'package:crypto/crypto.dart';
import 'package:dynamic_theme/dynamic_theme.dart';
Expand All @@ -28,24 +29,34 @@ import 'package:violet/pages/lock/lock_screen.dart';
import 'package:violet/pages/splash/splash_page.dart';
import 'package:violet/settings/settings.dart';
import 'package:violet/style/palette.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
import 'package:sqflite/sqflite.dart';

Future<void> main() async {
runZonedGuarded<Future<void>>(() async {
// https://stackoverflow.com/questions/66971604/sqlite-with-flutter-desktop-windows
if (Platform.isWindows || Platform.isLinux) {
// Initialize FFI
sqfliteFfiInit();
// Change the default factory
databaseFactory = databaseFactoryFfi;
}

WidgetsFlutterBinding.ensureInitialized();

await FlutterDownloader.initialize(); // @dependent: android
if(Platform.isAndroid || Platform.isIOS) await FlutterDownloader.initialize(); // @dependent: android
FlareCache.doesPrune = false;
FlutterError.onError = recordFlutterError;

await initFirebase();
if(Platform.isAndroid || Platform.isIOS) await initFirebase();
await Settings.initFirst();
await warmupFlare();
if(Platform.isAndroid || Platform.isIOS) await warmupFlare();

runApp(const MyApp());
}, (exception, stack) async {
Logger.error('[async-error] E: $exception\n$stack');

await FirebaseCrashlytics.instance.recordError(exception, stack);
if(Platform.isAndroid || Platform.isIOS) await FirebaseCrashlytics.instance.recordError(exception, stack);
});
}

Expand All @@ -67,15 +78,15 @@ Future<void> recordFlutterError(FlutterErrorDetails flutterErrorDetails) async {
'[unhandled-error] E: ${flutterErrorDetails.exceptionAsString()}\n'
'${flutterErrorDetails.stack}');

await FirebaseCrashlytics.instance.recordFlutterError(flutterErrorDetails);
if(Platform.isAndroid || Platform.isIOS) FirebaseCrashlytics.instance.recordFlutterError(flutterErrorDetails);
}

Future<void> initFirebase() async {
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);

// check user-id is set
final prefs = await SharedPreferences.getInstance();
var id = prefs.getString('fa_userid');
final prefs = await MultiPreferences.getInstance();
var id = await prefs.getString('fa_userid');
if (id == null) {
id = sha1.convert(utf8.encode(DateTime.now().toString())).toString();
prefs.setString('fa_userid', id);
Expand Down Expand Up @@ -153,7 +164,7 @@ class MyApp extends StatelessWidget {
Settings.useLockScreen ? const LockScreen() : const SplashPage();

final navigatorObservers = [
FirebaseAnalyticsObserver(analytics: FirebaseAnalytics.instance),
if(Platform.isAndroid || Platform.isIOS) FirebaseAnalyticsObserver(analytics: FirebaseAnalytics.instance),
];

return GetMaterialApp(
Expand Down
8 changes: 4 additions & 4 deletions lib/pages/article_info/article_info_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -601,8 +601,8 @@ class __CommentAreaState extends State<_CommentArea> {

Future.delayed(const Duration(milliseconds: 100)).then((value) async {
if (widget.queryResult.ehash() != null) {
final prefs = await SharedPreferences.getInstance();
var cookie = prefs.getString('eh_cookies');
final prefs = await MultiPreferences.getInstance();
var cookie = await prefs.getString('eh_cookies');
if (cookie != null) {
try {
final html = await EHSession.requestString(
Expand Down Expand Up @@ -776,8 +776,8 @@ class __InfoAreaWidgetState extends State<_InfoAreaWidget> {
return;
}

final prefs = await SharedPreferences.getInstance();
var cookie = prefs.getString('eh_cookies');
final prefs = await MultiPreferences.getInstance();
var cookie = await prefs.getString('eh_cookies');
if (cookie == null || !cookie.contains('ipb_pass_hash')) {
await showOkDialog(context, 'Please, Login First!');
return;
Expand Down
Loading

0 comments on commit 3a3da43

Please sign in to comment.