Skip to content

Commit

Permalink
Replace universal IO with conditional import
Browse files Browse the repository at this point in the history
  • Loading branch information
demchenkoalex committed Mar 8, 2021
1 parent 9ecef93 commit 95a2a66
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 39 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.6.3

- Replace universal IO with conditional import

## 0.6.2

- Update dependencies
Expand Down
12 changes: 6 additions & 6 deletions example/assets/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,25 +89,25 @@
},
{
"authorId": "b4878b96-efbc-479a-8291-474ef323dec7",
"height": 677,
"height": 600,
"id": "8fa70836-3309-4d09-a777-4d9603e1f123",
"imageName": "image",
"size": 197943,
"size": 59645,
"status": "read",
"timestamp": 1598438785,
"type": "image",
"uri": "https://i.ytimg.com/vi/L42-aFe8bMo/maxresdefault.jpg",
"width": 1203
"uri": "https://hcti.io/v1/image/c1ea794e-32c7-4516-bd0f-8b835e8a2f2e",
"width": 1128
},
{
"authorId": "b4878b96-efbc-479a-8291-474ef323dec7",
"fileName": "image.jpg",
"id": "8fa70836-3309-4d09-a777-4d9603e1f123",
"mimeType": "image/jpg",
"size": 197943,
"size": 59645,
"status": "read",
"timestamp": 1598438784,
"type": "file",
"uri": "https://i.ytimg.com/vi/L42-aFe8bMo/maxresdefault.jpg"
"uri": "https://hcti.io/v1/image/c1ea794e-32c7-4516-bd0f-8b835e8a2f2e"
}
]
4 changes: 4 additions & 0 deletions lib/src/conditional/base_conditional.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import 'conditional.dart';

/// The abstract base class for a conditional import feature.
abstract class BaseConditional implements Conditional {}
92 changes: 92 additions & 0 deletions lib/src/conditional/browser_conditional.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'base_conditional.dart';

/// Create a [BrowserConditional].
///
/// Used from conditional imports, matches the definition in `conditional_stub.dart`.
BaseConditional createConditional() => BrowserConditional();

/// A conditional for browser
class BrowserConditional extends BaseConditional {
/// Returns [NetworkImage] if URI starts with http
/// otherwise returns transparent image
@override
ImageProvider getProvider(String uri) {
if (uri.startsWith('http')) {
return NetworkImage(uri);
} else {
return MemoryImage(kTransparentImage);
}
}
}

/// Transparent image data
final kTransparentImage = Uint8List.fromList(
<int>[
0x89,
0x50,
0x4E,
0x47,
0x0D,
0x0A,
0x1A,
0x0A,
0x00,
0x00,
0x00,
0x0D,
0x49,
0x48,
0x44,
0x52,
0x00,
0x00,
0x00,
0x01,
0x00,
0x00,
0x00,
0x01,
0x08,
0x06,
0x00,
0x00,
0x00,
0x1F,
0x15,
0xC4,
0x89,
0x00,
0x00,
0x00,
0x0A,
0x49,
0x44,
0x41,
0x54,
0x78,
0x9C,
0x63,
0x00,
0x01,
0x00,
0x00,
0x05,
0x00,
0x01,
0x0D,
0x0A,
0x2D,
0xB4,
0x00,
0x00,
0x00,
0x00,
0x49,
0x45,
0x4E,
0x44,
0xAE,
],
);
16 changes: 16 additions & 0 deletions lib/src/conditional/conditional.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:flutter/material.dart';
import 'conditional_stub.dart'
if (dart.library.io) 'io_conditional.dart'
if (dart.library.html) 'browser_conditional.dart';

/// The abstract class for a conditional import feature.
abstract class Conditional {
/// Creates a new platform appropriate conditional.
///
/// Creates an `IOConditional` if `dart:io` is available and a `BrowserConditional` if
/// `dart:html` is available, otherwise it will throw an unsupported error.
factory Conditional() => createConditional();

/// Returns an appropriate platform ImageProvider for specified URI
ImageProvider getProvider(String uri);
}
5 changes: 5 additions & 0 deletions lib/src/conditional/conditional_stub.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'base_conditional.dart';

/// Implemented in `browser_conditional.dart` and `io_conditional.dart`.
BaseConditional createConditional() =>
throw UnsupportedError('Cannot create a conditional');
22 changes: 22 additions & 0 deletions lib/src/conditional/io_conditional.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'dart:io';
import 'package:flutter/material.dart';
import 'base_conditional.dart';

