Skip to content

Commit

Permalink
chore: improvements + doc (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasDevApps authored Apr 15, 2024
1 parent f87c25d commit 4de89da
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 51 deletions.
6 changes: 3 additions & 3 deletions assets/data_test.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
10;13;14;25;1;35;5
1;16;10;6;39;37;2
1;6;25;10;33;39;2
10;13;14;25;1;35;5;15/04/2024
1;16;10;6;39;37;2;18/04/2024
1;6;25;10;33;39;2;22/04/2024
4 changes: 2 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Lottery.initialize(
pathCsv: 'assets\\data_test.csv',
numbersColumn: [1, 2, 3, 4],
specialNumbersColumn: [5, 6, 7],
numberColumnIndexes: [0, 1, 2, 3],
specialNumberColumnIndexes: [4, 5, 6],
);
runApp(const MyApp());
}
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.0.4"
version: "0.0.5"
matcher:
dependency: transitive
description:
Expand Down
37 changes: 27 additions & 10 deletions lib/lottery.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'dart:convert';
import 'dart:io';
import 'dart:math';

import 'package:collection/collection.dart';
import 'package:csv/csv.dart';
import 'package:flutter/material.dart';
import 'package:lottery/src/models/grid_model/grid_model.dart';
Expand Down Expand Up @@ -34,10 +35,20 @@ class Lottery {
}

