|
| 1 | +import 'package:dipantau_desktop_client/core/util/helper.dart'; |
| 2 | +import 'package:dipantau_desktop_client/core/util/string_extension.dart'; |
| 3 | +import 'package:dipantau_desktop_client/core/util/widget_helper.dart'; |
| 4 | +import 'package:dipantau_desktop_client/feature/data/model/user_setting/user_setting_body.dart'; |
| 5 | +import 'package:dipantau_desktop_client/feature/data/model/user_setting/user_setting_response.dart'; |
| 6 | +import 'package:dipantau_desktop_client/feature/presentation/bloc/setting/setting_bloc.dart'; |
| 7 | +import 'package:dipantau_desktop_client/feature/presentation/widget/widget_custom_circular_progress_indicator.dart'; |
| 8 | +import 'package:dipantau_desktop_client/feature/presentation/widget/widget_error.dart'; |
| 9 | +import 'package:dipantau_desktop_client/feature/presentation/widget/widget_primary_button.dart'; |
| 10 | +import 'package:dipantau_desktop_client/injection_container.dart'; |
| 11 | +import 'package:easy_localization/easy_localization.dart'; |
| 12 | +import 'package:flutter/material.dart'; |
| 13 | +import 'package:flutter_bloc/flutter_bloc.dart'; |
| 14 | + |
| 15 | +class SettingMemberBlurScreenshotPage extends StatefulWidget { |
| 16 | + static const routePath = '/member-blur-screenshot'; |
| 17 | + static const routeName = 'member-blur-screenshot'; |
| 18 | + |
| 19 | + const SettingMemberBlurScreenshotPage({Key? key}) : super(key: key); |
| 20 | + |
| 21 | + @override |
| 22 | + State<SettingMemberBlurScreenshotPage> createState() => _SettingMemberBlurScreenshotPageState(); |
| 23 | +} |
| 24 | + |
| 25 | +class _SettingMemberBlurScreenshotPageState extends State<SettingMemberBlurScreenshotPage> { |
| 26 | + final settingBloc = sl<SettingBloc>(); |
| 27 | + final widgetHelper = WidgetHelper(); |
| 28 | + final helper = sl<Helper>(); |
| 29 | + final listData = <_ItemSettingMember>[]; |
| 30 | + |
| 31 | + var isLoadingButton = false; |
| 32 | + |
| 33 | + @override |
| 34 | + void setState(VoidCallback fn) { |
| 35 | + if (mounted) { |
| 36 | + super.setState(fn); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + @override |
| 41 | + void initState() { |
| 42 | + doLoadData(); |
| 43 | + super.initState(); |
| 44 | + } |
| 45 | + |
| 46 | + void doLoadData() { |
| 47 | + settingBloc.add(LoadAllUserSettingEvent()); |
| 48 | + } |
| 49 | + |
| 50 | + @override |
| 51 | + Widget build(BuildContext context) { |
| 52 | + return IgnorePointer( |
| 53 | + ignoring: isLoadingButton, |
| 54 | + child: BlocProvider<SettingBloc>( |
| 55 | + create: (context) => settingBloc, |
| 56 | + child: BlocListener<SettingBloc, SettingState>( |
| 57 | + listener: (context, state) { |
| 58 | + setState(() => isLoadingButton = state is LoadingButtonSettingState); |
| 59 | + if (state is FailureSettingState) { |
| 60 | + final errorMessage = state.errorMessage.convertErrorMessageToHumanMessage(); |
| 61 | + if (errorMessage.contains('401')) { |
| 62 | + widgetHelper.showDialog401(context); |
| 63 | + return; |
| 64 | + } |
| 65 | + } else if (state is FailureSnackBarSettingState) { |
| 66 | + final errorMessage = state.errorMessage.convertErrorMessageToHumanMessage(); |
| 67 | + if (errorMessage.contains('401')) { |
| 68 | + widgetHelper.showDialog401(context); |
| 69 | + return; |
| 70 | + } |
| 71 | + widgetHelper.showSnackBar(context, errorMessage.hideResponseCode()); |
| 72 | + } else if (state is SuccessLoadAllUserSettingState) { |
| 73 | + listData.clear(); |
| 74 | + for (final element in state.response.data ?? <UserSettingResponse>[]) { |
| 75 | + final id = element.id ?? -1; |
| 76 | + final userId = element.userId ?? -1; |
| 77 | + final name = element.name ?? ''; |
| 78 | + if (id == -1 || userId == -1 || name.isEmpty) { |
| 79 | + continue; |
| 80 | + } |
| 81 | + listData.add( |
| 82 | + _ItemSettingMember( |
| 83 | + id: id, |
| 84 | + userId: userId, |
| 85 | + name: name, |
| 86 | + isEnableBlurScreenshot: element.isEnableBlurScreenshot ?? false, |
| 87 | + ), |
| 88 | + ); |
| 89 | + } |
| 90 | + } else if (state is SuccessUpdateUserSettingState) { |
| 91 | + widgetHelper.showSnackBar(context, 'user_setting_updated_successfully'.tr()); |
| 92 | + } |
| 93 | + }, |
| 94 | + child: Scaffold( |
| 95 | + appBar: AppBar( |
| 96 | + title: Text( |
| 97 | + 'screenshot_blur'.tr(), |
| 98 | + ), |
| 99 | + centerTitle: false, |
| 100 | + ), |
| 101 | + body: Padding( |
| 102 | + padding: EdgeInsets.symmetric(horizontal: helper.getDefaultPaddingLayout), |
| 103 | + child: BlocBuilder<SettingBloc, SettingState>( |
| 104 | + builder: (context, state) { |
| 105 | + if (state is LoadingCenterSettingState) { |
| 106 | + return const WidgetCustomCircularProgressIndicator(); |
| 107 | + } else if (state is FailureSettingState) { |
| 108 | + final errorMessage = state.errorMessage.convertErrorMessageToHumanMessage(); |
| 109 | + return WidgetError( |
| 110 | + title: 'oops'.tr(), |
| 111 | + message: errorMessage.hideResponseCode(), |
| 112 | + onTryAgain: doLoadData, |
| 113 | + ); |
| 114 | + } |
| 115 | + if (listData.isEmpty) { |
| 116 | + return WidgetError( |
| 117 | + title: 'info'.tr(), |
| 118 | + message: 'no_data_to_display'.tr(), |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | + return Column( |
| 123 | + children: [ |
| 124 | + const SizedBox(height: 4), |
| 125 | + Row( |
| 126 | + children: [ |
| 127 | + Text( |
| 128 | + 'member_n'.plural(listData.length), |
| 129 | + style: Theme.of(context).textTheme.bodyMedium?.copyWith( |
| 130 | + color: Colors.grey, |
| 131 | + ), |
| 132 | + ), |
| 133 | + Expanded( |
| 134 | + child: Container(), |
| 135 | + ), |
| 136 | + TextButton( |
| 137 | + onPressed: () { |
| 138 | + for (final itemData in listData) { |
| 139 | + itemData.isEnableBlurScreenshot = false; |
| 140 | + } |
| 141 | + setState(() {}); |
| 142 | + }, |
| 143 | + child: Text('disable_all'.tr()), |
| 144 | + ), |
| 145 | + Container( |
| 146 | + width: 1, |
| 147 | + height: 24, |
| 148 | + color: Colors.grey, |
| 149 | + margin: const EdgeInsets.symmetric(horizontal: 8), |
| 150 | + ), |
| 151 | + TextButton( |
| 152 | + onPressed: () { |
| 153 | + for (final itemData in listData) { |
| 154 | + itemData.isEnableBlurScreenshot = true; |
| 155 | + } |
| 156 | + setState(() {}); |
| 157 | + }, |
| 158 | + child: Text('enable_all'.tr()), |
| 159 | + ), |
| 160 | + ], |
| 161 | + ), |
| 162 | + const SizedBox(height: 4), |
| 163 | + Expanded( |
| 164 | + child: buildWidgetListData(), |
| 165 | + ), |
| 166 | + Padding( |
| 167 | + padding: EdgeInsets.only( |
| 168 | + top: 16, |
| 169 | + bottom: helper.getDefaultPaddingLayoutBottom, |
| 170 | + ), |
| 171 | + child: SizedBox( |
| 172 | + width: double.infinity, |
| 173 | + child: WidgetPrimaryButton( |
| 174 | + onPressed: submit, |
| 175 | + isLoading: isLoadingButton, |
| 176 | + child: Text( |
| 177 | + 'save'.tr(), |
| 178 | + ), |
| 179 | + ), |
| 180 | + ), |
| 181 | + ), |
| 182 | + ], |
| 183 | + ); |
| 184 | + }, |
| 185 | + ), |
| 186 | + ), |
| 187 | + ), |
| 188 | + ), |
| 189 | + ), |
| 190 | + ); |
| 191 | + } |
| 192 | + |
| 193 | + void submit() { |
| 194 | + final body = UserSettingBody( |
| 195 | + data: listData |
| 196 | + .map( |
| 197 | + (e) => ItemUserSettingBody( |
| 198 | + id: e.id, |
| 199 | + isEnableBlurScreenshot: e.isEnableBlurScreenshot, |
| 200 | + userId: e.userId, |
| 201 | + ), |
| 202 | + ) |
| 203 | + .toList(), |
| 204 | + ); |
| 205 | + settingBloc.add( |
| 206 | + UpdateUserSettingEvent( |
| 207 | + body: body, |
| 208 | + ), |
| 209 | + ); |
| 210 | + } |
| 211 | + |
| 212 | + Widget buildWidgetListData() { |
| 213 | + return ListView.separated( |
| 214 | + padding: EdgeInsets.only( |
| 215 | + top: helper.getDefaultPaddingLayoutTop, |
| 216 | + ), |
| 217 | + itemBuilder: (context, index) { |
| 218 | + final element = listData[index]; |
| 219 | + final isEnableBlurScreenshot = element.isEnableBlurScreenshot; |
| 220 | + return Row( |
| 221 | + children: [ |
| 222 | + Expanded( |
| 223 | + child: Text(element.name), |
| 224 | + ), |
| 225 | + Switch.adaptive( |
| 226 | + value: isEnableBlurScreenshot, |
| 227 | + activeColor: Theme.of(context).colorScheme.primary, |
| 228 | + onChanged: (newValue) { |
| 229 | + listData[index].isEnableBlurScreenshot = newValue; |
| 230 | + setState(() {}); |
| 231 | + }, |
| 232 | + ), |
| 233 | + ], |
| 234 | + ); |
| 235 | + }, |
| 236 | + separatorBuilder: (context, index) => const Divider(), |
| 237 | + itemCount: listData.length, |
| 238 | + ); |
| 239 | + } |
| 240 | +} |
| 241 | + |
| 242 | +class _ItemSettingMember { |
| 243 | + final int id; |
| 244 | + final int userId; |
| 245 | + final String name; |
| 246 | + bool isEnableBlurScreenshot; |
| 247 | + |
| 248 | + _ItemSettingMember({ |
| 249 | + required this.id, |
| 250 | + required this.userId, |
| 251 | + required this.name, |
| 252 | + required this.isEnableBlurScreenshot, |
| 253 | + }); |
| 254 | + |
| 255 | + @override |
| 256 | + String toString() { |
| 257 | + return '_ItemSettingMember{id: $id, userId: $userId, name: $name, isEnableBlurScreenshot: $isEnableBlurScreenshot}'; |
| 258 | + } |
| 259 | +} |
0 commit comments