Improvement:

- Precache image widget
This commit is contained in:
Linloir 2022-10-20 11:07:37 +08:00
parent 72a898a8b0
commit 4366a49466
No known key found for this signature in database
GPG Key ID: 58EEB209A0F2C366
5 changed files with 26 additions and 17 deletions

View File

@ -1,7 +1,7 @@
/* /*
* @Author : Linloir * @Author : Linloir
* @Date : 2022-10-13 14:03:16 * @Date : 2022-10-13 14:03:16
* @LastEditTime : 2022-10-20 00:48:43 * @LastEditTime : 2022-10-20 10:52:30
* @Description : * @Description :
*/ */
@ -87,7 +87,6 @@ class ChatPage extends StatelessWidget {
else { else {
//Return history tile //Return history tile
return Padding( return Padding(
key: ValueKey(state.chatHistory[index].message.contentmd5),
padding: const EdgeInsets.symmetric( padding: const EdgeInsets.symmetric(
horizontal: 24, horizontal: 24,
vertical: 8 vertical: 8

View File

@ -1,7 +1,7 @@
/* /*
* @Author : Linloir * @Author : Linloir
* @Date : 2022-10-13 14:03:56 * @Date : 2022-10-13 14:03:56
* @LastEditTime : 2022-10-20 00:13:18 * @LastEditTime : 2022-10-20 11:04:40
* @Description : * @Description :
*/ */
@ -11,6 +11,7 @@ import 'dart:convert';
import 'package:bloc/bloc.dart'; import 'package:bloc/bloc.dart';
import 'package:convert/convert.dart'; import 'package:convert/convert.dart';
import 'package:crypto/crypto.dart'; import 'package:crypto/crypto.dart';
import 'package:flutter/material.dart';
import 'package:open_file/open_file.dart'; import 'package:open_file/open_file.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import 'package:tcp_client/chat/cubit/chat_state.dart'; import 'package:tcp_client/chat/cubit/chat_state.dart';
@ -78,7 +79,8 @@ class ChatCubit extends Cubit<ChatState> {
var newHistory = ChatHistory( var newHistory = ChatHistory(
message: msg, message: msg,
type: ChatHistoryType.outcome, type: ChatHistoryType.outcome,
status: ChatHistoryStatus.sending status: ChatHistoryStatus.sending,
preCachedImage: msg.type == MessageType.image ? Image.memory(base64.decode(msg.contentDecoded)) : null
); );
if(msg.type == MessageType.file) { if(msg.type == MessageType.file) {
//Remove mock history //Remove mock history
@ -112,7 +114,8 @@ class ChatCubit extends Cubit<ChatState> {
var history = ChatHistory( var history = ChatHistory(
message: message, message: message,
type: message.senderID == userID ? ChatHistoryType.income : ChatHistoryType.outcome, type: message.senderID == userID ? ChatHistoryType.income : ChatHistoryType.outcome,
status: ChatHistoryStatus.done status: ChatHistoryStatus.done,
preCachedImage: message.type == MessageType.image ? Image.memory(base64.decode(message.contentDecoded)) : null
); );
newHistories.add(history); newHistories.add(history);
} }
@ -208,7 +211,8 @@ class ChatCubit extends Cubit<ChatState> {
var newHistory = ChatHistory( var newHistory = ChatHistory(
message: response.message, message: response.message,
type: response.message.senderID == userID ? ChatHistoryType.income : ChatHistoryType.outcome, type: response.message.senderID == userID ? ChatHistoryType.income : ChatHistoryType.outcome,
status: ChatHistoryStatus.done status: ChatHistoryStatus.done,
preCachedImage: response.message.type == MessageType.image ? Image.memory(base64.decode(response.message.contentDecoded)) : null
); );
var newHistoryList = [newHistory, ...state.chatHistory]; var newHistoryList = [newHistory, ...state.chatHistory];
emit(state.copyWith(chatHistory: newHistoryList)); emit(state.copyWith(chatHistory: newHistoryList));
@ -223,7 +227,8 @@ class ChatCubit extends Cubit<ChatState> {
var newHistory = ChatHistory( var newHistory = ChatHistory(
message: message, message: message,
type: message.senderID == userID ? ChatHistoryType.income : ChatHistoryType.outcome, type: message.senderID == userID ? ChatHistoryType.income : ChatHistoryType.outcome,
status: ChatHistoryStatus.done status: ChatHistoryStatus.done,
preCachedImage: message.type == MessageType.image ? Image.memory(base64.decode(message.contentDecoded)) : null
); );
fetchedHistories.insert(0, newHistory); fetchedHistories.insert(0, newHistory);
} }

View File

@ -1,11 +1,12 @@
/* /*
* @Author : Linloir * @Author : Linloir
* @Date : 2022-10-14 14:55:20 * @Date : 2022-10-14 14:55:20
* @LastEditTime : 2022-10-18 15:19:46 * @LastEditTime : 2022-10-20 11:01:03
* @Description : * @Description :
*/ */
import 'package:equatable/equatable.dart'; import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import 'package:tcp_client/repositories/common_models/message.dart'; import 'package:tcp_client/repositories/common_models/message.dart';
enum ChatHistoryType { outcome, income } enum ChatHistoryType { outcome, income }
@ -15,22 +16,26 @@ class ChatHistory extends Equatable {
final Message message; final Message message;
final ChatHistoryType type; final ChatHistoryType type;
final ChatHistoryStatus status; final ChatHistoryStatus status;
final Image? preCachedImage;
const ChatHistory({ const ChatHistory({
required this.message, required this.message,
required this.type, required this.type,
required this.status required this.status,
this.preCachedImage,
}); });
ChatHistory copyWith({ ChatHistory copyWith({
Message? message, Message? message,
ChatHistoryType? type, ChatHistoryType? type,
ChatHistoryStatus? status ChatHistoryStatus? status,
Image? preCachedImage
}) { }) {
return ChatHistory( return ChatHistory(
message: message ?? this.message, message: message ?? this.message,
type: type ?? this.type, type: type ?? this.type,
status: status ?? this.status status: status ?? this.status,
preCachedImage: preCachedImage ?? this.preCachedImage
); );
} }

View File

@ -1,7 +1,7 @@
/* /*
* @Author : Linloir * @Author : Linloir
* @Date : 2022-10-14 17:04:20 * @Date : 2022-10-14 17:04:20
* @LastEditTime : 2022-10-20 00:47:44 * @LastEditTime : 2022-10-20 11:05:37
* @Description : * @Description :
*/ */
@ -24,7 +24,7 @@ class ImageBox extends StatelessWidget {
onTap: (){}, onTap: (){},
child: Container( child: Container(
constraints: const BoxConstraints(maxWidth: 500, maxHeight: 250), constraints: const BoxConstraints(maxWidth: 500, maxHeight: 250),
child: Image.memory(base64Decode(history.message.contentDecoded)), child: history.preCachedImage ?? Image.memory(base64Decode(history.message.contentDecoded)),
), ),
); );
} }

View File

@ -1,7 +1,7 @@
/* /*
* @Author : Linloir * @Author : Linloir
* @Date : 2022-10-10 08:04:53 * @Date : 2022-10-10 08:04:53
* @LastEditTime : 2022-10-19 11:17:44 * @LastEditTime : 2022-10-20 10:34:41
* @Description : * @Description :
*/ */
import 'package:easy_debounce/easy_debounce.dart'; import 'package:easy_debounce/easy_debounce.dart';
@ -38,12 +38,12 @@ void main() async {
skipTaskbar: false, skipTaskbar: false,
titleBarStyle: TitleBarStyle.normal titleBarStyle: TitleBarStyle.normal
); );
windowManager.waitUntilReadyToShow(windowOptions, () async { await windowManager.waitUntilReadyToShow(windowOptions, () async {
await windowManager.show();
await windowManager.focus();
if(posX != null && posY != null) { if(posX != null && posY != null) {
await windowManager.setPosition(Offset(posX, posY)); await windowManager.setPosition(Offset(posX, posY));
} }
await windowManager.show();
await windowManager.focus();
}); });
//--------------------------------------------------------------------- //---------------------------------------------------------------------