Refactor codebase resolving warnings and remove self-package imports

This commit is contained in:
2023-06-03 11:27:14 +02:00
parent 6d0898d6ac
commit f0da6f2596
79 changed files with 204 additions and 193 deletions

View File

@ -0,0 +1,253 @@
import 'package:better_open_file/better_open_file.dart';
import 'package:bubble/bubble.dart';
import 'package:flowder/flowder.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:jiffy/jiffy.dart';
import '../../../api/marianumcloud/talk/chat/getChatResponse.dart';
import '../../../api/marianumcloud/talk/room/getRoomResponse.dart';
import '../../../model/appTheme.dart';
import '../../settings/debug/jsonViewer.dart';
import '../files/fileElement.dart';
import 'chatMessage.dart';
class ChatBubble extends StatefulWidget {
final BuildContext context;
final bool isSender;
final GetChatResponseObject bubbleData;
final GetRoomResponseObject chatData;
const ChatBubble({
required this.context,
required this.isSender,
required this.bubbleData,
required this.chatData,
Key? key}) : super(key: key);
@override
State<ChatBubble> createState() => _ChatBubbleState();
}
class _ChatBubbleState extends State<ChatBubble> {
// late BubbleStyle styleSystem;
// late BubbleStyle Function(bool) styleRemote;
// late BubbleStyle Function(bool) styleSelf;
BubbleStyle getSystemStyle() {
return BubbleStyle(
color: AppTheme.isDarkMode(context) ? const Color(0xff182229) : Colors.white,
borderWidth: 1,
elevation: 2,
margin: const BubbleEdges.only(bottom: 20, top: 10),
alignment: Alignment.center,
);
}
BubbleStyle getRemoteStyle(bool seamless) {
var color = AppTheme.isDarkMode(context) ? const Color(0xff202c33) : Colors.white;
return BubbleStyle(
nip: BubbleNip.leftTop,
color: seamless ? Colors.transparent : color,
borderWidth: seamless ? 0 : 1,
elevation: seamless ? 0 : 1,
margin: const BubbleEdges.only(bottom: 10, left: 10, right: 50),
alignment: Alignment.topLeft,
);
}
BubbleStyle getSelfStyle(bool seamless) {
return BubbleStyle(
nip: BubbleNip.rightBottom,
color: seamless ? Colors.transparent : const Color(0xff005c4b),
borderWidth: seamless ? 0 : 1,
elevation: seamless ? 0 : 1,
margin: const BubbleEdges.only(bottom: 10, right: 10, left: 50),
alignment: Alignment.topRight,
);
}
late ChatMessage message;
double downloadProgress = 0;
Future<DownloaderCore>? downloadCore;
Size _textSize(String text, TextStyle style) {
final TextPainter textPainter = TextPainter(
text: TextSpan(text: text, style: style),
maxLines: 1,
textDirection: TextDirection.ltr)
..layout(minWidth: 0, maxWidth: double.infinity);
return textPainter.size;
}
BubbleStyle getStyle() {
if(widget.bubbleData.messageType == GetRoomResponseObjectMessageType.comment) {
if(widget.isSender) {
return getSelfStyle(message.containsFile);
} else {
return getRemoteStyle(message.containsFile);
}
} else {
return getSystemStyle();
}
}
@override
Widget build(BuildContext context) {
message = ChatMessage(originalMessage: widget.bubbleData.message, originalData: widget.bubbleData.messageParameters);
bool showActorDisplayName = widget.bubbleData.messageType == GetRoomResponseObjectMessageType.comment && widget.chatData.type != GetRoomResponseObjectConversationType.oneToOne;
bool showBubbleTime = widget.bubbleData.messageType != GetRoomResponseObjectMessageType.system;
var actorTextStyle = TextStyle(color: Theme.of(context).primaryColor, fontWeight: FontWeight.bold);
return GestureDetector(
child: Bubble(
style: getStyle(),
child: Container(
constraints: BoxConstraints(
maxWidth: MediaQuery.of(context).size.width * 0.9,
minWidth: showActorDisplayName ? _textSize(widget.bubbleData.actorDisplayName, actorTextStyle).width : 30,
),
child: Stack(
children: [
Padding(
padding: EdgeInsets.only(bottom: showBubbleTime ? 18 : 0, top: showActorDisplayName ? 18 : 0),
child: FutureBuilder(
future: message.getWidget(),
builder: (context, snapshot) {
if(!snapshot.hasData) return const CircularProgressIndicator();
return snapshot.data ?? const Icon(Icons.error);
},
)
),
Visibility(
visible: showActorDisplayName,
child: Positioned(
top: 0,
left: 0,
child: Text(
widget.bubbleData.actorDisplayName,
textAlign: TextAlign.start,
style: actorTextStyle,
),
),
),
Visibility(
visible: showBubbleTime,
child: Positioned(
bottom: 0,
right: 0,
child: Text(
Jiffy.parseFromMillisecondsSinceEpoch(widget.bubbleData.timestamp * 1000).format(pattern: "HH:mm"),
textAlign: TextAlign.end,
style: const TextStyle(color: Colors.grey, fontSize: 12),
),
),
),
Visibility(
visible: downloadProgress > 0,
child: Positioned(
top: 0,
left: 0,
right: 0,
bottom: 0,
child: Stack(
children: [
const Center(child: Icon(Icons.download)),
const Center(child: CircularProgressIndicator(color: Colors.white)),
Center(child: CircularProgressIndicator(value: downloadProgress/100)),
],
)
),
),
],
),
),
),
onLongPress: () {
showDialog(context: context, builder: (context) {
return SimpleDialog(
children: [
Visibility(
visible: !message.containsFile && widget.bubbleData.messageType == GetRoomResponseObjectMessageType.comment,
child: ListTile(
leading: const Icon(Icons.copy),
title: const Text("Nachricht kopieren"),
onTap: () => {
Clipboard.setData(ClipboardData(text: widget.bubbleData.message)),
Navigator.of(context).pop(),
},
),
),
Visibility(
visible: !widget.isSender && widget.chatData.type != GetRoomResponseObjectConversationType.oneToOne,
child: ListTile(
leading: const Icon(Icons.sms_outlined),
title: Text("Private Nachricht an '${widget.bubbleData.actorDisplayName}'"),
onTap: () => {},
),
),
ListTile(
leading: const Icon(Icons.bug_report_outlined),
title: const Text("Debugdaten anzeigen"),
onTap: () => JsonViewer.asDialog(context, widget.bubbleData.toJson()),
)
],
);
});
},
onTap: () {
if(message.file == null) return;
if(downloadProgress > 0) {
showDialog(context: context, builder: (context) {
return AlertDialog(
title: const Text("Download abbrechen?"),
content: const Text("Möchtest du den Download abbrechen?"),
actions: [
TextButton(onPressed: () {
Navigator.of(context).pop();
}, child: const Text("Nein")),
TextButton(onPressed: () {
downloadCore?.then((value) {
if(!value.isCancelled) value.cancel();
Navigator.of(context).pop();
});
setState(() {
downloadProgress = 0;
downloadCore = null;
});
}, child: const Text("Ja, Abbrechen"))
],
);
});
return;
}
downloadProgress = 1;
downloadCore = FileElement.download(message.file!.path!, message.file!.name, (progress) {
if(progress > 1) {
setState(() {
downloadProgress = progress;
});
}
}, (result) {
setState(() {
downloadProgress = 0;
});
if(result.type != ResultType.done) {
showDialog(context: context, builder: (context) {
return AlertDialog(
content: Text(result.message),
);
});
}
});
},
);
}
}

