Skip to content

Commit

Permalink
Merge pull request #51 from arafaysaleem/release
Browse files Browse the repository at this point in the history
Release-01/07/2021
  • Loading branch information
arafaysaleem authored Jun 30, 2021
2 parents acd1015 + 40a37b3 commit 35b0cc7
Show file tree
Hide file tree
Showing 13 changed files with 394 additions and 82 deletions.
34 changes: 20 additions & 14 deletions lib/providers/all_providers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ import '../services/networking/api_service.dart';

//repository imports
import '../services/repositories/auth_repository.dart';
import '../services/repositories/bookings_repository.dart';
import '../services/repositories/movies_repository.dart';
import '../services/repositories/payments_repository.dart';
import '../services/repositories/shows_repository.dart';
import '../services/repositories/theaters_repository.dart';
import '../services/repositories/bookings_repository.dart';
import '../services/repositories/payments_repository.dart';

//provider imports
import 'auth_provider.dart';
import 'movies_provider.dart';
import 'shows_provider.dart';
import 'theaters_provider.dart';
import 'bookings_provider.dart';
import 'movies_provider.dart';
import 'payments_provider.dart';
import 'shows_provider.dart';

//states
import 'states/auth_state.dart';
import 'theaters_provider.dart';

//service providers
final _apiServiceProvider = Provider<ApiService>((ref) => ApiService());
Expand All @@ -43,17 +43,17 @@ final _showsRepositoryProvider = Provider<ShowsRepository>((ref) {
return ShowsRepository(apiService: _apiService);
});

