first commit
This commit is contained in:
63
lib/ui/views/action_dialog/action_dialog_view.dart
Normal file
63
lib/ui/views/action_dialog/action_dialog_view.dart
Normal file
@ -0,0 +1,63 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:kamus_kesehatan/model/istilah_model.dart';
|
||||
import 'package:stacked/stacked.dart';
|
||||
import 'package:stacked_services/stacked_services.dart';
|
||||
|
||||
import './action_dialog_view_model.dart';
|
||||
|
||||
class ActionDialogView extends StatelessWidget {
|
||||
final DialogRequest<IstilahModel> request;
|
||||
final Function(DialogResponse) completer;
|
||||
|
||||
const ActionDialogView({
|
||||
Key? key,
|
||||
required this.request,
|
||||
required this.completer,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ViewModelBuilder<ActionDialogViewModel>.reactive(
|
||||
viewModelBuilder: () => ActionDialogViewModel(),
|
||||
onViewModelReady: (ActionDialogViewModel model) async {
|
||||
await model.init(request.data);
|
||||
},
|
||||
builder: (
|
||||
BuildContext context,
|
||||
ActionDialogViewModel model,
|
||||
Widget? child,
|
||||
) {
|
||||
return Dialog(
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(20),
|
||||
// create a row with 2 circle icon , 1 is whataspp icon, 2 is bookmark, no need text
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
||||
children: [
|
||||
CircleAvatar(
|
||||
backgroundColor: Colors.green,
|
||||
child: IconButton(
|
||||
icon: const Icon(Icons.phone),
|
||||
onPressed: () {
|
||||
// completer(DialogResponse(confirmed: true));
|
||||
model.openWhatsapp();
|
||||
},
|
||||
),
|
||||
),
|
||||
CircleAvatar(
|
||||
backgroundColor: Colors.blue,
|
||||
child: IconButton(
|
||||
icon: const Icon(Icons.bookmark),
|
||||
onPressed: () {
|
||||
model.addBookmark(request.data!);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
90
lib/ui/views/action_dialog/action_dialog_view_model.dart
Normal file
90
lib/ui/views/action_dialog/action_dialog_view_model.dart
Normal file
@ -0,0 +1,90 @@
|
||||
import '../../../app/app.dialogs.dart';
|
||||
import '../../../app/app.locator.dart';
|
||||
import '../../../app/app.logger.dart';
|
||||
import '../../../app/core/custom_base_view_model.dart';
|
||||
import '../../../model/istilah_model.dart';
|
||||
import '../../../services/my_storage.dart';
|
||||
|
||||
class ActionDialogViewModel extends CustomBaseViewModel {
|
||||
final log = getLogger('ActionDialogViewModel');
|
||||
final myStorage = locator<MyStorage>();
|
||||
IstilahModel? data;
|
||||
|
||||
Future<void> init(IstilahModel? data) async {
|
||||
// log.i('init');
|
||||
// log.i(data!.istilah.toString());
|
||||
// log.i(data.arti.toString());
|
||||
this.data = data;
|
||||
// await myStorage.clear();
|
||||
}
|
||||
|
||||
addBookmark(IstilahModel istilahModel) async {
|
||||
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,
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user