Redux (wip)

This commit is contained in:
vjrj 2018-06-27 12:08:36 +02:00
parent 445c9c06cd
commit 866e776389
13 changed files with 150 additions and 7 deletions

59
lib/models/appState.dart Normal file
View file

@ -0,0 +1,59 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:meta/meta.dart';
import 'fireMapState.dart';
import 'yourLocation.dart';
part 'appState.g.dart';
@immutable
@JsonSerializable(nullable: false)
class AppState extends Object with _$AppStateSerializerMixin {
@JsonKey(ignore: true)
final bool isLoading;
@JsonKey(ignore: true)
final bool isLoaded;
@JsonKey(ignore: true)
final String error;
final String userId;
final String token;
final List<YourLocation> yourLocations;
@JsonKey(ignore: true)
final FireMapState fireMapState;
@JsonKey(ignore: true)
factory AppState.fromJson(Map<String, dynamic> json) =>
_$AppStateFromJson(json);
AppState(
{this.yourLocations = const [],
this.userId,
this.token,
this.isLoading: false,
this.isLoaded: false,
this.error: null,
this.fireMapState: const FireMapState.initial()});
AppState copyWith(
{bool isLoading,
bool isLoaded,
String userId,
String token,
String error,
List<YourLocation> yourLocations,
FireMapState fireMapState}) {
return new AppState(
isLoading: isLoading ?? this.isLoading,
isLoaded: isLoaded ?? this.isLoaded,
userId: userId ?? this.userId,
token: token ?? this.token,
error: error ?? this.error,
yourLocations: yourLocations ?? this.yourLocations,
fireMapState: fireMapState ?? this.fireMapState);
}
@override
String toString() {
return 'AppState{\nuserId: $userId\ntoken: $token\nisLoading: $isLoading\nisLoaded: $isLoaded\nYourLocations: $yourLocations}';
}
}

View file

@ -0,0 +1,46 @@
// Copyright (c) 2018, Comunes Association.
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'appState.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
AppState _$AppStateFromJson(Map<String, dynamic> json) => new AppState(
yourLocations: (json['yourLocations'] as List)
.map((e) => new YourLocation.fromJson(e as Map<String, dynamic>))
.toList(),
userId: json['userId'] as String,
token: json['token'] as String);
abstract class _$AppStateSerializerMixin {
String get userId;
String get token;
List<YourLocation> get yourLocations;
Map<String, dynamic> toJson() => new _$AppStateJsonMapWrapper(this);
}
class _$AppStateJsonMapWrapper extends $JsonMapWrapper {
final _$AppStateSerializerMixin _v;
_$AppStateJsonMapWrapper(this._v);
@override
Iterable<String> get keys => const ['userId', 'token', 'yourLocations'];
@override
dynamic operator [](Object key) {
if (key is String) {
switch (key) {
case 'userId':
return _v.userId;
case 'token':
return _v.token;
case 'yourLocations':
return _v.yourLocations;
}
}
return null;
}
}

View file

@ -0,0 +1,38 @@
import 'package:flutter/material.dart';
class BasicLocation extends Comparable<BasicLocation> {
final double lat;
final double lon;
String description;
// static BasicLocation noLocation = new BasicLocation(lat: 0.0, lon: 0.0);
BasicLocation({@required this.lat, @required this.lon, this.description}) {
if (this.description == null)
this.description = 'Position: ${this.lat}, ${this.lon}';
}
int compareTo(BasicLocation other) {
return lat == other.lat && lon == other.lon ? 1 : 0;
}
bool operator ==(o) => o is BasicLocation && o.lat == lat && o.lon == lon;
int get hashCode {
int hash = 1;
hash = hash * 17 + lat.hashCode;
hash = hash * 31 + lon.hashCode;
return hash;
}
BasicLocation.fromJson(Map<String, dynamic> json)
: lat = json['lat'],
lon = json['lon'],
description = json['description'];
Map<String, dynamic> toJson() => {
'lat': lat,
'lon': lon,
'description': description,
};
}

View file

@ -0,0 +1,27 @@
import 'package:meta/meta.dart';
enum FireMapOperation { view, subscriptionConfirm, unsubscribe }
@immutable
class FireMapState {
final FireMapOperation currentOperation;
const FireMapState.initial(): this.currentOperation = FireMapOperation.view;
FireMapState({this.currentOperation: FireMapOperation.view});
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is FireMapState &&
runtimeType == other.runtimeType &&
currentOperation == other.currentOperation;
@override
int get hashCode => currentOperation.hashCode;
FireMapState copyWith({FireMapOperation currentOperation}) {
return new FireMapState(
currentOperation: currentOperation ?? this.currentOperation);
}
}

View file

@ -0,0 +1,58 @@
import 'package:bson_objectid/bson_objectid.dart';
import 'package:flutter/material.dart';
import 'package:json_annotation/json_annotation.dart';
part 'yourLocation.g.dart';
_objectIdFromJson(String json) {
return new ObjectId.fromHexString(json);
}
_objectIdToJson(ObjectId o) {
return o.toString();
}
@JsonSerializable(nullable: false)
class YourLocation extends Object with _$YourLocationSerializerMixin {
@JsonKey(toJson: _objectIdToJson, fromJson: _objectIdFromJson)
ObjectId id;
final double lat;
final double lon;
String description;
bool subscribed;
factory YourLocation.fromJson(Map<String, dynamic> json) =>
_$YourLocationFromJson(json);
static YourLocation noLocation = new YourLocation(lat: 0.0, lon: 0.0);
YourLocation(
{this.id,
@required this.lat,
@required this.lon,
this.description,
this.subscribed: false}) {
if (this.description == null)
this.description = 'Position: ${this.lat}, ${this.lon}';
if (this.id == null) this.id = new ObjectId();
}
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is YourLocation &&
runtimeType == other.runtimeType &&
id == other.id &&
lat == other.lat &&
lon == other.lon &&
description == other.description &&
subscribed == other.subscribed;
@override
int get hashCode =>
id.hashCode ^
lat.hashCode ^
lon.hashCode ^
description.hashCode ^
subscribed.hashCode;
}

View file

@ -0,0 +1,54 @@
// Copyright (c) 2018, Comunes Association.
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'yourLocation.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
YourLocation _$YourLocationFromJson(Map<String, dynamic> json) =>
new YourLocation(
id: _objectIdFromJson(json['id'] as String),
lat: (json['lat'] as num).toDouble(),
lon: (json['lon'] as num).toDouble(),
description: json['description'] as String,
subscribed: json['subscribed'] as bool);
abstract class _$YourLocationSerializerMixin {
ObjectId get id;
double get lat;
double get lon;
String get description;
bool get subscribed;
Map<String, dynamic> toJson() => new _$YourLocationJsonMapWrapper(this);
}
class _$YourLocationJsonMapWrapper extends $JsonMapWrapper {
final _$YourLocationSerializerMixin _v;
_$YourLocationJsonMapWrapper(this._v);
@override
Iterable<String> get keys =>
const ['id', 'lat', 'lon', 'description', 'subscribed'];
@override
dynamic operator [](Object key) {
if (key is String) {
switch (key) {
case 'id':
return _objectIdToJson(_v.id);
case 'lat':
return _v.lat;
case 'lon':
return _v.lon;
case 'description':
return _v.description;
case 'subscribed':
return _v.subscribed;
}
}
return null;
}
}