Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Up to date #2

Open
wants to merge 16 commits into
base: CollAndCalendarBranch
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
# plantapp

VUT FIT - ITU project: plant care
VUT FIT - ITU project: plant care

## Inštalácia

1. Stiahnite si aplikáciu (.apk) zo zdieľaného priečinka: https://drive.google.com/drive/folders/1sQ4Ks0TKdQL-SqnuJswaQkFLdi0TJEVA?usp=sharing

2. Presuňte aplikáciu do android telefónu

3. V telefóne nájdite a spustite .apk súbor, aplikácia sa nainštaluje

## Závislosti

cupertino_icons: https://pub.dev/packages/cupertino_icons/license
google_fonts: https://fonts.google.com/ (Free, Open Source Software)
path_provider: https://pub.dev/packages/path_provider/license
table_calendar: https://pub.dev/packages/table_calendar/license
image_picker: https://pub.dev/packages/image_picker/license
15 changes: 5 additions & 10 deletions assets/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,34 @@
"databaseId" : 1,
"nickname" : "Sal",
"needWater" : 1,
"needFertilizer" : 0,
"date" : "2023-12-17"
"needFertilizer" : 0
},

{
"databaseId" : 6,
"nickname" : "Joey",
"needWater" : 1,
"needFertilizer": 0,
"date" : "2023-12-17"
"needFertilizer": 0
},

{
"databaseId" : 7,
"nickname" : "Eldrani, the Chosen One",
"needWater" : 0,
"needFertilizer": 1,
"date" : "2023-12-17"
"needFertilizer": 1
},

{
"databaseId" : 8,
"nickname" : "Jenny",
"needWater" : 1,
"needFertilizer": 0,
"date" : "2023-12-17"
"needFertilizer": 0
},

{
"databaseId" : 8,
"nickname" : "Jenny",
"needWater" : 0,
"needFertilizer": 1,
"date" : "2023-12-17"
"needFertilizer": 1
}
]
2 changes: 1 addition & 1 deletion assets/userCollections.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"collName" : "_DEFAULT_",
"collName" : "My Plants",
"plantIds" : [1],
"plantNames" : ["Sal"]
},
Expand Down
5 changes: 5 additions & 0 deletions lib/controller/achievements_controller.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
Author: Timotej Halenár
Description: Used to retrieve, modify and store achievement data
*/

// ignore_for_file: avoid_print

import 'dart:io';
Expand Down
47 changes: 44 additions & 3 deletions lib/controller/calendar_controller.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'dart:convert';
import 'dart:io';
import 'package:flutter/services.dart' show rootBundle;
import 'package:path_provider/path_provider.dart';
import 'package:plantapp/model/task_model.dart';

/// Example event class.
Expand Down Expand Up @@ -27,8 +29,36 @@ Future<List<Event>> loadTasksFromJson() async {
String jsonString = await rootBundle.loadString('assets/tasks.json');
final List<dynamic> parsedJson = json.decode(jsonString);

final List<Task> plants = parsedJson.map((eventJson) => Task.fromJson(eventJson)).toList();

final List<Task> plants =
parsedJson.map((eventJson) => Task.fromJson(eventJson)).toList();

List<Event> tasks = [];

for (final plant in plants) {
if (plant.needWater == 1) {
tasks.add(Event("${plant.nickname} | Needs Watering", plant.date));
} else {
tasks.add(Event("${plant.nickname} | Needs Fertilizing", plant.date));
}
}

return tasks;
} catch (e) {
// ignore: avoid_print
print('Error loading tasks from JSON: $e');
return [];
}
}

