Untitled

mail@pastecode.io avatar
unknown
plain_text
2 days ago
10 kB
2
Indexable
Never
import 'dart:async';

import 'package:Buildeffective/src/app.dart';
import 'package:Buildeffective/src/app_helper.dart';
import 'package:Buildeffective/src/app_notification.dart';
import 'package:Buildeffective/src/screens/homeguide_screen/store/home_guide_viewmodel.dart';
import 'package:Buildeffective/src/screens/message/component/invite_component.dart';
import 'package:Buildeffective/src/screens/message/screens/create_channel_screen.dart';
import 'package:Buildeffective/src/screens/message/store/message_store.dart';
import 'package:Buildeffective/src/screens/message/viewmodels/message_filter.dart';
import 'package:Buildeffective/src/screens/message/viewmodels/message_viewmodel.dart';
import 'package:Buildeffective/src/screens/message/widgets/message_chat_item_widget.dart';
import 'package:Buildeffective/src/ui_component/app_loader.dart';
import 'package:Buildeffective/src/ui_component/common_appbar.dart';
import 'package:Buildeffective/src/utils/comman_export.dart';
import 'package:flutter/material.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:get_it/get_it.dart';
import 'package:nb_utils/nb_utils.dart';
import 'package:stream_chat_flutter/stream_chat_flutter.dart';

class MessageListingView extends StatefulWidget {
  static const String route = "message_screen";

  final MessageFilter messageFilter;

  final bool isFromNotification;
  String? memberName;

  MessageListingView({required this.messageFilter, this.isFromNotification = false, this.memberName});

  @override
  State<MessageListingView> createState() => _MessageListingViewState();
}

class _MessageListingViewState extends State<MessageListingView> {
  Future<List<String>>? future;

  MessageViewModel messageViewModel = MessageViewModel();

  HomeGuideViewModel homeGuideViewModel = HomeGuideViewModel();

  List<String> tempSearchList = [];

  MessageStore messageStore = MessageStore();

  String? emailId;

  @override
  void initState() {
    RegisterInGetIt<HomeGuideViewModel>(homeGuideViewModel, name: "homeGuideViewModel");

    homeGuideViewModel = GetIt.instance.get<HomeGuideViewModel>(instanceName: "homeGuideViewModel");

    future = messageViewModel.getChannelApi({"property_slug": widget.messageFilter.property.slug!});

    getUserId();

    super.initState();
  }

  void getUserId() async {
    final preferences = GetIt.instance<Preferences>();
    emailId = await preferences.getEmail();
  }

  Widget buildBodyWidget({required AsyncSnapshot<List<String>> snap}) {
    if (snap.hasData) {
      if (snap.data!.isEmpty) {
        return InviteComponent();
      }

      return Observer(
        builder: (context) {
          return Container(
            decoration: BoxDecoration(
              boxShadow: [
                BoxShadow(
                  color: Color.fromRGBO(0, 0, 0, 0.15),
                  spreadRadius: 0,
                  blurRadius: 4,
                  offset: Offset(0, 5),
                ),
              ],
              color: Colors.white,
            ),
            child: StreamChannelListView(
              controller: messageViewModel.controller,
              shrinkWrap: true,
              padding: EdgeInsets.only(top: 6, bottom: 6),
              emptyBuilder: (context) {
                return NoDataWidget(title: "No Results Found");
              },
              itemBuilder: (context, channels, index, defaultWidget) {
                return MessageChatItemWidget(
                  channel: channels[index],
                  messageViewModel: messageViewModel,
                  loginEmailId: emailId,
                );
              },
              loadingBuilder: (context) => BEAppLoader(),
              errorBuilder: (p0, p1) {
                return Center(
                  child: Padding(
                    padding: const EdgeInsets.all(16.0),
                    child: Text(p1.message.toString(), style: BETextStyles.BodyStyle),
                  ),
                );
              },
            ),
          );
        },
      );
    }

    return snapWidgetHelper(snap, loadingWidget: BEAppLoader());
  }