/// Function to initialize [_instance].
///
/// - [numberColumnIndexes] is a list containing all the indexes
/// of the columns containing the numbers.
///
/// - [specialNumberColumnIndexes] is a list containing all the indexes
/// of the columns containing the special numbers.
///
/// - [dateTimeColumnIndex] is the index of the column containing
/// the date of the draw for the grid (example : 15/04/2024)
static Future<void> initialize({
required String pathCsv,
required List<int> numbersColumn,
required List<int> specialNumbersColumn,
required List<int> numberColumnIndexes,
required List<int> specialNumberColumnIndexes,
int? dateTimeColumnIndex,
Pattern pattern = ';',
}) async {
_instance ??= Lottery._();
Expand All @@ -48,12 +59,15 @@ class Lottery {
// Split field
List<dynamic> fields = (line.first as String).split(pattern);
final grid = GridModel(
numbers: numbersColumn.map((e) {
return int.parse(fields[e - 1]);
}).toSet(),
specialNumbers: specialNumbersColumn.map((e) {
return int.parse(fields[e - 1]);
}).toSet(),
numbers: numberColumnIndexes.map((e) => int.parse(fields[e])).toSet(),
specialNumbers: specialNumberColumnIndexes
.map((e) => int.parse(fields[e]))
.toSet(),
drawnAt: dateTimeColumnIndex != null
? (fields.elementAt(dateTimeColumnIndex) as String)
.split('\r')
.first
: null,
);
_instance!.gridsFromCsv.add(grid);
}
Expand Down Expand Up @@ -83,8 +97,11 @@ class Lottery {
}

/// Function to know if [gridModel] is a winning grid.
bool wasWinningGrid(GridModel gridModel) {
return gridsFromCsv.contains(gridModel);
///
/// If it's the case it will return the grid winner from [gridsFromCsv],
/// otherwise it will return null.
GridModel? wasWinningGrid(GridModel gridModel) {
return gridsFromCsv.firstWhereOrNull((element) => element == gridModel);
}

/// Function to drawn a random [GridModel] while taking into account
Expand Down
16 changes: 16 additions & 0 deletions lib/src/models/grid_model/grid_model.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
import 'package:collection/collection.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

part 'grid_model.freezed.dart';
part 'grid_model.g.dart';

@freezed
class GridModel with _$GridModel {
const GridModel._();

const factory GridModel({
required Set<int> numbers,
required Set<int> specialNumbers,
String? drawnAt,
}) = _GridModel;

factory GridModel.fromJson(Map<String, dynamic> json) =>
_$GridModelFromJson(json);

@override
bool operator ==(Object other) {
return other is GridModel &&
const SetEquality().equals(numbers, other.numbers) &&
const SetEquality().equals(specialNumbers, other.specialNumbers);
}

@override
int get hashCode {
return Object.hash(runtimeType, numbers, specialNumbers);
}
}
52 changes: 28 additions & 24 deletions lib/src/models/grid_model/grid_model.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ GridModel _$GridModelFromJson(Map<String, dynamic> json) {
mixin _$GridModel {
Set<int> get numbers => throw _privateConstructorUsedError;
Set<int> get specialNumbers => throw _privateConstructorUsedError;
String? get drawnAt => throw _privateConstructorUsedError;

Map<String, dynamic> toJson() => throw _privateConstructorUsedError;
@JsonKey(ignore: true)
Expand All @@ -34,7 +35,7 @@ abstract class $GridModelCopyWith<$Res> {
factory $GridModelCopyWith(GridModel value, $Res Function(GridModel) then) =
_$GridModelCopyWithImpl<$Res, GridModel>;
@useResult
$Res call({Set<int> numbers, Set<int> specialNumbers});
$Res call({Set<int> numbers, Set<int> specialNumbers, String? drawnAt});
}

/// @nodoc
Expand All @@ -52,6 +53,7 @@ class _$GridModelCopyWithImpl<$Res, $Val extends GridModel>
$Res call({
Object? numbers = null,
Object? specialNumbers = null,
Object? drawnAt = freezed,
}) {
return _then(_value.copyWith(
numbers: null == numbers
Expand All @@ -62,6 +64,10 @@ class _$GridModelCopyWithImpl<$Res, $Val extends GridModel>
? _value.specialNumbers
: specialNumbers // ignore: cast_nullable_to_non_nullable
as Set<int>,
drawnAt: freezed == drawnAt
? _value.drawnAt
: drawnAt // ignore: cast_nullable_to_non_nullable
as String?,
) as $Val);
}
}
Expand All @@ -74,7 +80,7 @@ abstract class _$$GridModelImplCopyWith<$Res>
__$$GridModelImplCopyWithImpl<$Res>;
@override
@useResult
$Res call({Set<int> numbers, Set<int> specialNumbers});
$Res call({Set<int> numbers, Set<int> specialNumbers, String? drawnAt});
}

/// @nodoc
Expand All @@ -90,6 +96,7 @@ class __$$GridModelImplCopyWithImpl<$Res>
$Res call({
Object? numbers = null,
Object? specialNumbers = null,
Object? drawnAt = freezed,
}) {
return _then(_$GridModelImpl(
numbers: null == numbers
Expand All @@ -100,17 +107,24 @@ class __$$GridModelImplCopyWithImpl<$Res>
? _value._specialNumbers
: specialNumbers // ignore: cast_nullable_to_non_nullable
as Set<int>,
drawnAt: freezed == drawnAt
? _value.drawnAt
: drawnAt // ignore: cast_nullable_to_non_nullable
as String?,
));
}
}

/// @nodoc
@JsonSerializable()
class _$GridModelImpl implements _GridModel {
class _$GridModelImpl extends _GridModel {
const _$GridModelImpl(
{required final Set<int> numbers, required final Set<int> specialNumbers})
{required final Set<int> numbers,
required final Set<int> specialNumbers,
this.drawnAt})
: _numbers = numbers,
_specialNumbers = specialNumbers;
_specialNumbers = specialNumbers,
super._();

factory _$GridModelImpl.fromJson(Map<String, dynamic> json) =>
_$$GridModelImplFromJson(json);
Expand All @@ -132,27 +146,13 @@ class _$GridModelImpl implements _GridModel {
}

@override
String toString() {
return 'GridModel(numbers: $numbers, specialNumbers: $specialNumbers)';
}
final String? drawnAt;

@override
bool operator ==(Object other) {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is _$GridModelImpl &&
const DeepCollectionEquality().equals(other._numbers, _numbers) &&
const DeepCollectionEquality()
.equals(other._specialNumbers, _specialNumbers));
String toString() {
return 'GridModel(numbers: $numbers, specialNumbers: $specialNumbers, drawnAt: $drawnAt)';
}

@JsonKey(ignore: true)
@override
int get hashCode => Object.hash(
runtimeType,
const DeepCollectionEquality().hash(_numbers),
const DeepCollectionEquality().hash(_specialNumbers));

@JsonKey(ignore: true)
@override
@pragma('vm:prefer-inline')
Expand All @@ -167,10 +167,12 @@ class _$GridModelImpl implements _GridModel {
}
}

abstract class _GridModel implements GridModel {
abstract class _GridModel extends GridModel {
const factory _GridModel(
{required final Set<int> numbers,
required final Set<int> specialNumbers}) = _$GridModelImpl;
required final Set<int> specialNumbers,
final String? drawnAt}) = _$GridModelImpl;
const _GridModel._() : super._();

factory _GridModel.fromJson(Map<String, dynamic> json) =
_$GridModelImpl.fromJson;
Expand All @@ -180,6 +182,8 @@ abstract class _GridModel implements GridModel {
@override
Set<int> get specialNumbers;
@override
String? get drawnAt;
@override
@JsonKey(ignore: true)
_$$GridModelImplCopyWith<_$GridModelImpl> get copyWith =>
throw _privateConstructorUsedError;
Expand Down
2 changes: 2 additions & 0 deletions lib/src/models/grid_model/grid_model.g.dart

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

12 changes: 6 additions & 6 deletions lib/src/view/widgets/lottery_random_pick.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class LotteryRandomPick extends StatefulWidget {
class _LotteryRandomPickState extends State<LotteryRandomPick> {
List<int>? numbersDrawn;
List<int>? specialNumbersDrawn;
bool? wasWinningGrid;
GridModel? winningGrid;

Widget displayGridModel() {
return Row(
Expand Down Expand Up @@ -73,7 +73,7 @@ class _LotteryRandomPickState extends State<LotteryRandomPick> {
setState(() {
numbersDrawn = grid.numbers.toList();
specialNumbersDrawn = grid.specialNumbers.toList();
wasWinningGrid = Lottery().wasWinningGrid(grid);
winningGrid = Lottery().wasWinningGrid(grid);
});
},
child: const Text('Drawn'),
Expand All @@ -84,11 +84,11 @@ class _LotteryRandomPickState extends State<LotteryRandomPick> {
child: Column(
children: [
displayGridModel(),
if (wasWinningGrid!) ...[
if (winningGrid != null) ...[
const SizedBox(height: 32),
const Text(
'Grille gagnante !',
style: TextStyle(color: Colors.amber),
Text(
'Grille gagnante ! (TAS le ${winningGrid?.drawnAt ?? '??'})',
style: const TextStyle(color: Colors.amber),
),
],
],
Expand Down
3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
name: lottery
description: "A new Flutter project."
version: 0.0.4
version: 0.0.5

environment:
sdk: '>=3.3.3 <4.0.0'
flutter: ">=1.17.0"

dependencies:
collection: ^1.18.0
csv: ^6.0.0
flutter:
sdk: flutter
Expand Down
19 changes: 15 additions & 4 deletions test/lottery_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ Future<void> _initializeLottery() async {
WidgetsFlutterBinding.ensureInitialized();
await Lottery.initialize(
pathCsv: 'assets/data_test.csv',
numbersColumn: [1, 2, 3, 4],
specialNumbersColumn: [5, 6, 7],
numberColumnIndexes: [0, 1, 2, 3],
specialNumberColumnIndexes: [4, 5, 6],
dateTimeColumnIndex: 7,
);
addTearDown(Lottery().dispose);
}
Expand Down Expand Up @@ -41,6 +42,13 @@ void main() {
await _initializeLottery();
expect(Lottery().specialNumbers, outputsExpected);
});

test('Test initialize drawnAt for each grid', () async {
await _initializeLottery();
expect(Lottery().gridsFromCsv[0].drawnAt, '15/04/2024');
expect(Lottery().gridsFromCsv[1].drawnAt, '18/04/2024');
expect(Lottery().gridsFromCsv[2].drawnAt, '22/04/2024');
});
});

test('Test createListProbabilities', () async {
Expand All @@ -59,7 +67,10 @@ void main() {
numbers: {10, 13, 14, 25},
specialNumbers: {1, 35, 5},
);
expect(Lottery().wasWinningGrid(gridModel), true);
expect(
Lottery().wasWinningGrid(gridModel) == Lottery().gridsFromCsv.first,
true,
);
});

test("Test that it's was NOT a winning grid", () async {
Expand All @@ -68,7 +79,7 @@ void main() {
numbers: {36, 12, 41, 25},
specialNumbers: {1, 35, 5},
);
expect(Lottery().wasWinningGrid(gridModel), false);
expect(Lottery().wasWinningGrid(gridModel), null);
});
});
}
Loading

0 comments on commit 4de89da

Please sign in to comment.