fbmobile/lib/core/viewmodels/history_model.dart

177 lines
6 KiB
Dart
Raw Normal View History

import 'dart:async';
2021-02-02 14:33:23 +00:00
import 'dart:io';
import 'package:flutter_translate/flutter_translate.dart';
import 'package:logger/logger.dart';
import '../../locator.dart';
import '../datamodels/dialog_response.dart';
import '../enums/error_code.dart';
import '../enums/refresh_event.dart';
2021-02-02 14:33:23 +00:00
import '../enums/viewstate.dart';
import '../error/rest_service_exception.dart';
import '../error/service_exception.dart';
import '../models/rest/history.dart';
import '../models/rest/rest_error.dart';
import '../models/uploaded_paste.dart';
import '../services/dialog_service.dart';
import '../services/file_service.dart';
import '../services/link_service.dart';
import '../services/refresh_service.dart';
2021-02-02 14:33:23 +00:00
import '../util/logger.dart';
import 'base_model.dart';
class HistoryModel extends BaseModel {
final Logger _logger = getLogger();
final FileService _fileService = locator<FileService>();
final RefreshService _refreshService = locator<RefreshService>();
2021-02-02 14:33:23 +00:00
final LinkService _linkService = locator<LinkService>();
final DialogService _dialogService = locator<DialogService>();
late StreamSubscription _refreshTriggerSubscription;
2021-02-02 14:33:23 +00:00
List<UploadedPaste> pastes = [];
String? errorMessage;
void init() {
2023-01-16 00:44:34 +00:00
_refreshTriggerSubscription =
_refreshService.refreshEventController.stream.listen((event) {
2023-01-04 20:17:54 +00:00
if (event == RefreshEvent.refreshHistory) {
_logger.d('History needs a refresh');
getHistory();
}
});
}
2021-02-02 14:33:23 +00:00
Future getHistory() async {
2023-01-04 20:17:54 +00:00
setStateView(ViewState.busy);
2021-02-02 14:33:23 +00:00
try {
pastes.clear();
2023-01-04 20:17:54 +00:00
History history = await _fileService.getHistory();
if (history.items.isNotEmpty) {
history.items.forEach((key, value) {
2021-02-02 14:33:23 +00:00
var millisecondsSinceEpoch = int.parse(value.date) * 1000;
pastes.add(
UploadedPaste(
id: key,
2023-01-16 00:44:34 +00:00
date:
DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch),
2021-02-02 14:33:23 +00:00
filename: value.filename,
filesize: int.parse(value.filesize),
hash: value.hash,
mimetype: value.mimetype,
isMulti: false,
items: [],
thumbnail: value.thumbnail),
);
});
}
2023-01-04 20:17:54 +00:00
if (history.multipasteItems.isNotEmpty) {
history.multipasteItems.forEach((key, multiPaste) {
2021-02-02 14:33:23 +00:00
var millisecondsSinceEpoch = int.parse(multiPaste.date) * 1000;
pastes.add(UploadedPaste(
id: key,
date: DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch),
isMulti: true,
items: multiPaste.items.entries.map((e) => e.value.id).toList()));
});
}
pastes.sort((a, b) => a.date!.compareTo(b.date!));
2021-02-02 14:33:23 +00:00
errorMessage = null;
} catch (e) {
if (e is RestServiceException) {
if (e.statusCode == HttpStatus.notFound) {
errorMessage = translate('history.errors.not_found');
} else if (e.statusCode == HttpStatus.forbidden) {
errorMessage = translate('api.forbidden');
} else if (e.statusCode != HttpStatus.notFound &&
e.statusCode != HttpStatus.forbidden &&
e.responseBody is RestError &&
e.responseBody.message != null) {
if (e.statusCode == HttpStatus.badRequest) {
2023-01-16 00:44:34 +00:00
errorMessage = translate('api.bad_request',
args: {'reason': e.responseBody.message});
2021-02-02 14:33:23 +00:00
} else {
2023-01-16 00:44:34 +00:00
errorMessage = translate('api.general_rest_error_payload',
args: {'message': e.responseBody.message});
2021-02-02 14:33:23 +00:00
}
} else {
errorMessage = translate('api.general_rest_error');
}
2023-01-04 20:17:54 +00:00
} else if (e is ServiceException && e.code == ErrorCode.socketError) {
2021-02-02 14:33:23 +00:00
errorMessage = translate('api.socket_error');
2023-01-04 20:17:54 +00:00
} else if (e is ServiceException && e.code == ErrorCode.socketTimeout) {
2021-02-02 14:33:23 +00:00
errorMessage = translate('api.socket_timeout');
} else {
errorMessage = translate('app.unknown_error');
2023-01-04 20:17:54 +00:00
setStateView(ViewState.idle);
2021-02-02 14:33:23 +00:00
_logger.e('An unknown error occurred', e);
2023-01-04 20:17:54 +00:00
rethrow;
2021-02-02 14:33:23 +00:00
}
}
2023-01-04 20:17:54 +00:00
setStateView(ViewState.idle);
2021-02-02 14:33:23 +00:00
}
Future deletePaste(String id) async {
DialogResponse res = await _dialogService.showConfirmationDialog(
title: translate('history.delete_dialog.title'),
2023-01-16 00:44:34 +00:00
description:
translate('history.delete_dialog.description', args: {'id': id}),
2021-02-02 14:33:23 +00:00
buttonTitleAccept: translate('history.delete_dialog.accept'),
buttonTitleDeny: translate('history.delete_dialog.deny'));
if (!res.confirmed!) {
2021-02-02 14:33:23 +00:00
return;
}
2023-01-04 20:17:54 +00:00
setStateView(ViewState.busy);
2021-02-02 14:33:23 +00:00
try {
await _fileService.deletePaste(id);
await getHistory();
errorMessage = null;
} catch (e) {
if (e is RestServiceException) {
if (e.statusCode == HttpStatus.notFound) {
errorMessage = translate('project.errors.not_found');
} else if (e.statusCode == HttpStatus.forbidden) {
errorMessage = translate('api.forbidden');
} else if (e.statusCode != HttpStatus.notFound &&
e.statusCode != HttpStatus.forbidden &&
e.responseBody is RestError &&
e.responseBody.message != null) {
2023-01-16 00:44:34 +00:00
errorMessage = translate('api.general_rest_error_payload',
args: {'message': e.responseBody.message});
2021-02-02 14:33:23 +00:00
} else {
errorMessage = translate('api.general_rest_error');
}
2023-01-04 20:17:54 +00:00
} else if (e is ServiceException && e.code == ErrorCode.socketError) {
2021-02-02 14:33:23 +00:00
errorMessage = translate('api.socket_error');
2023-01-04 20:17:54 +00:00
} else if (e is ServiceException && e.code == ErrorCode.socketTimeout) {
2021-02-02 14:33:23 +00:00
errorMessage = translate('api.socket_timeout');
} else {
errorMessage = translate('app.unknown_error');
2023-01-04 20:17:54 +00:00
setStateView(ViewState.idle);
2021-02-02 14:33:23 +00:00
_logger.e('An unknown error occurred', e);
2023-01-04 20:17:54 +00:00
rethrow;
2021-02-02 14:33:23 +00:00
}
}
2023-01-04 20:17:54 +00:00
setStateView(ViewState.idle);
2021-02-02 14:33:23 +00:00
}
void openLink(String link) {
_linkService.open(link);
}
@override
void dispose() {
_refreshTriggerSubscription.cancel();
super.dispose();
}
2021-02-02 14:33:23 +00:00
}