padmanto
2 years ago
4 changed files with 177 additions and 3 deletions
@ -0,0 +1,75 @@
|
||||
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 mitraDeleteProvider = |
||||
StateNotifierProvider<MitraDeleteNotifier, MitraDeleteState>( |
||||
(ref) => MitraDeleteNotifier(ref: ref), |
||||
); |
||||
|
||||
class MitraDeleteNotifier extends StateNotifier<MitraDeleteState> { |
||||
final Ref ref; |
||||
CancelToken? cancelToken; |
||||
MitraDeleteNotifier({ |
||||
required this.ref, |
||||
}) : super(MitraDeleteStateInit()); |
||||
|
||||
void reset() { |
||||
state = MitraDeleteStateInit(); |
||||
} |
||||
|
||||
void delete({ |
||||
required String mitraID, |
||||
required String query, |
||||
}) async { |
||||
try { |
||||
state = MitraDeleteStateLoading(); |
||||
final dio = ref.read(dioProvider); |
||||
final localAuth = ref.read(localAuthProvider); |
||||
if (localAuth?.token == null) { |
||||
throw BaseRepositoryException(message: "Invalid Token"); |
||||
} |
||||
await MitraRepository(dio: dio).delete( |
||||
mitraID: mitraID, |
||||
token: localAuth!.token!, |
||||
); |
||||
state = MitraDeleteStateDone(); |
||||
ref.read(mitraSearchProvider.notifier).search(query: query); |
||||
} catch (e) { |
||||
if (e is BaseRepositoryException) { |
||||
state = MitraDeleteStateError(message: e.message); |
||||
} else { |
||||
state = MitraDeleteStateError(message: "Unknown Error "); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
abstract class MitraDeleteState extends Equatable { |
||||
final DateTime date; |
||||
MitraDeleteState() : date = DateTime.now(); |
||||
@override |
||||
List<Object?> get props => throw [date]; |
||||
} |
||||
|
||||
class MitraDeleteStateInit extends MitraDeleteState {} |
||||
|
||||
class MitraDeleteStateLoading extends MitraDeleteState {} |
||||
|
||||
class MitraDeleteStateError extends MitraDeleteState { |
||||
final String message; |
||||
|
||||
MitraDeleteStateError({ |
||||
required this.message, |
||||
}); |
||||
} |
||||
|
||||
class MitraDeleteStateDone extends MitraDeleteState { |
||||
MitraDeleteStateDone(); |
||||
} |
Loading…
Reference in new issue