forked from krille-chan/fluffychat
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from Tawkie/feature-room-bot-can-be-removed-aft…
…er-disconnection Feature room bot can be removed after disconnection
- Loading branch information
Showing
21 changed files
with
1,119 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:fluffychat/pages/add_bridge/service/bot_bridge_connection.dart'; | ||
import 'package:fluffychat/pages/add_bridge/service/hostname.dart'; | ||
import 'package:fluffychat/pages/add_bridge/show_bottom_sheet.dart'; | ||
import 'package:fluffychat/pages/add_bridge/show_delete_conversation_dialog.dart'; | ||
import 'package:fluffychat/utils/platform_infos.dart'; | ||
import 'package:fluffychat/utils/platform_size.dart'; | ||
import 'package:flutter_gen/gen_l10n/l10n.dart'; | ||
import 'package:flutter/cupertino.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
import '../../widgets/matrix.dart'; | ||
import 'add_bridge_header.dart'; | ||
import 'connection_bridge_dialog.dart'; | ||
import 'error_message_dialog.dart'; | ||
import 'model/social_network.dart'; | ||
|
||
// Page offering brigde bot connections to social network chats | ||
class AddBridgeBody extends StatefulWidget { | ||
const AddBridgeBody({ | ||
super.key, | ||
}); | ||
|
||
@override | ||
State<AddBridgeBody> createState() => _AddBridgeBodyState(); | ||
} | ||
|
||
class _AddBridgeBodyState extends State<AddBridgeBody> { | ||
late BotBridgeConnection botConnection; | ||
late String hostname; | ||
bool timeoutErrorOccurred = false; | ||
|
||
@override | ||
void initState() { | ||
final client = Matrix.of(context).client; | ||
String fullUrl = client.homeserver!.host; | ||
hostname = extractHostName(fullUrl); | ||
botConnection = BotBridgeConnection(client: client, hostname: hostname); | ||
super.initState(); | ||
_initStateAsync(); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
botConnection.stopProcess(); | ||
super.dispose(); | ||
} | ||
|
||
// Online status update when page is opened | ||
Future<void> _initStateAsync() async { | ||
try { | ||
final instagramConnected = await botConnection.pingWithTimeout( | ||
context, | ||
botConnection.instagramPing(), | ||
); | ||
if (!mounted) return; // Check if the widget is still mounted | ||
|
||
if (instagramConnected != 'error') { | ||
setState(() { | ||
socialNetwork | ||
.firstWhere((element) => element.name == "Instagram") | ||
.connected = instagramConnected == 'Connected' ? true : false; | ||
socialNetwork | ||
.firstWhere((element) => element.name == "Instagram") | ||
.loading = false; | ||
}); | ||
} else if (mounted) { | ||
showCatchErrorDialog( | ||
context, | ||
"${L10n.of(context)!.err_toConnect} ${L10n.of(context)!.instagram}", | ||
); | ||
} | ||
} on TimeoutException { | ||
// To indicate that the time-out error has occurred | ||
if (mounted) { | ||
timeoutErrorOccurred = true; | ||
} | ||
} catch (error) { | ||
print("Error pinging Instagram: $error"); | ||
await Future.delayed( | ||
const Duration(seconds: 1), | ||
); // Precaution to let the page load | ||
if (mounted && !timeoutErrorOccurred) { | ||
showCatchErrorDialog( | ||
context, | ||
"${L10n.of(context)!.err_toConnect} ${L10n.of(context)!.instagram}", | ||
); | ||
} | ||
} | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar(), | ||
body: SingleChildScrollView( | ||
child: Column( | ||
crossAxisAlignment: CrossAxisAlignment.center, | ||
children: <Widget>[ | ||
buildHeaderBridgeText(context), | ||
buildHeaderBridgeSubText(context), | ||
Center( | ||
child: SizedBox( | ||
width: PlatformInfos.isWeb ? PlatformWidth.webWidth : null, | ||
child: ListView.builder( | ||
shrinkWrap: true, | ||
physics: const NeverScrollableScrollPhysics(), | ||
itemCount: socialNetwork.length, | ||
itemBuilder: (BuildContext context, int index) { | ||
return ListTile( | ||
leading: socialNetwork[index].logo, | ||
title: Text( | ||
socialNetwork[index].name, | ||
), | ||
// Different build of subtle depending on the social network, for now only Instagram | ||
subtitle: buildSubtitle(socialNetwork[index]), | ||
trailing: const Icon( | ||
CupertinoIcons.right_chevron, | ||
), | ||
|
||
// Different ways of connecting and disconnecting depending on the social network | ||
onTap: () => | ||
handleSocialNetworkAction(socialNetwork[index]), | ||
); | ||
}, | ||
), | ||
), | ||
), | ||
], | ||
), | ||
), | ||
); | ||
} | ||
|
||
// Different ways of connecting and disconnecting depending on the social network, for now only Instagram | ||
void handleSocialNetworkAction(SocialNetwork network) async { | ||
if (network.loading == false) { | ||
if (network.connected != true) { | ||
bool success = false; | ||
switch (network.name) { | ||
case "Instagram": | ||
// Trying to connect to Instagram | ||
success = await connectToInstagram(context, network, botConnection); | ||
break; | ||
|
||
// For other networks | ||
} | ||
if (success) { | ||
setState(() { | ||
network.connected = true; | ||
}); | ||
} | ||
} else { | ||
// Disconnect button, for the moment only this choice | ||
final bool success = await showBottomSheetBridge( | ||
context, | ||
network, | ||
botConnection, | ||
); | ||
|
||
if (success) { | ||
setState(() { | ||
network.connected = false; | ||
}); | ||
|
||
// Show the dialog for deleting the conversation | ||
await showDeleteConversationDialog(context, network, botConnection); | ||
} else { | ||
// Display error message to warn user | ||
showCatchErrorDialog(context, L10n.of(context)!.err_timeOut); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Different build of subtle depending on the social network, for now only Instagram | ||
Widget buildSubtitle(SocialNetwork network) { | ||
if (network.loading == true) { | ||
return const Align( | ||
alignment: Alignment.centerLeft, | ||
child: CircularProgressIndicator( | ||
color: Colors.grey, | ||
), | ||
); | ||
} else { | ||
return Text( | ||
network.connected == true | ||
? L10n.of(context)!.connected | ||
: L10n.of(context)!.notConnected, | ||
style: TextStyle( | ||
color: network.connected == true ? Colors.green : Colors.grey, | ||
), | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_gen/gen_l10n/l10n.dart'; | ||
|
||
// AddBridge page title and subtitle | ||
|
||
Widget buildHeaderBridgeText(BuildContext context) { | ||
return Padding( | ||
padding: const EdgeInsets.all(16.0), | ||
child: Text( | ||
L10n.of(context)!.addSocialMessagingAccounts, | ||
textAlign: TextAlign.center, | ||
style: const TextStyle( | ||
fontSize: 24.0, | ||
fontWeight: FontWeight.bold, | ||
// color: Color(0xFFFAAB22), | ||
), | ||
), | ||
); | ||
} | ||
|
||
Widget buildHeaderBridgeSubText(BuildContext context) { | ||
return Padding( | ||
padding: const EdgeInsets.all(16.0), | ||
child: Text( | ||
L10n.of(context)!.addSocialMessagingAccountsText, | ||
textAlign: TextAlign.center, | ||
style: const TextStyle(fontSize: 16.0), | ||
), | ||
); | ||
} |
Oops, something went wrong.