698 lines
31 KiB
Dart
698 lines
31 KiB
Dart
// ignore_for_file: deprecated_member_use
|
|
|
|
import 'package:cached_network_image/cached_network_image.dart';
|
|
import 'package:didvan/config/design_config.dart';
|
|
import 'package:didvan/config/theme_data.dart';
|
|
import 'package:didvan/constants/app_icons.dart';
|
|
import 'package:didvan/constants/assets.dart';
|
|
import 'package:didvan/main.dart';
|
|
import 'package:didvan/models/ai/ai_chat_args.dart';
|
|
import 'package:didvan/models/ai/chats_model.dart';
|
|
import 'package:didvan/models/enums.dart';
|
|
import 'package:didvan/models/notification_message.dart';
|
|
import 'package:didvan/models/view/action_sheet_data.dart';
|
|
import 'package:didvan/models/view/alert_data.dart';
|
|
import 'package:didvan/providers/theme.dart';
|
|
import 'package:didvan/routes/routes.dart';
|
|
import 'package:didvan/services/app_initalizer.dart';
|
|
import 'package:didvan/services/notification/notification_service.dart';
|
|
import 'package:didvan/utils/action_sheet.dart';
|
|
import 'package:didvan/views/ai/ai.dart';
|
|
import 'package:didvan/views/ai/history_ai_chat_state.dart';
|
|
import 'package:didvan/views/ai/widgets/ai_message_bar.dart';
|
|
import 'package:didvan/views/home/categories/categories_page.dart';
|
|
import 'package:didvan/views/home/main/main_page.dart';
|
|
import 'package:didvan/views/home/home_state.dart';
|
|
import 'package:didvan/views/home/new_statistic/new_statistic.dart';
|
|
import 'package:didvan/views/home/search/search.dart';
|
|
import 'package:didvan/views/widgets/didvan/divider.dart';
|
|
import 'package:didvan/views/widgets/didvan/text.dart';
|
|
import 'package:didvan/views/widgets/ink_wrapper.dart';
|
|
import 'package:didvan/views/widgets/logo_app_bar.dart';
|
|
import 'package:didvan/views/widgets/didvan/bnb.dart';
|
|
import 'package:didvan/views/widgets/shimmer_placeholder.dart';
|
|
import 'package:didvan/views/widgets/state_handlers/empty_state.dart';
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:provider/provider.dart';
|
|
import '../../services/app_home_widget/home_widget_repository.dart';
|
|
|
|
final GlobalKey<ScaffoldState> homeScaffKey = GlobalKey<ScaffoldState>();
|
|
|
|
class Home extends StatefulWidget {
|
|
final bool? showDialogs;
|
|
const Home({Key? key, this.showDialogs}) : super(key: key);
|
|
|
|
@override
|
|
State<Home> createState() => _HomeState();
|
|
}
|
|
|
|
class _HomeState extends State<Home>
|
|
with SingleTickerProviderStateMixin, WidgetsBindingObserver {
|
|
late final TabController _tabController;
|
|
|
|
Future<void> _showDialog(BuildContext context) async {
|
|
WidgetsBinding.instance?.addPostFrameCallback((_) {
|
|
ActionSheetUtils(context)
|
|
.openDialog(
|
|
data: ActionSheetData(
|
|
content: const DidvanText(
|
|
'خوش آمدید!\nبرای امنیت بیشتر، رمز عبور خود را تغییر دهید.',
|
|
),
|
|
onConfirmed: () {
|
|
Future.delayed(
|
|
Duration.zero,
|
|
() => Navigator.of(context)
|
|
.pushNamed(Routes.authenticaion, arguments: true),
|
|
);
|
|
},
|
|
isBackgroundDropBlur: false,
|
|
confrimTitle: 'تغییر رمز عبور',
|
|
dismissTitle: 'بعدا',
|
|
),
|
|
)
|
|
.then((value) => ActionSheetUtils(context).openDialog(
|
|
data: ActionSheetData(
|
|
backgroundColor: Theme.of(context).colorScheme.background,
|
|
isBackgroundDropBlur: true,
|
|
content: Column(
|
|
children: [
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
InkWrapper(
|
|
onPressed: () {
|
|
Future.delayed(
|
|
Duration.zero,
|
|
() => Navigator.of(context).pop(),
|
|
);
|
|
},
|
|
child: const Icon(
|
|
DidvanIcons.close_solid,
|
|
size: 24,
|
|
),
|
|
),
|
|
DidvanText(
|
|
'شخصی سازی محتوا',
|
|
style: Theme.of(context).textTheme.displaySmall,
|
|
color: Theme.of(context).colorScheme.text,
|
|
),
|
|
const InkWrapper(
|
|
child: Icon(
|
|
DidvanIcons.close_regular,
|
|
size: 24,
|
|
color: Colors.transparent,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(
|
|
height: 12,
|
|
),
|
|
const DidvanText(
|
|
"کاربر گرامی\nلطفا جهت شخصیسازی و استفاده بهتر از برنامه، دستهبندیهای مورد علاقه خود و زمان دریافت اعلانات را انتخاب نمایید.")
|
|
],
|
|
),
|
|
// hasDismissButton: false,
|
|
onConfirmed: () {
|
|
Future.delayed(
|
|
Duration.zero,
|
|
() =>
|
|
Navigator.of(navigatorKey.currentContext!).pushNamed(
|
|
Routes.favouritesStep,
|
|
arguments: {"toTimer": true},
|
|
),
|
|
);
|
|
},
|
|
confrimTitle: 'تایید',
|
|
),
|
|
));
|
|
});
|
|
}
|
|
|
|
@override
|
|
void initState() {
|
|
if (widget.showDialogs ?? false) {
|
|
_showDialog(context);
|
|
}
|
|
if (!kIsWeb) {
|
|
NotificationService.startListeningNotificationEvents();
|
|
}
|
|
|
|
final state = context.read<HomeState>();
|
|
DesignConfig.updateSystemUiOverlayStyle();
|
|
_tabController = TabController(length: 4, vsync: this, initialIndex: 0);
|
|
state.tabController = _tabController;
|
|
_tabController.addListener(() {
|
|
state.currentPageIndex = _tabController.index;
|
|
if (_tabController.index == 2) {
|
|
homeScaffKey.currentState!.closeDrawer();
|
|
|
|
context.read<HistoryAiChatState>().getChats();
|
|
context.read<HistoryAiChatState>().getBots();
|
|
}
|
|
});
|
|
if (!kIsWeb) {
|
|
Future.delayed(Duration.zero, () {
|
|
HomeWidgetRepository.fetchWidget();
|
|
HomeWidgetRepository.decideWhereToGo();
|
|
NotificationMessage? data = HomeWidgetRepository.data;
|
|
if (data != null) {
|
|
HomeWidgetRepository.decideWhereToGoNotif();
|
|
}
|
|
AppInitializer.handleCLick(state, _tabController);
|
|
});
|
|
}
|
|
state.refresh();
|
|
context.read<ThemeProvider>().addListener(() {
|
|
state.refresh();
|
|
});
|
|
|
|
super.initState();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
key: homeScaffKey,
|
|
appBar: LogoAppBar(
|
|
canSearch: context.watch<HomeState>().tabController.index != 2,
|
|
),
|
|
resizeToAvoidBottomInset: false,
|
|
drawer: context.watch<HomeState>().tabController.index == 2
|
|
? Drawer(
|
|
child: Consumer<HistoryAiChatState>(
|
|
builder: (context, state, child) {
|
|
return Column(
|
|
children: [
|
|
const SizedBox(
|
|
height: 8,
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.only(left: 20.0, top: 8),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.end,
|
|
children: [
|
|
InkWell(
|
|
onTap: () =>
|
|
homeScaffKey.currentState!.closeDrawer(),
|
|
child: const Icon(
|
|
DidvanIcons.close_regular,
|
|
),
|
|
)
|
|
],
|
|
),
|
|
),
|
|
Icon(
|
|
DidvanIcons.ai_solid,
|
|
size: MediaQuery.sizeOf(context).width / 5,
|
|
color: Theme.of(context).colorScheme.title,
|
|
),
|
|
DidvanText(
|
|
'هوشان',
|
|
color: Theme.of(context).colorScheme.title,
|
|
),
|
|
const SizedBox(
|
|
height: 24,
|
|
),
|
|
Expanded(
|
|
child: Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 20.0),
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
Expanded(
|
|
child: Column(
|
|
children: [
|
|
drawerBtn(
|
|
icon: Icons.handshake_rounded,
|
|
text: 'ساخت دستیار شخصی',
|
|
crossAxisAlignment:
|
|
CrossAxisAlignment.start,
|
|
enable: false),
|
|
const DidvanDivider(),
|
|
drawerBtn(
|
|
icon: CupertinoIcons.doc_text_search,
|
|
text: 'جستجو در مدلها',
|
|
click: () {
|
|
ActionSheetUtils(context)
|
|
.botsDialogSelect(
|
|
context: context,
|
|
state: state);
|
|
homeScaffKey.currentState!
|
|
.closeDrawer();
|
|
},
|
|
enable: false),
|
|
const DidvanDivider(),
|
|
drawerBtn(
|
|
icon: DidvanIcons.chats_regular,
|
|
text: 'تاریخچه همه گفتگوها',
|
|
label: 'حذف همه',
|
|
click: () {
|
|
Navigator.of(context)
|
|
.pushNamed(Routes.aiHistory);
|
|
},
|
|
labelClick: state.chats.isEmpty
|
|
? null
|
|
: () async {
|
|
await ActionSheetUtils(context)
|
|
.openDialog(
|
|
data: ActionSheetData(
|
|
onConfirmed:
|
|
() async {
|
|
await state
|
|
.deleteAllChat(
|
|
refresh:
|
|
false);
|
|
},
|
|
content: Column(
|
|
children: [
|
|
Row(
|
|
crossAxisAlignment:
|
|
CrossAxisAlignment
|
|
.center,
|
|
children: [
|
|
Icon(
|
|
DidvanIcons
|
|
.trash_solid,
|
|
color: Theme.of(
|
|
context)
|
|
.colorScheme
|
|
.error,
|
|
),
|
|
const SizedBox(
|
|
width: 8,
|
|
),
|
|
DidvanText(
|
|
'پاک کردن همه گفتوگوها',
|
|
color: Theme.of(
|
|
context)
|
|
.colorScheme
|
|
.error,
|
|
fontSize:
|
|
20,
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(
|
|
height: 12,
|
|
),
|
|
const DidvanText(
|
|
'آیا از پاک کردن تمامی گفتوگوهای انجام شده با هوشان اطمینان دارید؟'),
|
|
],
|
|
)));
|
|
},
|
|
),
|
|
const SizedBox(
|
|
height: 12,
|
|
),
|
|
// SearchField(
|
|
// title: 'title',
|
|
// onChanged: (value) {},
|
|
// focusNode: FocusNode()),
|
|
// SizedBox(
|
|
// height: 12,
|
|
// ),
|
|
Expanded(
|
|
child: state.chats.isEmpty
|
|
? Padding(
|
|
padding:
|
|
const EdgeInsets.all(12.0),
|
|
child: EmptyState(
|
|
asset: Assets.emptyResult,
|
|
title: 'لیست خالی است',
|
|
),
|
|
)
|
|
: state.loadingdeleteAll ||
|
|
state.appState ==
|
|
AppState.busy
|
|
? ListView.builder(
|
|
shrinkWrap: true,
|
|
itemCount: 10,
|
|
padding: const EdgeInsets
|
|
.symmetric(
|
|
horizontal: 12),
|
|
physics:
|
|
const NeverScrollableScrollPhysics(),
|
|
itemBuilder:
|
|
(context, index) {
|
|
return const Padding(
|
|
padding:
|
|
EdgeInsets.symmetric(
|
|
vertical: 12.0),
|
|
child: Row(
|
|
crossAxisAlignment:
|
|
CrossAxisAlignment
|
|
.start,
|
|
children: [
|
|
ClipOval(
|
|
child:
|
|
ShimmerPlaceholder(
|
|
height: 24,
|
|
width: 24,
|
|
),
|
|
),
|
|
SizedBox(width: 12),
|
|
Expanded(
|
|
child:
|
|
ShimmerPlaceholder(
|
|
height: 24,
|
|
),
|
|
),
|
|
SizedBox(width: 12),
|
|
ShimmerPlaceholder(
|
|
height: 24,
|
|
width: 24,
|
|
),
|
|
SizedBox(width: 8),
|
|
ShimmerPlaceholder(
|
|
height: 24,
|
|
width: 12,
|
|
),
|
|
],
|
|
),
|
|
);
|
|
},
|
|
)
|
|
: ListView.builder(
|
|
shrinkWrap: true,
|
|
itemCount: state.chats.length,
|
|
padding: const EdgeInsets
|
|
.symmetric(
|
|
horizontal: 12),
|
|
physics:
|
|
const BouncingScrollPhysics(),
|
|
itemBuilder:
|
|
(context, index) {
|
|
final chat =
|
|
state.chats[index];
|
|
TextEditingController
|
|
title =
|
|
TextEditingController(
|
|
text: chat.title);
|
|
|
|
return chatRow(chat, title,
|
|
state, index);
|
|
},
|
|
),
|
|
),
|
|
// SizedBox(
|
|
// height: 12,
|
|
// ),
|
|
// Text('نمایش قدیمیترها')
|
|
],
|
|
),
|
|
),
|
|
Column(
|
|
children: [
|
|
const DidvanDivider(),
|
|
drawerBtn(
|
|
icon: Icons.folder_copy_outlined,
|
|
text: 'گفتوگوهای آرشیو شده',
|
|
click: () {
|
|
Navigator.of(context).pushNamed(
|
|
Routes.aiHistory,
|
|
arguments: true);
|
|
},
|
|
),
|
|
const SizedBox(
|
|
height: 12,
|
|
),
|
|
drawerBtn(
|
|
icon: DidvanIcons.support_regular,
|
|
text: 'پیام به پشتیبانی',
|
|
click: () {
|
|
Navigator.of(context).pushNamed(
|
|
Routes.direct,
|
|
arguments: {
|
|
'type': 'پشتیبانی اپلیکیشن'
|
|
},
|
|
);
|
|
},
|
|
),
|
|
],
|
|
)
|
|
],
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(
|
|
height: 32,
|
|
)
|
|
],
|
|
);
|
|
},
|
|
),
|
|
)
|
|
: null,
|
|
body: WillPopScope(
|
|
onWillPop: () async {
|
|
if (context.read<HomeState>().tabController.index == 0) {
|
|
ActionSheetUtils(context).openDialog(
|
|
data: ActionSheetData(
|
|
content: const DidvanText(
|
|
'آیا قصد خروج از برنامه را دارید؟',
|
|
),
|
|
onConfirmed: () {
|
|
SystemChannels.platform.invokeMethod('SystemNavigator.pop');
|
|
},
|
|
isBackgroundDropBlur: true,
|
|
confrimTitle: 'بله',
|
|
onDismissed: Navigator.of(navigatorKey.currentContext!).pop,
|
|
dismissTitle: 'خیر',
|
|
));
|
|
} else {
|
|
_tabController.animateTo(0);
|
|
}
|
|
return false;
|
|
},
|
|
child: Consumer<HomeState>(
|
|
builder: (context, state, child) => AnimatedCrossFade(
|
|
duration: DesignConfig.lowAnimationDuration,
|
|
crossFadeState: state.filtering
|
|
? CrossFadeState.showSecond
|
|
: CrossFadeState.showFirst,
|
|
firstChild: SizedBox(
|
|
height: MediaQuery.of(context).size.height,
|
|
width: MediaQuery.of(context).size.width,
|
|
child: TabBarView(
|
|
physics: const NeverScrollableScrollPhysics(),
|
|
controller: _tabController,
|
|
children: const [
|
|
MainPage(),
|
|
CategoriesPage(),
|
|
Ai(),
|
|
NewStatistic(),
|
|
//Statistic(),
|
|
// Bookmarks(),
|
|
],
|
|
),
|
|
),
|
|
secondChild: const SearchPage(),
|
|
),
|
|
),
|
|
),
|
|
bottomNavigationBar: Consumer<HomeState>(
|
|
builder: (context, state, child) => DidvanBNB(
|
|
currentTabIndex: state.currentPageIndex,
|
|
onTabChanged: (index) {
|
|
state.currentPageIndex = index;
|
|
FocusScope.of(context).unfocus();
|
|
state.resetFilters(false);
|
|
_tabController.animateTo(index);
|
|
},
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Padding chatRow(ChatsModel chat, TextEditingController title,
|
|
HistoryAiChatState state, int index) {
|
|
return Padding(
|
|
padding: const EdgeInsets.symmetric(vertical: 8.0),
|
|
child: InkWell(
|
|
onTap: () {
|
|
navigatorKey.currentState!.pushNamed(Routes.aiChat,
|
|
arguments: AiChatArgs(bot: chat.bot!, chat: chat));
|
|
},
|
|
child: Row(
|
|
children: [
|
|
ClipOval(
|
|
child: CachedNetworkImage(
|
|
imageUrl: chat.bot!.image.toString(),
|
|
width: 24,
|
|
height: 24,
|
|
)),
|
|
const SizedBox(
|
|
width: 12,
|
|
),
|
|
Expanded(
|
|
child: chat.isEditing != null && chat.isEditing!
|
|
? TextFormField(
|
|
controller: title,
|
|
style: const TextStyle(fontSize: 12),
|
|
textAlignVertical: TextAlignVertical.bottom,
|
|
maxLines: 1,
|
|
decoration: const InputDecoration(
|
|
isDense: true,
|
|
contentPadding:
|
|
EdgeInsets.symmetric(vertical: 5, horizontal: 10),
|
|
border: OutlineInputBorder(),
|
|
))
|
|
: Text(
|
|
chat.title.toString(),
|
|
overflow: TextOverflow.ellipsis,
|
|
maxLines: 1,
|
|
),
|
|
),
|
|
const SizedBox(
|
|
width: 24,
|
|
),
|
|
Row(
|
|
children: [
|
|
chat.isEditing != null &&
|
|
chat.isEditing! &&
|
|
state.loadingchangeTitle
|
|
? const SizedBox(
|
|
width: 12,
|
|
height: 12,
|
|
child: CircularProgressIndicator())
|
|
: InkWell(
|
|
onTap: () async {
|
|
if (title.text.isNotEmpty) {
|
|
await state.changeNameChat(
|
|
chat.id!, index, title.text,
|
|
refresh: false);
|
|
title.clear();
|
|
}
|
|
if (chat.isEditing != null) {
|
|
chat.isEditing = !chat.isEditing!;
|
|
state.update();
|
|
return;
|
|
}
|
|
chat.isEditing = true;
|
|
|
|
state.update();
|
|
},
|
|
child: Icon(
|
|
chat.isEditing != null && chat.isEditing!
|
|
? Icons.save
|
|
: Icons.edit_outlined,
|
|
size: 18,
|
|
),
|
|
),
|
|
const SizedBox(
|
|
width: 8,
|
|
),
|
|
PopupMenuButton(
|
|
onSelected: (value) async {
|
|
switch (value) {
|
|
case 'حذف پیام':
|
|
await state.deleteChat(chat.id!, index, refresh: false);
|
|
break;
|
|
|
|
case 'آرشیو':
|
|
await state.archivedChat(chat.id!, index,
|
|
refresh: false);
|
|
break;
|
|
default:
|
|
}
|
|
|
|
state.update();
|
|
},
|
|
itemBuilder: (BuildContext context) {
|
|
return <PopupMenuEntry>[
|
|
AiMessageBar.popUpBtns(
|
|
value: 'حذف پیام',
|
|
icon: DidvanIcons.trash_regular,
|
|
color: Theme.of(context).colorScheme.error,
|
|
height: 24,
|
|
size: 12),
|
|
AiMessageBar.popUpBtns(
|
|
value: 'آرشیو',
|
|
icon: Icons.folder_copy,
|
|
height: 24,
|
|
size: 12,
|
|
),
|
|
];
|
|
},
|
|
offset: const Offset(0, 0),
|
|
position: PopupMenuPosition.under,
|
|
useRootNavigator: true,
|
|
child: const Icon(
|
|
Icons.more_vert,
|
|
size: 18,
|
|
),
|
|
),
|
|
],
|
|
)
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget drawerBtn(
|
|
{final CrossAxisAlignment? crossAxisAlignment,
|
|
required final IconData icon,
|
|
required final String text,
|
|
final bool enable = true,
|
|
final String? label,
|
|
final Function()? labelClick,
|
|
final Function()? click}) {
|
|
return InkWell(
|
|
onTap: enable
|
|
? click
|
|
: () {
|
|
ActionSheetUtils(context).showAlert(
|
|
AlertData(message: 'درحال توسعه', aLertType: ALertType.info));
|
|
},
|
|
child: Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 12.0),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
Row(
|
|
crossAxisAlignment:
|
|
crossAxisAlignment ?? CrossAxisAlignment.center,
|
|
children: [
|
|
Icon(
|
|
icon,
|
|
color: enable
|
|
? Theme.of(context).colorScheme.title
|
|
: Theme.of(context).colorScheme.disabledText,
|
|
),
|
|
const SizedBox(
|
|
width: 8,
|
|
),
|
|
Column(
|
|
children: [
|
|
DidvanText(text,
|
|
fontSize: 16,
|
|
color: enable
|
|
? Theme.of(context).colorScheme.title
|
|
: Theme.of(context).colorScheme.disabledText),
|
|
// if (!enable) Text('در حال توسعه ...')
|
|
],
|
|
)
|
|
],
|
|
),
|
|
if (label != null)
|
|
InkWell(
|
|
onTap: labelClick,
|
|
child: DidvanText(
|
|
label,
|
|
color: Theme.of(context).colorScheme.primary,
|
|
fontSize: 12,
|
|
),
|
|
)
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|