@@ -2,10 +2,14 @@ import 'package:dipantau_desktop_client/core/util/enum/global_variable.dart';
22import 'package:dipantau_desktop_client/core/util/helper.dart' ;
33import 'package:dipantau_desktop_client/core/util/shared_preferences_manager.dart' ;
44import 'package:dipantau_desktop_client/core/util/widget_helper.dart' ;
5+ import 'package:dipantau_desktop_client/feature/presentation/bloc/setup_credential/setup_credential_bloc.dart' ;
6+ import 'package:dipantau_desktop_client/feature/presentation/widget/widget_loading_center_full_screen.dart' ;
57import 'package:dipantau_desktop_client/feature/presentation/widget/widget_primary_button.dart' ;
68import 'package:dipantau_desktop_client/injection_container.dart' as di;
9+ import 'package:dipantau_desktop_client/injection_container.dart' ;
710import 'package:easy_localization/easy_localization.dart' ;
811import 'package:flutter/material.dart' ;
12+ import 'package:flutter_bloc/flutter_bloc.dart' ;
913import 'package:go_router/go_router.dart' ;
1014
1115class SetupCredentialPage extends StatefulWidget {
@@ -32,6 +36,7 @@ class _SetupCredentialPageState extends State<SetupCredentialPage> {
3236 final controllerHostname = TextEditingController ();
3337 final widgetHelper = WidgetHelper ();
3438 final formState = GlobalKey <FormState >();
39+ final setupCredentialBloc = sl <SetupCredentialBloc >();
3540
3641 var isLogin = false ;
3742 var defaultDomainApi = '' ;
@@ -52,37 +57,81 @@ class _SetupCredentialPageState extends State<SetupCredentialPage> {
5257 appBar: AppBar (
5358 automaticallyImplyLeading: widget.isFromSplashScreen ? false : true ,
5459 ),
55- body: Padding (
56- padding: EdgeInsets .only (
57- left: helper.getDefaultPaddingLayout,
58- top: helper.getDefaultPaddingLayoutTop,
59- right: helper.getDefaultPaddingLayout,
60- bottom: helper.getDefaultPaddingLayout,
60+ body: BlocProvider (
61+ create: (context) => setupCredentialBloc,
62+ child: BlocListener <SetupCredentialBloc , SetupCredentialState >(
63+ listener: (context, state) {
64+ if (state is FailureSetupCredentialState ) {
65+ final errorMessage = 'invalid_hostname' .tr ();
66+ widgetHelper.showDialogMessage (
67+ context,
68+ 'info' .tr (),
69+ errorMessage,
70+ );
71+ } else if (state is SuccessPingSetupCredentialState ) {
72+ final hostname = state.baseUrl;
73+ sharedPreferencesManager.putString (SharedPreferencesManager .keyDomainApi, hostname).then ((value) {
74+ helper.setDomainApiToFlavor (hostname);
75+ di.init ();
76+ if (mounted) {
77+ context.go ('/' );
78+ }
79+ });
80+ }
81+ },
82+ child: Stack (
83+ children: [
84+ buildWidgetBody (),
85+ buildWidgetLoadingOverlay (),
86+ ],
87+ ),
6188 ),
62- child: SizedBox (
63- width: double .infinity,
64- child: Form (
65- key: formState,
66- autovalidateMode: AutovalidateMode .onUserInteraction,
67- child: Column (
68- mainAxisSize: MainAxisSize .max,
69- mainAxisAlignment: MainAxisAlignment .center,
70- children: [
71- buildWidgetTitle (),
72- const SizedBox (height: 8 ),
73- Text (
74- 'subtitle_set_hostname' .tr (),
75- style: Theme .of (context).textTheme.bodyMedium? .copyWith (
76- color: Colors .grey,
77- ),
78- textAlign: TextAlign .center,
79- ),
80- const SizedBox (height: 24 ),
81- buildWidgetTextFieldHostname (),
82- const SizedBox (height: 24 ),
83- buildWidgetButtonSave (),
84- ],
85- ),
89+ ),
90+ );
91+ }
92+
93+ Widget buildWidgetLoadingOverlay () {
94+ return BlocBuilder <SetupCredentialBloc , SetupCredentialState >(
95+ builder: (context, state) {
96+ if (state is LoadingSetupCredentialState ) {
97+ return const WidgetLoadingCenterFullScreen ();
98+ }
99+ return Container ();
100+ },
101+ );
102+ }
103+
104+ Widget buildWidgetBody () {
105+ return Padding (
106+ padding: EdgeInsets .only (
107+ left: helper.getDefaultPaddingLayout,
108+ top: helper.getDefaultPaddingLayoutTop,
109+ right: helper.getDefaultPaddingLayout,
110+ bottom: helper.getDefaultPaddingLayout,
111+ ),
112+ child: SizedBox (
113+ width: double .infinity,
114+ child: Form (
115+ key: formState,
116+ autovalidateMode: AutovalidateMode .onUserInteraction,
117+ child: Column (
118+ mainAxisSize: MainAxisSize .max,
119+ mainAxisAlignment: MainAxisAlignment .center,
120+ children: [
121+ buildWidgetTitle (),
122+ const SizedBox (height: 8 ),
123+ Text (
124+ 'subtitle_set_hostname' .tr (),
125+ style: Theme .of (context).textTheme.bodyMedium? .copyWith (
126+ color: Colors .grey,
127+ ),
128+ textAlign: TextAlign .center,
129+ ),
130+ const SizedBox (height: 24 ),
131+ buildWidgetTextFieldHostname (),
132+ const SizedBox (height: 24 ),
133+ buildWidgetButtonSave (),
134+ ],
86135 ),
87136 ),
88137 ),
@@ -149,12 +198,11 @@ class _SetupCredentialPageState extends State<SetupCredentialPage> {
149198 }
150199 if (isContinue != null && isContinue) {
151200 final hostname = helper.removeTrailingSlash (controllerHostname.text.trim ()).trim ();
152- await sharedPreferencesManager.putString (SharedPreferencesManager .keyDomainApi, hostname);
153- helper.setDomainApiToFlavor (hostname);
154- di.init ();
155- if (mounted) {
156- context.go ('/' );
157- }
201+ setupCredentialBloc.add (
202+ PingSetupCredentialEvent (
203+ baseUrl: hostname,
204+ ),
205+ );
158206 }
159207 }
160208 }
0 commit comments