Skip to content

Commit

Permalink
Merge pull request #68 from arafaysaleem/refact/add-typedefs
Browse files Browse the repository at this point in the history
Add typedefs for longer type names
  • Loading branch information
arafaysaleem authored Aug 5, 2021
2 parents 1e0660a + 71ffd20 commit 4e46c11
Show file tree
Hide file tree
Showing 40 changed files with 239 additions and 144 deletions.
2 changes: 1 addition & 1 deletion Prototype/Theater class code.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Hall {
required this.name,
});

factory Hall.fromJson(Map<String, dynamic> json) {
factory Hall.fromJson(JSON json) {
return Hall(
numRows: json["num_rows"] as int,
numCols: json["num_cols"] as int,
Expand Down
2 changes: 2 additions & 0 deletions lib/helper/typedefs.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
typedef JSON = Map<String, dynamic>;
typedef QueryParams = Map<String, String>;
2 changes: 1 addition & 1 deletion lib/helper/utils/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -187,5 +187,5 @@ class Constants {
/// The error message for invalid credit card expiry input.
static const invalidCreditCardExpiryError = 'Please enter a valid expiry date';

static T? toNull<T>(dynamic _) => null;
static T? toNull<T>(Object? _) => null;
}
36 changes: 36 additions & 0 deletions lib/helper/utils/exception_constants.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// ignore_for_file: constant_identifier_names
import 'package:flutter/foundation.dart';

/// A utility class that holds constants for our custom exception names.
/// This class has no constructor and all variables are `static`.
@immutable
class ExceptionConstants {
const ExceptionConstants._();

/// The name of the exception for an expired bearer token.
static const String TokenExpiredException = 'TokenExpiredException';

/// The name of the exception for a prematurely cancelled request.
static const String CancelException = 'CancelException';

/// The name of the exception for a failed connection attempt.
static const String ConnectTimeoutException = 'ConnectTimeoutException';

/// The name of the exception for failing to send a request.
static const String SendTimeoutException = 'SendTimeoutException';

/// The name of the exception for failing to receive a response.
static const String ReceiveTimeoutException = 'ReceiveTimeoutException';

/// The name of the exception for no internet connectivity.
static const String SocketException = 'SocketException';

/// A better name for the socket exception.
static const String FetchDataException = 'FetchDataException';

/// The name of the exception for an incorrect parameter in a request/response.
static const String FormatException = 'FormatException';

/// The name of the exception for an unknown type of failure.
static const String UnrecognizedException = 'UnrecognizedException';
}
7 changes: 4 additions & 3 deletions lib/models/booking_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:freezed_annotation/freezed_annotation.dart';

import '../enums/booking_status_enum.dart';
import '../helper/utils/constants.dart';
import '../helper/typedefs.dart';

part 'booking_model.freezed.dart';

Expand All @@ -24,7 +25,7 @@ class BookingModel with _$BookingModel {
required DateTime bookingDatetime,
}) = _BookingModel;

Map<String, dynamic> toUpdateJson({
JSON toUpdateJson({
int? userId,
int? showId,
String? seatRow,
Expand All @@ -39,7 +40,7 @@ class BookingModel with _$BookingModel {
seatNumber == null &&
price == null &&
bookingStatus == null &&
bookingDatetime == null) return const <String, dynamic>{};
bookingDatetime == null) return const <String, Object>{};
return copyWith(
userId: userId,
showId: showId ?? this.showId,
Expand All @@ -51,6 +52,6 @@ class BookingModel with _$BookingModel {
).toJson();
}

factory BookingModel.fromJson(Map<String, dynamic> json) =>
factory BookingModel.fromJson(JSON json) =>
_$BookingModelFromJson(json);
}
3 changes: 2 additions & 1 deletion lib/models/genre_model.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:freezed_annotation/freezed_annotation.dart';
import '../helper/typedefs.dart';

part 'genre_model.freezed.dart';

Expand All @@ -12,6 +13,6 @@ class GenreModel with _$GenreModel {
required String genre,
}) = _GenreModel;

factory GenreModel.fromJson(Map<String, dynamic> json) =>
factory GenreModel.fromJson(JSON json) =>
_$GenreModelFromJson(json);
}
7 changes: 4 additions & 3 deletions lib/models/movie_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:freezed_annotation/freezed_annotation.dart';

import '../enums/movie_type_enum.dart';
import '../helper/utils/constants.dart';
import '../helper/typedefs.dart';
import 'genre_model.dart';

part 'movie_model.freezed.dart';
Expand Down Expand Up @@ -42,7 +43,7 @@ class MovieModel with _$MovieModel {
);
}

Map<String, dynamic> toUpdateJson({
JSON toUpdateJson({
int? year,
String? title,
String? summary,
Expand All @@ -58,7 +59,7 @@ class MovieModel with _$MovieModel {
posterUrl == null &&
rating == null &&
movieType == null
) return const <String, dynamic>{};
) return const <String, Object>{};
return copyWith(
movieId: movieId,
year: year ?? this.year,
Expand All @@ -71,7 +72,7 @@ class MovieModel with _$MovieModel {
).toJson();
}

factory MovieModel.fromJson(Map<String, dynamic> json) =>
factory MovieModel.fromJson(JSON json) =>
_$MovieModelFromJson(json);

late final List<String> genreNames =
Expand Down
7 changes: 4 additions & 3 deletions lib/models/movie_role_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:freezed_annotation/freezed_annotation.dart';

//Enums
import '../enums/role_type_enum.dart';
import '../helper/typedefs.dart';

//Models
import 'role_model.dart';
Expand All @@ -20,11 +21,11 @@ class MovieRoleModel with _$MovieRoleModel {
required RoleType roleType,
}) = _MovieRoleModel;

factory MovieRoleModel.fromJson(Map<String, dynamic> json) =>
factory MovieRoleModel.fromJson(JSON json) =>
_$MovieRoleModelFromJson(json);

Map<String, dynamic> toCustomJson() {
return <String, dynamic>{
JSON toCustomJson() {
return <String, Object>{
'role_id': role.roleId,
'role_type': roleType.toJson,
};
Expand Down
7 changes: 4 additions & 3 deletions lib/models/payment_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:freezed_annotation/freezed_annotation.dart';

import '../enums/payment_method_enum.dart';
import '../helper/utils/constants.dart';
import '../helper/typedefs.dart';

part 'payment_model.freezed.dart';

Expand All @@ -23,7 +24,7 @@ class PaymentModel with _$PaymentModel {
required List<int>? bookings,
}) = _PaymentModel;

Map<String, dynamic> toUpdateJson({
JSON toUpdateJson({
int? userId,
int? showId,
double? amount,
Expand All @@ -34,7 +35,7 @@ class PaymentModel with _$PaymentModel {
showId == null &&
amount == null &&
paymentMethod == null &&
paymentDatetime == null) return const <String, dynamic>{};
paymentDatetime == null) return const <String, Object>{};
return copyWith(
paymentId: paymentId,
showId: showId ?? this.showId,
Expand All @@ -44,6 +45,6 @@ class PaymentModel with _$PaymentModel {
).toJson();
}

factory PaymentModel.fromJson(Map<String, dynamic> json) =>
factory PaymentModel.fromJson(JSON json) =>
_$PaymentModelFromJson(json);
}
4 changes: 3 additions & 1 deletion lib/models/role_model.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:freezed_annotation/freezed_annotation.dart';

import '../helper/typedefs.dart';

part 'role_model.freezed.dart';
part 'role_model.g.dart';

Expand All @@ -14,6 +16,6 @@ class RoleModel with _$RoleModel {
required String pictureUrl,
}) = _RoleModel;

factory RoleModel.fromJson(Map<String, dynamic> json) =>
factory RoleModel.fromJson(JSON json) =>
_$RoleModelFromJson(json);
}
4 changes: 3 additions & 1 deletion lib/models/seat_model.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:freezed_annotation/freezed_annotation.dart';

import '../helper/typedefs.dart';

part 'seat_model.freezed.dart';
part 'seat_model.g.dart';

Expand All @@ -11,5 +13,5 @@ class SeatModel with _$SeatModel {
required int seatNumber,
}) = _SeatModel;

factory SeatModel.fromJson(Map<String, dynamic> json) => _$SeatModelFromJson(json);
factory SeatModel.fromJson(JSON json) => _$SeatModelFromJson(json);
}
3 changes: 2 additions & 1 deletion lib/models/show_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:clock/clock.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

import 'show_time_model.dart';
import '../helper/typedefs.dart';

part 'show_model.freezed.dart';
part 'show_model.g.dart';
Expand All @@ -25,6 +26,6 @@ class ShowModel with _$ShowModel {
);
}

factory ShowModel.fromJson(Map<String, dynamic> json) =>
factory ShowModel.fromJson(JSON json) =>
_$ShowModelFromJson(json);
}
3 changes: 2 additions & 1 deletion lib/models/show_time_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:clock/clock.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

import '../helper/utils/constants.dart';
import '../helper/typedefs.dart';
import '../enums/show_status_enum.dart';
import '../enums/show_type_enum.dart';

Expand Down Expand Up @@ -34,6 +35,6 @@ class ShowTimeModel with _$ShowTimeModel {
);
}

