Skip to content

Commit

Permalink
refactor: 🔨 updated config dependencies using inherited widget and re…
Browse files Browse the repository at this point in the history
…move unnecessary repetitive code
  • Loading branch information
vatsaltanna-simformsolutions committed Jul 2, 2024
1 parent 2f530e0 commit 371f8d4
Show file tree
Hide file tree
Showing 10 changed files with 309 additions and 419 deletions.
23 changes: 23 additions & 0 deletions lib/src/extensions/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* SOFTWARE.
*/
import 'package:chatview/chatview.dart';
import 'package:chatview/src/inherited_widgets/configurations_inherited_widgets.dart';
import 'package:chatview/src/widgets/chat_view_inherited_widget.dart';
import 'package:chatview/src/widgets/profile_image_widget.dart';
import 'package:chatview/src/widgets/suggestions/suggestions_config_inherited_widget.dart';
Expand Down Expand Up @@ -134,6 +135,17 @@ extension StatefulWidgetExtension on State {

ReplySuggestionsConfig? get suggestionsConfig =>
mounted ? SuggestionsConfigIW.of(context)?.suggestionsConfig : null;

ConfigurationsInheritedWidget get chatListConfig => mounted
? ConfigurationsInheritedWidget.of(context) ??
const ConfigurationsInheritedWidget(
chatBackgroundConfig: ChatBackgroundConfiguration(),
child: SizedBox(),
)
: const ConfigurationsInheritedWidget(
chatBackgroundConfig: ChatBackgroundConfiguration(),
child: SizedBox(),
);
}

/// Extension on State for accessing inherited widget.
Expand All @@ -143,4 +155,15 @@ extension BuildContextExtension on BuildContext {

ReplySuggestionsConfig? get suggestionsConfig =>
mounted ? SuggestionsConfigIW.of(this)?.suggestionsConfig : null;

ConfigurationsInheritedWidget get chatListConfig => mounted
? ConfigurationsInheritedWidget.of(this) ??
const ConfigurationsInheritedWidget(
chatBackgroundConfig: ChatBackgroundConfiguration(),
child: SizedBox(),
)
: const ConfigurationsInheritedWidget(
chatBackgroundConfig: ChatBackgroundConfiguration(),
child: SizedBox(),
);
}
59 changes: 59 additions & 0 deletions lib/src/inherited_widgets/configurations_inherited_widgets.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import 'package:chatview/src/models/models.dart';
import 'package:emoji_picker_flutter/emoji_picker_flutter.dart';
import 'package:flutter/material.dart';

class ConfigurationsInheritedWidget extends InheritedWidget {
const ConfigurationsInheritedWidget({
Key? key,
required Widget child,
required this.chatBackgroundConfig,
this.reactionPopupConfig,
this.messageConfig,
this.chatBubbleConfig,
this.profileCircleConfig,
this.swipeToReplyConfig,
this.repliedMessageConfig,
this.typeIndicatorConfig,
this.replyPopupConfig,
this.emojiPickerSheetConfig,
}) : super(key: key, child: child);

/// Provides configuration for background of chat.
final ChatBackgroundConfiguration chatBackgroundConfig;

/// Provides configuration for reaction pop up appearance.
final ReactionPopupConfiguration? reactionPopupConfig;

/// Provides configuration for customisation of different types
/// messages.
final MessageConfiguration? messageConfig;

/// Provides configuration of chat bubble's appearance.
final ChatBubbleConfiguration? chatBubbleConfig;

/// Provides configuration for profile circle avatar of user.
final ProfileCircleConfiguration? profileCircleConfig;

/// Provides configuration for when user swipe to chat bubble.
final SwipeToReplyConfiguration? swipeToReplyConfig;

/// Provides configuration for replied message view which is located upon chat
/// bubble.
final RepliedMessageConfiguration? repliedMessageConfig;

/// Provides configuration of typing indicator's appearance.
final TypeIndicatorConfiguration? typeIndicatorConfig;

/// Provides configuration for reply snack bar's appearance and options.
final ReplyPopupConfiguration? replyPopupConfig;

/// Configuration for emoji picker sheet
final Config? emojiPickerSheetConfig;

static ConfigurationsInheritedWidget? of(BuildContext context) => context
.dependOnInheritedWidgetOfExactType<ConfigurationsInheritedWidget>();

@override
bool updateShouldNotify(covariant ConfigurationsInheritedWidget oldWidget) =>
oldWidget != this;
}
Loading

0 comments on commit 371f8d4

Please sign in to comment.