You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
139 lines
4.9 KiB
Dart
139 lines
4.9 KiB
Dart
import 'dart:developer';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:test_sa/extensions/context_extension.dart';
|
|
import 'package:test_sa/extensions/int_extensions.dart';
|
|
import 'package:test_sa/extensions/string_extensions.dart';
|
|
import 'package:test_sa/modules/cm_module/views/components/action_button/footer_action_button.dart';
|
|
import 'package:test_sa/modules/notification_setting/provider/notification_setting_provider.dart';
|
|
import 'package:test_sa/modules/notification_setting/view/widgets/notification_preference_card.dart';
|
|
import 'package:test_sa/new_views/common_widgets/app_filled_button.dart';
|
|
import 'package:test_sa/new_views/common_widgets/app_lazy_loading.dart';
|
|
import 'package:test_sa/new_views/common_widgets/default_app_bar.dart';
|
|
import 'package:test_sa/views/widgets/loaders/app_loading.dart';
|
|
import 'package:test_sa/views/widgets/loaders/no_data_found.dart';
|
|
|
|
class NotificationSettingPage extends StatefulWidget {
|
|
static const String id = "notification_setting_page";
|
|
|
|
const NotificationSettingPage({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<NotificationSettingPage> createState() => _NotificationSettingPageState();
|
|
}
|
|
|
|
class _NotificationSettingPageState extends State<NotificationSettingPage> {
|
|
final ScrollController _scrollController = ScrollController();
|
|
late NotificationSettingProvider _provider;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_scrollController.addListener(_onScroll);
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
_provider = Provider.of<NotificationSettingProvider>(context, listen: false);
|
|
_provider.loadNotificationPreferences(refresh: true);
|
|
});
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_scrollController.removeListener(_onScroll);
|
|
_scrollController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
void _onScroll() {
|
|
if (_scrollController.position.pixels >= _scrollController.position.maxScrollExtent - 200) {
|
|
_provider.loadMoreData();
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: DefaultAppBar(
|
|
title: 'Notification Settings'.addTranslation,
|
|
),
|
|
body: Consumer<NotificationSettingProvider>(
|
|
builder: (context, provider, child) {
|
|
_provider = provider;
|
|
if (provider.isLoading) {
|
|
return const Center(child: ALoading());
|
|
}
|
|
if (provider.isEmpty || (provider.errorMessage != null && provider.templates.isEmpty)) {
|
|
return const Center(child: NoDataFound());
|
|
}
|
|
return Column(
|
|
children: [
|
|
Expanded(
|
|
child: RefreshIndicator(
|
|
onRefresh: () async {
|
|
await provider.loadNotificationPreferences(refresh: true);
|
|
},
|
|
child: ListView.separated(
|
|
controller: _scrollController,
|
|
padding: EdgeInsets.all(16.toScreenWidth),
|
|
itemCount: provider.templates.length + (provider.isLoadingMore ? 1 : 0),
|
|
itemBuilder: (context, index) {
|
|
if (index == provider.templates.length) {
|
|
return const Center(
|
|
child: ALoading());
|
|
}
|
|
|
|
final template = provider.templates[index];
|
|
return NotificationPreferenceCard(
|
|
key: ValueKey('${template.notificationTemplateId}_${template.isDisabled}'),
|
|
template: template,
|
|
onToggleChange: (isDisabled) {
|
|
provider.updateTemplateState(index, isDisabled);
|
|
},
|
|
onChannelChange: (channelIds) {
|
|
provider.updateTemplateChannels(index, channelIds);
|
|
},
|
|
);
|
|
},
|
|
separatorBuilder: (cxt, index) => 12.height,
|
|
),
|
|
),
|
|
),
|
|
|
|
// Save button - sticky at bottom
|
|
if (provider.hasChanges)
|
|
FooterActionButton.footerContainer(
|
|
context: context,
|
|
child: AppFilledButton(
|
|
onPressed: provider.isSaving ? null : _handleSave,
|
|
label: context.translation.save,
|
|
loading: provider.isSaving,
|
|
disableButton: provider.isSaving,
|
|
),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
|
|
|
|
Future<void> _handleSave() async {
|
|
showDialog(
|
|
context: context,
|
|
barrierDismissible: false,
|
|
builder: (context) => const Center(child: AppLazyLoading()),
|
|
);
|
|
|
|
final success = await _provider.savePreferences();
|
|
// Navigator.pop(context); // Close loading dialog
|
|
|
|
if (success) {
|
|
_provider.loadNotificationPreferences(refresh: true);
|
|
Navigator.pop(context); // Go back to previous screen
|
|
}
|
|
}
|
|
}
|
|
|
|
|