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

Load locale on startup #176

Merged
merged 8 commits into from
Dec 9, 2023
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
12 changes: 6 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ jobs:
- name: Code Generation
run: |
cd .\commet
git config --global core.longpaths true
dart run scripts/codegen.dart

- name: Build Windows App
run: |
cd .\commet
dart run scripts/build_release.dart --platform windows --version_tag ${{ github.event.release.tag_name || 'v0.0.0' }} --git_hash ${{ github.sha }}
flutter build windows --release --dart-define PLATFORM=windows

build-android:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -65,7 +66,7 @@ jobs:
- name: Build APK
run: |
cd $PROJECT_PATH
dart run scripts/build_release.dart --platform android --version_tag ${{ github.event.release.tag_name || 'v0.0.0' }} --git_hash ${{ github.sha }}
flutter build apk --debug --dart-define PLATFORM=android

build-android-google-services:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -100,8 +101,7 @@ jobs:
- name: Build APK
run: |
cd $PROJECT_PATH
dart run scripts/build_release.dart --platform android --enable_google_services true --version_tag ${{ github.event.release.tag_name || 'v0.0.0' }} --git_hash ${{ github.sha }}

flutter build apk --debug --dart-define ENABLE_GOOGLE_SERVICES=true --dart-define PLATFORM=android

build-linux:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -130,7 +130,7 @@ jobs:
- name: Build Linux
run: |
cd $PROJECT_PATH
dart run scripts/build_release.dart --platform linux --version_tag ${{ github.event.release.tag_name || 'v0.0.0' }} --git_hash ${{ github.sha }} --build_detail debian
flutter build linux --release --dart-define PLATFORM=linux

