Skip to content

Commit

Permalink
Merge pull request #380 from gtalha07/issue377_image-decode-patch
Browse files Browse the repository at this point in the history
optimize decoded images
  • Loading branch information
gtalha07 authored Nov 24, 2022
2 parents a7756af + beed269 commit 7b8c768
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 71 deletions.
1 change: 1 addition & 0 deletions app/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class Effektio extends StatelessWidget {

@override
Widget build(BuildContext context) {
debugInvertOversizedImages = true; // detect non-optimized images
return Portal(
child: Themed(
child: GetMaterialApp(
Expand Down
2 changes: 2 additions & 0 deletions app/lib/screens/HomeScreens/chat/ChatProfile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ class ChatProfileScreen extends StatelessWidget {
avatar: roomAvatar,
displayName: roomName,
radius: 20,
cacheHeight: 120,
cacheWidth: 120,
isGroup: true,
stringName: simplifyRoomId(room.getRoomId())!,
),
Expand Down
50 changes: 40 additions & 10 deletions app/lib/screens/HomeScreens/chat/ChatScreen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'dart:io';
import 'dart:math';

import 'package:bubble/bubble.dart';
import 'package:cached_memory_image/cached_memory_image.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:effektio/common/constants.dart';
import 'package:effektio/common/store/themes/ChatTheme.dart';
Expand Down Expand Up @@ -144,6 +143,8 @@ class _ChatScreenState extends State<ChatScreen>
displayName: roomController.getUserName(userId),
radius: 15,
isGroup: false,
cacheHeight: 120,
cacheWidth: 120,
stringName: simplifyUserId(userId)!,
),
),
Expand Down Expand Up @@ -176,20 +177,47 @@ class _ChatScreenState extends State<ChatScreen>
}) {
if (imageMessage.uri.isEmpty) {
// binary data
// CachedMemoryImage cannot be used, because uniqueKey not working
// If uniqueKey not working, it means cache is not working
// So use Image.memory
// ToDo: must implement image caching someday
if (imageMessage.metadata?.containsKey('binary') ?? false) {
return CachedMemoryImage(
uniqueKey: imageMessage.id,
bytes: imageMessage.metadata?['binary'],
width: messageWidth.toDouble(),
placeholder: const CircularProgressIndicator(
color: AppCommonTheme.primaryColor,
debugPrint('$messageWidth');
return Image.memory(
imageMessage.metadata?['binary'],
errorBuilder: (context, url, error) => const Text(
'Could not load image',
),
frameBuilder: ((context, child, frame, wasSynchronouslyLoaded) {
if (wasSynchronouslyLoaded) return child;
return AnimatedSwitcher(
duration: const Duration(milliseconds: 200),
child: frame != null
? child
: const SizedBox(
height: 60,
width: 60,
child: CircularProgressIndicator(
strokeWidth: 6,
color: AppCommonTheme.primaryColor,
),
),
);
}),
width: messageWidth.toDouble(),
cacheHeight: 288,
cacheWidth: 512,
fit: BoxFit.cover,
);
}
return CachedMemoryImage(
uniqueKey: imageMessage.id,
bytes: kTransparentImage,
return Image.memory(
kTransparentImage,
errorBuilder: (context, url, error) => Text(
'Could not load image due to $error',
),
width: messageWidth.toDouble(),
cacheWidth: messageWidth,
cacheHeight: 150,
);
}
if (isURL(imageMessage.uri)) {
Expand Down Expand Up @@ -299,6 +327,8 @@ class _ChatScreenState extends State<ChatScreen>
avatar: widget.roomAvatar,
displayName: widget.roomName,
radius: 20,
cacheHeight: 120,
cacheWidth: 120,
isGroup: true,
stringName: simplifyRoomId(widget.room.getRoomId())!,
),
Expand Down
2 changes: 2 additions & 0 deletions app/lib/widgets/ChatListItem.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ class _ChatListItemState extends State<ChatListItem> {
avatar: avatar,
displayName: displayName,
radius: 25,
cacheHeight: 120,
cacheWidth: 120,
isGroup: true,
stringName: simplifyRoomId(roomId)!,
),
Expand Down
44 changes: 22 additions & 22 deletions app/lib/widgets/CrossSigning.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,28 @@ class CrossSigning {
if (!_shouldShowNewDevicePopup()) {
return;
}
Get.generalDialog(
pageBuilder: (context, anim1, anim2) {
return Container(
width: MediaQuery.of(context).size.width,
color: Colors.white,
child: Card(
child: ListView(
shrinkWrap: true,
children: [
ListTile(
title: const Text('New device detected'),
onTap: () async {
await event.requestVerificationToUser();
Get.back();
},
),
],
),
),
);
},
);
// Get.generalDialog(
// pageBuilder: (context, anim1, anim2) {
// return Container(
// width: MediaQuery.of(context).size.width,
// color: Colors.white,
// child: Card(
// child: ListView(
// shrinkWrap: true,
// children: [
// ListTile(
// title: const Text('New device detected'),
// onTap: () async {
// await event.requestVerificationToUser();
// Get.back();
// },
// ),
// ],
// ),
// ),
// );
// },
// );
});
}

Expand Down
14 changes: 10 additions & 4 deletions app/lib/widgets/CustomAvatar.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dart:typed_data';

import 'package:cached_memory_image/provider/cached_memory_image_provider.dart';
import 'package:colorize_text_avatar/colorize_text_avatar.dart';
import 'package:effektio/common/store/themes/SeperatedThemes.dart';
import 'package:effektio_flutter_sdk/effektio_flutter_sdk_ffi.dart';
Expand All @@ -10,6 +9,8 @@ import 'package:flutter_svg/flutter_svg.dart';
class CustomAvatar extends StatefulWidget {
final String uniqueKey;
final Future<FfiBufferUint8>? avatar;
final int? cacheHeight;
final int? cacheWidth;
final String? displayName;
final double radius;
final bool isGroup;
Expand All @@ -23,6 +24,8 @@ class CustomAvatar extends StatefulWidget {
required this.radius,
required this.isGroup,
required this.stringName,
this.cacheHeight,
this.cacheWidth,
}) : super(key: key);

@override
Expand Down Expand Up @@ -55,9 +58,12 @@ class _CustomAvatarState extends State<CustomAvatar> {
}
if (snapshot.hasData && snapshot.requireData.isNotEmpty) {
return CircleAvatar(
backgroundImage: CachedMemoryImageProvider(
widget.uniqueKey,
bytes: snapshot.requireData,
backgroundImage: ResizeImage(
MemoryImage(
snapshot.requireData,
),
height: widget.cacheHeight ?? widget.radius.toInt(),
width: widget.cacheWidth ?? widget.radius.toInt(),
),
radius: widget.radius,
);
Expand Down
2 changes: 2 additions & 0 deletions app/lib/widgets/GroupMember.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class GroupMember extends StatelessWidget {
stringName: name ?? ' ',
avatar: avatar,
displayName: name,
cacheHeight: 150,
cacheWidth: 150,
),
Expanded(
// fit: FlexFit.loose,
Expand Down
15 changes: 8 additions & 7 deletions app/lib/widgets/NewsItem.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'dart:typed_data';
import 'dart:ui' as ui;
import 'dart:io' show Platform;

import 'package:cached_memory_image/cached_memory_image.dart';
import 'package:effektio/common/store/themes/SeperatedThemes.dart';
import 'package:effektio/widgets/NewsSideBar.dart';
import 'package:effektio_flutter_sdk/effektio_flutter_sdk.dart';
Expand Down Expand Up @@ -36,7 +35,7 @@ class NewsItem extends StatelessWidget {
color: bgColor,
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
child: _buildImage(),
child: _buildImage(isDesktop),
clipBehavior: Clip.none,
),
LayoutBuilder(
Expand Down Expand Up @@ -79,17 +78,19 @@ class NewsItem extends StatelessWidget {
);
}

Widget? _buildImage() {
Widget? _buildImage(bool isDesktop) {
var image = news.image();
Size size = WidgetsBinding.instance.window.physicalSize;
if (image == null) {
return null;
}
var id = news.id();

// return Image.memory(Uint8List.fromList(image), fit: BoxFit.cover);
return CachedMemoryImage(
uniqueKey: 'news-item-$id',
bytes: Uint8List.fromList(image),
return Image.memory(
Uint8List.fromList(image),
fit: BoxFit.cover,
cacheWidth: size.width.toInt(),
cacheHeight: size.height.toInt(),
);
}

Expand Down
43 changes: 23 additions & 20 deletions app/lib/widgets/NewsSideBar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ class _NewsSideBarState extends State<NewsSideBar> {
),
),
),
maxHeightDiskCache: 120,
maxWidthDiskCache: 120,
placeholder: (context, url) => const CircularProgressIndicator(),
errorWidget: (context, url, error) => const Icon(Icons.error),
),
Expand Down Expand Up @@ -180,26 +182,27 @@ class _NewsSideBarState extends State<NewsSideBar> {
),
),
),
GetBuilder<NewsCommentController>(builder:
(NewsCommentController newsCommentController) {
return Expanded(
child: ListView.builder(
physics: const BouncingScrollPhysics(),
controller: scrollController,
itemCount: 10,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.all(12),
child: CommentView(
commentModel:
newsCommentController.listComments[index],
postition: index,
),
);
},
),
);
},),
GetBuilder<NewsCommentController>(
builder: (NewsCommentController newsCommentController) {
return Expanded(
child: ListView.builder(
physics: const BouncingScrollPhysics(),
controller: scrollController,
itemCount: 10,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.all(12),
child: CommentView(
commentModel: newsCommentController
.listComments[index],
postition: index,
),
);
},
),
);
},
),
Padding(
padding: EdgeInsets.only(
left: 8,
Expand Down
2 changes: 2 additions & 0 deletions app/lib/widgets/SideMenu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ class SideDrawer extends StatelessWidget {
child: CustomAvatar(
uniqueKey: userId,
radius: 24,
cacheHeight: 120,
cacheWidth: 120,
avatar: displayAvatar,
displayName: displayName,
isGroup: false,
Expand Down
7 changes: 0 additions & 7 deletions app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,6 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.1"
cached_memory_image:
dependency: "direct main"
description:
name: cached_memory_image
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.1"
cached_network_image:
dependency: "direct main"
description:
Expand Down
1 change: 0 additions & 1 deletion app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ dependencies:
flutter_lorem: ^2.0.0
get_time_ago: ^1.1.5
implicitly_animated_reorderable_list: ^0.4.2
cached_memory_image: ^1.3.1
flutter_mentions: ^2.0.1
flutter_html: ^3.0.0-alpha.5
flutter_icons_null_safety: ^1.1.0
Expand Down

0 comments on commit 7b8c768

Please sign in to comment.