File indexing completed on 2024-05-12 17:24:09

0001 // SPDX-FileCopyrightText: 2023 Carl Schwan <carl@carlschwan.eu>
0002 // SPDX-License-Identifier: LGPL-2.0-or-later
0003 
0004 #include "planteditor.h"
0005 #include "database.h"
0006 #include <QCoroTask>
0007 #include <QCoroFuture>
0008 
0009 Plant::Plant(QObject *parent)
0010     : QObject(parent)
0011 {}
0012 
0013 DB::Plant::Id Plant::plantId() const
0014 {
0015     return m_plantId;
0016 }
0017 
0018 void Plant::setPlantId(const DB::Plant::Id plantId)
0019 {
0020     if (m_plantId == plantId) {
0021         return;
0022     }
0023     if (m_plantId >= 0) {
0024         disconnect(&Database::instance(), &Database::plantChanged, this, nullptr);
0025     }
0026 
0027     m_plantId = plantId;
0028     refresh();
0029 
0030     if (m_plantId >= 0) {
0031         connect(&Database::instance(), &Database::plantChanged, this, [this](DB::Plant::Id plantId) {
0032             if (m_plantId == plantId) {
0033                 refresh();
0034             }
0035         });
0036     }
0037 
0038     Q_EMIT plantIdChanged();
0039 }
0040 
0041 void Plant::refresh()
0042 {
0043     if (m_plantId == -1) {
0044         return;
0045     }
0046 
0047     auto future = Database::instance().plant(m_plantId);
0048 
0049     QCoro::connect(std::move(future), this, [this](auto &&plant) {
0050         if (!plant.has_value()) {
0051             return;
0052         }
0053 
0054         m_name = plant->name;
0055         Q_EMIT nameChanged();
0056 
0057         m_species = plant->species;
0058         Q_EMIT speciesChanged();
0059 
0060         m_imgUrl = plant->img_url;
0061         Q_EMIT imgUrlChanged();
0062 
0063         m_waterIntervall = plant->water_intervall;
0064         Q_EMIT waterIntervallChanged();
0065 
0066         m_location = plant->location;
0067         Q_EMIT locationChanged();
0068 
0069         m_dateOfBirth = QDateTime::fromSecsSinceEpoch(plant->date_of_birth).date();
0070         Q_EMIT dateOfBirthChanged();
0071 
0072         m_lastWatered = QDateTime::fromSecsSinceEpoch(plant->last_watered).date();
0073         Q_EMIT lastWateredChanged();
0074 
0075         m_currentHealth = plant->current_health;
0076         Q_EMIT currentHealthChanged();
0077     });
0078 }
0079 
0080 int Plant::wantsToBeWateredIn() const
0081 {
0082     return QDate::currentDate().daysTo(m_lastWatered.addDays(m_waterIntervall));
0083 }
0084 
0085 PlantEditor::PlantEditor(QObject *parent)
0086     : QObject(parent)
0087     , m_plant(new Plant(this))
0088 {}
0089 
0090 DB::Plant::Id PlantEditor::plantId() const
0091 {
0092     return m_plantId;
0093 }
0094 
0095 void PlantEditor::setPlantId(const DB::Plant::Id plantId)
0096 {
0097     if (m_plantId == plantId) {
0098         return;
0099     }
0100     m_plantId = plantId;
0101     if (m_plantId >= 0) {
0102         m_plant->setPlantId(plantId);
0103     }
0104     Q_EMIT plantIdChanged();
0105 }
0106 
0107 Plant *PlantEditor::plant() const
0108 {
0109     return m_plant;
0110 }
0111 
0112 void PlantEditor::save()
0113 {
0114     if (m_mode == Creator) {
0115         m_plantsModel->addPlant(
0116             m_plant->m_name,
0117             m_plant->m_species,
0118             m_plant->m_imgUrl.toString(),
0119             m_plant->m_waterIntervall,
0120             m_plant->m_location,
0121             m_plant->m_dateOfBirth.startOfDay().toSecsSinceEpoch(),
0122             m_plant->m_currentHealth
0123         );
0124     } else {
0125         m_plantsModel->editPlant(
0126             m_plant->m_plantId,
0127             m_plant->m_name,
0128             m_plant->m_species,
0129             m_plant->m_imgUrl.toString(),
0130             m_plant->m_waterIntervall,
0131             m_plant->m_location,
0132             m_plant->m_dateOfBirth.startOfDay().toSecsSinceEpoch()
0133         );
0134     }
0135 }