final _theatersRepositoryProvider = Provider<TheatersRepository>((ref){
final _theatersRepositoryProvider = Provider<TheatersRepository>((ref) {
final _apiService = ref.watch(_apiServiceProvider);
return TheatersRepository(apiService: _apiService);
});

final _bookingsRepositoryProvider = Provider<BookingsRepository>((ref){
final _bookingsRepositoryProvider = Provider<BookingsRepository>((ref) {
final _apiService = ref.watch(_apiServiceProvider);
return BookingsRepository(apiService: _apiService);
});

final _paymentsRepositoryProvider = Provider<PaymentsRepository>((ref){
final _paymentsRepositoryProvider = Provider<PaymentsRepository>((ref) {
final _apiService = ref.watch(_apiServiceProvider);
return PaymentsRepository(apiService: _apiService);
});
Expand All @@ -62,7 +62,11 @@ final _paymentsRepositoryProvider = Provider<PaymentsRepository>((ref){
final authProvider = StateNotifierProvider<AuthProvider, AuthState>((ref) {
final _authRepository = ref.watch(_authRepositoryProvider);
final _prefsService = ref.watch(_prefsServiceProvider);
return AuthProvider(_authRepository, _prefsService);
return AuthProvider(
reader: ref.read,
authRepository: _authRepository,
prefsService: _prefsService,
);
});

//data providers
Expand All @@ -76,17 +80,19 @@ final showsProvider = Provider<ShowsProvider>((ref) {
return ShowsProvider(_showsRepository);
});

final theatersProvider = ChangeNotifierProvider<TheatersProvider>((ref){
final theatersProvider = ChangeNotifierProvider<TheatersProvider>((ref) {
final _theatersRepository = ref.watch(_theatersRepositoryProvider);
return TheatersProvider(_theatersRepository);
});

final bookingsProvider = Provider<BookingsProvider>((ref){
final bookingsProvider = Provider<BookingsProvider>((ref) {
final _bookingsRepository = ref.watch(_bookingsRepositoryProvider);
return BookingsProvider(read: ref.read, bookingsRepository: _bookingsRepository);
return BookingsProvider(
read: ref.read, bookingsRepository: _bookingsRepository);
});

final paymentsProvider = Provider<PaymentsProvider>((ref){
final paymentsProvider = Provider<PaymentsProvider>((ref) {
final _paymentsRepository = ref.watch(_paymentsRepositoryProvider);
return PaymentsProvider(read: ref.read, paymentsRepository: _paymentsRepository);
return PaymentsProvider(
read: ref.read, paymentsRepository: _paymentsRepository);
});
52 changes: 33 additions & 19 deletions lib/providers/auth_provider.dart
Original file line number Diff line number Diff line change
@@ -1,28 +1,40 @@
import 'package:hooks_riverpod/hooks_riverpod.dart';

//enums
//Enums
import '../enums/user_role_enum.dart';

//models
//Models
import '../models/user_model.dart';

//services
//Services
import '../services/local_storage/prefs_service.dart';
import '../services/networking/network_exception.dart';
import '../services/repositories/auth_repository.dart';

//states
import 'states/auth_state.dart';

//States
import 'states/future_state.dart';

final changePasswordStateProvider = StateProvider(
(ref) => const FutureState<String>.idle(),
);

class AuthProvider extends StateNotifier<AuthState> {
late UserModel? _currentUser;
final AuthRepository _authRepository;
final PrefsService _prefsService;
final Reader _reader;
String _token = "";
String _password = "";

AuthProvider(this._authRepository, this._prefsService)
: super(const AuthState.unauthenticated()) {
AuthProvider({
required AuthRepository authRepository,
required PrefsService prefsService,
required Reader reader,
}) : _authRepository = authRepository,
_prefsService = prefsService,
_reader = reader,
super(const AuthState.unauthenticated()) {
init();
}

Expand Down Expand Up @@ -62,7 +74,7 @@ class AuthProvider extends StateNotifier<AuthState> {
}
}

void login({
Future<void> login({
required String email,
required String password,
}) async {
Expand All @@ -81,7 +93,7 @@ class AuthProvider extends StateNotifier<AuthState> {
}
}

void register({
Future<void> register({
required String email,
required String password,
required String fullName,
Expand Down Expand Up @@ -128,19 +140,21 @@ class AuthProvider extends StateNotifier<AuthState> {
return result;
}

Future<bool> changePassword({
required String email,
required String oldPassword,
required String newPassword,
}) async {
Future<void> changePassword({required String newPassword}) async {
final data = {
"email": email,
"password": oldPassword,
"email": currentUserEmail,
"password": currentUserPassword,
"new_password": newPassword,
};
final result = await _authRepository.sendChangePasswordData(data: data);
if (result) _updatePassword(newPassword);
return result;
final _changePasswordState = _reader(changePasswordStateProvider);
_changePasswordState.state = const FutureState.loading();
try {
final result = await _authRepository.sendChangePasswordData(data: data);
_updatePassword(newPassword);
_changePasswordState.state = FutureState.data(data: result);
} on NetworkException catch (e) {
_changePasswordState.state = FutureState.failed(reason: e.message);
}
}

Future<bool> verifyOtp({required String email, required int otp}) async {
Expand Down
17 changes: 17 additions & 0 deletions lib/providers/states/future_state.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'package:freezed_annotation/freezed_annotation.dart';

part 'future_state.freezed.dart';

@freezed
class FutureState<T> with _$FutureState<T> {

const factory FutureState.idle() = IDLE;

const factory FutureState.loading() = LOADING;

const factory FutureState.data({required T data}) = DATA;

const factory FutureState.failed({required String reason}) = FAILED;
}


2 changes: 2 additions & 0 deletions lib/routes/app_router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import '../views/screens/ticket_summary_screen.dart';
import '../views/screens/payment_screen.dart';
import '../views/screens/confirmation_screen.dart';
import '../views/screens/user_bookings_screen.dart';
import '../views/screens/change_password_screen.dart';

@MaterialAutoRouter(
routes: <AutoRoute>[
Expand All @@ -27,6 +28,7 @@ import '../views/screens/user_bookings_screen.dart';
AutoRoute(page: PaymentScreen),
AutoRoute(page: ConfirmationScreen),
AutoRoute(page: UserBookingsScreen),
AutoRoute(page: ChangePasswordScreen),
],
)
class $AppRouter{}
6 changes: 3 additions & 3 deletions lib/services/repositories/auth_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ class AuthRepository {
);
}

Future<bool> sendChangePasswordData({
Future<String> sendChangePasswordData({
required Map<String, dynamic> data,
}) async {
return await _apiService.setData<bool>(
return await _apiService.setData<String>(
endpoint: ApiEndpoint.auth(AuthEndpoint.CHANGE_PASSWORD),
data: data,
requiresAuthToken: false,
converter: (response) => response["headers"]["success"] == 1,
converter: (response) => response["headers"]["message"],
);
}

Expand Down
115 changes: 115 additions & 0 deletions lib/views/screens/change_password_screen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

//Helpers
import '../../helper/extensions/context_extensions.dart';
import '../../helper/utils/constants.dart';

//Providers
import '../../providers/all_providers.dart';
import '../../providers/auth_provider.dart';
import '../../providers/states/future_state.dart';

//Widgets
import '../widgets/common/custom_dialog.dart';
import '../widgets/common/scrollable_column.dart';
import '../widgets/common/rounded_bottom_container.dart';
import '../widgets/change_password/change_password_fields.dart';
import '../widgets/change_password/save_password_button.dart';

class ChangePasswordScreen extends HookWidget {
const ChangePasswordScreen();

@override
Widget build(BuildContext context) {
final currentPasswordController = useTextEditingController();
final newPasswordController = useTextEditingController();
final cNewPasswordController = useTextEditingController();
late final _formKey = useMemoized(() => GlobalKey<FormState>());
return Scaffold(
body: ProviderListener<StateController<FutureState<String>>>(
provider: changePasswordStateProvider,
onChange: (_, changePasswordStateController) async {
final changePasswordState = changePasswordStateController.state;
changePasswordState.maybeWhen(
data: (message) async {
currentPasswordController.clear();
newPasswordController.clear();
cNewPasswordController.clear();
await showDialog<bool>(
context: context,
barrierColor: Constants.barrierColor.withOpacity(0.75),
builder: (ctx) => CustomDialog.alert(
title: "Change Password Success",
body: message,
buttonText: "Okay",
),
);
},
failed: (reason) async => await showDialog<bool>(
context: context,
barrierColor: Constants.barrierColor.withOpacity(0.75),
builder: (ctx) => CustomDialog.alert(
title: "Change Password Failed",
body: reason,
buttonText: "Retry",
),
),
orElse: () {},
);
},
child: GestureDetector(
onTap: () => FocusScope.of(context).unfocus(),
child: ScrollableColumn(
children: [
//Input card
Form(
key: _formKey,
child: RoundedBottomContainer(
children: [
//Page name
Text(
"Your profile",
textAlign: TextAlign.center,
style: context.headline3.copyWith(fontSize: 22),
),

const SizedBox(height: 20),

//Password fields
ChangePasswordFields(
currentPasswordController: currentPasswordController,
newPasswordController: newPasswordController,
cNewPasswordController: cNewPasswordController,
),
],
),
),

const Spacer(),

//Save Password Button
Padding(
padding: const EdgeInsets.fromLTRB(20, 40, 20, Constants.bottomInsets),
child: SavePasswordButton(
onPressed: () {
if (_formKey.currentState!.validate()) {
_formKey.currentState!.save();
final _authProv = context.read(authProvider.notifier);
_authProv.changePassword(
newPassword: newPasswordController.text,
);
}
},
),
),

const SizedBox(height: 5),
],
),
),
),
);
}
}
9 changes: 4 additions & 5 deletions lib/views/screens/login_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,16 @@ class LoginScreen extends HookWidget {

//Login button
Padding(
padding: const EdgeInsets.fromLTRB(
20, 40, 20, Constants.bottomInsets),
padding: const EdgeInsets.fromLTRB(20, 40, 20, Constants.bottomInsets),
child: CustomTextButton.gradient(
width: double.infinity,
onPressed: () async {
if (formKey.currentState!.validate()) {
formKey.currentState!.save();
context.read(authProvider.notifier).login(
email: emailController.text,
password: passwordController.text,
);
email: emailController.text,
password: passwordController.text,
);
}
},
gradient: Constants.buttonGradientOrange,
Expand Down
2 changes: 1 addition & 1 deletion lib/views/screens/user_bookings_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class UserBookingsScreen extends StatelessWidget {

const SizedBox(width: 20),

//Movie Title
//Page Title
Expanded(
child: Text(
"Your bookings",
Expand Down
Loading

0 comments on commit 35b0cc7

Please sign in to comment.