factory ShowTimeModel.fromJson(Map<String, dynamic> json) =>
factory ShowTimeModel.fromJson(JSON json) =>
_$ShowTimeModelFromJson(json);
}
7 changes: 4 additions & 3 deletions lib/models/theater_model.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:freezed_annotation/freezed_annotation.dart';

import '../helper/utils/constants.dart';
import '../helper/typedefs.dart';
import '../enums/theater_type_enum.dart';
import 'seat_model.dart';

Expand All @@ -23,7 +24,7 @@ class TheaterModel with _$TheaterModel {
required List<SeatModel> blocked,
}) = _TheaterModel;

Map<String, dynamic> toUpdateJson({
JSON toUpdateJson({
String? theaterName,
int? numOfRows,
int? seatsPerRow,
Expand All @@ -37,7 +38,7 @@ class TheaterModel with _$TheaterModel {
theaterType == null &&
missing == null &&
blocked == null
) return const <String, dynamic>{};
) return const <String, Object>{};
return copyWith(
theaterId: theaterId,
numOfRows: numOfRows ?? this.numOfRows,
Expand All @@ -48,6 +49,6 @@ class TheaterModel with _$TheaterModel {
).toJson();
}

factory TheaterModel.fromJson(Map<String, dynamic> json) =>
factory TheaterModel.fromJson(JSON json) =>
_$TheaterModelFromJson(json);
}
3 changes: 2 additions & 1 deletion lib/models/user_booking_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:freezed_annotation/freezed_annotation.dart';

import 'booking_model.dart';
import 'user_booking_show_model.dart';
import '../helper/typedefs.dart';

part 'user_booking_model.freezed.dart';
part 'user_booking_model.g.dart';
Expand All @@ -16,5 +17,5 @@ class UserBookingModel with _$UserBookingModel {
required List<BookingModel> bookings,
}) = _UserBookingModel;

factory UserBookingModel.fromJson(Map<String, dynamic> json) => _$UserBookingModelFromJson(json);
factory UserBookingModel.fromJson(JSON json) => _$UserBookingModelFromJson(json);
}
4 changes: 3 additions & 1 deletion lib/models/user_booking_show_model.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:freezed_annotation/freezed_annotation.dart';

