kamus-kesehatan/lib/ui/views/action_dialog/action_dialog_view_model.dart

91 lines
2.4 KiB
Dart
Raw Permalink Normal View History

2024-02-04 12:57:23 +00:00
import '../../../app/app.dialogs.dart';
import '../../../app/app.locator.dart';
import '../../../app/app.logger.dart';
import '../../../app/core/custom_base_view_model.dart';
2024-07-27 12:06:56 +00:00
import '../../../model/istilah_model1.dart';
2024-02-04 12:57:23 +00:00
import '../../../services/my_storage.dart';
class ActionDialogViewModel extends CustomBaseViewModel {
final log = getLogger('ActionDialogViewModel');
final myStorage = locator<MyStorage>();
2024-07-27 12:06:56 +00:00
IstilahModel1? data;
2024-02-04 12:57:23 +00:00
2024-07-27 12:06:56 +00:00
Future<void> init(IstilahModel1? data) async {
2024-02-04 12:57:23 +00:00
// log.i('init');
// log.i(data!.istilah.toString());
// log.i(data.arti.toString());
this.data = data;
// await myStorage.clear();
}
2024-07-27 12:06:56 +00:00
addBookmark(IstilahModel1 istilahModel) async {
2024-02-04 12:57:23 +00:00
List<dynamic>? listBookmark;
listBookmark = await myStorage.read('listBookmark');
listBookmark ??= [];
log.i('ini panjang listBookmark ${listBookmark.length}');
log.i('ini listBookmark $listBookmark');
// check if istilahModel is already in listBookmark
bool isExist = false;
for (var item in listBookmark) {
if (item['istilah'] == istilahModel.istilah) {
isExist = true;
break;
}
}
if (isExist) {
snackbarService.showSnackbar(
message: 'Bookmark sudah ada',
duration: const Duration(seconds: 2),
);
return;
}
listBookmark.add({
'istilah': istilahModel.istilah,
'arti': istilahModel.arti,
});
await myStorage.write('listBookmark', listBookmark);
snackbarService.showSnackbar(
message: 'Berhasil menambahkan bookmark',
duration: const Duration(seconds: 2),
);
// // check if istilahModel is already in listBookmark
// bool isExist = false;
// for (var item in listBookmark) {
// if (item.istilah == istilahModel.istilah) {
// isExist = true;
// break;
// }
// }
// if (!isExist) {
// listBookmark.add(istilahModel);
// await myStorage.write('listBookmark', listBookmark);
// snackbarService.showSnackbar(
// message: 'Berhasil menambahkan bookmark',
// duration: const Duration(seconds: 2),
// );
// } else {
// snackbarService.showSnackbar(
// message: 'Bookmark sudah ada',
// duration: const Duration(seconds: 2),
// );
// }
}
openWhatsapp() async {
await dialogService.showCustomDialog(
variant: DialogType.nomorTelponDialogView,
data: data,
);
}
}