Future<List<Event>> loadTasksFromFile() async {
try {
final file = await _localFile;
String jsonString = await file.readAsString();
final List<dynamic> parsedJson = json.decode(jsonString);

final List<Task> plants =
parsedJson.map((eventJson) => Task.fromJson(eventJson)).toList();

List<Event> tasks = [];

for (final plant in plants) {
Expand All @@ -47,6 +77,17 @@ Future<List<Event>> loadTasksFromJson() async {
}
}

Future<File> get _localFile async {
final path = await _localPath;
return File('$path/tasks.json');
}

Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();

return directory.path;
}

final kToday = DateTime.now();
final kFirstDay = DateTime(kToday.year, kToday.month - 3, kToday.day);
final kLastDay = DateTime(kToday.year, kToday.month + 3, kToday.day);
final kLastDay = DateTime(kToday.year, kToday.month + 3, kToday.day);
132 changes: 81 additions & 51 deletions lib/controller/coll_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
It includes methods for loading plant data from an asset, saving new plants, updating existing plants,
retrieving a specific plant, deleting a plant, and performing calculations related to plant actions.
The class uses the PlantController and TaskController classes for additional functionality.

Co-Author: Timotej Halenár
Contribution: Added task scheduling functionality.
*/

// ignore_for_file: avoid_print
Expand All @@ -16,7 +19,7 @@ import 'package:plantapp/model/coll_model.dart';
import 'package:plantapp/model/task_model.dart';
import 'task_controller.dart';
import 'plant_controller.dart';

import 'usercoll_controller.dart';

class CollController {
List<Collection> _collection = [];
Expand All @@ -35,61 +38,66 @@ class CollController {
}

Future<void> savePlant(Collection newPlant) async {
await loadPlantsFromFile();
_collection.add(newPlant);
print("databaseId: ${newPlant.databaseId}");
await loadPlantsFromFile();
_collection.add(newPlant);
print("databaseId: ${newPlant.databaseId}");

Directory appDocumentsDirectory = await getApplicationDocumentsDirectory();
String imageDirectory = "${appDocumentsDirectory.path}/images";
Directory appDocumentsDirectory = await getApplicationDocumentsDirectory();
String imageDirectory = "${appDocumentsDirectory.path}/images";

// Create the directory if it doesn't exist
await Directory(imageDirectory).create(recursive: true);
// Create the directory if it doesn't exist
await Directory(imageDirectory).create(recursive: true);

String imagePath = "$imageDirectory/${newPlant.databaseId}.jpg";
String imagePath = "$imageDirectory/${newPlant.databaseId}.jpg";

// Save the image file
if (newPlant.imageFile != null) {
// Save the image file to the specified path
await newPlant.imageFile!.copy(imagePath);
newPlant.photo = imagePath;
}
// Save the image file
if (newPlant.imageFile != null) {
// Save the image file to the specified path
await newPlant.imageFile!.copy(imagePath);
newPlant.photo = imagePath;
}

await _writeToFile(_collection);

TaskController taskController = TaskController();
await taskController.loadTasksFromFile();

PlantController plantController = PlantController();
await plantController.loadPlantsFromAsset();
int waterInterval =
plantController.plants[newPlant.databaseId - 1].wateringPeriod;
int fertilizeInterval =
plantController.plants[newPlant.databaseId - 1].fertilizingPeriod;

DateTime waterDate =
newPlant.lastWatered.add(Duration(days: waterInterval));
DateTime fertilizeDate =
newPlant.lastFertilized.add(Duration(days: fertilizeInterval));

Task waterTask = Task(
databaseId: newPlant.databaseId,
nickname: newPlant.nickname,
needWater: 1,
needFertilizer: 0,
date: waterDate);

Task fertilizeTask = Task(
databaseId: newPlant.databaseId,
nickname: newPlant.nickname,
needWater: 0,
needFertilizer: 1,
date: fertilizeDate);

taskController.tasks.add(waterTask);
taskController.tasks.add(fertilizeTask);
String encoded = jsonEncode(taskController.tasks);
taskController.writeTasks(encoded);
}
await _writeToFile(_collection);

TaskController taskController = TaskController();
await taskController.loadTasksFromFile();

PlantController plantController = PlantController();
await plantController.loadPlantsFromAsset();
int waterInterval =
plantController.plants[newPlant.databaseId - 1].wateringPeriod;
int fertilizeInterval =
plantController.plants[newPlant.databaseId - 1].fertilizingPeriod;

DateTime waterDate =
newPlant.lastWatered.add(Duration(days: waterInterval));
DateTime fertilizeDate =
newPlant.lastFertilized.add(Duration(days: fertilizeInterval));

Task waterTask = Task(
databaseId: newPlant.databaseId,
nickname: newPlant.nickname,
needWater: 1,
needFertilizer: 0,
date: waterDate);

Task fertilizeTask = Task(
databaseId: newPlant.databaseId,
nickname: newPlant.nickname,
needWater: 0,
needFertilizer: 1,
date: fertilizeDate);

taskController.tasks.add(waterTask);
taskController.tasks.add(fertilizeTask);
String encoded = jsonEncode(taskController.tasks);
taskController.writeTasks(encoded);

UserCollController userCollController = UserCollController();
await userCollController.loadCollectionsFromFile();

userCollController.addPlantToCollectionByPlant(newPlant);
}

Future<void> _writeToFile(List<Collection> plants) async {
try {
Expand Down Expand Up @@ -151,6 +159,28 @@ class CollController {
_writeToFile(_collection);
}

Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();

return directory.path;
}

Future<File> get _localFile async {
final path = await _localPath;
return File('$path/plantColl.json');
}

void seedIfNoFileExists() async {
final file = await _localFile;
bool fileExists = await file.exists();
if (fileExists) {
print('file do be existin doe');
} else {
print('file aint there brudda');
seedFile();
}
}

int calculateDaysUntilNextAction(DateTime lastDate, int period) {
// Calculate the next watering date
DateTime nextDate = lastDate.add(Duration(days: period));
Expand Down
32 changes: 0 additions & 32 deletions lib/controller/collection_controller.dart

This file was deleted.

5 changes: 5 additions & 0 deletions lib/controller/task_controller.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
Author: Timotej Halenár
Description: Used to retrieve, modify and store task data,
provides converter functions
*/
// ignore_for_file: avoid_print, curly_braces_in_flow_control_structures

import 'dart:convert';
Expand Down
Loading