Skip to content

Commit

Permalink
TW-616 Refactor getSuccessOrNull
Browse files Browse the repository at this point in the history
  • Loading branch information
drminh2807 authored and hoangdat committed Sep 21, 2023
1 parent 8a4d487 commit edf7a2d
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 28 deletions.
8 changes: 8 additions & 0 deletions lib/app_state/success.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import 'package:dartz/dartz.dart';
import 'package:equatable/equatable.dart';

abstract class Success extends Equatable {
const Success();
}

extension SuccessExtension on Either {
T? getSuccessOrNull<T extends Success>({T? fallbackValue}) => fold(
(failure) => fallbackValue,
(success) => success is T ? success : fallbackValue,
);
}
9 changes: 0 additions & 9 deletions lib/domain/app_state/room/chat_room_search_state.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'package:dartz/dartz.dart';

import 'package:fluffychat/app_state/failure.dart';
import 'package:fluffychat/app_state/initial.dart';
import 'package:fluffychat/app_state/success.dart';
Expand Down Expand Up @@ -40,10 +38,3 @@ class ChatRoomSearchFailure extends Failure {
@override
List<Object?> get props => [exception];
}

extension ChatRoomSearchEitherExtension on Either {
ChatRoomSearchSuccess? getSuccessOrNull() => fold(
(failure) => null,
(success) => success is ChatRoomSearchSuccess ? success : null,
);
}
8 changes: 0 additions & 8 deletions lib/domain/app_state/room/timeline_search_event_state.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:dartz/dartz.dart';
import 'package:fluffychat/app_state/failure.dart';
import 'package:fluffychat/app_state/success.dart';
import 'package:matrix/matrix.dart';
Expand Down Expand Up @@ -33,10 +32,3 @@ extension TimelineSearchEventSuccessExtension on TimelineSearchEventSuccess {
);
}
}

extension TimelineSearchEventEitherExtension on Either {
TimelineSearchEventSuccess? getSuccessOrNull() => fold(
(failure) => null,
(success) => success is TimelineSearchEventSuccess ? success : null,
);
}
7 changes: 4 additions & 3 deletions lib/pages/chat/chat_room_search_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ mixin ChatRoomSearchMixin {
);
searchStatus.value = event;
_scrollToEvent(event);
canGoUp.value = event.getSuccessOrNull() != null;
canGoUp.value = event.getSuccessOrNull<ChatRoomSearchSuccess>() != null;
canGoDown.value = false;
},
);
Expand All @@ -93,7 +93,7 @@ mixin ChatRoomSearchMixin {
}

void _scrollToEvent(Either<Failure, Success> event) {
final index = event.getSuccessOrNull()?.eventIndex;
final index = event.getSuccessOrNull<ChatRoomSearchSuccess>()?.eventIndex;
if (index != null) {
_scrollToIndex?.call(index);
}
Expand All @@ -108,7 +108,8 @@ mixin ChatRoomSearchMixin {
}

void _goSearchResult(BuildContext context, {required Direction direction}) {
final lastSuccess = searchStatus.value.getSuccessOrNull();
final lastSuccess =
searchStatus.value.getSuccessOrNull<ChatRoomSearchSuccess>();
final timeline = _getTimeline?.call();
if (timeline == null || lastSuccess == null) return;
_searchInteractor
Expand Down
10 changes: 7 additions & 3 deletions lib/pages/chat/events/message_content.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:fluffychat/app_state/success.dart';
import 'package:fluffychat/domain/app_state/room/chat_room_search_state.dart';
import 'package:fluffychat/pages/chat/chat.dart';
import 'package:fluffychat/config/app_config.dart';
Expand Down Expand Up @@ -213,7 +214,9 @@ class MessageContent extends StatelessWidget {
return HtmlMessage(
event: event,
html: html,
highlightText: searchStatus.getSuccessOrNull()?.keyword,
highlightText: searchStatus
.getSuccessOrNull<ChatRoomSearchSuccess>()
?.keyword,
defaultTextStyle:
Theme.of(context).textTheme.bodyLarge?.copyWith(
// color: textColor,
Expand Down Expand Up @@ -323,8 +326,9 @@ class MessageContent extends StatelessWidget {
return ValueListenableBuilder(
valueListenable: controller.searchStatus,
builder: (context, searchStatus, child) {
final highlightText =
searchStatus.getSuccessOrNull()?.keyword;
final highlightText = searchStatus
.getSuccessOrNull<ChatRoomSearchSuccess>()
?.keyword;
return TwakeLinkPreview(
text: text,
textStyle:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:fluffychat/app_state/success.dart';
import 'package:fluffychat/domain/app_state/room/timeline_search_event_state.dart';
import 'package:fluffychat/pages/chat_details/chat_details_page_view/links/chat_details_links_style.dart';
import 'package:fluffychat/utils/string_extension.dart';
Expand All @@ -23,7 +24,10 @@ class ChatDetailsLinksPage extends StatelessWidget {
controller: eventsListController,
getTimeline: getTimeline,
builder: (context, eventsState) {
final events = eventsState.getSuccessOrNull()?.events ?? [];
final events = eventsState
.getSuccessOrNull<TimelineSearchEventSuccess>()
?.events ??
[];
return ListView.separated(
itemCount: events.length,
itemBuilder: (context, index) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:fluffychat/app_state/success.dart';
import 'package:fluffychat/config/app_config.dart';
import 'package:fluffychat/domain/app_state/room/timeline_search_event_state.dart';
import 'package:fluffychat/pages/chat_details/chat_details_page_view/media/chat_details_media_style.dart';
Expand Down Expand Up @@ -26,7 +27,10 @@ class ChatDetailsMediaPage extends StatelessWidget {
controller: eventsListController,
getTimeline: getTimeline,
builder: (context, eventsState) {
final events = eventsState.getSuccessOrNull()?.events ?? [];
final events = eventsState
.getSuccessOrNull<TimelineSearchEventSuccess>()
?.events ??
[];
return CustomScrollView(
slivers: [
SliverGrid.builder(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ class SameTypeEventsListController {
}

void loadMore({required Future<Timeline> Function() getTimeline}) async {
final lastSuccess = eventsNotifier.value.getSuccessOrNull();
final lastSuccess =
eventsNotifier.value.getSuccessOrNull<TimelineSearchEventSuccess>();
if (lastSuccess == null || lastSuccess.events.isEmpty) {
refreshController.loadComplete();
return;
Expand Down
4 changes: 2 additions & 2 deletions lib/widgets/mxc_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,8 @@ class _MxcImageState extends State<MxcImage>
data,
width: widget.width,
height: widget.height,
cacheWidth: widget.width?.toInt(),
cacheHeight: widget.height?.toInt(),
cacheWidth: widget.event != null ? widget.width?.toInt() : null,
cacheHeight: widget.event != null ? widget.height?.toInt() : null,
fit: widget.fit,
filterQuality: FilterQuality.medium,
errorBuilder: (context, __, ___) {
Expand Down

0 comments on commit edf7a2d

Please sign in to comment.