1+ import 'dart:async' ;
12import 'dart:io' ;
23
34import 'package:equatable/equatable.dart' ;
5+ import 'package:flutter/widgets.dart' ;
46import 'package:flutter_bloc/flutter_bloc.dart' ;
57import 'package:freezed_annotation/freezed_annotation.dart' ;
68
@@ -9,6 +11,8 @@ import '../../../data/services/local/storage_service.dart';
911import '../../../domain/models/status.dart' ;
1012import '../../../domain/models/user.dart' ;
1113import '../../../domain/repositories/user_repository.dart' ;
14+ import '../../../utils/failures.dart' ;
15+ import '../../../utils/snackbar.dart' ;
1216
1317part 'update_profile_event.dart' ;
1418part 'update_profile_state.dart' ;
@@ -19,24 +23,41 @@ class UpdateProfileBloc extends Bloc<UpdateProfileEvent, UpdateProfileState> {
1923 on < Initialize > (_onInitialize);
2024 on < SelectImage > (_onSelectImage);
2125 on < UpdateInstitute > (_onUpdateInstitute);
26+ on < SwitchView > (_onSwitchView);
27+ on < UpdateFocusField > (_onUpdateFocusField);
28+ on < ToggleObscure > (_onToggleObscure);
29+ on < UpdatePassword > (_onUpdatePassword);
2230 }
2331
32+ static List <String > institutes = < String > [
33+ 'Indian Institute of Technology Roorkee' ,
34+ 'Indian Institute of Technology Delhi' ,
35+ 'Indian Institute of Technology Mandi' ,
36+ 'Indian Institute of Technology Indore' ,
37+ 'Indian Institute of Technology Bombay' ,
38+ ];
39+
2440 void _onInitialize (Initialize event, Emitter <UpdateProfileState > emit) async {
2541 final _instituteList = await UserRepository .getInstituteList ();
26- if (_instituteList.isEmpty) {
27- _instituteList.addAll ([
28- 'Indian Institute of Technology Roorkee' ,
29- 'Indian Institute of Technology Delhi' ,
30- 'Indian Institute of Technology Mandi' ,
31- 'Indian Institute of Technology Indore' ,
32- 'Indian Institute of Technology Bombay'
33- ]);
42+ if (_instituteList.isNotEmpty) {
43+ institutes = _instituteList;
3444 }
3545
46+ final _controllers = < String , TextEditingController ? > {
47+ 'codechef' : TextEditingController (),
48+ 'codeforces' : TextEditingController (),
49+ 'hackerrank' : TextEditingController (),
50+ 'spoj' : TextEditingController (),
51+ 'leetcode' : TextEditingController (),
52+ 'old_pass' : TextEditingController (),
53+ 'new_pass' : TextEditingController (),
54+ 're_enter' : TextEditingController (),
55+ };
56+
3657 emit (state.copyWith (
3758 status: const Status (),
38- instituteList: _instituteList,
3959 user: _currentUser,
60+ controllers: _controllers,
4061 ));
4162 }
4263
@@ -52,6 +73,60 @@ class UpdateProfileBloc extends Bloc<UpdateProfileEvent, UpdateProfileState> {
5273 emit (state.copyWith (user: _updatedUser));
5374 }
5475
76+ void _onSwitchView (SwitchView event, Emitter <UpdateProfileState > emit) {
77+ final currentState = state.showChangePasswordView;
78+ final _controllers = state.controllers;
79+ _controllers['old_pass' ]? .text = '' ;
80+ _controllers['new_pass' ]? .text = '' ;
81+ _controllers['re_enter' ]? .text = '' ;
82+ emit (state.copyWith (
83+ showChangePasswordView: ! currentState,
84+ activePasswordTextField: - 1 ,
85+ controllers: _controllers,
86+ ));
87+ }
88+
89+ void _onUpdateFocusField (
90+ UpdateFocusField event, Emitter <UpdateProfileState > emit) {
91+ emit (state.copyWith (activePasswordTextField: event.index));
92+ }
93+
94+ void _onToggleObscure (ToggleObscure event, Emitter <UpdateProfileState > emit) {
95+ final list = state.passwordFieldObscureState;
96+ emit (state.copyWith (passwordFieldObscureState: [
97+ ...List .generate (
98+ list.length,
99+ (index) {
100+ if (index == event.index) return ! list[index];
101+ return list[index];
102+ },
103+ )
104+ ]));
105+ }
106+
107+ void _onUpdatePassword (
108+ UpdatePassword event, Emitter <UpdateProfileState > emit) async {
109+ emit (state.copyWith (isUpdating: true ));
110+ try {
111+ await UserRepository .updatePassword (state.controllers['old_pass' ]! .text,
112+ state.controllers['new_pass' ]! .text);
113+ add (const SwitchView ());
114+ showSnackBar (message: 'Password Changed' );
115+ } on Failure catch (err) {
116+ showSnackBar (message: err.message);
117+ }
118+ emit (state.copyWith (isUpdating: false ));
119+ }
120+
121+ Future <bool > onWillPop () async {
122+ if (state.showChangePasswordView) {
123+ add (const SwitchView ());
124+ return false ;
125+ }
126+
127+ return true ;
128+ }
129+
55130 final User _currentUser = StorageService .user! ;
56131 User _updatedUser = StorageService .user! ;
57132}
0 commit comments