Skip to content

Commit

Permalink
Cleanup (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matej-Hlatky authored Jul 31, 2024
1 parent acd9126 commit 83dbbef
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 111 deletions.
2 changes: 1 addition & 1 deletion lib/app_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:injectable/injectable.dart';
import 'package:logging/logging.dart' show Logger;
import 'package:path_provider/path_provider.dart' as provider;

import 'file_extensions.dart';
import 'file_system_entity_extensions.dart';

/// Provides platform (iOS and Android) specific app functions:
///
Expand Down
2 changes: 1 addition & 1 deletion lib/bloc/create_document_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'package:injectable/injectable.dart';
import 'package:logging/logging.dart';

import '../data/pdf_signing_option.dart';
import '../file_extensions.dart';
import '../file_system_entity_extensions.dart';
import '../files.dart';
import '../ui/screens/open_document_screen.dart';
import 'create_document_state.dart';
Expand Down
1 change: 1 addition & 0 deletions lib/bloc/present_signed_document_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:path_provider/path_provider.dart';

import '../app_service.dart';
import '../file_extensions.dart';
import '../file_system_entity_extensions.dart';
import '../ui/screens/present_signed_document_screen.dart';
import 'present_signed_document_state.dart';

Expand Down
4 changes: 2 additions & 2 deletions lib/directory_extensions.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import 'dart:io' show Directory, File;

import 'package:path/path.dart' as p;
import 'package:path/path.dart' as path;

/// Set of extensions on [Directory] type.
extension DirectoryExtensions on Directory {
/// Returns flag indicating whether can write into this [Directory].
Future<bool> canWrite() async {
final tempFile = File(p.join(path, ".can_write"));
final tempFile = File(path.join(this.path, ".can_write"));

try {
await tempFile.writeAsBytes(const [], flush: true);
Expand Down
15 changes: 3 additions & 12 deletions lib/file_extensions.dart
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
import 'dart:io' show File;

import 'package:flutter/foundation.dart';
import 'package:path/path.dart' as path;

import 'file_system_entity_extensions.dart';

/// Set of extensions on [File] type.
extension FileExtensions on File {
/// Calls [path.extension] on this [File].
String get extension => path.extension(this.path);

/// Calls [path.basename] on this [File].
String get basename => path.basename(this.path);

/// Calls [path.basenameWithoutExtension] on this [File].
String get basenameWithoutExtension =>
path.basenameWithoutExtension(this.path);

/// Returns redacted file info usable for logging.
///
/// In case of [kDebugMode], full [File.path] is returned;
/// "???.[FileExtensions.extension]" otherwise.
String get redactedInfo => (kDebugMode ? this.path : "???$extension");
String get redactedInfo => (kDebugMode ? path : "???$extension");
}
23 changes: 23 additions & 0 deletions lib/file_system_entity_extensions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'dart:io' show FileSystemEntity;

import 'package:path/path.dart' as path;

import 'directory_extensions.dart';
import 'file_extensions.dart';

/// Set of extensions on [FileSystemEntity] type.
///
/// See also:
/// - [FileExtensions]
/// - [DirectoryExtensions]
extension FileSystemEntityExtensions on FileSystemEntity {
/// Calls [path.extension] on this [FileSystemEntity.path].
String get extension => path.extension(this.path);

/// Calls [path.basename] on this [FileSystemEntity.path].
String get basename => path.basename(this.path);

/// Calls [path.basenameWithoutExtension] on this [FileSystemEntity.path].
String get basenameWithoutExtension =>
path.basenameWithoutExtension(this.path);
}
2 changes: 1 addition & 1 deletion lib/files.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'dart:io' show File;

import 'file_extensions.dart';
import 'file_system_entity_extensions.dart';

abstract class Files {
/// Mapping of extension => MIME type.
Expand Down
12 changes: 3 additions & 9 deletions lib/l10n/app_localizations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -490,15 +490,9 @@ abstract class AppLocalizations {
/// No description provided for @selectSigningCertificateNoCertificateBody.
///
/// In sk, this message translates to:
/// **'Nepodarilo sa nájsť certifikát pre kvalifikovaný elektronický podpis.\n\nCertifikát je potrebné vydať v aplikácii eID klient, prípadne použiť iný občiansky preukaz.\nNávod na vydanie certifikátu nájdete na '**
/// **'Nepodarilo sa nájsť certifikát pre **kvalifikovaný elektronický podpis**.\n\nCertifikát je potrebné vydať v aplikácii eID Klient”, prípadne použiť iný občiansky preukaz.\nNávod na vydanie certifikátu nájdete na [navody.digital](https://navody.digital/zivotne-situacie/aktivacia-eid/krok/certifikaty).'**
String get selectSigningCertificateNoCertificateBody;

/// No description provided for @selectSigningCertificateNoCertificateGuideUrl.
///
/// In sk, this message translates to:
/// **'https://navody.digital/zivotne-situacie/aktivacia-eid/krok/certifikaty'**
String get selectSigningCertificateNoCertificateGuideUrl;

/// No description provided for @selectSigningCertificateErrorHeading.
///
/// In sk, this message translates to:
Expand Down Expand Up @@ -544,8 +538,8 @@ abstract class AppLocalizations {
/// No description provided for @saveSignedDocumentSuccessMessage.
///
/// In sk, this message translates to:
/// **'Dokument bol uložený do Downloads pod názvom '**
String get saveSignedDocumentSuccessMessage;
/// **'Dokument bol uložený do **{directory}** pod názvom [{name}](#).'**
String saveSignedDocumentSuccessMessage(Object directory, Object name);

/// No description provided for @saveSignedDocumentErrorMessage.
///
Expand Down
9 changes: 4 additions & 5 deletions lib/l10n/app_localizations_sk.dart
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,7 @@ class AppLocalizationsSk extends AppLocalizations {
String get selectSigningCertificateNoCertificateHeading => 'Certifikát nebol nájdený';

@override
String get selectSigningCertificateNoCertificateBody => 'Nepodarilo sa nájsť certifikát pre kvalifikovaný elektronický podpis.\n\nCertifikát je potrebné vydať v aplikácii eID klient, prípadne použiť iný občiansky preukaz.\nNávod na vydanie certifikátu nájdete na ';

@override
String get selectSigningCertificateNoCertificateGuideUrl => 'https://navody.digital/zivotne-situacie/aktivacia-eid/krok/certifikaty';
String get selectSigningCertificateNoCertificateBody => 'Nepodarilo sa nájsť certifikát pre **kvalifikovaný elektronický podpis**.\n\nCertifikát je potrebné vydať v aplikácii “eID Klient”, prípadne použiť iný občiansky preukaz.\nNávod na vydanie certifikátu nájdete na [navody.digital](https://navody.digital/zivotne-situacie/aktivacia-eid/krok/certifikaty).';

@override
String get selectSigningCertificateErrorHeading => 'Chyba pri načítavaní certifikátov z občianskeho preukazu.';
Expand Down Expand Up @@ -284,7 +281,9 @@ class AppLocalizationsSk extends AppLocalizations {
String get documentSigningSuccessTitle => 'Dokument bol úspešne podpísaný';

@override
String get saveSignedDocumentSuccessMessage => 'Dokument bol uložený do Downloads pod názvom ';
String saveSignedDocumentSuccessMessage(Object directory, Object name) {
return 'Dokument bol uložený do **$directory** pod názvom [$name](#).';
}

@override
String saveSignedDocumentErrorMessage(Object error) {
Expand Down
5 changes: 2 additions & 3 deletions lib/l10n/app_sk.arb
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@
"selectSigningCertificateCanceledHeading": "Čítanie certifikátu bolo prerušené",
"selectSigningCertificateCanceledBody": "Skúste prosím znovu načítať certifikát z vášho občianskeho preukazu.",
"selectSigningCertificateNoCertificateHeading": "Certifikát nebol nájdený",
"selectSigningCertificateNoCertificateBody": "Nepodarilo sa nájsť certifikát pre kvalifikovaný elektronický podpis.\n\nCertifikát je potrebné vydať v aplikácii eID klient, prípadne použiť iný občiansky preukaz.\nNávod na vydanie certifikátu nájdete na ",
"selectSigningCertificateNoCertificateGuideUrl": "https://navody.digital/zivotne-situacie/aktivacia-eid/krok/certifikaty",
"selectSigningCertificateNoCertificateBody": "Nepodarilo sa nájsť certifikát pre **kvalifikovaný elektronický podpis**.\n\nCertifikát je potrebné vydať v aplikácii “eID Klient”, prípadne použiť iný občiansky preukaz.\nNávod na vydanie certifikátu nájdete na [navody.digital](https://navody.digital/zivotne-situacie/aktivacia-eid/krok/certifikaty).",

"selectSigningCertificateErrorHeading": "Chyba pri načítavaní certifikátov z občianskeho preukazu.",

Expand All @@ -92,7 +91,7 @@
"signDocumentErrorHeading": "Pri podpisovaní sa vyskytla chyba",

"documentSigningSuccessTitle": "Dokument bol úspešne podpísaný",
"saveSignedDocumentSuccessMessage": "Dokument bol uložený do Downloads pod názvom ",
"saveSignedDocumentSuccessMessage": "Dokument bol uložený do **{directory}** pod názvom [{name}](#).",
"saveSignedDocumentErrorMessage": "Pri ukladaní súboru sa vyskytla chyba:\n{error}",

"shareSignedDocumentLabel": "Zdieľať podpísaný dokument",
Expand Down
38 changes: 4 additions & 34 deletions lib/ui/fragment/select_signing_certificate_fragment.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import 'package:eidmsdk/types.dart' show Certificate;
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart' show launchUrl;

import '../../bloc/select_signing_certificate_cubit.dart';
import '../../strings_context.dart';
import '../widgets/error_content.dart';
import '../widgets/loading_content.dart';
import '../widgets/markdown_text.dart';
import '../widgets/result_view.dart';

/// Fragment that is used in screens working with [SelectSigningCertificateState].
Expand Down Expand Up @@ -61,42 +60,13 @@ class SelectSigningCertificateFragment extends StatelessWidget {
/// Partial content for "no certificates" state.
static Widget noCertificatesContent(BuildContext context) {
final strings = context.strings;
final address =
Uri.parse(strings.selectSigningCertificateNoCertificateGuideUrl);

final textStyle = Theme.of(context).textTheme.bodyMedium;
// TODO Use MarkdownText instead of RichText
final body = RichText(
text: TextSpan(
children: [
TextSpan(
text: strings.selectSigningCertificateNoCertificateBody,
style: textStyle,
),
TextSpan(
text: address.authority,
style: textStyle?.copyWith(
color: Theme.of(context).colorScheme.primary,
decoration: TextDecoration.underline,
fontWeight: FontWeight.bold,
),
recognizer: TapGestureRecognizer()
..onTap = () {
launchUrl(address);
},
),
TextSpan(
text: ".",
style: textStyle,
),
],
),
);

return ResultView(
icon: 'assets/images/lock.svg',
titleText: strings.selectSigningCertificateNoCertificateHeading,
body: body,
body: MarkdownText(
strings.selectSigningCertificateNoCertificateBody,
),
);
}
}
61 changes: 29 additions & 32 deletions lib/ui/screens/present_signed_document_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'dart:developer' as developer;
import 'dart:io' show File, OSError, PathAccessException;

import 'package:autogram_sign/autogram_sign.dart' show SignDocumentResponseBody;
import 'package:flutter/gestures.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:share_plus/share_plus.dart';
Expand All @@ -12,11 +12,12 @@ import 'package:widgetbook_annotation/widgetbook_annotation.dart' as widgetbook;
import '../../bloc/present_signed_document_cubit.dart';
import '../../data/document_signing_type.dart';
import '../../di.dart';
import '../../file_extensions.dart';
import '../../file_system_entity_extensions.dart';
import '../../strings_context.dart';
import '../../util/errors.dart';
import '../app_theme.dart';
import '../widgets/loading_content.dart';
import '../widgets/markdown_text.dart';
import '../widgets/result_view.dart';

/// Screen for presenting signed document.
Expand Down Expand Up @@ -153,11 +154,7 @@ class _Body extends StatelessWidget {
sharingEnabled ? this.onShareFileRequested : null;

return switch (state) {
PresentSignedDocumentInitialState _ => _SuccessContent(
file: null,
onShareFileRequested: null,
onCloseRequested: onCloseRequested,
),
PresentSignedDocumentInitialState _ => const LoadingContent(),
PresentSignedDocumentLoadingState _ => const LoadingContent(),
PresentSignedDocumentErrorState _ => _SuccessContent(
file: null,
Expand Down Expand Up @@ -193,27 +190,15 @@ class _SuccessContent extends StatelessWidget {
Widget body = const SizedBox(height: 58);

if (file != null) {
final fileNameTextStyle = TextStyle(
color: Theme.of(context).colorScheme.primary,
decoration: TextDecoration.underline,
fontWeight: FontWeight.bold,
);
// TODO Use MarkdownText instead of RichText
body = RichText(
text: TextSpan(
text: strings.saveSignedDocumentSuccessMessage,
style: Theme.of(context).textTheme.bodyLarge,
//style: TextStyle(color: Theme.of(context).colorScheme.onBackground),
children: [
// Emphasize file name
TextSpan(
text: file.basename,
style: fileNameTextStyle,
recognizer: TapGestureRecognizer()..onTap = onShareFileRequested,
)
],
),
textAlign: TextAlign.center,
final directory = _getParentDirectoryName(file);
final name = file.basename;
final text = strings.saveSignedDocumentSuccessMessage(directory, name);

body = MarkdownText(
text,
onLinkTap: (_, __, ___) {
onShareFileRequested?.call();
},
);
}

Expand Down Expand Up @@ -249,6 +234,18 @@ class _SuccessContent extends StatelessWidget {
],
);
}

static String _getParentDirectoryName(File file) {
return kIsWeb
? file.uri
.resolve('.')
.path
.split('/')
.where((e) => e.isNotEmpty)
.lastOrNull ??
'&nbsp;'
: file.parent.basename;
}
}

@widgetbook.UseCase(
Expand Down Expand Up @@ -341,11 +338,11 @@ Widget previewSuccessPresentSignedDocumentScreen(BuildContext context) {
options: DocumentSigningType.values,
initialOption: DocumentSigningType.local,
);
final fileName = context.knobs.string(
label: "File name",
initialValue: "document_signed.pdf",
final path = context.knobs.string(
label: "File path",
initialValue: "Downloads/document_signed.pdf",
);
final file = File(fileName);
final file = File(path);

return _Body(
state: PresentSignedDocumentSuccessState(file),
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/screens/preview_document_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import 'package:widgetbook_annotation/widgetbook_annotation.dart' as widgetbook;

import '../../bloc/preview_document_cubit.dart';
import '../../data/document_signing_type.dart';
import '../../file_extensions.dart';
import '../../file_system_entity_extensions.dart';
import '../../strings_context.dart';
import '../app_theme.dart';
import '../widgets/document_visualization.dart';
Expand Down
Loading

0 comments on commit 83dbbef

Please sign in to comment.