import '../helper/typedefs.dart';
import '../enums/show_type_enum.dart';

part 'user_booking_show_model.freezed.dart';
Expand All @@ -13,5 +15,5 @@ class UserBookingShowModel with _$UserBookingShowModel {
required DateTime showDatetime,
}) = _UserBookingShowModel;

factory UserBookingShowModel.fromJson(Map<String, dynamic> json) => _$UserBookingShowModelFromJson(json);
factory UserBookingShowModel.fromJson(JSON json) => _$UserBookingShowModelFromJson(json);
}
3 changes: 2 additions & 1 deletion lib/models/user_model.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:freezed_annotation/freezed_annotation.dart';

import '../helper/typedefs.dart';
import '../enums/user_role_enum.dart';

part 'user_model.freezed.dart';
Expand All @@ -18,6 +19,6 @@ class UserModel with _$UserModel {
required UserRole role,
}) = _UserModel;

factory UserModel.fromJson(Map<String, dynamic> json) =>
factory UserModel.fromJson(JSON json) =>
_$UserModelFromJson(json);
}
5 changes: 3 additions & 2 deletions lib/models/user_payment_model.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:freezed_annotation/freezed_annotation.dart';

import '../helper/typedefs.dart';
import '../enums/payment_method_enum.dart';

part 'user_payment_model.freezed.dart';
Expand All @@ -16,7 +17,7 @@ class UserPaymentModel with _$UserPaymentModel {
required UserPaymentMovieModel movie,
}) = _UserPaymentModel;

factory UserPaymentModel.fromJson(Map<String, dynamic> json) => _$UserPaymentModelFromJson(json);
factory UserPaymentModel.fromJson(JSON json) => _$UserPaymentModelFromJson(json);
}

@freezed
Expand All @@ -28,7 +29,7 @@ class UserPaymentMovieModel with _$UserPaymentMovieModel {
required String posterUrl,
}) = _UserPaymentMovieModel;

factory UserPaymentMovieModel.fromJson(Map<String, dynamic> json) => _$UserPaymentMovieModelFromJson(json);
factory UserPaymentMovieModel.fromJson(JSON json) => _$UserPaymentMovieModelFromJson(json);
}


6 changes: 3 additions & 3 deletions lib/providers/bookings_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import '../enums/booking_status_enum.dart';

//Helpers
import '../helper/utils/constants.dart';
import '../helper/typedefs.dart';

//Models
import '../models/booking_model.dart';
Expand Down Expand Up @@ -46,10 +47,9 @@ class BookingsProvider {
int? userId,
int? showId,
}) async {
final Map<String, dynamic>? queryParams = <String, dynamic>{
final JSON? queryParams = <String, Object?>{
if (bookingStatus != null) 'booking_status': bookingStatus.toJson,
if (bookingDatetime != null)
'booking_datetime': bookingDatetime.toString(),
if (bookingDatetime != null) 'booking_datetime': bookingDatetime.toString(),
if (userId != null) 'user_id': userId,
if (showId != null) 'show_id': showId,
};
Expand Down
Loading

0 comments on commit 4e46c11

Please sign in to comment.