Skip to content

Commit

Permalink
TW-403: add share screen and share intent
Browse files Browse the repository at this point in the history
  • Loading branch information
sherlockvn committed Aug 29, 2023
1 parent d072481 commit 7822458
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 37 deletions.
17 changes: 1 addition & 16 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,25 +79,10 @@
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="im.fluffychat" android:host="chat" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="document/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="video/*" />
<data android:mimeType="*/*" />
</intent-filter>
</activity>

Expand Down
4 changes: 2 additions & 2 deletions ios/FluffyChat Share/ShareViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Photos

class ShareViewController: SLComposeServiceViewController {
// TODO: IMPORTANT: This should be your host app bundle identifier
let hostAppBundleIdentifier = "im.fluffychat.app"
let hostAppBundleIdentifier = "com.linagora.ios.twake"
let sharedKey = "ShareKey"
var sharedMedia: [SharedMediaFile] = []
var sharedText: [String] = []
Expand Down Expand Up @@ -200,7 +200,7 @@ class ShareViewController: SLComposeServiceViewController {
}

private func redirectToHostApp(type: RedirectType) {
let url = URL(string: "ShareMedia://dataUrl=\(sharedKey)#\(type)")
let url = URL(string: "ShareMedia-\(hostAppBundleIdentifier)://dataUrl=\(sharedKey)#\(type)")
var responder = self as UIResponder?
let selectorOpenURL = sel_registerName("openURL:")

Expand Down
2 changes: 1 addition & 1 deletion ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<string>im.fluffychat.app.uris</string>
<key>CFBundleURLSchemes</key>
<array>
<string>ShareMedia</string>
<string>ShareMedia-$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<string>twake.chat</string>
<string>matrix</string>
</array>
Expand Down
42 changes: 29 additions & 13 deletions lib/config/go_routes/go_router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:fluffychat/pages/chat_details/chat_details.dart';
import 'package:fluffychat/pages/chat_draft/draft_chat.dart';
import 'package:fluffychat/pages/chat_encryption_settings/chat_encryption_settings.dart';
import 'package:fluffychat/pages/homeserver_picker/homeserver_picker.dart';
import 'package:fluffychat/pages/share/share.dart';
import 'package:fluffychat/pages/story/story_page.dart';
import 'package:fluffychat/utils/responsive/responsive_utils.dart';
import 'package:fluffychat/widgets/layouts/adaptive_layout/adaptive_scaffold.dart';
Expand Down Expand Up @@ -36,6 +37,7 @@ import 'package:fluffychat/widgets/log_view.dart';
import 'package:fluffychat/widgets/matrix.dart';
import 'package:flutter/cupertino.dart';
import 'package:go_router/go_router.dart';
import 'package:matrix/matrix.dart';

abstract class AppRoutes {
static FutureOr<String?> loggedInRedirect(
Expand Down Expand Up @@ -160,12 +162,14 @@ abstract class AppRoutes {
routes: [
GoRoute(
path: ':roomid',
pageBuilder: (context, state) => defaultPageBuilder(
context,
Chat(
roomId: state.pathParameters['roomid']!,
),
),
pageBuilder: (context, state) {
return defaultPageBuilder(
context,
Chat(
roomId: state.pathParameters['roomid']!,
),
);
},
redirect: loggedOutRedirect,
),
],
Expand Down Expand Up @@ -346,13 +350,17 @@ abstract class AppRoutes {
),
GoRoute(
path: ':roomid',
pageBuilder: (context, state) => defaultPageBuilder(
context,
Chat(
roomId: state.pathParameters['roomid']!,
key: Key(state.pathParameters['roomid']!),
),
),
pageBuilder: (context, state) {
final shareFile = state.extra as MatrixFile?;
return defaultPageBuilder(
context,
Chat(
roomId: state.pathParameters['roomid']!,
key: Key(state.pathParameters['roomid']!),
shareFile: shareFile,
),
);
},
redirect: loggedOutRedirect,
routes: [
GoRoute(
Expand Down Expand Up @@ -431,6 +439,14 @@ abstract class AppRoutes {
),
],
),
GoRoute(
path: '/share',
pageBuilder: (context, state) => defaultPageBuilder(
context,
const Share(),
),
redirect: loggedOutRedirect,
),
],
),
];
Expand Down
66 changes: 66 additions & 0 deletions lib/pages/share/share.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import 'package:collection/collection.dart';
import 'package:fluffychat/di/global/get_it_initializer.dart';
import 'package:fluffychat/domain/usecase/send_file_interactor.dart';
import 'package:fluffychat/pages/share/share_view.dart';
import 'package:fluffychat/presentation/mixins/send_files_mixin.dart';
import 'package:fluffychat/widgets/matrix.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:matrix/matrix.dart';
import 'package:scroll_to_index/scroll_to_index.dart';

class Share extends StatefulWidget {
const Share({super.key});

@override
State<Share> createState() => ShareController();
}

class ShareController extends State<Share> with SendFilesMixin {
final sendFileInteractor = getIt.get<SendFileInteractor>();

final isShowRecentlyChatsNotifier = ValueNotifier(true);

final AutoScrollController recentChatScrollController =
AutoScrollController();

final selectedRoomsNotifier = ValueNotifier(<String>[]);

void onSelectChat(String id) {
if (selectedRoomsNotifier.value.contains(id)) {
selectedRoomsNotifier.value.remove(id);
} else {
selectedRoomsNotifier.value.add(id);
}
selectedRoomsNotifier.value = selectedRoomsNotifier.value.sorted(
(current, next) => current.compareTo(next),
);
}

void toggleRecentlyChats() {
isShowRecentlyChatsNotifier.value = !isShowRecentlyChatsNotifier.value;
}

void shareTo(String roomId) async {
final room = Room(
id: selectedRoomsNotifier.value.first,
client: Matrix.of(context).client,
);
final shareContent = Matrix.of(context).shareContent;
if (shareContent != null) {
final shareFile = shareContent.tryGet<MatrixFile>('file');
if (shareContent.tryGet<String>('msgtype') == 'chat.fluffy.shared_file') {
context.go('/rooms/${room.id}', extra: shareFile);
} else {
room.sendEvent(shareContent);
context.go('/rooms/${room.id}');
}
Matrix.of(context).shareContent = null;
}
}

@override
Widget build(BuildContext context) {
return ShareView(this);
}
}
80 changes: 80 additions & 0 deletions lib/pages/share/share_view.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import 'package:fluffychat/pages/forward/recent_chat_list.dart';
import 'package:fluffychat/pages/forward/recent_chat_title.dart';
import 'package:fluffychat/pages/share/share.dart';
import 'package:fluffychat/widgets/matrix.dart';
import 'package:fluffychat/widgets/twake_components/twake_fab.dart';
import 'package:fluffychat/widgets/twake_components/twake_icon_button.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:go_router/go_router.dart';

class ShareView extends StatelessWidget {
const ShareView(this.controller, {super.key});

final ShareController controller;

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text(
L10n.of(context)!.share,
style: Theme.of(context).textTheme.headlineLarge,
),
leading: TwakeIconButton(
tooltip: L10n.of(context)!.cancel,
icon: Icons.close,
onPressed: () => context.pop(),
),
),
body: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: SingleChildScrollView(
child: Column(
children: [
RecentChatsTitle(
isShowRecentlyChats:
controller.isShowRecentlyChatsNotifier.value,
toggleRecentChat: controller.toggleRecentlyChats,
),
ValueListenableBuilder<bool>(
valueListenable: controller.isShowRecentlyChatsNotifier,
builder: (context, isShowRecentlyChat, child) {
if (isShowRecentlyChat) {
return RecentChatList(
rooms: Matrix.of(context).client.rooms,
selectedEventsNotifier: controller.selectedRoomsNotifier,
onSelectedChat: (roomId) =>
controller.onSelectChat(roomId),
recentChatScrollController:
controller.recentChatScrollController,
);
}

return const SizedBox.shrink();
},
),
],
),
),
),
floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
floatingActionButton: ValueListenableBuilder<List<String>>(
valueListenable: controller.selectedRoomsNotifier,
builder: ((context, selectedChats, child) {
if (selectedChats.length != 1) {
return const SizedBox.shrink();
}
return TwakeFloatingActionButton(
icon: Icons.send,
size: 18.0,
onTap: () => controller.shareTo(
controller.selectedRoomsNotifier.value.first,
),
);
}),
),
);
}
}
9 changes: 5 additions & 4 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1933,10 +1933,11 @@ packages:
receive_sharing_intent:
dependency: "direct main"
description:
name: receive_sharing_intent
sha256: "912bebb551bce75a14098891fd750305b30d53eba0d61cc70cd9973be9866e8d"
url: "https://pub.dev"
source: hosted
path: "."
ref: "receive-.txt-v2"
resolved-ref: "7ac2cccb3e1ac4bddce7e287adcb69726f368b89"
url: "[email protected]:krabbenprgr/receive_sharing_intent.git"
source: git
version: "1.4.5"
record:
dependency: "direct main"
Expand Down
5 changes: 4 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ dependencies:
punycode: ^1.0.0
qr_code_scanner: ^1.0.0
qr_flutter: ^4.0.0
receive_sharing_intent: ^1.4.5
receive_sharing_intent:
git:
url: [email protected]:krabbenprgr/receive_sharing_intent.git
ref: receive-.txt-v2
record: ^4.4.4
scroll_to_index: ^3.0.1
share_plus: ^4.0.10+1
Expand Down

0 comments on commit 7822458

Please sign in to comment.