Skip to content

Commit

Permalink
Merge pull request roughike#89 from roughike/bugfix/equals-in-viewmodels
Browse files Browse the repository at this point in the history
Fix hashcode & equals in viewmodels
  • Loading branch information
roughike authored Jul 3, 2018
2 parents 7e0ec9f + 33e73fc commit e41349e
Show file tree
Hide file tree
Showing 10 changed files with 264 additions and 16 deletions.
50 changes: 50 additions & 0 deletions lib/models/event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,37 @@ class Event {
return 'https://youtube.com/watch?v=' + tagContents(node, 'Location');
}).toList();
}

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is Event &&
runtimeType == other.runtimeType &&
id == other.id &&
title == other.title &&
originalTitle == other.originalTitle &&
genres == other.genres &&
directors == other.directors &&
lengthInMinutes == other.lengthInMinutes &&
shortSynopsis == other.shortSynopsis &&
synopsis == other.synopsis &&
images == other.images &&
youtubeTrailers == other.youtubeTrailers &&
actors == other.actors;

@override
int get hashCode =>
id.hashCode ^
title.hashCode ^
originalTitle.hashCode ^
genres.hashCode ^
directors.hashCode ^
lengthInMinutes.hashCode ^
shortSynopsis.hashCode ^
synopsis.hashCode ^
images.hashCode ^
youtubeTrailers.hashCode ^
actors.hashCode;
}

class EventImageData {
Expand Down Expand Up @@ -134,4 +165,23 @@ class EventImageData {
landscapeBig: tagContentsOrNull(root, 'EventLargeImageLandscape'),
);
}

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is EventImageData &&
runtimeType == other.runtimeType &&
portraitSmall == other.portraitSmall &&
portraitMedium == other.portraitMedium &&
portraitLarge == other.portraitLarge &&
landscapeSmall == other.landscapeSmall &&
landscapeBig == other.landscapeBig;

@override
int get hashCode =>
portraitSmall.hashCode ^
portraitMedium.hashCode ^
portraitLarge.hashCode ^
landscapeSmall.hashCode ^
landscapeBig.hashCode;
}
27 changes: 27 additions & 0 deletions lib/models/show.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,31 @@ class Show {
);
}).toList();
}

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is Show &&
runtimeType == other.runtimeType &&
id == other.id &&
eventId == other.eventId &&
title == other.title &&
originalTitle == other.originalTitle &&
url == other.url &&
presentationMethod == other.presentationMethod &&
theaterAndAuditorium == other.theaterAndAuditorium &&
start == other.start &&
end == other.end;

@override
int get hashCode =>
id.hashCode ^
eventId.hashCode ^
title.hashCode ^
originalTitle.hashCode ^
url.hashCode ^
presentationMethod.hashCode ^
theaterAndAuditorium.hashCode ^
start.hashCode ^
end.hashCode;
}
13 changes: 13 additions & 0 deletions lib/models/theater.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,17 @@ class Theater {
return '${match.group(1)}${match.group(2).toLowerCase()}';
});
}

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is Theater &&
runtimeType == other.runtimeType &&
id == other.id &&
name == other.name;

@override
int get hashCode =>
id.hashCode ^
name.hashCode;
}
4 changes: 4 additions & 0 deletions lib/networking/tmdb_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import 'package:inkino/utils/http_utils.dart';
/// is not present on the project. Refer to the README for instructions
/// on how to do so.
/// If this has a red underline, it means that the lib/tmdb_config.dart file
/// is not present on the project. Refer to the README for instructions
/// on how to do so.


class TMDBApi {
Expand Down
7 changes: 3 additions & 4 deletions lib/ui/events/events_page_view_model.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:collection/collection.dart';
import 'package:inkino/models/event.dart';
import 'package:inkino/models/loading_status.dart';
import 'package:inkino/redux/app/app_state.dart';
Expand Down Expand Up @@ -34,10 +35,8 @@ class EventsPageViewModel {
other is EventsPageViewModel &&
runtimeType == other.runtimeType &&
status == other.status &&
events == other.events &&
refreshEvents == other.refreshEvents;
const IterableEquality().equals(events, other.events);

@override
int get hashCode =>
status.hashCode ^ events.hashCode ^ refreshEvents.hashCode;
int get hashCode => status.hashCode ^ const IterableEquality().hash(events);
}
13 changes: 5 additions & 8 deletions lib/ui/showtimes/showtime_page_view_model.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:collection/collection.dart';
import 'package:inkino/models/loading_status.dart';
import 'package:inkino/models/show.dart';
import 'package:inkino/redux/app/app_state.dart';
Expand Down Expand Up @@ -42,18 +43,14 @@ class ShowtimesPageViewModel {
other is ShowtimesPageViewModel &&
runtimeType == other.runtimeType &&
status == other.status &&
dates == other.dates &&
const IterableEquality().equals(dates, other.dates) &&
selectedDate == other.selectedDate &&
shows == other.shows &&
changeCurrentDate == other.changeCurrentDate &&
refreshShowtimes == other.refreshShowtimes;
const IterableEquality().equals(shows, other.shows);

@override
int get hashCode =>
status.hashCode ^
dates.hashCode ^
const IterableEquality().hash(dates) ^
selectedDate.hashCode ^
shows.hashCode ^
changeCurrentDate.hashCode ^
refreshShowtimes.hashCode;
const IterableEquality().hash(shows);
}
7 changes: 3 additions & 4 deletions lib/ui/theater_list/theater_list_view_model.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:collection/collection.dart';
import 'package:inkino/models/theater.dart';
import 'package:inkino/redux/app/app_state.dart';
import 'package:inkino/redux/common_actions.dart';
Expand Down Expand Up @@ -32,12 +33,10 @@ class TheaterListViewModel {
other is TheaterListViewModel &&
runtimeType == other.runtimeType &&
currentTheater == other.currentTheater &&
theaters == other.theaters &&
changeCurrentTheater == other.changeCurrentTheater;
const IterableEquality().equals(theaters, other.theaters);

@override
int get hashCode =>
currentTheater.hashCode ^
theaters.hashCode ^
changeCurrentTheater.hashCode;
const IterableEquality().hash(theaters);
}
48 changes: 48 additions & 0 deletions test/ui/events/events_page_view_model_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import 'package:inkino/models/event.dart';
import 'package:inkino/models/loading_status.dart';
import 'package:inkino/ui/events/events_page_view_model.dart';
import 'package:test/test.dart';

void main() {
group('EventsPageViewModel', () {
test('equal', () {
var first = EventsPageViewModel(
status: LoadingStatus.success,
events: <Event>[
Event(id: 'abc123'),
],
refreshEvents: () {},
);

var second = EventsPageViewModel(
status: LoadingStatus.success,
events: <Event>[
Event(id: 'abc123'),
],
refreshEvents: () {},
);

expect(first, second);
});

test('not equal', () {
var first = EventsPageViewModel(
status: LoadingStatus.success,
events: <Event>[
Event(id: 'abc123'),
],
refreshEvents: () {},
);

var second = EventsPageViewModel(
status: LoadingStatus.success,
events: <Event>[
Event(id: 'xyz456'),
],
refreshEvents: () {},
);

expect(first, isNot(second));
});
});
}
60 changes: 60 additions & 0 deletions test/ui/showtimes/showtimes_page_view_model_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import 'package:inkino/models/loading_status.dart';
import 'package:inkino/models/show.dart';
import 'package:inkino/ui/showtimes/showtime_page_view_model.dart';
import 'package:test/test.dart';

void main() {
group('ShowtimesPageViewModel', () {
test('equal', () {
var first = ShowtimesPageViewModel(
status: LoadingStatus.success,
dates: <DateTime>[DateTime(2018)],
selectedDate: null,
shows: <Show>[
Show(id: 'abc123'),
],
changeCurrentDate: (DateTime newDate) {},
refreshShowtimes: () {},
);

var second = ShowtimesPageViewModel(
status: LoadingStatus.success,
dates: <DateTime>[DateTime(2018)],
selectedDate: null,
shows: <Show>[
Show(id: 'abc123'),
],
changeCurrentDate: (DateTime newDate) {},
refreshShowtimes: () {},
);

expect(first, second);
});

test('not equal', () {
var first = ShowtimesPageViewModel(
status: LoadingStatus.success,
dates: <DateTime>[DateTime(2018)],
selectedDate: null,
shows: <Show>[
Show(id: 'abc123'),
],
changeCurrentDate: (DateTime newDate) {},
refreshShowtimes: () {},
);

var second = ShowtimesPageViewModel(
status: LoadingStatus.success,
dates: <DateTime>[DateTime(2018)],
selectedDate: null,
shows: <Show>[
Show(id: 'xyz456'),
],
changeCurrentDate: (DateTime newDate) {},
refreshShowtimes: () {},
);

expect(first, isNot(second));
});
});
}
51 changes: 51 additions & 0 deletions test/ui/theater_list/theater_list_view_model_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import 'package:inkino/models/theater.dart';
import 'package:inkino/ui/theater_list/theater_list_view_model.dart';
import 'package:test/test.dart';

void main() {
group('TheaterListViewModel', () {
test('equal', () {
var first = TheaterListViewModel(
currentTheater: Theater(id: 'abc123', name: 'Test 1'),
theaters: <Theater>[
Theater(id: 'abc123', name: 'Test 1'),
Theater(id: 'xyz456', name: 'Test 2'),
],
changeCurrentTheater: (Theater newTheater) {},
);

var second = TheaterListViewModel(
currentTheater: Theater(id: 'abc123', name: 'Test 1'),
theaters: <Theater>[
Theater(id: 'abc123', name: 'Test 1'),
Theater(id: 'xyz456', name: 'Test 2'),
],
changeCurrentTheater: (Theater newTheater) {},
);

expect(first, second);
});

test('not equal', () {
var first = TheaterListViewModel(
currentTheater: Theater(id: 'abc123', name: 'Test 1'),
theaters: <Theater>[
Theater(id: 'abc123', name: 'Test 1'),
Theater(id: 'xyz456', name: 'Test 2'),
],
changeCurrentTheater: (Theater newTheater) {},
);

var second = TheaterListViewModel(
currentTheater: Theater(id: 'abc123', name: 'Test 1'),
theaters: <Theater>[
Theater(id: 'efg123', name: 'Test 3'),
Theater(id: 'hjk456', name: 'Test 4'),
],
changeCurrentTheater: (Theater newTheater) {},
);

expect(first, isNot(second));
});
});
}

0 comments on commit e41349e

Please sign in to comment.