Commit 8522348a authored by Benoît Harrault's avatar Benoît Harrault
Browse files

Merge branch '52-clean-data-provider-code' into 'master'

Resolve "Clean data provider code"

Closes #52

See merge request !47
parents c6ab1240 dcc0d80c
Pipeline #3097 passed with stage
in 32 seconds
org.gradle.jvmargs=-Xmx1536M
android.useAndroidX=true
android.enableJetifier=true
app.versionName=0.0.45
app.versionCode=45
app.versionName=0.0.46
app.versionCode=46
Améliorations du code du fournisseur de données
......@@ -6,53 +6,57 @@ class Data extends ChangeNotifier {
int _tilesCount = 4;
// application data
List _images = [];
String _image = '';
List _availableImages = [];
String _selectedImage = '';
List<MovingTile> _tiles = [];
// application state
bool _isShufflingBoard = false;
bool _displayTipImage = false;
bool _isTipImageDisplayed = false;
String get image => _image;
String get selectedImage => _selectedImage;
set updateImage(String value) {
_image = value;
void updateSelectedImage(String value) {
_selectedImage = value;
notifyListeners();
}
void resetSelectedImage() {
updateSelectedImage('');
}
int get tilesCount => _tilesCount;
set updateTilesCount(int value) {
_tilesCount = value;
void updateTilesCount(int tilesCountPerSide) {
_tilesCount = tilesCountPerSide;
notifyListeners();
}
List get images => _images;
List get availableImages => _availableImages;
set updateImages(List value) {
_images = value;
void updateAvailableImages(List availableImages) {
_availableImages = availableImages;
notifyListeners();
}
List<MovingTile> get tiles => _tiles;
set updateTiles(List<MovingTile> value) {
_tiles = value;
void updateTiles(List<MovingTile> tiles) {
_tiles = tiles;
notifyListeners();
}
bool get isShufflingBoard => _isShufflingBoard;
set updateIsShufflingBoard(bool value) {
_isShufflingBoard = value;
void updateIsShufflingBoard(bool isShuffling) {
_isShufflingBoard = isShuffling;
notifyListeners();
}
bool get displayTipImage => _displayTipImage;
bool get displayTipImage => _isTipImageDisplayed;
set updateDisplayTipImage(bool value) {
_displayTipImage = value;
void updateIsTipImageDisplayed(bool isDisplayed) {
_isTipImageDisplayed = isDisplayed;
notifyListeners();
}
......@@ -76,9 +80,4 @@ class Data extends ChangeNotifier {
notifyListeners();
}
void resetGame() {
_image = '';
notifyListeners();
}
}
......@@ -5,7 +5,7 @@ import 'package:puzzlegame/utils/game_utils.dart';
class Game {
static void toggleDisplayTipImage(Data myProvider) {
myProvider.updateDisplayTipImage = !myProvider.displayTipImage;
myProvider.updateIsTipImageDisplayed(!myProvider.displayTipImage);
}
static Container buildTilesetWidget(Data myProvider) {
......@@ -79,7 +79,7 @@ class Game {
child: Image(
image: AssetImage(
myProvider.displayTipImage
? GameUtils.getImageAssetName(myProvider.image)
? GameUtils.getImageAssetName(myProvider.selectedImage)
: 'assets/icons/tip_hidden.png',
),
fit: BoxFit.contain,
......
......@@ -18,22 +18,22 @@ class Home extends StatelessWidget {
GetImagesList getImagesList;
getImagesList = GetImagesList();
await getImagesList.init();
if (getImagesList.images.isNotEmpty) {
myProvider.updateImages = getImagesList.images;
if (getImagesList.availableImages.isNotEmpty) {
myProvider.updateAvailableImages(getImagesList.availableImages);
shuffleAvailableImages(myProvider);
}
}
Future<void> selectImage(Data myProvider, String imageCode, double tileImageSize) async {
myProvider.updateIsShufflingBoard = true;
myProvider.updateImage = imageCode;
myProvider.updateIsShufflingBoard(true);
myProvider.updateSelectedImage(imageCode);
GameUtils.startGame(myProvider, tileImageSize);
}
void shuffleAvailableImages(Data myProvider) {
List images = myProvider.images;
List images = myProvider.availableImages;
images.shuffle();
myProvider.updateImages = images;
myProvider.updateAvailableImages(images);
}
Container _buildImageSelectorItem(Data myProvider, String image, double tileImageSize) {
......@@ -63,10 +63,10 @@ class Home extends StatelessWidget {
}
Container _buildImageSelector(Data myProvider, double tileImageSize) {
if (myProvider.images.length == 0) {
if (myProvider.availableImages.length == 0) {
getImagesList(myProvider);
}
List images = myProvider.images;
List images = myProvider.availableImages;
return Container(
padding: EdgeInsets.all(2),
......@@ -122,7 +122,7 @@ class Home extends StatelessWidget {
),
),
onPressed: () {
myProvider.updateTilesCount = value;
myProvider.updateTilesCount(value);
},
);
}
......@@ -139,7 +139,7 @@ class Home extends StatelessWidget {
content = Game.buildShufflingIndicatorWidget();
precacheImage(new AssetImage('assets/icons/game_win.png'), context);
} else {
if (_myProvider.image == '') {
if (_myProvider.selectedImage == '') {
content = _buildImageSelector(_myProvider, _tileImageSize);
} else {
content = Game.buildGameWidget(_myProvider);
......@@ -190,7 +190,7 @@ class Home extends StatelessWidget {
return Scaffold(
appBar: AppBar(
backgroundColor: themePrimaryColor,
actions: _myProvider.image == '' ? menuActions : gameActions,
actions: _myProvider.selectedImage == '' ? menuActions : gameActions,
),
body: SafeArea(
child: Center(child: content),
......
......@@ -21,8 +21,8 @@ class GameUtils {
}
static Future<void> resetGame(Data myProvider) async {
myProvider.updateImage = '';
myProvider.updateDisplayTipImage = false;
myProvider.resetSelectedImage();
myProvider.updateIsTipImageDisplayed(false);
}
static bool checkTilesetIsCleared(List<MovingTile> tiles) {
......@@ -59,7 +59,7 @@ class GameUtils {
}
static Future<void> splitImageInTiles(Data myProvider, double tileImageSize) async {
String imageAsset = getImageAssetName(myProvider.image);
String imageAsset = getImageAssetName(myProvider.selectedImage);
Uint8List imageData = (await rootBundle.load(imageAsset)).buffer.asUint8List();
imglib.Image image = imglib.decodeImage(imageData) ?? imglib.Image.fromBytes(1, 1, []);
......@@ -94,7 +94,7 @@ class GameUtils {
y += height;
}
myProvider.updateTiles = shuffleMovingTiles(tiles);
myProvider.updateIsShufflingBoard = false;
myProvider.updateTiles(shuffleMovingTiles(tiles));
myProvider.updateIsShufflingBoard(false);
}
}
......@@ -5,7 +5,7 @@ import 'package:flutter/services.dart';
class GetImagesList {
GetImagesList();
List _images = [];
List _availableImages = [];
init() async {
await imagesFromLocalFile();
......@@ -17,14 +17,14 @@ class GetImagesList {
jsonString = await rootBundle.loadString('assets/files/images.json');
final jsonResponse = await json.decode(jsonString);
List imagesList = jsonResponse['images'];
_images = imagesList;
_availableImages = imagesList;
} catch (e) {
_images = [];
_availableImages = [];
}
// Remove empty images
_images.removeWhere((value) => (value == ''));
_availableImages.removeWhere((value) => (value == ''));
}
List get images => _images;
List get availableImages => _availableImages;
}
......@@ -7,7 +7,7 @@ packages:
name: archive
url: "https://pub.dartlang.org"
source: hosted
version: "3.3.0"
version: "3.3.1"
async:
dependency: transitive
description:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment