Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added the cubit and fake repo for change phone number process #75

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/core/res/strings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ class S {
static const String networkException =
"Request couldn't be completed. Make sure the device is online";
static const String unknownException = "Something went wrong. Please try again later.";
static const String wrongOtp = "Wrong OTP entered";
}
90 changes: 90 additions & 0 deletions lib/screens/change_number/change_number.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import 'package:ecellapp/screens/change_number/cubit/change_number_cubit.dart';
import 'package:ecellapp/screens/change_number/widgets/mobile_number_field.dart';
import 'package:ecellapp/screens/change_number/widgets/otp_field.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class ChangeNumberScreen extends StatelessWidget {
final TextEditingController mobileController = TextEditingController();
final TextEditingController otpController = TextEditingController();

@override
Widget build(BuildContext context) {
return Scaffold(
body: BlocConsumer<ChangeNumberCubit, ChangeNumberState>(
listener: (context, state) {
if (state is ChangeNumberError) {
Scaffold.of(context).showSnackBar(
SnackBar(content: Text(state.error)),
);
} else if (state is ChangeNumberWrongOtp) {
Scaffold.of(context).showSnackBar(
SnackBar(content: Text(state.error)),
);
} else if (state is ChangeNumberOtpSuccessful) {
Scaffold.of(context).showSnackBar(
SnackBar(content: Text("Successful")),
);
}
},
builder: (context, state) {
if (state is ChangeNumberInitial) {
return _initial(context);
} else if (state is ChangeNumberOTP) {
return _enterOtp(context);
} else if (state is ChangeNumberOtpSuccessful) {
///
return _success();
} else {
return _initial(context);
}
},
),
);
}

Widget _initial(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(50.0),
child: Column(
children: [
MobileNumberField(mobileController),
FlatButton(
onPressed: () {
final cubit = context.read<ChangeNumberCubit>();
cubit.getOtp(mobileController.text);
},
child: Text("Get otp")),
],
),
);
}

Widget _enterOtp(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(50.0),
child: Column(
children: [
OtpField(otpController),
FlatButton(
onPressed: () {
final cubit = context.read<ChangeNumberCubit>();
cubit.verifyOtp(otpController.text);
},
child: Text("verify"),
),
],
),
);
}

Widget _success() {
return Padding(
padding: const EdgeInsets.all(50.0),
child: Column(
children: [Text("Success")],
),
);
}
}
33 changes: 33 additions & 0 deletions lib/screens/change_number/change_number_repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import 'dart:math';

import 'package:ecellapp/core/res/errors.dart';

abstract class ChangeNumberRepository {
Future<void> getOtp(String number);
}

class FakeChangeNumberRepository extends ChangeNumberRepository {
String generated;

@override
Future<void> getOtp(String number) async {
await Future.delayed(Duration(seconds: 1));
if (Random().nextBool()) {
//print("Fail");
// Represents a network failure
throw NetworkException();
} else {
generated = '1234';
// returns a dummy otp
}
}

Future<int> verifyOtp(String sent) async {
await Future.delayed(Duration(seconds: 1));
if (sent == generated) {
return 200;
} else {
throw ValidationException("Wrong OTP has been entered");
}
}
}
33 changes: 33 additions & 0 deletions lib/screens/change_number/cubit/change_number_cubit.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import 'package:bloc/bloc.dart';
import 'package:ecellapp/core/res/errors.dart';
import 'package:ecellapp/screens/change_number/change_number_repository.dart';
import 'package:meta/meta.dart';
import 'package:ecellapp/core/res/strings.dart';

part 'change_number_state.dart';

class ChangeNumberCubit extends Cubit<ChangeNumberState> {
FakeChangeNumberRepository _changeNumberRepository = FakeChangeNumberRepository();

ChangeNumberCubit() : super(ChangeNumberInitial());

Future<void> getOtp(String number) async {
try {
await _changeNumberRepository.getOtp(number);
emit(ChangeNumberOTP());
} on NetworkException {
emit(ChangeNumberError(S.unknownException));
}
}

Future<void> verifyOtp(String otp) async {
try {
int x = await _changeNumberRepository.verifyOtp(otp);
if (x == 200) {
emit(ChangeNumberOtpSuccessful());
}
} on ValidationException {
emit(ChangeNumberError(S.wrongOtp));
}
}
}
24 changes: 24 additions & 0 deletions lib/screens/change_number/cubit/change_number_state.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
part of 'change_number_cubit.dart';

@immutable
abstract class ChangeNumberState {
const ChangeNumberState();
}

class ChangeNumberInitial extends ChangeNumberState {}

class ChangeNumberOTP extends ChangeNumberState {}

class ChangeNumberError extends ChangeNumberState {
final String error;

ChangeNumberError(this.error);
}

class ChangeNumberWrongOtp extends ChangeNumberState {
final String error;

ChangeNumberWrongOtp(this.error);
}

class ChangeNumberOtpSuccessful extends ChangeNumberState {}
22 changes: 22 additions & 0 deletions lib/screens/change_number/widgets/mobile_number_field.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'package:flutter/material.dart';

class MobileNumberField extends StatelessWidget {
const MobileNumberField(this.controller);

final TextEditingController controller;

@override
Widget build(BuildContext context) {
return TextFormField(
controller: controller,
keyboardType: TextInputType.number,
decoration: InputDecoration(
suffixText: '*',
suffixStyle: TextStyle(color: Colors.red, fontSize: 20),
prefixIcon: Icon(Icons.phone),
border: OutlineInputBorder(),
labelText: "Mobile Number",
),
);
}
}
15 changes: 15 additions & 0 deletions lib/screens/change_number/widgets/otp_field.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class OtpField extends StatelessWidget {
final TextEditingController controller;

OtpField(this.controller);

@override
Widget build(BuildContext context) {
return TextField(
controller: controller,
);
}
}
3 changes: 3 additions & 0 deletions lib/screens/signup/cubit/signup_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ part of 'signup_cubit.dart';
@immutable
abstract class SignupState extends Equatable {
const SignupState();

@override
List<Object> get props => [];
}
Expand All @@ -21,7 +22,9 @@ class SignupSuccess extends SignupState {

class SignupError extends SignupState {
final String message;

const SignupError(this.message);

@override
List<Object> get props => [message];
}