Skip to content

Commit

Permalink
Merge branch 'master' into map-marker-beta
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSchwierzeck authored Oct 29, 2019
2 parents 16da0b3 + e0e3149 commit a0bb227
Show file tree
Hide file tree
Showing 22 changed files with 722 additions and 178 deletions.
49 changes: 49 additions & 0 deletions lib/backend/repositories/inspections_repository.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:convert';
import 'package:nesteo_app/backend/services/inspections/inspections_api_service.dart';
import 'package:nesteo_app/backend/services/nestingboxes/nestingboxes_api_service.dart';
import 'package:nesteo_app/model/inspection.dart';

/// Wrapper that generates and returns [Inspection] objects from json returned by a [InspectionsApiService].
Expand All @@ -9,9 +10,11 @@ import 'package:nesteo_app/model/inspection.dart';
/// *Author: Simon Oyen*
class InspectionsRepository {
InspectionsApiService _inspectionsApi;
NestingBoxesApiService _nestingBoxesApi;

InspectionsRepository() {
_inspectionsApi = InspectionsApiService.create();
_nestingBoxesApi = NestingBoxesApiService.create();
}

/// Requests information about an specific Inspection by [id] from a [InspectionsApiService] and converts it to a [Inspection] object.
Expand Down Expand Up @@ -97,4 +100,50 @@ class InspectionsRepository {
return null;
}
}

/// Requests information about all Inspections for a NestingBox with [id] from a [NestingBoxesApiService] and converts it to a [List<Inspection>].
///
/// The [Inspection] objects in the returned [List<Inspection>] **are preview-versions**
///
/// ```dart
/// var inspectionsRepository = InspectionsRepository();
/// List<Inspection> inspections = await inspectionsRepository.getInspectionPreviewsByNestingBoxId();
/// inspections[0].isPreview == true;
/// ```
Future<List<Inspection>> getInspectionPreviewsByNestingBoxId(
String id) async {
final response =
await _nestingBoxesApi.getInspectionPreviewsByNestingBoxId(id);
if (response.statusCode == 200) {
final List results = json.decode(response.body);
return results
.map((inspection) => Inspection.previewFromJson(inspection))
.toList();
} else {
print('Request failed');
return null;
}
}

/// Requests information about all Inspections for a NestingBox with [id] from a [NestingBoxesApiService] and converts it to a [List<Inspection>].
///
/// The [Inspection] objects in the returned [List<Inspection>] are **NOT** preview-versions.
///
/// ```dart
/// var inspectionsRepository = InspectionsRepository();
/// List<Inspection> inspections = await inspectionsRepository.getInspectionsByNestingBoxId();
/// inspections[0].isPreview == true;
/// ```
Future<List<Inspection>> getInspectionsByNestingBoxId(String id) async {
final response = await _nestingBoxesApi.getInspectionsByNestingBoxId(id);
if (response.statusCode == 200) {
final List results = json.decode(response.body);
return results
.map((inspection) => Inspection.fromJson(inspection))
.toList();
} else {
print('Request failed');
return null;
}
}
}

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

12 changes: 12 additions & 0 deletions lib/backend/services/nestingboxes/nestingboxes_api_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ abstract class NestingBoxesApiService extends ChopperService {
)
Future<Response> getNestingBoxById(@Path('id') String id);

@Get(
path: '/{id}/inspections',
headers: {'Authorization': 'Basic QWRtaW46QWRtaW4xMjM='},
)
Future<Response> getInspectionsByNestingBoxId(@Path('id') String id);

@Get(
path: '/{id}/inspections/previews',
headers: {'Authorization': 'Basic QWRtaW46QWRtaW4xMjM='},
)
Future<Response> getInspectionPreviewsByNestingBoxId(@Path('id') String id);

