Skip to content

Commit

Permalink
Merge pull request #3 from farmassistX/master_0.1
Browse files Browse the repository at this point in the history
Master 0.1
  • Loading branch information
DanielChang98 authored Jan 10, 2021
2 parents cf1808e + b5cb3e3 commit 91414fe
Show file tree
Hide file tree
Showing 46 changed files with 2,761 additions and 103 deletions.
Binary file added assets/images/harvesting_create.jfif
Binary file not shown.
Binary file added assets/images/harvesting_create.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/harvesting_view.jfif
Binary file not shown.
Binary file added assets/images/planting_create.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/planting_view.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added fonts/weathericons-regular-webfont.ttf
Binary file not shown.
Binary file added fonts/weathericons-regular.ttf
Binary file not shown.
10 changes: 9 additions & 1 deletion lib/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import 'package:farmassist/ui/login/login_page.dart';
import 'package:farmassist/ui/splash_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:provider/provider.dart';

import 'data/farm/view_model/cityEntryViewModel.dart';
import 'data/farm/view_model/weather_app_forecast_viewmodel.dart';

class App extends StatelessWidget {
App({Key key}) : super(key: key);
Expand All @@ -19,7 +23,7 @@ class App extends StatelessWidget {

@override
Widget build(BuildContext context) {
return MultiBlocProvider(
return MultiProvider(
providers: [
BlocProvider<AuthenticationBloc>(
create: (_) => AuthenticationBloc(
Expand All @@ -30,6 +34,10 @@ class App extends StatelessWidget {
BlocProvider<DetailBloc>(
create: (_) => DetailBloc(null),
),
ChangeNotifierProvider<ForecastViewModel>(
create: (_) => ForecastViewModel()),
ChangeNotifierProvider<CityEntryViewModel>(
create: (_) => CityEntryViewModel()),
],
child: MultiRepositoryProvider(
providers: [
Expand Down
1 change: 1 addition & 0 deletions lib/app_theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class AppTheme {
static const Color background = Color(0xFFEEEFF4);
static const Color nearlyDarkGreen = Color(0xFF2EC821);
static const Color nearlyGreen = Color(0xFF24D900);
static const Color pastelGreen = Color(0xFF10d180);

static const Color notWhite = Color(0xFFEDF0F2);
static const Color nearlyWhite = Color(0xFFFEFEFE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,24 @@ class AuthenticationRepository {
'email': email,
'displayName': name,
});
await FirebaseFirestore.instance.collection('Planting').doc(user.uid).set({
'id': user.uid,
});
await FirebaseFirestore.instance.collection('Harvesting').doc(user.uid).set({
'id': user.uid,
});
await FirebaseFirestore.instance.collection('Planting').doc(user.uid).collection("January").doc().set({
'month': 1,
});
await FirebaseFirestore.instance.collection('Planting').doc(user.uid).collection("February").doc().set({
'month': 2,
});
await FirebaseFirestore.instance.collection('Harvesting').doc(user.uid).collection("January").doc().set({
'month': 1,
});
await FirebaseFirestore.instance.collection('Harvesting').doc(user.uid).collection("February").doc().set({
'month': 2,
});
} on Exception {
throw SignUpFailure();
}
Expand Down
42 changes: 42 additions & 0 deletions lib/data/farm/api/openweathermap_weather_api.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'dart:convert';
import 'package:farmassist/data/farm/services/weather_api.dart';
import 'package:http/http.dart' as http;
import 'package:farmassist/data/farm/models/Forecast.dart';
import 'dart:developer';

import 'package:farmassist/data/farm/models/Location.dart';

class OpenWeatherMapWeatherApi extends WeatherApi {
static const endPointUrl = 'https://api.openweathermap.org/data/2.5';
static const apiKey = "92ef38427d2c28239bb706af37d77737";
http.Client httpClient;

OpenWeatherMapWeatherApi() {
this.httpClient = new http.Client();
}

Future<Location> getLocation(String city) async {
final requestUrl = '$endPointUrl/weather?q=$city&APPID=$apiKey';
final response = await this.httpClient.get(Uri.encodeFull(requestUrl));

if (response.statusCode != 200) {
throw Exception(
'error retrieving location for city $city: ${response.statusCode}');
}

return Location.fromJson(jsonDecode(response.body));
}

@override
Future<Forecast> getWeather(Location location) async {
final requestUrl =
'$endPointUrl/onecall?lat=${location.latitude}&lon=${location.longitude}&exclude=hourly,minutely&APPID=$apiKey';
final response = await this.httpClient.get(Uri.encodeFull(requestUrl));

if (response.statusCode != 200) {
throw Exception('error retrieving weather: ${response.statusCode}');
}

return Forecast.fromJson(jsonDecode(response.body));
}
}
67 changes: 67 additions & 0 deletions lib/data/farm/models/Forecast.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import 'package:farmassist/data/farm/models/Weather.dart';

class Forecast {
final DateTime lastUpdated;
final double longitude;
final double latitude;
final List<Weather> daily;
final Weather current;
final bool isDayTime;
String city;

Forecast(
{this.lastUpdated,
this.longitude,
this.latitude,
this.daily: const [],
this.current,
this.city,
this.isDayTime});

static Forecast fromJson(dynamic json) {
var weather = json['current']['weather'][0];
var date = DateTime.fromMillisecondsSinceEpoch(json['current']['dt'] * 1000,
isUtc: true);

var sunrise = DateTime.fromMillisecondsSinceEpoch(
json['current']['sunrise'] * 1000,
isUtc: true);

var sunset = DateTime.fromMillisecondsSinceEpoch(
json['current']['sunset'] * 1000,
isUtc: true);

bool isDay = date.isAfter(sunrise) && date.isBefore(sunset);

// get the forecast for the next 3 days, excluding the current day
bool hasDaily = json['daily'] != null;
var tempDaily = [];
if (hasDaily) {
List items = json['daily'];
tempDaily = items
.map((item) => Weather.fromDailyJson(item))
.toList()
.skip(1)
.take(5)
.toList();
}

var currentForcast = Weather(
cloudiness: int.parse(json['current']['clouds'].toString()),
temp: json['current']['temp'].toDouble(),
iconCode: weather['icon'],
condition: Weather.mapStringToWeatherCondition(
weather['main'], int.parse(json['current']['clouds'].toString())),
description: weather['description'],
feelLikeTemp: json['current']['feels_like'],
date: date);

return Forecast(
lastUpdated: DateTime.now(),
current: currentForcast,
latitude: json['lat'].toDouble(),
longitude: json['lon'].toDouble(),
daily: tempDaily,
isDayTime: isDay);
}
}
15 changes: 15 additions & 0 deletions lib/data/farm/models/Location.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Location {
final double longitude;
final double latitude;

Location({
this.longitude,
this.latitude,
});

static Location fromJson(dynamic json) {
return Location(
longitude: json['coord']['lon'].toDouble(),
latitude: json['coord']['lat'].toDouble());
}
}
6 changes: 3 additions & 3 deletions lib/data/farm/models/Planting.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Planting {
String _name;
int _noOfPlants;
DateTime _date;
String _date;
String _estimatedHarvest;
String _location;
String _fertilizers;
Expand Down Expand Up @@ -40,9 +40,9 @@ class Planting {
_estimatedHarvest = value;
}

DateTime get date => _date;
String get date => _date;

set date(DateTime value) {
set date(String value) {
_date = value;
}

Expand Down
123 changes: 123 additions & 0 deletions lib/data/farm/models/Weather.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import 'package:farmassist/data/farm/utils/WeatherIconMapper.dart';
import 'package:flutter/material.dart';

enum WeatherCondition {
thunderstorm,
drizzle,
rain,
snow,
atmosphere, // dust, ash, fog, sand etc.
mist,
fog,
lightCloud,
heavyCloud,
clear,
unknown
}

class Weather {
final WeatherCondition condition;
final String description;
final String iconCode;
final double temp;
final double feelLikeTemp;
final int cloudiness;
final DateTime date;

Weather(
{this.condition,
this.description,
this.iconCode,
this.temp,
this.feelLikeTemp,
this.cloudiness,
this.date});

static Weather fromDailyJson(dynamic daily) {
var cloudiness = daily['clouds'];
var weather = daily['weather'][0];

return Weather(
condition: mapStringToWeatherCondition(weather['main'], cloudiness),
description: weather['description'],
cloudiness: cloudiness,
iconCode: weather['icon'],
temp: daily['temp']['day'].toDouble(),
date: DateTime.fromMillisecondsSinceEpoch(daily['dt'] * 1000,
isUtc: true),
feelLikeTemp: daily['feels_like']['day'].toDouble()
);
}

static WeatherCondition mapStringToWeatherCondition(
String input, int cloudiness) {
WeatherCondition condition;
switch (input) {
case 'Thunderstorm':
condition = WeatherCondition.thunderstorm;
break;
case 'Drizzle':
condition = WeatherCondition.drizzle;
break;
case 'Rain':
condition = WeatherCondition.rain;
break;
case 'Snow':
condition = WeatherCondition.snow;
break;
case 'Clear':
condition = WeatherCondition.clear;
break;
case 'Clouds':
condition = (cloudiness >= 85)
? WeatherCondition.heavyCloud
: WeatherCondition.lightCloud;
break;
case 'Mist':
condition = WeatherCondition.mist;
break;
case 'fog':
condition = WeatherCondition.fog;
break;
case 'Smoke':
case 'Haze':
case 'Dust':
case 'Sand':
case 'Ash':
case 'Squall':
case 'Tornado':
condition = WeatherCondition.atmosphere;
break;
default:
condition = WeatherCondition.unknown;
}

return condition;
}

IconData getIconData(String iconCode){
switch(iconCode){
case '01d': return WeatherIcons.clear_day;
case '01n': return WeatherIcons.clear_night;
case '02d': return WeatherIcons.few_clouds_day;
case '02n': return WeatherIcons.few_clouds_day;
case '03d':
case '04d':
return WeatherIcons.clouds_day;
case '03n':
case '04n':
return WeatherIcons.clear_night;
case '09d': return WeatherIcons.shower_rain_day;
case '09n': return WeatherIcons.shower_rain_night;
case '10d': return WeatherIcons.rain_day;
case '10n': return WeatherIcons.rain_night;
case '11d': return WeatherIcons.thunder_storm_day;
case '11n': return WeatherIcons.thunder_storm_night;
case '13d': return WeatherIcons.snow_day;
case '13n': return WeatherIcons.snow_night;
case '50d': return WeatherIcons.mist_day;
case '50n': return WeatherIcons.mist_night;
default: return WeatherIcons.clear_day;
}
}
}
Loading

0 comments on commit 91414fe

Please sign in to comment.