View File

@ -0,0 +1,119 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:jiffy/jiffy.dart';
import 'package:persistent_bottom_nav_bar/persistent_tab_view.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../../../api/marianumcloud/talk/chat/richObjectStringProcessor.dart';
import '../../../api/marianumcloud/talk/room/getRoomResponse.dart';
import '../../../model/chatList/chatListProps.dart';
import '../../../widget/unimplementedDialog.dart';
import 'chatView.dart';
class ChatList extends StatefulWidget {
const ChatList({Key? key}) : super(key: key);
@override
State<ChatList> createState() => _ChatListState();
}
class _ChatListState extends State<ChatList> {
late String username;
@override
void initState() {
super.initState();
SharedPreferences.getInstance().then((value) => {
username = value.getString("username")!
});
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
Provider.of<ChatListProps>(context, listen: false).run();
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Talk"),
actions: [
IconButton(
icon: const Icon(Icons.search),
onPressed: () => {
UnimplementedDialog.show(context)
},
)
],
),
body: Consumer<ChatListProps>(
builder: (context, data, child) {
if(data.primaryLoading()) {
return const Center(child: CircularProgressIndicator());
}
List<ListTile> chats = List<ListTile>.empty(growable: true);
for (var chatRoom in data.getRoomsResponse.sortByLastActivity()) {
CircleAvatar circleAvatar = CircleAvatar(
foregroundImage: chatRoom.type == GetRoomResponseObjectConversationType.oneToOne ? Image.network("https://cloud.marianum-fulda.de/avatar/${chatRoom.name}/128").image : null,
backgroundColor: Theme.of(context).primaryColor,
foregroundColor: Colors.white,
child: chatRoom.type == GetRoomResponseObjectConversationType.group ? const Icon(Icons.group) : const Icon(Icons.person),
);
chats.add(ListTile(
title: Text(chatRoom.displayName),
subtitle: Text("${Jiffy.parseFromMillisecondsSinceEpoch(chatRoom.lastMessage.timestamp * 1000).fromNow()}: ${RichObjectStringProcessor.parseToString(chatRoom.lastMessage.message.replaceAll("\n", " "), chatRoom.lastMessage.messageParameters)}", overflow: TextOverflow.ellipsis),
trailing: Visibility(
visible: chatRoom.unreadMessages > 0,
child: Container(
padding: const EdgeInsets.all(1),
decoration: BoxDecoration(
color: Theme.of(context).primaryColor,
borderRadius: BorderRadius.circular(30),
),
constraints: const BoxConstraints(
minWidth: 20,
minHeight: 20,
),
child: Text(
"${chatRoom.unreadMessages}",
style: const TextStyle(
color: Colors.white,
fontSize: 15,
),
textAlign: TextAlign.center,
),
),
),
leading: circleAvatar,
onTap: () async {
PersistentNavBarNavigator.pushNewScreen(
context,
screen: ChatView(room: chatRoom, selfId: username, avatar: circleAvatar),
withNavBar: false
);
},
));
}
return RefreshIndicator(
color: Theme.of(context).primaryColor,
onRefresh: () {
Provider.of<ChatListProps>(context, listen: false).run(renew: true);
return Future.delayed(const Duration(seconds: 3));
},
child: ListView(children: chats),
);
},
),
);
}
}

