One UI in flutter
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.3 KiB

2 years ago
import 'package:dio/dio.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../provider/dio_provider.dart';
import '../../provider/local_auth_provider.dart';
import '../../repository/base_repository.dart';
import '../../repository/mitra_repository.dart';
import '../../screen/md_lab_mitra/mitra_search_provider.dart';
final mitraEditProvider =
StateNotifierProvider<MitraEditNotifier, MitraEditState>(
(ref) => MitraEditNotifier(ref: ref),
);
class MitraEditNotifier extends StateNotifier<MitraEditState> {
final Ref ref;
CancelToken? cancelToken;
MitraEditNotifier({
required this.ref,
}) : super(MitraEditStateInit());
void reset() {
state = MitraEditStateInit();
}
void edit({
required String mitraID,
required String companyID,
required List<String> mouID,
required String doctorID,
required String doctorAddressID,
required String login,
required String password,
required String query,
}) async {
try {
state = MitraEditStateLoading();
final dio = ref.read(dioProvider);
final localAuth = ref.read(localAuthProvider);
if (localAuth?.token == null) {
throw BaseRepositoryException(message: "Invalid Token");
}
await MitraRepository(dio: dio).edit(
mitraID: mitraID,
token: localAuth!.token!,
mouID: mouID,
companyID: companyID,
doctorID: doctorID,
doctorAddressID: doctorAddressID,
login: login,
password: password,
);
state = MitraEditStateDone();
ref.read(mitraSearchProvider.notifier).search(query: query);
} catch (e) {
if (e is BaseRepositoryException) {
state = MitraEditStateError(message: e.message);
} else {
state = MitraEditStateError(message: "Unknown Error ");
}
}
}
}
abstract class MitraEditState extends Equatable {
final DateTime date;
MitraEditState() : date = DateTime.now();
@override
List<Object?> get props => throw [date];
}
class MitraEditStateInit extends MitraEditState {}
class MitraEditStateLoading extends MitraEditState {}
class MitraEditStateError extends MitraEditState {
final String message;
MitraEditStateError({
required this.message,
});
}
class MitraEditStateDone extends MitraEditState {
MitraEditStateDone();
}