Skip to content
Merged
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
19 changes: 19 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,25 @@ jobs:
run: |
choco install innosetup -y
refreshenv
- name: Extract version from pubspec.yaml
shell: pwsh
id: extract_version
run: |
$pubspec = Get-Content pubspec.yaml
foreach ($line in $pubspec) {
if ($line -match '^version:\s*(.+)$') {
$version = $matches[1]
break
}
}
Write-Host "VERSION=$version"
echo "VERSION=$version" >> $env:GITHUB_ENV
- name: Patch ISS with correct version
shell: powershell
run: |
$issPath = ".\windows\wispar_setup.iss"
(Get-Content $issPath) -replace '#define MyAppVersion ".*"', '#define MyAppVersion "${{ env.VERSION }}"' | Set-Content $issPath
Write-Host "Patched ISS file with version ${{ env.VERSION }}"
- name: Create Windows installer
shell: powershell
run: |
Expand Down
19 changes: 19 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,29 @@ import './services/background_service.dart';
import './services/logs_helper.dart';
import 'package:background_fetch/background_fetch.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:path_provider/path_provider.dart';
import 'package:wispar/webview_env.dart';
import 'dart:io' show Platform;

void main() async {
WidgetsFlutterBinding.ensureInitialized();

if (Platform.isWindows) {
final availableVersion = await WebViewEnvironment.getAvailableVersion();
if (availableVersion == null) {
return;
}

final appDataDir = await getApplicationSupportDirectory();

webViewEnvironment = await WebViewEnvironment.create(
settings: WebViewEnvironmentSettings(
userDataFolder: '${appDataDir.path}\\WisparWebView',
),
);
}

if (Platform.isLinux || Platform.isWindows) {
sqfliteFfiInit();
databaseFactory = databaseFactoryFfi;
Expand Down
9 changes: 8 additions & 1 deletion lib/screens/article_website.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import 'package:path_provider/path_provider.dart';
import 'dart:io';
import 'package:http/http.dart' as http;
import 'package:wispar/services/logs_helper.dart';
import '../services/database_helper.dart';
import 'package:wispar/webview_env.dart';
import 'package:wispar/services/database_helper.dart';

class ArticleWebsite extends StatefulWidget {
final PublicationCard publicationCard;
Expand Down Expand Up @@ -307,6 +308,8 @@ class ArticleWebsiteState extends State<ArticleWebsite> {
isReadyToLoad
? InAppWebView(
key: webViewKey,
webViewEnvironment:
Platform.isWindows ? webViewEnvironment : null,
initialUrlRequest: URLRequest(url: WebUri(pdfUrl)),
initialSettings: settings,
pullToRefreshController: pullToRefreshController,
Expand Down Expand Up @@ -979,6 +982,10 @@ class ArticleWebsiteState extends State<ArticleWebsite> {

if (useCustomPath && customPath != null) {
baseDirPath = customPath;
} else if (Platform.isWindows) {
final defaultAppDir =
await getApplicationSupportDirectory();
baseDirPath = defaultAppDir.path;
} else {
final defaultAppDir =
await getApplicationDocumentsDirectory();
Expand Down
74 changes: 34 additions & 40 deletions lib/screens/database_settings_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ class DatabaseSettingsScreenState extends State<DatabaseSettingsScreen> {
String? targetPath;

if (Platform.isIOS) {
targetPath =
await DatabaseHelper.resolveCustomDatabasePath(pickedFolder);
targetPath = await DatabaseHelper.resolveBookmarkPath(pickedFolder);
if (targetPath == null) {
throw Exception('Failed to resolve custom database bookmark on iOS.');
}
Expand Down Expand Up @@ -150,22 +149,21 @@ class DatabaseSettingsScreenState extends State<DatabaseSettingsScreen> {
try {
await _showLoadingDialog(
AppLocalizations.of(context)!.movingDatabase);
final dbHelper = DatabaseHelper();
await dbHelper.closeDatabase();
final oldDbPath = await dbHelper.getDbPath();
final oldBaseDir = Directory(p.dirname(oldDbPath));

final defaultAppDir = await getApplicationDocumentsDirectory();
final oldDBPath = p.join(await getDatabasesPath(), 'wispar.db');
final newDBPath = p.join(targetPath, 'wispar.db');
final newDbPath = p.join(targetPath, 'wispar.db');

// Move database
final oldDBFile = File(oldDBPath);
final oldDBFile = File(oldDbPath);
if (await oldDBFile.exists()) {
await oldDBFile.copy(newDBPath);
await oldDBFile.copy(newDbPath);
await oldDBFile.delete();
}

// Move PDFs
await for (final file in defaultAppDir.list()) {
await for (final file in oldBaseDir.list()) {
if (file is File && file.path.endsWith('.pdf')) {
final newFile = File(p.join(targetPath, p.basename(file.path)));
await file.copy(newFile.path);
Expand All @@ -175,7 +173,7 @@ class DatabaseSettingsScreenState extends State<DatabaseSettingsScreen> {

// Move graphical abstracts
final oldGraphicalDir =
Directory(p.join(defaultAppDir.path, 'graphical_abstracts'));
Directory(p.join(oldBaseDir.path, 'graphical_abstracts'));
final newGraphicalDir =
Directory(p.join(targetPath, 'graphical_abstracts'));

Expand Down Expand Up @@ -251,14 +249,24 @@ class DatabaseSettingsScreenState extends State<DatabaseSettingsScreen> {
if (customPath == null) {
throw Exception("Custom DB folder not accessible");
}
String defaultDBPath;
Directory defaultBaseDir;

if (Platform.isWindows) {
final appDir = await getApplicationSupportDirectory();
defaultDBPath = p.join(appDir.path, 'wispar.db');
defaultBaseDir = appDir;
} else {
final defaultPath = await getDatabasesPath();
defaultDBPath = p.join(defaultPath, 'wispar.db');
defaultBaseDir = Directory(defaultPath);
}

final defaultDBPath = p.join(await getDatabasesPath(), 'wispar.db');
final appDir = await getApplicationDocumentsDirectory();
final oldDBPath = p.join(customPath, 'wispar.db');
final oldGraphicalDir =
Directory(p.join(customPath, 'graphical_abstracts'));
final newGraphicalDir =
Directory(p.join(appDir.path, 'graphical_abstracts'));
Directory(p.join(defaultBaseDir.path, 'graphical_abstracts'));

// Move DB
final oldDBFile = File(oldDBPath);
Expand All @@ -271,7 +279,8 @@ class DatabaseSettingsScreenState extends State<DatabaseSettingsScreen> {
final customDir = Directory(customPath);
await for (final file in customDir.list()) {
if (file is File && file.path.endsWith('.pdf')) {
final newFile = File(p.join(appDir.path, p.basename(file.path)));
final newFile =
File(p.join(defaultBaseDir.path, p.basename(file.path)));
await file.copy(newFile.path);
await file.delete();
}
Expand Down Expand Up @@ -372,22 +381,9 @@ class DatabaseSettingsScreenState extends State<DatabaseSettingsScreen> {
final String outputFile =
p.join(outputDirectory, 'wispar_backup_$timestamp.zip');

final customSourcePath = await _getUsableCustomPath();
String sourceBasePath;
String dbDirectoryPath;

if (customSourcePath != null) {
sourceBasePath = customSourcePath;
dbDirectoryPath = customSourcePath;
} else {
// Use default paths
final defaultAppDir = await getApplicationDocumentsDirectory();
sourceBasePath = defaultAppDir.path;
dbDirectoryPath = await getDatabasesPath();
}

String dbPath = p.join(dbDirectoryPath, "wispar.db");
File dbFile = File(dbPath);
final dbPath = await DatabaseHelper().getDbPath();
final dbFile = File(dbPath);
final sourceBasePath = p.dirname(dbPath);

final sourceDir = Directory(sourceBasePath);

Expand Down Expand Up @@ -453,20 +449,18 @@ class DatabaseSettingsScreenState extends State<DatabaseSettingsScreen> {
await dbHelper.closeDatabase();
File selectedFile = File(result.files.single.path!);

final dbPath = await dbHelper.getDbPath();
final dbDirectoryPath = p.dirname(dbPath);

final prefs = await SharedPreferences.getInstance();
final useCustomPath = prefs.getBool('useCustomDatabasePath') ?? false;
final customPath = prefs.getString('customDatabasePath');

String dbDestinationPath;
if (useCustomPath && customPath != null) {
dbDestinationPath = customPath;
} else {
dbDestinationPath = await getDatabasesPath();
}

String docsDestinationPath;
if (useCustomPath && customPath != null) {
docsDestinationPath = customPath;
} else if (Platform.isWindows) {
final appDir = await getApplicationSupportDirectory();
docsDestinationPath = appDir.path;
} else {
final appDir = await getApplicationDocumentsDirectory();
docsDestinationPath = appDir.path;
Expand All @@ -478,7 +472,7 @@ class DatabaseSettingsScreenState extends State<DatabaseSettingsScreen> {

for (final file in archive) {
final destinationBasePath =
file.name == 'wispar.db' ? dbDestinationPath : docsDestinationPath;
file.name == 'wispar.db' ? dbDirectoryPath : docsDestinationPath;

final filePath = p.join(destinationBasePath, file.name);
final outFile = File(filePath);
Expand All @@ -502,7 +496,7 @@ class DatabaseSettingsScreenState extends State<DatabaseSettingsScreen> {
}

logger.info(
'The database was successfully imported to $dbDestinationPath from ${selectedFile.path}');
'The database was successfully imported to $dbDirectoryPath from ${selectedFile.path}');

if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
Expand Down
4 changes: 4 additions & 0 deletions lib/screens/pdf_reader.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:io';
import 'package:flutter/material.dart';
import '../generated_l10n/app_localizations.dart';
import 'package:pdfrx/pdfrx.dart';
Expand Down Expand Up @@ -80,6 +81,9 @@ class PdfReaderState extends State<PdfReader> {
String basePath;
if (_useCustomPath && _customPath != null) {
basePath = _customPath!;
} else if (Platform.isWindows) {
final defaultDirectory = await getApplicationSupportDirectory();
basePath = defaultDirectory.path;
} else {
final defaultDirectory = await getApplicationDocumentsDirectory();
basePath = defaultDirectory.path;
Expand Down
6 changes: 4 additions & 2 deletions lib/services/abstract_scraper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import 'dart:async';
import 'dart:io' show Platform;
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:shared_preferences/shared_preferences.dart';
import './string_format_helper.dart';
import './logs_helper.dart';
import 'package:wispar/webview_env.dart';
import 'package:wispar/services/string_format_helper.dart';
import 'package:wispar/services/logs_helper.dart';

class AbstractScraper {
Completer<Map<String, String?>> _completer =
Expand Down Expand Up @@ -47,6 +48,7 @@ class AbstractScraper {
initialSettings: InAppWebViewSettings(
userAgent: userAgent,
),
webViewEnvironment: Platform.isWindows ? webViewEnvironment : null,
initialUrlRequest: URLRequest(url: WebUri(url)),
onLoadStop: (controller, loadedUrl) async {
await Future.delayed(const Duration(seconds: 3));
Expand Down
29 changes: 17 additions & 12 deletions lib/services/database_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import 'package:path_provider/path_provider.dart';
class DatabaseHelper {
static const platform = MethodChannel('app.wispar.wispar/database_access');

static Future<String?> resolveCustomDatabasePath(String? path) async {
static Future<String?> resolveBookmarkPath(String? path) async {
if (path == null) return null;

if (Platform.isIOS) {
Expand All @@ -29,7 +29,6 @@ class DatabaseHelper {
return null;
}
} else {
// Android can use the path directly
return path;
}
}
Expand All @@ -44,23 +43,26 @@ class DatabaseHelper {
return _database!;
}

Future<Database> initDatabase() async {
Future<String> getDbPath() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String? customPath = prefs.getString('customDatabasePath');
String? bookmark = prefs.getString('customDatabaseBookmark');

if (Platform.isIOS && bookmark != null) {
final resolvedPath = await DatabaseHelper.platform
.invokeMethod('resolveCustomPath', bookmark);
if (resolvedPath != null) {
customPath = resolvedPath;
} else {
customPath = null;
}
final resolved = await resolveBookmarkPath(bookmark);
if (resolved != null) customPath = resolved;
}
String defaultPath = await getDatabasesPath();
if (Platform.isWindows) {
final dir = await getApplicationSupportDirectory();
defaultPath = dir.path;
}

final defaultPath = await getDatabasesPath();
final databasePath = join(customPath ?? defaultPath, 'wispar.db');
return databasePath;
}

Future<Database> initDatabase() async {
String databasePath = await getDbPath();

return openDatabase(databasePath, version: 9, onOpen: (db) async {
await db.execute('PRAGMA foreign_keys = ON');
Expand Down Expand Up @@ -1115,6 +1117,9 @@ class DatabaseHelper {
String baseDirPath;
if (useCustomPath && customPath != null) {
baseDirPath = customPath;
} else if (Platform.isWindows) {
final dir = await getApplicationSupportDirectory();
baseDirPath = dir.path;
} else {
final dir = await getApplicationDocumentsDirectory();
baseDirPath = dir.path;
Expand Down
3 changes: 3 additions & 0 deletions lib/services/graphical_abstract_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class GraphicalAbstractManager {

if (useCustomPath && customPath != null) {
basePath = customPath;
} else if (Platform.isWindows) {
final dir = await getApplicationSupportDirectory();
basePath = dir.path;
} else {
final dir = await getApplicationDocumentsDirectory();
basePath = dir.path;
Expand Down
5 changes: 5 additions & 0 deletions lib/webview_env.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'package:flutter_inappwebview/flutter_inappwebview.dart';

/* Global variable to specify where to save the inappwebview cache
on Windows*/
WebViewEnvironment? webViewEnvironment;
4 changes: 4 additions & 0 deletions lib/widgets/publication_card/publication_card.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:wispar/generated_l10n/app_localizations.dart';
Expand Down Expand Up @@ -401,6 +402,9 @@ class PublicationCardState extends State<PublicationCard>
String basePath;
if (useCustomPath && customPath != null) {
basePath = customPath;
} else if (Platform.isWindows) {
final defaultDirectory = await getApplicationSupportDirectory();
basePath = defaultDirectory.path;
} else {
final defaultDirectory = await getApplicationDocumentsDirectory();
basePath = defaultDirectory.path;
Expand Down
Loading