View File

@ -0,0 +1,68 @@
import 'dart:convert';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_linkify/flutter_linkify.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:url_launcher/url_launcher_string.dart';
import '../../../api/marianumcloud/talk/chat/getChatResponse.dart';
import '../../../api/marianumcloud/talk/chat/richObjectStringProcessor.dart';
class ChatMessage {
String originalMessage;
Map<String, RichObjectString>? originalData;
RichObjectString? file;
String content = "";
bool get containsFile => file != null;
ChatMessage({required this.originalMessage, this.originalData}) {
if(originalData?.containsKey("file") ?? false) {
file = originalData?['file'];
content = file?.name ?? "Datei";
} else {
content = RichObjectStringProcessor.parseToString(originalMessage, originalData);
}
}
Future<Widget> getWidget() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
if(file == null) {
return SelectableLinkify(
text: content,
onOpen: onOpen,
);
}
return CachedNetworkImage(
errorWidget: (context, url, error) {
return Column(
children: [
const Icon(Icons.image_not_supported_outlined, size: 35),
Text("Keine Dateivorschau:\n${file!.name}", style: const TextStyle(fontWeight: FontWeight.bold))
],
);
},
alignment: Alignment.center,
placeholder: (context, url) {
return const Padding(padding: EdgeInsets.all(10), child: CircularProgressIndicator());
},
fadeInDuration: const Duration(seconds: 1),
imageUrl: "https://cloud.marianum-fulda.de/core/preview?fileId=${file!.id}&x=100&y=-1&a=1",
httpHeaders: {
"Authorization": "Basic ${base64.encode(utf8.encode("${preferences.getString("username")}:${preferences.getString("password")}"))}" // TODO move authentication
},
);
}
void onOpen(LinkableElement link) async {
if(await canLaunchUrlString(link.url)) {
await launchUrlString(link.url);
} else {
}
}
}