@Get(
path: '/previews',
headers: {'Authorization': 'Basic QWRtaW46QWRtaW4xMjM='},
Expand Down
3 changes: 0 additions & 3 deletions lib/blocs/boxdata_bloc/boxdata_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ class BoxDataBloc extends Bloc<BoxDataEvent, BoxDataState> {
//List of Functions to use for sorting the List, accessed via a counter
//that increments with every sort-button press and then modulo the length of the functions list,
// to iterate through it again and again
//List<Function> sortOptions = [sortByIdAsc, sortByLastInspectedAsc];
//var boxes = new List.generate(width, (_) => new List(height));

var sortOptions = [
[sortByIdAsc, sortByIdDesc],
[sortByLastInspectedAsc, sortByLastInspectedDesc]
Expand Down
75 changes: 73 additions & 2 deletions lib/blocs/inspectiondata_bloc/inspectiondata_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
import 'dart:async';
import 'package:bloc/bloc.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:nesteo_app/backend/repositories/inspections_repository.dart';
import 'package:nesteo_app/blocs/boxdata_bloc/boxdata_bloc.dart';
import 'package:nesteo_app/model/inspection.dart';
import 'package:nesteo_app/model/nestingbox.dart';
import './inspectiondata.dart';

class InspectionDataBloc
extends Bloc<InspectionDataEvent, InspectionDataState> {
@override
InspectionDataState get initialState => InitialInspectionDataState();

int _sortOptionCounter = 0;
int _ascDescCounter = 0;
InspectionsRepository _inspectionRepo = InspectionsRepository();
List<Inspection> inspectionList = new List<Inspection>();

String boxId = "";
Inspection inspection = new Inspection();
int inspectionId = 0;

List<String> sortOptionNames = [
"sortbyidasc",
"sortbydatenasc"
]; //todo put output text for snackbar in screen
String currentSortOption = "";
@override
Stream<InspectionDataState> mapEventToState(
InspectionDataEvent event) async* {
var sortOptions = [
[sortByIdAsc, sortByIdDesc],
[sortByDateAsc, sortByDateDesc]
];

if (event is GetInspectionEvent) {
if (this.state is! InitialInspectionDataState) {
yield InspectionChangingState();
Expand All @@ -35,5 +49,62 @@ class InspectionDataBloc
print(inspectionList.length);
yield InspectionReadyState();
}
if (event is GetAllInspectionPreviewEvent) {
if (this.state is! InitialInspectionDataState) {
yield InspectionChangingState();
}
inspectionList = await _inspectionRepo.getAllInspectionPreviews();
print(inspectionList.length);
yield InspectionReadyState();
}

if (event is GetInspectionPreviewsByNestingBoxEvent) {
if (this.state is! InitialInspectionDataState) {
yield InspectionChangingState();
}

inspectionList =
await _inspectionRepo.getInspectionPreviewsByNestingBoxId(boxId);
print(inspectionList.length);
yield InspectionReadyState();
}

if (event is SortInspectionEvent) {
if (this.state is! InitialInspectionDataState) {
yield InspectionChangingState();
}

inspectionList.sort((a, b) =>
sortOptions[_sortOptionCounter % sortOptions[0].length]
[_ascDescCounter % sortOptions[1].length](a, b));
currentSortOption =
sortOptionNames[_sortOptionCounter % sortOptionNames.length];
yield InspectionReadyState();
}

if (event is ChangeSortDirectionEvent) {
_ascDescCounter++;
add(SortInspectionEvent());
}
if (event is ChangeSortTypeEvent) {
_sortOptionCounter++;
add(SortInspectionEvent());
}
}

int sortByIdAsc(a, b) {
return a.id.compareTo(b.id);
}

int sortByIdDesc(a, b) {
return b.id.compareTo(a.id);
}

int sortByDateAsc(a, b) {
return a.inspectionDate.compareTo(b.inspectionDate);
}

int sortByDateDesc(a, b) {
return b.inspectionDate.compareTo(a.inspectionDate);
}
}
24 changes: 24 additions & 0 deletions lib/blocs/inspectiondata_bloc/inspectiondata_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ class GetInspectionEvent extends InspectionDataEvent {
}

class GetAllInspectionEvent extends InspectionDataEvent {
@override
List<Object> get props => null;
}

class GetAllInspectionPreviewEvent extends InspectionDataEvent {
@override
List<Object> get props => null;
}
Expand All @@ -25,3 +29,23 @@ class ChangeInspectionTypeEvent extends InspectionDataEvent {
@override
List<Object> get props => null;
}

class SortInspectionEvent extends InspectionDataEvent {
@override
List<Object> get props => [];
}

class ChangeSortDirectionEvent extends InspectionDataEvent {
@override
List<Object> get props => [];
}

class ChangeSortTypeEvent extends InspectionDataEvent {
@override
List<Object> get props => [];
}

class GetInspectionPreviewsByNestingBoxEvent extends InspectionDataEvent {
@override
List<Object> get props => [];
}
65 changes: 32 additions & 33 deletions lib/blocs/pagecontrol_bloc/pagecontrol_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,57 @@ import 'package:back_button_interceptor/back_button_interceptor.dart';
import 'package:bloc/bloc.dart';
import './pagecontrol.dart';

/// BLoC that handles page control and transitioning.
///
/// *Author: Simon Oyen*
class PageControlBloc extends Bloc<PageControlEvent, PageControlState> {
bool navigationBarEnabled = false;
List<PageControlState> history = new List();
final Map<Type, PageControlState> eventStateMap = {
GoToMapEvent: MapScreenState(),
GoToBoxListEvent: BoxListScreenState(),
GoToBoxInfoEvent: BoxInfoScreenState(),
GoToInspectionEvent: InspectionScreenState(),
GoToNewInspectionEvent: NewInspectionScreenState(),
GoToNewBoxEvent: NewBoxScreenState(),
GoToInspectionListEvent: InspectionListScreenState(),
};

PageControlBloc() : super() {
BackButtonInterceptor.add(myInterceptor);
BackButtonInterceptor.add(interceptFunc);
}
bool myInterceptor(bool stopDefaultButtonEvent) {

/// Is called when the hardware backbutton is pressed
///
/// Returns true when the normal backbutton behaviour should be blocked.
bool interceptFunc(bool stopDefaultButtonEvent) {
this.add(BackButtonEvent());
return true;
}

bool navigationBarEnabled = false;
List<PageControlState> history =
new List(); //state history since starting the app
/// Returns the BLoCs initial state, [LoginScreenState]
@override
PageControlState get initialState => LoginScreenState();

/// Maps received events to corresponding states and handles the navigationbar
@override
Stream<PageControlState> mapEventToState(
PageControlEvent event,
) async* {
if (event is! BackButtonEvent && state != LoginScreenState()) {
history.add(state);
}

if (event is GoToMapEvent) {
navigationBarEnabled = true;
yield MapScreenState();
}

if (event is GoToBoxListEvent) {
navigationBarEnabled = true;
yield BoxListScreenState();
}

if (event is GoToNewBoxEvent) {
navigationBarEnabled = false;
yield NewBoxScreenState();
if (event is! BackButtonEvent) {
if (state != LoginScreenState()) {
history.add(state);
}
navigationBarEnabled =
(event is GoToMapEvent || event is GoToBoxListEvent);
}

if (event is GoToBoxInfoEvent) {
navigationBarEnabled = false;
yield BoxInfoScreenState();
}
PageControlState nextState = eventStateMap[event.runtimeType];

if (event is GoToInspectionEvent) {
navigationBarEnabled = false;
yield InspectionScreenState();
if (nextState != null) {
yield nextState;
}

if (event is GoToNewInspectionEvent) {
navigationBarEnabled = false;
yield NewInspectionScreenState();
}
if (event is BackButtonEvent) {
if (history.isNotEmpty) {
yield history.last;
Expand Down
5 changes: 5 additions & 0 deletions lib/blocs/pagecontrol_bloc/pagecontrol_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ class GoToBoxInfoEvent extends PageControlEvent {
List<Object> get props => [];
}

class GoToInspectionListEvent extends PageControlEvent {
@override
List<Object> get props => [];
}

class GoToInspectionEvent extends PageControlEvent {
@override
List<Object> get props => [];
Expand Down
7 changes: 7 additions & 0 deletions lib/blocs/pagecontrol_bloc/pagecontrol_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,10 @@ class NewInspectionScreenState extends PageControlState {
@override
List<Object> get props => [];
}

class InspectionListScreenState extends PageControlState {
InspectionListScreenState() : super(false);

@override
List<Object> get props => [];
}
2 changes: 2 additions & 0 deletions lib/frames.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class FullScreen extends StatelessWidget {
screen = NewBoxScreen(context);
} else if (state is InspectionScreenState) {
screen = InspectionScreen(context);
} else if (state is InspectionListScreenState) {
screen = InspectionListScreen(context);
} else if (state is NewInspectionScreenState) {
screen = NewInspectionScreen(context);
} else {
Expand Down
1 change: 1 addition & 0 deletions lib/generated/locale_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,5 @@ class LocalescreenName {
String get boxNew => _data["boxNew"];
String get load => _data["load"];
String get list => _data["list"];
String get inspectionlist => _data["inspectionlist"];
}
Loading

0 comments on commit a0bb227

Please sign in to comment.