build-web:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -159,4 +159,4 @@ jobs:
- name: Build Web
run: |
cd $PROJECT_PATH
dart run scripts/build_release.dart --platform web --version_tag ${{ github.event.release.tag_name || 'v0.0.0' }} --git_hash ${{ github.sha }}
flutter build web --release --dart-define PLATFORM=web
13 changes: 9 additions & 4 deletions commet/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,18 @@ android {

signingConfigs {
release {
keyAlias keystoreProperties['keyAlias']
keyPassword keystoreProperties['keyPassword']
storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
storePassword keystoreProperties['storePassword']
if (keystorePropertiesFile.exists()) {
keyAlias keystoreProperties['keyAlias']
keyPassword keystoreProperties['keyPassword']
storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
storePassword keystoreProperties['storePassword']
}
}
}
buildTypes {
debug {
signingConfig signingConfigs.debug
}
release {
signingConfig signingConfigs.release
}
Expand Down
2 changes: 1 addition & 1 deletion commet/integration_test/extensions/common_flows.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ extension CommonFlows on WidgetTester {
}

Future<void> clean() async {
await fileCache.close();
await fileCache?.close();
await preferences.clear();
await clearUserData();
}
Expand Down
4 changes: 2 additions & 2 deletions commet/lib/cache/cache_file_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class CacheFileProvider implements FileProvider {
: fileIdentifier = "thumbnail_$fileId";

@override
Future<Uri> resolve({String? savePath}) async {
return fileCache.fetchFile(fileIdentifier, getter);
Future<Uri?> resolve({String? savePath}) async {
return fileCache?.fetchFile(fileIdentifier, getter);
}

@override
Expand Down
169 changes: 12 additions & 157 deletions commet/lib/cache/file_cache.dart
Original file line number Diff line number Diff line change
@@ -1,169 +1,24 @@
import 'dart:io';
import 'file_cache_stub.dart'
if (dart.library.io) "package:commet/cache/isar_file_cache.dart";

import 'package:commet/cache/cached_file.dart';
import 'package:commet/config/app_config.dart';
import 'package:commet/config/build_config.dart';
import 'package:commet/debug/log.dart';
import 'package:commet/utils/rng.dart';
import 'package:flutter/foundation.dart';
import 'package:isar/isar.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart' as p;

class FileCacheInstance {
Isar? db;
IsarCollection<CachedFile>? files;
abstract class FileCache {
Future<void> init();

Future<String> newPath() async {
final dir = await getTemporaryDirectory();
String fileName = RandomUtils.getRandomString(30);
return p.join(dir.path, "chat.commet.app", "file_cache", fileName);
}

Future<String> generateTempFilePath() async {
var path = "";
for (path = await newPath();
await File(path).exists();
path = await newPath()) {}

return path;
}

Future<void> init() async {
final dir = p.join(await AppConfig.getDatabasePath(), "cache");
var directory = Directory(dir);
if (!await directory.exists()) {
await directory.create(recursive: true);
}

db =
await Isar.open([CachedFileSchema], directory: dir, name: "file_cache");

files = db!.collection<CachedFile>();
clean();
}

Future<void> close() async {
await db!.close();
}

Future<CachedFile?> _getByFileId(String fileId) async {
return await files!.filter().fileIdEqualTo(fileId).findFirst();
}

Future<bool> hasFile(String identifier) async {
var file = await _getByFileId(identifier);
if (file == null) return false;

// if the file exists in the database but not on disk, we should remove it from db
var exists = await File(file.filePath).exists();
if (!exists)
await db!.writeTxn(() async {
files!.delete(file.id);
});

return exists;
}

Future<Uri?> getFile(String identifier) async {
if (!await hasFile(identifier)) return null;

var entry = await _getByFileId(identifier);
if (entry == null) return null;
//var lastAccess = DateTime.fromMillisecondsSinceEpoch(entry!.lastAccessedTimestamp).toLocal().toString();

entry.lastAccessedTimestamp = DateTime.now().millisecondsSinceEpoch;

db!.writeTxn(() async {
await files!.put(entry);
});

var file = File(entry.filePath);
Future<void> close();

return file.uri;
}

Future<Uri> putFile(String identifier, Uint8List bytes) async {
var path = await generateTempFilePath();
if (BuildConfig.DEBUG) path += "_${Uri.encodeComponent(identifier)}";
var file = File(path);
await file.create(recursive: true);
await file.writeAsBytes(bytes);

CachedFile entry = CachedFile(
file.path, identifier, DateTime.now().millisecondsSinceEpoch);

db!.writeTxn(() async {
await files!.put(entry);
});

return file.uri;
}
Future<bool> hasFile(String identifier);

Future<Uri> fetchFile(
String identifier, Future<Uint8List> Function() getter) async {
var existing = await getFile(identifier);
if (existing != null) return existing;
Future<Uri?> getFile(String identifier);

var bytes = await getter();
var path = await generateTempFilePath();
if (BuildConfig.DEBUG) path += "_${Uri.encodeComponent(identifier)}";
Future<Uri> putFile(String identifier, Uint8List bytes);

var file = File(path);
await file.create(recursive: true);
await file.writeAsBytes(bytes);
Future<Uri> fetchFile(String identifier, Future<Uint8List> Function() getter);

CachedFile entry = CachedFile(
file.path, identifier, DateTime.now().millisecondsSinceEpoch);
Future<void> clean();

db!.writeTxn(() async {
await files!.put(entry);
});

return file.uri;
static FileCache? getFileCacheInstance() {
return getFileCacheImplementation();
}

Future<void> clean() async {
Log.i("Cleaning files");

var now = DateTime.now();
var cutoffTime = now.subtract(const Duration(days: 2));
var timeMs = cutoffTime.millisecondsSinceEpoch;

var removeFiles =
await files!.filter().lastAccessedTimestampLessThan(timeMs).findAll();

var allFiles = await files!.where().findAll();

Log.i("Found: ${removeFiles.length}/${allFiles.length} files for cleaning");

for (var file in removeFiles) {
_cleanFile(file);
}

db!.writeTxn(() async {
files!.deleteAll(removeFiles.map((e) => e.id).toList());
});
}

Future<void> _cleanFile(CachedFile entry) async {
var file = File(entry.filePath);
if (!await file.exists()) {
return;
}

file.delete();
}

// static bool _shouldRemoveFile(
// {required DateTime lastAccessedTime, required int fileSize}) {
// const largeFileSize = 10 * 1048576; //Ten Megabytes
// var diff = DateTime.now().difference(lastAccessedTime);

// if (fileSize > largeFileSize) {
// return diff.inDays > 1;
// }

// return true;
// }
}
3 changes: 3 additions & 0 deletions commet/lib/cache/file_cache_stub.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import 'package:commet/cache/file_cache.dart';

FileCache? getFileCacheImplementation() => null;
Loading
Loading