Skip to content

Commit

Permalink
Organized reducers and middlewares to appropriate places.
Browse files Browse the repository at this point in the history
  • Loading branch information
Iiro Krankka committed Apr 10, 2018
1 parent 01ef9f9 commit c7d3fc9
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import 'package:inkino/data/models/actor.dart';
import 'package:inkino/data/models/event.dart';

class SearchQueryChangedAction {
SearchQueryChangedAction(this.query);
final String query;
}

class FetchActorAvatarsAction {
FetchActorAvatarsAction(this.event);
final Event event;
Expand All @@ -19,4 +14,4 @@ class ActorsUpdatedAction {
class ReceivedActorAvatarsAction {
ReceivedActorAvatarsAction(this.actors);
final List<Actor> actors;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import 'dart:async';

import 'package:inkino/data/networking/tmdb_api.dart';
import 'package:inkino/redux/app/app_actions.dart';
import 'package:inkino/redux/actor/actor_actions.dart';
import 'package:inkino/redux/app/app_state.dart';
import 'package:inkino/redux/common_actions.dart';
import 'package:redux/redux.dart';

class AppMiddleware extends MiddlewareClass<AppState> {
AppMiddleware(this.tmdbApi);
class ActorMiddleware extends MiddlewareClass<AppState> {
ActorMiddleware(this.tmdbApi);
final TMDBApi tmdbApi;

@override
Expand All @@ -29,8 +29,10 @@ class AppMiddleware extends MiddlewareClass<AppState> {
next(new UpdateActorsForEventAction(action.event, actorsWithAvatars));
next(new ReceivedActorAvatarsAction(actorsWithAvatars));
} catch (e) {
// YOLO! We don't need to handle this. If fetching actor avatars
// fails, we don't care.
// We don't need to handle this. If fetching actor avatars
// fails, we don't care: the UI just simply won't display
// any actor avatars and falls back to placeholder icons
// instead.
}
}
}
Expand Down
29 changes: 29 additions & 0 deletions lib/redux/actor/actor_reducer.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import 'package:inkino/data/models/actor.dart';
import 'package:inkino/redux/actor/actor_actions.dart';
import 'package:redux/redux.dart';

final actorReducer = combineTypedReducers<Map<String, Actor>>([
new ReducerBinding<Map<String, Actor>, ActorsUpdatedAction>(_actorsUpdated),
new ReducerBinding<Map<String, Actor>, ReceivedActorAvatarsAction>(_receivedAvatars),
]);

Map<String, Actor> _actorsUpdated(Map<String, Actor> actorsByName, action) {
var actors = <String, Actor>{}..addAll(actorsByName);
action.actors.forEach((actor) {
actors.putIfAbsent(actor.name, () => new Actor(name: actor.name));
});

return actors;
}

Map<String, Actor> _receivedAvatars(Map<String, Actor> actorsByName, action) {
var actorsWithAvatars = <String, Actor>{}..addAll(actorsByName);
action.actors.forEach((actor) {
actorsWithAvatars[actor.name] = new Actor(
name: actor.name,
avatarUrl: actor.avatarUrl,
);
});

return actorsWithAvatars;
}
File renamed without changes.
41 changes: 3 additions & 38 deletions lib/redux/app/app_reducer.dart
Original file line number Diff line number Diff line change
@@ -1,51 +1,16 @@
import 'package:inkino/data/models/actor.dart';
import 'package:inkino/redux/app/app_actions.dart';
import 'package:inkino/redux/actor/actor_reducer.dart';
import 'package:inkino/redux/app/app_state.dart';
import 'package:inkino/redux/event/event_reducer.dart';
import 'package:inkino/redux/search/search_reducer.dart';
import 'package:inkino/redux/show/show_reducer.dart';
import 'package:inkino/redux/theater/theater_reducer.dart';
import 'package:redux/redux.dart';

AppState appReducer(AppState state, dynamic action) {
return new AppState(
searchQuery: _searchQueryReducer(state.searchQuery, action),
searchQuery: searchQueryReducer(state.searchQuery, action),
actorsByName: actorReducer(state.actorsByName, action),
theaterState: theaterReducer(state.theaterState, action),
showState: showReducer(state.showState, action),
eventState: eventReducer(state.eventState, action),
);
}

String _searchQueryReducer(String searchQuery, action) {
if (action is SearchQueryChangedAction) {
return action.query;
}

return searchQuery;
}

final actorReducer = combineTypedReducers<Map<String, Actor>>([
new ReducerBinding<Map<String, Actor>, ActorsUpdatedAction>(_actorsUpdated),
new ReducerBinding<Map<String, Actor>, ReceivedActorAvatarsAction>(_receivedAvatars),
]);

Map<String, Actor> _actorsUpdated(Map<String, Actor> actorsByName, action) {
var actors = <String, Actor>{}..addAll(actorsByName);
action.actors.forEach((actor) {
actors.putIfAbsent(actor.name, () => new Actor(name: actor.name));
});

return actors;
}

Map<String, Actor> _receivedAvatars(Map<String, Actor> actorsByName, action) {
var actorsWithAvatars = <String, Actor>{}..addAll(actorsByName);
action.actors.forEach((actor) {
actorsWithAvatars[actor.name] = new Actor(
name: actor.name,
avatarUrl: actor.avatarUrl,
);
});

return actorsWithAvatars;
}
4 changes: 4 additions & 0 deletions lib/redux/search/search_actions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class SearchQueryChangedAction {
SearchQueryChangedAction(this.query);
final String query;
}
9 changes: 9 additions & 0 deletions lib/redux/search/search_reducer.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'package:inkino/redux/search/search_actions.dart';

String searchQueryReducer(String searchQuery, action) {
if (action is SearchQueryChangedAction) {
return action.query;
}

return searchQuery;
}
4 changes: 2 additions & 2 deletions lib/redux/store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'dart:async';
import 'package:flutter/services.dart';
import 'package:inkino/data/networking/finnkino_api.dart';
import 'package:inkino/data/networking/tmdb_api.dart';
import 'package:inkino/redux/app/app_middleware.dart';
import 'package:inkino/redux/actor/actor_middleware.dart';
import 'package:inkino/redux/app/app_reducer.dart';
import 'package:inkino/redux/app/app_state.dart';
import 'package:inkino/redux/event/event_middleware.dart';
Expand All @@ -22,7 +22,7 @@ Future<Store<AppState>> createStore() async {
initialState: new AppState.initial(),
distinct: true,
middleware: [
new AppMiddleware(tmdbApi),
new ActorMiddleware(tmdbApi),
new TheaterMiddleware(rootBundle, prefs),
new ShowMiddleware(finnkinoApi),
new EventMiddleware(finnkinoApi),
Expand Down
4 changes: 2 additions & 2 deletions lib/ui/event_details/actor_scroller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import 'package:flutter_redux/flutter_redux.dart';
import 'package:inkino/assets.dart';
import 'package:inkino/data/models/actor.dart';
import 'package:inkino/data/models/event.dart';
import 'package:inkino/redux/app/app_actions.dart';
import 'package:inkino/redux/app/app_selectors.dart';
import 'package:inkino/redux/actor/actor_actions.dart';
import 'package:inkino/redux/actor/actor_selectors.dart';
import 'package:inkino/redux/app/app_state.dart';

class ActorScroller extends StatelessWidget {
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/main_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:inkino/data/models/event.dart';
import 'package:inkino/data/models/theater.dart';
import 'package:inkino/redux/app/app_actions.dart';
import 'package:inkino/redux/app/app_state.dart';
import 'package:inkino/redux/search/search_actions.dart';
import 'package:inkino/ui/events/events_page.dart';
import 'package:inkino/ui/showtimes/showtimes_page.dart';
import 'package:inkino/ui/theater_list/inkino_drawer_header.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'package:inkino/data/models/actor.dart';
import 'package:inkino/data/models/event.dart';
import 'package:inkino/data/networking/tmdb_api.dart';
import 'package:inkino/redux/app/app_actions.dart';
import 'package:inkino/redux/app/app_middleware.dart';
import 'package:inkino/redux/actor/actor_actions.dart';
import 'package:inkino/redux/actor/actor_middleware.dart';
import 'package:inkino/redux/common_actions.dart';
import 'package:mockito/mockito.dart';
import 'package:test/test.dart';
Expand Down Expand Up @@ -34,11 +34,11 @@ void main() {
final Function(dynamic) next = (action) => actionLog.add(action);

MockTMDBApi mockTMDBApi;
AppMiddleware sut;
ActorMiddleware sut;

setUp(() {
mockTMDBApi = new MockTMDBApi();
sut = new AppMiddleware(mockTMDBApi);
sut = new ActorMiddleware(mockTMDBApi);
});

tearDown(() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:inkino/data/models/actor.dart';
import 'package:inkino/redux/app/app_actions.dart';
import 'package:inkino/redux/actor/actor_actions.dart';
import 'package:inkino/redux/app/app_reducer.dart';
import 'package:inkino/redux/app/app_state.dart';
import 'package:test/test.dart';
Expand Down

0 comments on commit c7d3fc9

Please sign in to comment.