/// Create a [IOConditional].
///
/// Used from conditional imports, matches the definition in `conditional_stub.dart`.
BaseConditional createConditional() => IOConditional();

/// A conditional for anything but browser
class IOConditional extends BaseConditional {
/// Returns [NetworkImage] if URI starts with http
/// otherwise uses IO to create File
@override
ImageProvider getProvider(String uri) {
if (uri.startsWith('http')) {
return NetworkImage(uri);
} else {
return FileImage(File(uri));
}
}
}
50 changes: 26 additions & 24 deletions lib/src/widgets/chat.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import 'dart:math';
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_chat_types/flutter_chat_types.dart' as types;
import 'package:flutter_chat_ui/src/widgets/inherited_l10n.dart';
import 'package:photo_view/photo_view_gallery.dart';
import 'package:universal_io/io.dart';
import '../chat_l10n.dart';
import '../chat_theme.dart';
import '../conditional/conditional.dart';
import '../util.dart';
import 'inherited_chat_theme.dart';
import 'inherited_user.dart';
Expand Down Expand Up @@ -73,13 +74,13 @@ class _ChatState extends State<Chat> {
}

void _onImagePressed(
String url,
String uri,
List<String> galleryItems,
) {
SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.bottom]);
setState(() {
_isImageViewVisible = true;
_imageViewIndex = galleryItems.indexOf(url);
_imageViewIndex = galleryItems.indexOf(uri);
});
}

Expand All @@ -106,7 +107,7 @@ class _ChatState extends State<Chat> {
PhotoViewGallery.builder(
builder: (BuildContext context, int index) =>
PhotoViewGalleryPageOptions(
imageProvider: _renderImageProvider(galleryItems[index]),
imageProvider: Conditional().getProvider(galleryItems[index]),
),
itemCount: galleryItems.length,
loadingBuilder: (context, event) =>
Expand All @@ -128,29 +129,30 @@ class _ChatState extends State<Chat> {
);
}

ImageProvider _renderImageProvider(String url) {
if (url.startsWith('http')) {
return NetworkImage(url);
}
return FileImage(File(url));
}

@override
Widget build(BuildContext context) {
final _messageWidth =
min(MediaQuery.of(context).size.width * 0.77, 440).floor();

final galleryItems = widget.messages.fold<List<String>>(
[],
(previousValue, element) => element is types.ImageMessage
? List.from(
[
element.uri,
...previousValue,
],
)
: previousValue,
);
final galleryItems =
widget.messages.fold<List<String>>([], (previousValue, element) {
// Check if element is image message
if (element is types.ImageMessage) {
// For web add only remote uri, local files are not yet supported
if (kIsWeb) {
if (element.uri.startsWith('http')) {
return [element.uri, ...previousValue];
} else {
return previousValue;
}
// For everything else add uri
} else {
return [element.uri, ...previousValue];
}
}

return previousValue;
});

return InheritedUser(
user: widget.user,
Expand Down Expand Up @@ -274,8 +276,8 @@ class _ChatState extends State<Chat> {
Message(
key: ValueKey(message),
dateLocale: widget.dateLocale,
onImagePressed: (url) {
_onImagePressed(url, galleryItems);
onImagePressed: (uri) {
_onImagePressed(uri, galleryItems);
},
message: message,
messageWidth: _messageWidth,
Expand Down
9 changes: 2 additions & 7 deletions lib/src/widgets/image_message.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter_chat_types/flutter_chat_types.dart' as types;
import 'package:universal_io/io.dart';
import '../conditional/conditional.dart';
import '../util.dart';
import 'inherited_chat_theme.dart';
import 'inherited_user.dart';
Expand Down Expand Up @@ -30,12 +30,7 @@ class _ImageMessageState extends State<ImageMessage> {
@override
void initState() {
super.initState();
if (widget.message.uri.startsWith('http')) {
_image = NetworkImage(widget.message.uri);
} else {
_image = FileImage(File(widget.message.uri));
}

_image = Conditional().getProvider(widget.message.uri);
_size = Size(widget.message.width ?? 0, widget.message.height ?? 0);
}

Expand Down
3 changes: 1 addition & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_chat_ui
description: Flutter Chat UI
version: 0.6.2
version: 0.6.3
homepage: https://github.com/flyerhq/flutter_chat_ui

environment:
Expand All @@ -14,7 +14,6 @@ dependencies:
flutter_link_previewer: ^1.0.1
intl: ^0.17.0
photo_view: ^0.11.0
universal_io: ^2.0.0

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 95a2a66

Please sign in to comment.