Skip to content

Commit

Permalink
implemented reading of pdf files
Browse files Browse the repository at this point in the history
  • Loading branch information
FaFre committed May 7, 2024
1 parent fbae085 commit ad77951
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 15 deletions.
5 changes: 5 additions & 0 deletions app/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/pdf" />
</intent-filter>
</activity>
<receiver android:name="BangNavigatorWidgetProvider" android:exported="true">
<intent-filter>
Expand Down
53 changes: 39 additions & 14 deletions app/lib/features/share_intent/domain/services/sharing_intent.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,52 @@
import 'dart:async';

import 'package:bang_navigator/core/logger.dart';
import 'package:bang_navigator/domain/entities/received_parameter.dart';
import 'package:bang_navigator/features/search_browser/domain/entities/modes.dart';
import 'package:collection/collection.dart';
import 'package:flutter_pdf_text/flutter_pdf_text.dart';
import 'package:flutter_sharing_intent/flutter_sharing_intent.dart';
import 'package:flutter_sharing_intent/model/sharing_file.dart';
import 'package:mime/mime.dart' as mime;
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:rxdart/rxdart.dart';
import 'package:uri_to_file/uri_to_file.dart' as uri_to_file;

part 'sharing_intent.g.dart';

final _sharingIntentTransformer =
StreamTransformer<List<SharedFile>, ReceivedParameter>.fromHandlers(
handleData: (files, sink) async {
//For now only one file is supported to share with the app
final data = files.firstOrNull;

if (data != null && data.value != null) {
switch (data.type) {
case SharedMediaType.TEXT:
case SharedMediaType.URL:
if (uri_to_file.isUriSupported(data.value!)) {
final file = await uri_to_file.toFile(data.value!);
final mimeType = mime.lookupMimeType(file.path);
switch (mimeType) {
case 'application/pdf':
final content =
await PDFDoc.fromFile(file).then((doc) => doc.text);
sink.add(
ReceivedParameter(content, KagiTool.summarizer.name),
);
default:
logger.w('Unhandled mime type: $mimeType');
}
} else {
sink.add(ReceivedParameter(data.value, null));
}
default:
logger.w('Unhandled media type: $data');
}
}
},
);

@riverpod
Raw<Stream<ReceivedParameter>> sharingIntentStream(SharingIntentStreamRef ref) {
final initialStream = FlutterSharingIntent.instance
Expand All @@ -22,18 +60,5 @@ Raw<Stream<ReceivedParameter>> sharingIntentStream(SharingIntentStreamRef ref) {

return ConcatStream(
[initialStream, FlutterSharingIntent.instance.getMediaStream()],
)
.map(
(event) => event
.where(
(shared) =>
shared.type == SharedMediaType.TEXT ||
shared.type == SharedMediaType.URL,
)
.map((shared) => shared.value)
.whereNotNull()
.firstOrNull,
)
.whereNotNull()
.map((content) => ReceivedParameter(content, null));
).transform(_sharingIntentTransformer);
}
18 changes: 17 additions & 1 deletion app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.1.7447"
flutter_pdf_text:
dependency: "direct main"
description:
name: flutter_pdf_text
sha256: "97a49ecba6cf711a607642785758bf74fe0aaa3a2223524fa0c142e3966c56a0"
url: "https://pub.dev"
source: hosted
version: "0.6.0"
flutter_riverpod:
dependency: transitive
description:
Expand Down Expand Up @@ -660,7 +668,7 @@ packages:
source: hosted
version: "1.12.0"
mime:
dependency: transitive
dependency: "direct main"
description:
name: mime
sha256: "2e123074287cc9fd6c09de8336dae606d1ddb88d9ac47358826db698c176a1f2"
Expand Down Expand Up @@ -1064,6 +1072,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.2.2"
uri_to_file:
dependency: "direct main"
description:
name: uri_to_file
sha256: cbbb38f975d22311efacc9a53cdcb806708606666d8092314b81c5cb7983b700
url: "https://pub.dev"
source: hosted
version: "1.0.0"
url_launcher:
dependency: "direct main"
description:
Expand Down
3 changes: 3 additions & 0 deletions app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dependencies:
flutter_hooks: ^0.20.5
flutter_inappwebview: ^6.0.0
flutter_material_design_icons: ^1.1.7447
flutter_pdf_text: ^0.6.0
flutter_secure_storage: ^9.0.0
flutter_sharing_intent: ^1.1.1
go_router: ^13.2.2
Expand All @@ -26,6 +27,7 @@ dependencies:
html: ^0.15.4
http: ^1.2.1
logger: ^2.2.0
mime: ^1.0.5
riverpod: ^2.5.1
riverpod_annotation: ^2.3.5
rxdart: ^0.27.7
Expand All @@ -35,6 +37,7 @@ dependencies:
speech_to_text_google_dialog: ^0.0.6
text_scroll: ^0.2.0
universal_io: ^2.2.2
uri_to_file: ^1.0.0
url_launcher: ^6.2.5

dev_dependencies:
Expand Down

0 comments on commit ad77951

Please sign in to comment.