Use localChannel if present instead of creating a new
Signed-off-by: Sahil Kumar <[email protected]>
This commit is contained in:
@@ -335,10 +335,11 @@ class _ChannelListPageState extends State<ChannelListPage> {
|
|||||||
onItemTap: (messageResponse) async {
|
onItemTap: (messageResponse) async {
|
||||||
final client = StreamChat.of(context).client;
|
final client = StreamChat.of(context).client;
|
||||||
final message = messageResponse.message;
|
final message = messageResponse.message;
|
||||||
final channel = Channel.fromState(
|
final channel = client.channel(
|
||||||
client,
|
messageResponse.channel.type,
|
||||||
ChannelState(channel: messageResponse.channel),
|
id: messageResponse.channel.id,
|
||||||
);
|
);
|
||||||
|
await channel.watch();
|
||||||
Navigator.pushNamed(
|
Navigator.pushNamed(
|
||||||
context,
|
context,
|
||||||
Routes.CHANNEL_PAGE,
|
Routes.CHANNEL_PAGE,
|
||||||
|
|||||||
+204
-192
@@ -183,7 +183,7 @@ class _MessageListViewState extends State<MessageListView> {
|
|||||||
final messageIndex = messages.indexWhere((e) {
|
final messageIndex = messages.indexWhere((e) {
|
||||||
return e.id == streamChannel.initialMessageId;
|
return e.id == streamChannel.initialMessageId;
|
||||||
});
|
});
|
||||||
return totalMessages - messageIndex - 1;
|
return totalMessages - messageIndex;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -234,223 +234,235 @@ class _MessageListViewState extends State<MessageListView> {
|
|||||||
initialAlignment = _initialAlignment;
|
initialAlignment = _initialAlignment;
|
||||||
}
|
}
|
||||||
|
|
||||||
return StreamBuilder<List<Message>>(
|
return WillPopScope(
|
||||||
stream: messagesStream?.map((messages) => messages
|
onWillPop: () async {
|
||||||
?.where((e) =>
|
if (!_upToDate) {
|
||||||
!e.isDeleted ||
|
await streamChannel.reloadChannel();
|
||||||
(e.isDeleted &&
|
}
|
||||||
e.user.id == streamChannel.channel.client.state.user.id))
|
return true;
|
||||||
?.toList()),
|
},
|
||||||
builder: (context, snapshot) {
|
child: StreamBuilder<List<Message>>(
|
||||||
if (!snapshot.hasData) {
|
stream: messagesStream?.map((messages) => messages
|
||||||
return Center(
|
?.where((e) =>
|
||||||
child: CircularProgressIndicator(),
|
!e.isDeleted ||
|
||||||
);
|
(e.isDeleted &&
|
||||||
}
|
e.user.id == streamChannel.channel.client.state.user.id))
|
||||||
|
?.toList()),
|
||||||
final messageList = snapshot.data?.reversed?.toList() ?? [];
|
builder: (context, snapshot) {
|
||||||
|
if (!snapshot.hasData) {
|
||||||
if (messageList.isEmpty) {
|
|
||||||
if (_upToDate) {
|
|
||||||
return Center(
|
return Center(
|
||||||
child: Text(
|
child: CircularProgressIndicator(),
|
||||||
'No chats here yet...',
|
|
||||||
style: TextStyle(
|
|
||||||
fontSize: 12,
|
|
||||||
color: Colors.black.withOpacity(.5),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
messages = messageList;
|
|
||||||
}
|
|
||||||
|
|
||||||
final newMessagesListLength = messages.length;
|
final messageList = snapshot.data?.reversed?.toList() ?? [];
|
||||||
|
|
||||||
if (_bottomPaginationActive) {
|
if (messageList.isEmpty) {
|
||||||
if (_itemPositionListener.itemPositions.value?.isNotEmpty == true &&
|
if (_upToDate) {
|
||||||
_messageListLength != null) {
|
return Center(
|
||||||
final first = _itemPositionListener.itemPositions.value.first;
|
child: Text(
|
||||||
final diff = newMessagesListLength - _messageListLength;
|
'No chats here yet...',
|
||||||
if (diff > 0) {
|
style: TextStyle(
|
||||||
initialIndex = first.index + diff;
|
fontSize: 12,
|
||||||
initialAlignment = first.itemLeadingEdge;
|
color: Colors.black.withOpacity(.5),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
messages = messageList;
|
||||||
}
|
}
|
||||||
} else if (!_topPaginationActive && _upToDate) {
|
|
||||||
// Reset the index in-case we send any new message
|
|
||||||
initialIndex = 0;
|
|
||||||
initialAlignment = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
_messageListLength = newMessagesListLength;
|
final newMessagesListLength = messages.length;
|
||||||
|
|
||||||
return Stack(
|
if (_bottomPaginationActive) {
|
||||||
alignment: Alignment.center,
|
if (_itemPositionListener.itemPositions.value?.isNotEmpty ==
|
||||||
children: [
|
true &&
|
||||||
LazyLoadScrollView(
|
_messageListLength != null) {
|
||||||
onStartOfPage: () async {
|
final first = _itemPositionListener.itemPositions.value.first;
|
||||||
if (!_upToDate) {
|
final diff = newMessagesListLength - _messageListLength;
|
||||||
_topPaginationActive = false;
|
if (diff > 0) {
|
||||||
_bottomPaginationActive = true;
|
initialIndex = first.index + diff;
|
||||||
return _paginateData(streamChannel, QueryDirection.bottom);
|
initialAlignment = first.itemLeadingEdge;
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
onEndOfPage: () async {
|
} else if (!_topPaginationActive && _upToDate) {
|
||||||
_topPaginationActive = true;
|
// Reset the index in-case we send any new message
|
||||||
_bottomPaginationActive = false;
|
initialIndex = 0;
|
||||||
return _paginateData(streamChannel, QueryDirection.top);
|
initialAlignment = 0;
|
||||||
},
|
}
|
||||||
child: ScrollablePositionedList.builder(
|
|
||||||
key: ValueKey(initialIndex + initialAlignment),
|
_messageListLength = newMessagesListLength;
|
||||||
itemPositionsListener: _itemPositionListener,
|
|
||||||
addAutomaticKeepAlives: true,
|
return Stack(
|
||||||
initialScrollIndex: initialIndex ?? 0,
|
alignment: Alignment.center,
|
||||||
initialAlignment: initialAlignment ?? 0,
|
children: [
|
||||||
physics: widget.scrollPhysics,
|
LazyLoadScrollView(
|
||||||
itemScrollController: _scrollController,
|
onStartOfPage: () async {
|
||||||
reverse: true,
|
if (!_upToDate) {
|
||||||
itemCount: messages.length +
|
_topPaginationActive = false;
|
||||||
2 +
|
_bottomPaginationActive = true;
|
||||||
(widget.parentMessage != null ? 1 : 0),
|
return _paginateData(
|
||||||
itemBuilder: (context, i) {
|
streamChannel, QueryDirection.bottom);
|
||||||
if (i == messages.length + 2) {
|
}
|
||||||
if (widget.parentMessageBuilder != null) {
|
},
|
||||||
return widget.parentMessageBuilder(
|
onEndOfPage: () async {
|
||||||
|
_topPaginationActive = true;
|
||||||
|
_bottomPaginationActive = false;
|
||||||
|
return _paginateData(streamChannel, QueryDirection.top);
|
||||||
|
},
|
||||||
|
child: ScrollablePositionedList.builder(
|
||||||
|
key: ValueKey(initialIndex + initialAlignment),
|
||||||
|
itemPositionsListener: _itemPositionListener,
|
||||||
|
addAutomaticKeepAlives: true,
|
||||||
|
initialScrollIndex: initialIndex ?? 0,
|
||||||
|
initialAlignment: initialAlignment ?? 0,
|
||||||
|
physics: widget.scrollPhysics,
|
||||||
|
itemScrollController: _scrollController,
|
||||||
|
reverse: true,
|
||||||
|
itemCount: messages.length +
|
||||||
|
2 +
|
||||||
|
(widget.parentMessage != null ? 1 : 0),
|
||||||
|
itemBuilder: (context, i) {
|
||||||
|
if (i == messages.length + 2) {
|
||||||
|
if (widget.parentMessageBuilder != null) {
|
||||||
|
return widget.parentMessageBuilder(
|
||||||
|
context,
|
||||||
|
widget.parentMessage,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
return Column(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||||
|
children: <Widget>[
|
||||||
|
buildParentMessage(widget.parentMessage),
|
||||||
|
Padding(
|
||||||
|
padding:
|
||||||
|
const EdgeInsets.symmetric(horizontal: 32),
|
||||||
|
child: Container(
|
||||||
|
padding: const EdgeInsets.all(8),
|
||||||
|
child: Text(
|
||||||
|
'Start of thread',
|
||||||
|
textAlign: TextAlign.center,
|
||||||
|
),
|
||||||
|
color: Theme.of(context)
|
||||||
|
.accentColor
|
||||||
|
.withAlpha(50),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (i == messages.length + 1) {
|
||||||
|
return _buildLoadingIndicator(
|
||||||
|
streamChannel,
|
||||||
|
QueryDirection.top,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (i == 0) {
|
||||||
|
return _buildLoadingIndicator(
|
||||||
|
streamChannel,
|
||||||
|
QueryDirection.bottom,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
final message = messages[i - 1];
|
||||||
|
final nextMessage = (i - 1) > 0 ? messages[i - 2] : null;
|
||||||
|
|
||||||
|
Widget messageWidget;
|
||||||
|
|
||||||
|
if (i == 1) {
|
||||||
|
messageWidget = _buildBottomMessage(
|
||||||
context,
|
context,
|
||||||
widget.parentMessage,
|
message,
|
||||||
|
messages,
|
||||||
|
streamChannel,
|
||||||
|
);
|
||||||
|
} else if (i == messages.length - 1) {
|
||||||
|
messageWidget = _buildTopMessage(
|
||||||
|
context,
|
||||||
|
message,
|
||||||
|
messages,
|
||||||
|
streamChannel,
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
if (widget.messageBuilder != null) {
|
||||||
|
messageWidget = Builder(
|
||||||
|
key: ValueKey<String>('MESSAGE-${message.id}'),
|
||||||
|
builder: (_) => widget.messageBuilder(
|
||||||
|
context,
|
||||||
|
MessageDetails(
|
||||||
|
context,
|
||||||
|
message,
|
||||||
|
messages,
|
||||||
|
i,
|
||||||
|
),
|
||||||
|
messages),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
messageWidget = buildMessage(message, messages, i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nextMessage != null &&
|
||||||
|
!Jiffy(message.createdAt.toLocal()).isSame(
|
||||||
|
nextMessage.createdAt.toLocal(), Units.DAY)) {
|
||||||
return Column(
|
return Column(
|
||||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||||
children: <Widget>[
|
children: <Widget>[
|
||||||
buildParentMessage(widget.parentMessage),
|
messageWidget,
|
||||||
Padding(
|
Padding(
|
||||||
padding:
|
padding:
|
||||||
const EdgeInsets.symmetric(horizontal: 32),
|
const EdgeInsets.symmetric(vertical: 12.0),
|
||||||
child: Container(
|
child: widget.dateDividerBuilder != null
|
||||||
padding: const EdgeInsets.all(8),
|
? widget.dateDividerBuilder(
|
||||||
child: Text(
|
nextMessage.createdAt.toLocal())
|
||||||
'Start of thread',
|
: DateDivider(
|
||||||
textAlign: TextAlign.center,
|
dateTime: nextMessage.createdAt.toLocal(),
|
||||||
),
|
),
|
||||||
color:
|
|
||||||
Theme.of(context).accentColor.withAlpha(50),
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if (i == messages.length + 1) {
|
|
||||||
return _buildLoadingIndicator(
|
|
||||||
streamChannel,
|
|
||||||
QueryDirection.top,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (i == 0) {
|
|
||||||
return _buildLoadingIndicator(
|
|
||||||
streamChannel,
|
|
||||||
QueryDirection.bottom,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
final message = messages[i - 1];
|
|
||||||
final nextMessage = (i - 1) > 0 ? messages[i - 2] : null;
|
|
||||||
|
|
||||||
Widget messageWidget;
|
return messageWidget;
|
||||||
|
},
|
||||||
if (i == 1) {
|
),
|
||||||
messageWidget = _buildBottomMessage(
|
),
|
||||||
context,
|
if (widget.showScrollToBottom) _buildScrollToBottom(),
|
||||||
message,
|
Positioned(
|
||||||
messages,
|
top: 20.0,
|
||||||
streamChannel,
|
child: ValueListenableBuilder<Iterable<ItemPosition>>(
|
||||||
);
|
valueListenable: _itemPositionListener.itemPositions,
|
||||||
} else if (i == messages.length - 1) {
|
builder: (context, values, _) {
|
||||||
messageWidget = _buildTopMessage(
|
final items = _itemPositionListener.itemPositions?.value;
|
||||||
context,
|
if (items.isEmpty || messages.isEmpty) {
|
||||||
message,
|
return SizedBox();
|
||||||
messages,
|
|
||||||
streamChannel,
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
if (widget.messageBuilder != null) {
|
|
||||||
messageWidget = Builder(
|
|
||||||
key: ValueKey<String>('MESSAGE-${message.id}'),
|
|
||||||
builder: (_) => widget.messageBuilder(
|
|
||||||
context,
|
|
||||||
MessageDetails(
|
|
||||||
context,
|
|
||||||
message,
|
|
||||||
messages,
|
|
||||||
i,
|
|
||||||
),
|
|
||||||
messages),
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
messageWidget = buildMessage(message, messages, i);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (nextMessage != null &&
|
var index = _getTopElement(values).index;
|
||||||
!Jiffy(message.createdAt.toLocal()).isSame(
|
|
||||||
nextMessage.createdAt.toLocal(), Units.DAY)) {
|
|
||||||
return Column(
|
|
||||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
||||||
children: <Widget>[
|
|
||||||
messageWidget,
|
|
||||||
Padding(
|
|
||||||
padding: const EdgeInsets.symmetric(vertical: 12.0),
|
|
||||||
child: widget.dateDividerBuilder != null
|
|
||||||
? widget.dateDividerBuilder(
|
|
||||||
nextMessage.createdAt.toLocal())
|
|
||||||
: DateDivider(
|
|
||||||
dateTime: nextMessage.createdAt.toLocal(),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return messageWidget;
|
if (index > messages.length) {
|
||||||
},
|
return SizedBox();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (index == messages.length) {
|
||||||
|
index = max(index - 1, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
return widget.dateDividerBuilder != null
|
||||||
|
? widget.dateDividerBuilder(
|
||||||
|
messages[index].createdAt.toLocal(),
|
||||||
|
)
|
||||||
|
: DateDivider(
|
||||||
|
dateTime: messages[index].createdAt.toLocal(),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
),
|
),
|
||||||
),
|
],
|
||||||
if (widget.showScrollToBottom) _buildScrollToBottom(),
|
);
|
||||||
Positioned(
|
}),
|
||||||
top: 20.0,
|
);
|
||||||
child: ValueListenableBuilder<Iterable<ItemPosition>>(
|
|
||||||
valueListenable: _itemPositionListener.itemPositions,
|
|
||||||
builder: (context, values, _) {
|
|
||||||
final items = _itemPositionListener.itemPositions?.value;
|
|
||||||
if (items.isEmpty || messages.isEmpty) {
|
|
||||||
return SizedBox();
|
|
||||||
}
|
|
||||||
|
|
||||||
var index = _getTopElement(values).index;
|
|
||||||
|
|
||||||
if (index > messages.length) {
|
|
||||||
return SizedBox();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (index == messages.length) {
|
|
||||||
index = max(index - 1, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
return widget.dateDividerBuilder != null
|
|
||||||
? widget.dateDividerBuilder(
|
|
||||||
messages[index].createdAt.toLocal(),
|
|
||||||
)
|
|
||||||
: DateDivider(
|
|
||||||
dateTime: messages[index].createdAt.toLocal(),
|
|
||||||
);
|
|
||||||
},
|
|
||||||
),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _paginateData(
|
Future<void> _paginateData(
|
||||||
|
|||||||
@@ -307,16 +307,29 @@ class StreamChannelState extends State<StreamChannel> {
|
|||||||
if (initialMessageId != null) false,
|
if (initialMessageId != null) false,
|
||||||
],
|
],
|
||||||
builder: (context, snapshot) {
|
builder: (context, snapshot) {
|
||||||
|
if (snapshot.hasError) {
|
||||||
|
if (snapshot.error is Error) {
|
||||||
|
print((snapshot.error as Error).stackTrace);
|
||||||
|
}
|
||||||
|
var message = snapshot.error.toString();
|
||||||
|
if (snapshot.error is DioError) {
|
||||||
|
final dioError = snapshot.error as DioError;
|
||||||
|
if (dioError.type == DioErrorType.RESPONSE) {
|
||||||
|
message = dioError.message;
|
||||||
|
} else {
|
||||||
|
message = 'Check your connection and retry';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Center(
|
||||||
|
child: Text(message),
|
||||||
|
);
|
||||||
|
}
|
||||||
final initialized = snapshot.data[0];
|
final initialized = snapshot.data[0];
|
||||||
final dataLoaded = initialMessageId == null ? true : snapshot.data[1];
|
final dataLoaded = initialMessageId == null ? true : snapshot.data[1];
|
||||||
if (widget.showLoading && (!initialized || !dataLoaded)) {
|
if (widget.showLoading && (!initialized || !dataLoaded)) {
|
||||||
return Center(
|
return Center(
|
||||||
child: CircularProgressIndicator(),
|
child: CircularProgressIndicator(),
|
||||||
);
|
);
|
||||||
} else if (snapshot.hasError) {
|
|
||||||
return Center(
|
|
||||||
child: Text(snapshot.error),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
return widget.child;
|
return widget.child;
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user