View File

@ -0,0 +1,145 @@
import 'dart:developer';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:loader_overlay/loader_overlay.dart';
import 'package:provider/provider.dart';
import '../../../api/marianumcloud/talk/sendMessage/sendMessage.dart';
import '../../../api/marianumcloud/talk/sendMessage/sendMessageParams.dart';
import '../../../model/chatList/chatProps.dart';
import '../../../widget/filePick.dart';
class ChatTextfield extends StatefulWidget {
final String sendToToken;
const ChatTextfield(this.sendToToken, {Key? key}) : super(key: key);
@override
State<ChatTextfield> createState() => _ChatTextfieldState();
}
class _ChatTextfieldState extends State<ChatTextfield> {
final TextEditingController _textBoxController = TextEditingController();
bool sending = false;
bool isLoading = false;
void mediaUpload(String? path) {
if(path == null) {
context.loaderOverlay.hide();
return;
}
showDialog(context: context, builder: (context) {
return AlertDialog(
title: const Text("Datei senden"),
content: Image.file(File(path)),
actions: [
TextButton(onPressed: () {
Navigator.of(context).pop();
context.loaderOverlay.hide();
}, child: const Text("Abbrechen")),
TextButton(onPressed: () {
context.loaderOverlay.hide();
}, child: const Text("Senden")),
],
);
});
}
@override
Widget build(BuildContext context) {
return Stack(
children: <Widget>[
Align(
alignment: Alignment.bottomLeft,
child: Container(
padding: const EdgeInsets.only(left: 10, bottom: 10, top: 10),
height: 60,
width: double.infinity,
color: Theme.of(context).colorScheme.secondary,
child: Row(
children: <Widget>[
GestureDetector(
onTap: (){
showDialog(context: context, builder: (context) {
return SimpleDialog(
children: [
ListTile(
leading: const Icon(Icons.file_open),
title: const Text("Aus Dateien auswählen"),
onTap: () {
context.loaderOverlay.show();
FilePick.documentPick().then((value) {
log(value ?? "?");
mediaUpload(value);
});
Navigator.of(context).pop();
},
),
ListTile(
leading: const Icon(Icons.image),
title: const Text("Aus Gallerie auswählen"),
onTap: () {
context.loaderOverlay.show();
FilePick.galleryPick().then((value) {
log(value?.path ?? "?");
mediaUpload(value?.path);
});
Navigator.of(context).pop();
},
),
],
);
});
},
child: Container(
height: 30,
width: 30,
decoration: BoxDecoration(
color: Theme.of(context).primaryColor,
borderRadius: BorderRadius.circular(30),
),
child: const Icon(Icons.add, color: Colors.white, size: 20, ),
),
),
const SizedBox(width: 15),
Expanded(
child: TextField(
controller: _textBoxController,
readOnly: sending,
maxLines: 10,
decoration: InputDecoration(
hintText: "Nachricht schreiben...",
hintStyle: TextStyle(color: Theme.of(context).colorScheme.onSecondary),
border: InputBorder.none
),
),
),
const SizedBox(width: 15),
FloatingActionButton(
onPressed: (){
if(_textBoxController.text.isEmpty) return;
setState(() {
sending = true;
});
SendMessage(widget.sendToToken, SendMessageParams(_textBoxController.text)).run().then((value) => {
Provider.of<ChatProps>(context, listen: false).run(),
_textBoxController.text = "",
setState(() {
sending = false;
}),
});
},
backgroundColor: Theme.of(context).primaryColor,
elevation: 0,
child: const Icon(Icons.send, color: Colors.white, size: 18),
),
],
),
),
),
],
);
}
}