  void setToDefault({required List<String> data}) {
    messageViewModel.controller = StreamChannelListController(
      client: App.client,
      filter: messageViewModel.filterFunction(channel: data),
      channelStateSort: [SortOption('last_message_at'), SortOption('updated_at')],
    );
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: future,
      builder: (context, snap) {
        if (snap.hasData) {
          tempSearchList = snap.data.validate();
        }
        return Observer(builder: (context) {
          return Scaffold(
            resizeToAvoidBottomInset: false,
            backgroundColor: BEColors.streamChatBackColor,
            appBar: AppAppBar(
              title: "Messages",
              needBackButton: true,
              titleWidget: messageStore.isSearchVisible
                  ? AppTextField(
                      controller: messageStore.searchCont,
                      onChanged: (p0) {
                        messageStore.getAreaSearchValue(p0);
                        if (p0.isEmpty) {
                          setToDefault(data: tempSearchList);
                        } else {
                          messageViewModel.controller = StreamChannelListController(
                            client: App.client,
                            filter: Filter.and([
                              Filter.in_('id', tempSearchList),
                              Filter.in_('members', [StreamChat.of(App.navigatorKey.currentContext!).currentUser!.id]),
                              Filter.or([
                                Filter.autoComplete('member.user.name', "${p0}"),
                                Filter.autoComplete('name', "${p0}"),
                                Filter.autoComplete('channelTags', "${p0}"),
                                Filter.autoComplete('associatedWith', "${p0}"),
                              ])
                            ]),
                            channelStateSort: [SortOption('last_message_at'), SortOption('updated_at')],
                          );
                        }
                      },
                      textFieldType: TextFieldType.NAME,
                      onFieldSubmitted: (p0) {
                        if (p0.validate().isEmpty && messageStore.isSearchVisible) {
                          messageStore.searchCont.clear();
                          messageStore.searchText = "";
                          messageStore.setSearchVisible(!messageStore.isSearchVisible);
                        }
                      },
                      decoration: InputDecoration(
                        border: InputBorder.none,
                        hintText: "Type Here...",
                        alignLabelWithHint: true,
                        hintStyle: BETextStyles.TabStyleWithSecondGrayColor,
                        enabledBorder: InputBorder.none,
                      ),
                      autoFocus: true,
                    )
                  : null,
              onLeadingPressed: () {
                if (messageStore.isSearchVisible) {
                  messageStore.setSearchVisible(!messageStore.isSearchVisible);
                  messageStore.searchCont.clear();
                  messageStore.searchText = "";
                  setToDefault(data: snap.data.validate());
                  return;
                }
                finish(context);
              },
              actions: [
                if ((snap.hasData && snap.data!.isNotEmpty)) ...{
                  IconButton(
                    onPressed: () {
                      if (messageStore.isSearchVisible) {
                        messageStore.searchCont.clear();
                        messageStore.searchText = "";
                        setToDefault(data: tempSearchList);
                      }
                      messageStore.setSearchVisible(!messageStore.isSearchVisible);
                    },
                    icon: Observer(builder: (context) {
                      return SvgPicture.asset(
                        messageStore.isSearchVisible ? ResImages.ic_close : ResImages.ic_search_dashboard,
                        color: Colors.black,
                      );
                    }),
                  ),
                  if ((!messageStore.isSearchVisible)) ...{
                    IconButton(
                      onPressed: () async {
                        if (homeGuideViewModel.isChantEnable == false) {
                          toast("Currently no members available in property.");
                        } else {
                          AppHelper.locatableIdAndName = null;
                          await CreateChannelScreen().launch(context).then(
                            (value) async {
                              if (value != null) {
                                if (value == "added") {
                                  tempSearchList = await messageViewModel.getChannelApi({"property_slug": widget.messageFilter.property.slug!});
                                } else {
                                  AppNotification.selectedChannel!.extraData['title'] = value;
                                }

                                // setState(() {});
                              }
                            },
                          );
                        }
                      },
                      icon: Icon(Icons.add, color: Colors.black),
                    ),
                  },
                  8.width,
                }
              ],
            ),
            body: buildBodyWidget(snap: snap),
          );
        });
      },
    );
  }
}
Leave a Comment