mobile_skt/lib/screens/new_task/new_task_controller.dart

144 lines
4.3 KiB
Dart

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sk_base_mobile/db_helper/dbHelper.dart';
import 'package:sk_base_mobile/models/task_model.dart';
import 'package:sk_base_mobile/screens/home/home_controller.dart';
import 'package:sk_base_mobile/util/util.dart';
class NewTaskController extends GetxController {
DateTime? pickedDate;
final DbHelper db = DbHelper();
RxInt selectedImage = 0.obs;
RxBool lowPeriority = false.obs;
RxBool labelFocus = false.obs;
RxBool categoryFocus = false.obs;
RxBool descriptionFocus = false.obs;
RxString selectedDate = ''.obs;
RxString startTime = ''.obs;
RxString endTime = ''.obs;
RxBool loading = false.obs;
final homeController = Get.put(HomeController());
final label = TextEditingController().obs;
final description = TextEditingController().obs;
final category = TextEditingController().obs;
picStartTime(BuildContext context) async {
var picker =
await showTimePicker(context: context, initialTime: TimeOfDay.now());
if (picker != null) {
startTime.value =
'${DateUtil.addPrefix(picker.hourOfPeriod.toString())}:${DateUtil.addPrefix(picker.minute.toString())}:${picker.period.name.toUpperCase()}';
}
}
picEndTime(BuildContext context) async {
var picker =
await showTimePicker(context: context, initialTime: TimeOfDay.now());
if (picker != null) {
endTime.value =
'${DateUtil.addPrefix(picker.hourOfPeriod.toString())}:${DateUtil.addPrefix(picker.minute.toString())}:${picker.period.name.toUpperCase()}';
}
}
showDatePick(BuildContext context) async {
var picker = await showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime.now(),
lastDate: DateTime.now().add(const Duration(days: 7)));
if (picker != null) {
pickedDate = picker;
selectedDate.value =
'${DateUtil.addPrefix(picker.day.toString())}/${DateUtil.addPrefix(picker.month.toString())}/${picker.year}';
}
}
changeImage(int index) {
selectedImage.value = index;
}
setLabelFocus() {
labelFocus.value = true;
categoryFocus.value = false;
descriptionFocus.value = false;
}
setCategoryFocus() {
labelFocus.value = false;
categoryFocus.value = true;
descriptionFocus.value = false;
}
setDescriptionFocus() {
labelFocus.value = false;
categoryFocus.value = false;
descriptionFocus.value = true;
}
onTapOutside() {
labelFocus.value = false;
categoryFocus.value = false;
descriptionFocus.value = false;
}
insertTask(BuildContext context) {
if (label.value.text.toString().isEmpty) {
SnackBarUtil().warning(
'Warning',
message: 'Enter valid label',
);
return;
}
if (category.value.text.toString().isEmpty) {
SnackBarUtil().warning(
'Warning',
message: 'Enter Correct Category',
);
return;
}
if (selectedDate.isEmpty) {
// selectedDate.value =
// '${Utils.addPrefix(DateTime.now().day.toString())}/${Utils.addPrefix(DateTime.now().month.toString())}/${Utils.addPrefix(DateTime.now().year.toString())}';
showDatePick(context);
}
if (startTime.isEmpty) {
picStartTime(context);
return;
}
if (endTime.isEmpty) {
picEndTime(context);
return;
}
loading.value = true;
db
.insert(TaskModel(
key: DateTime.now().microsecondsSinceEpoch.toString(),
startTime: startTime.value,
endTime: endTime.value,
date: selectedDate.value,
periority: lowPeriority.value ? 'Low' : 'High',
description: description.value.text.toString(),
category: category.value.text.toString(),
title: label.value.text.toString(),
image: selectedImage.value.toString(),
show: 'yes',
status: 'unComplete'))
.then((value) {
Duration dif = pickedDate!.difference(DateTime(
DateTime.now().year, DateTime.now().month, DateTime.now().day));
homeController.list[dif.inDays].add(value);
Timer(const Duration(seconds: 1), () {
loading.value = false;
Get.back();
SnackBarUtil().success(
'Successful',
message: 'Task is created',
);
});
});
}
}