View File

@ -0,0 +1,117 @@
import 'package:flutter/material.dart';
import 'package:jiffy/jiffy.dart';
import 'package:loader_overlay/loader_overlay.dart';
import 'package:provider/provider.dart';
import '../../../api/marianumcloud/talk/chat/getChatResponse.dart';
import '../../../api/marianumcloud/talk/room/getRoomResponse.dart';
import '../../../model/appTheme.dart';
import '../../../model/chatList/chatProps.dart';
import 'chatBubble.dart';
import 'chatTextfield.dart';
class ChatView extends StatefulWidget {
final GetRoomResponseObject room;
final String selfId;
final CircleAvatar avatar;
const ChatView({Key? key, required this.room, required this.selfId, required this.avatar}) : super(key: key);
@override
State<ChatView> createState() => _ChatViewState();
}
class _ChatViewState extends State<ChatView> {
final ScrollController _listController = ScrollController();
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
Provider.of<ChatProps>(context, listen: false).setQueryToken(widget.room.token);
});
}
@override
Widget build(BuildContext context) {
return Consumer<ChatProps>(
builder: (context, data, child) {
List<Widget> messages = List<Widget>.empty(growable: true);
if(!data.primaryLoading()) {
DateTime lastDate = DateTime.now();
data.getChatResponse.sortByTimestamp().forEach((element) {
DateTime elementDate = DateTime.fromMillisecondsSinceEpoch(element.timestamp * 1000);
if(elementDate.weekday != lastDate.weekday) {
lastDate = elementDate;
messages.add(ChatBubble(
context: context,
isSender: true,
bubbleData: GetChatResponseObject(
1,
"asd",
GetRoomResponseObjectMessageActorType.bridge,
"system",
"System",
element.timestamp,
elementDate.toIso8601String(),
GetRoomResponseObjectMessageType.system,
false,
"",
Jiffy.parseFromDateTime(elementDate).format(pattern: "dd.MM.yyyy"),
null
),
chatData: widget.room
));
}
messages.add(ChatBubble(context: context, isSender: element.actorId == widget.selfId, bubbleData: element, chatData: widget.room));
});
}
return Scaffold(
backgroundColor: const Color(0xffefeae2),
appBar: AppBar(
title: Row(
children: [
widget.avatar,
const SizedBox(width: 10),
Expanded(
child: Text(widget.room.displayName, overflow: TextOverflow.ellipsis, maxLines: 1),
)
],
),
),
body: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AppTheme.isDarkMode(context) ? const AssetImage("assets/background/chatDark.png") : const AssetImage("assets/background/chat.png"),
scale: 1.5,
opacity: 0.5,
repeat: ImageRepeat.repeat,
colorFilter: const ColorFilter.linearToSrgbGamma()
)
),
child: LoaderOverlay(
child: data.primaryLoading() ? const Center(child: CircularProgressIndicator()) : Column(
children: [
Expanded(
child: ListView(
reverse: true,
controller: _listController,
children: messages.reversed.toList(),
),
),
ChatTextfield(widget.room.token),
],
),
)
),
);
},
);
}
}