Skip to content

Commit

Permalink
Merge pull request #6 from aeri/f-droid
Browse files Browse the repository at this point in the history
FOSS transition
  • Loading branch information
NAVAL committed Aug 13, 2023
2 parents b81a665 + e71544b commit a99a1eb
Show file tree
Hide file tree
Showing 21 changed files with 1,135 additions and 538 deletions.
1 change: 1 addition & 0 deletions .flutter
Submodule .flutter added at 4d9e56
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
.dart_tool/
.packages
build/
# If you're building an application, you may want to check-in your pubspec.lock
pubspec.lock

# Directory created by dartdoc
# If you don't generate documentation locally you can remove this line.
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule ".flutter"]
path = .flutter
url = https://github.com/flutter/flutter
2 changes: 1 addition & 1 deletion android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ android {

defaultConfig {
applicationId "cat.naval.florae"
minSdkVersion 21
minSdkVersion 16
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
Expand Down
2 changes: 2 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ allprojects {
repositories {
google()
mavenCentral()
// for FDroid
mavenLocal()
maven {
// [required] background_fetch
url "${project(':background_fetch').projectDir}/libs"
Expand Down
56 changes: 0 additions & 56 deletions lib/data/box.dart

This file was deleted.

21 changes: 21 additions & 0 deletions lib/data/care.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'package:json_annotation/json_annotation.dart';

part 'care.g.dart';

@JsonSerializable()
class Care {
int id = 0;
String name;
int cycles = 0;
DateTime? effected;

Care(
{required this.name,
required this.cycles,
required this.effected,
required this.id});

factory Care.fromJson(Map<String, dynamic> json) => _$CareFromJson(json);

Map<String, dynamic> toJson() => _$CareToJson(this);
}
23 changes: 23 additions & 0 deletions lib/data/care.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 83 additions & 0 deletions lib/data/garden.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import 'dart:convert';
import 'package:florae/data/plant.dart';
import 'package:shared_preferences/shared_preferences.dart';

class Garden {
late final SharedPreferences store;

Garden(this.store);

static Future<Garden> load() async {
var store = await SharedPreferences.getInstance();

await store.reload();

return (Garden(store));
}

Future<List<Plant>> getAllPlants() async {
List<Plant> allPlants = [];
var rawPlants = store.getString("plants");
if (rawPlants != null) {
Iterable l = json.decode(rawPlants);
allPlants = List<Plant>.from(l.map((model) => Plant.fromJson(model)));
}
return allPlants;
}

// Returns true if update
// Return false if create
Future<bool> addOrUpdatePlant(Plant plant) async {
List<Plant> allPlants = await getAllPlants();
bool status;

var plantIndex =
allPlants.indexWhere((element) => element.name == plant.name);
if (plantIndex == -1) {
allPlants.add(plant);
status = false;
} else {
allPlants[allPlants.indexWhere((element) => element.name == plant.name)] =
plant;
status = true;
}
String jsonPlants = jsonEncode(allPlants);
await store.setString("plants", jsonPlants);

return status;
}

Future<bool> deletePlant(Plant plant) async {
List<Plant> allPlants = await getAllPlants();

var plantIndex =
allPlants.indexWhere((element) => element.name == plant.name);
if (plantIndex != -1) {
allPlants.removeAt(plantIndex);

String jsonPlants = jsonEncode(allPlants);
await store.setString("plants", jsonPlants);

return true;
} else {
return false;
}
}

Future<bool> updatePlant(Plant plant) async {
List<Plant> allPlants = await getAllPlants();

var plantIndex =
allPlants.indexWhere((element) => element.name == plant.name);
if (plantIndex != -1) {
allPlants[allPlants.indexWhere((element) => element.name == plant.name)] =
plant;

String jsonPlants = jsonEncode(allPlants);
await store.setString("plants", jsonPlants);
return true;
} else {
return false;
}
}
}
45 changes: 9 additions & 36 deletions lib/data/plant.dart
Original file line number Diff line number Diff line change
@@ -1,55 +1,28 @@
import 'package:objectbox/objectbox.dart';
import 'package:json_annotation/json_annotation.dart';
import 'care.dart';

@Entity()
class Care {
int id = 0;
String name;
int cycles = 0;
@Property(type: PropertyType.date)
DateTime? effected;

Care({required this.name, required this.cycles, required this.effected});

factory Care.fromJson(Map<String, dynamic> json) => Care(
cycles: json["cycles"], effected: json["effected"], name: json["name"]);
part 'plant.g.dart';

Map<String, dynamic> toJson() =>
{"cycles": cycles, "effected": effected, "name": name};
}

@Entity()
@JsonSerializable(explicitToJson: true)
class Plant {
int id = 0;
String name;
String? location;
String description;
@Property(type: PropertyType.date)
DateTime createdAt;
String? picture;

final cares = ToMany<Care>();
List<Care> cares = [];

Plant(
{required this.name,
this.id = 0,
this.location,
this.description = "",
required this.createdAt,
this.picture});
this.picture,
required this.cares});

factory Plant.fromJson(Map<String, dynamic> json) => Plant(
name: json["name"],
location: json["location"],
description: json["description"],
createdAt: json["createdAt"],
picture: json["picture"],
);
factory Plant.fromJson(Map<String, dynamic> json) => _$PlantFromJson(json);

Map<String, dynamic> toJson() => {
"name": name,
"location": location,
"description": description,
"createdAt": createdAt,
"picture": picture,
};
Map<String, dynamic> toJson() => _$PlantToJson(this);
}
29 changes: 29 additions & 0 deletions lib/data/plant.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 8 additions & 9 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import 'dart:io';

import 'package:background_fetch/background_fetch.dart';
import 'package:florae/screens/error.dart';
import 'package:flutter/material.dart';
import 'data/box.dart';
import 'data/care.dart';
import 'data/plant.dart';
import 'data/garden.dart';
import 'screens/home_page.dart';
import 'package:florae/notifications.dart' as notify;
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:shared_preferences/shared_preferences.dart';

late ObjectBox objectbox;
late Garden garden;

Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();

objectbox = await ObjectBox.create();
garden = await Garden.load();

// Set default locale for background service
final prefs = await SharedPreferences.getInstance();
Expand All @@ -41,9 +41,10 @@ void backgroundFetchHeadlessTask(HeadlessTask task) async {

print("[BackgroundFetch] Headless event received: $taskId");

ObjectBox obx = await ObjectBox.create();
Garden gr = await Garden.load();

List<Plant> allPlants = await gr.getAllPlants();

List<Plant> allPlants = obx.plantBox.getAll();
List<String> plants = [];
String notificationTitle = "Plants require care";

Expand All @@ -60,8 +61,6 @@ void backgroundFetchHeadlessTask(HeadlessTask task) async {
}
}

obx.store.close();

try {
final prefs = await SharedPreferences.getInstance();

Expand Down Expand Up @@ -109,7 +108,7 @@ class FloraeApp extends StatelessWidget {
};

return widget!;
},
},
supportedLocales: const [
Locale('en'), // English
Locale('es'), // Spanish
Expand Down
Loading

0 comments on commit a99a1eb

Please sign in to comment.