Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import 'dart:async';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:suguru/config/application_config.dart';
import 'package:suguru/models/activity/cell.dart';
import 'package:suguru/models/activity/cell_location.dart';
import 'package:suguru/models/activity/activity.dart';
import 'package:suguru/utils/board_animate.dart';
part 'activity_state.dart';
class ActivityCubit extends HydratedCubit<ActivityState> {
ActivityCubit()
: super(ActivityState(
currentActivity: Activity.createEmpty(),
));
void updateState(Activity activity) {
emit(ActivityState(
currentActivity: activity,
));
}
void refresh() {
final Activity activity = Activity(
// Settings
activitySettings: state.currentActivity.activitySettings,
// State
isRunning: state.currentActivity.isRunning,
isStarted: state.currentActivity.isStarted,
isFinished: state.currentActivity.isFinished,
animationInProgress: state.currentActivity.animationInProgress,
boardAnimated: state.currentActivity.boardAnimated,
// Base data
board: state.currentActivity.board,
// Game data
boardConflicts: state.currentActivity.boardConflicts,
selectedCell: state.currentActivity.selectedCell,
showConflicts: state.currentActivity.showConflicts,
givenTipsCount: state.currentActivity.givenTipsCount,
buttonTipsCountdown: state.currentActivity.buttonTipsCountdown,
);
// activity.dump();
updateState(activity);
}
void startNewActivity(BuildContext context) {
final ActivitySettingsCubit activitySettingsCubit =
BlocProvider.of<ActivitySettingsCubit>(context);
final Activity newActivity = Activity.createNew(
// Settings
activitySettings: activitySettingsCubit.state.settings,
);
newActivity.dump();
updateState(newActivity);
refresh();
BoardAnimate.startAnimation(this, 'start');
}
void selectCell(CellLocation location) {
state.currentActivity.selectedCell = state.currentActivity.board.get(location);
refresh();
}
void unselectCell() {
state.currentActivity.selectedCell = null;
refresh();
}
void updateCellValue(CellLocation location, int value) {
final cell = state.currentActivity.board.get(location);
if (cell.isFixed == false) {
state.currentActivity.board.setCell(
location,
Cell(
location: location,
blockId: cell.blockId,
value: value,
isFixed: false,
),
);
state.currentActivity.isStarted = true;
refresh();
}
state.currentActivity.updateConflictsInBoard();
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
if (state.currentActivity.checkBoardIsSolved()) {
BoardAnimate.startAnimation(this, 'win');
state.currentActivity.isFinished = true;
refresh();
}
}
void toggleShowConflicts() {
state.currentActivity.showConflicts = !state.currentActivity.showConflicts;
refresh();
}
void increaseGivenTipsCount() {
state.currentActivity.givenTipsCount++;
state.currentActivity.buttonTipsCountdown =
ApplicationConfig.defaultTipCountDownValueInSeconds;
refresh();
const Duration interval = Duration(milliseconds: 500);
Timer.periodic(
interval,
(Timer timer) {
if (state.currentActivity.buttonTipsCountdown == 0) {
timer.cancel();
} else {
state.currentActivity.buttonTipsCountdown =
max(state.currentActivity.buttonTipsCountdown - 1, 0);
}
refresh();
},
);
}
bool canBeResumed() {
return state.currentActivity.canBeResumed;
}
void quitActivity() {
state.currentActivity.isRunning = false;
refresh();
}
void resumeSavedActivity() {
state.currentActivity.isRunning = true;
refresh();
}
void deleteSavedActivity() {
state.currentActivity.isRunning = false;
state.currentActivity.isFinished = true;
refresh();
}
void updateAnimationInProgress(bool animationInProgress) {
state.currentActivity.animationInProgress = animationInProgress;
refresh();
}
void setAnimatedBackground(List animatedCellsPattern) {
for (int row = 0; row < state.currentActivity.board.boardSizeVertical; row++) {
for (int col = 0; col < state.currentActivity.board.boardSizeHorizontal; col++) {
state.currentActivity.boardAnimated[row][col] = animatedCellsPattern[row][col];
}
}
refresh();
}
void resetAnimatedBackground() {
for (int row = 0; row < state.currentActivity.board.boardSizeVertical; row++) {
for (int col = 0; col < state.currentActivity.board.boardSizeHorizontal; col++) {
state.currentActivity.boardAnimated[row][col] = false;
}
}
refresh();
}
@override
ActivityState? fromJson(Map<String, dynamic> json) {
final Activity currentActivity = json['currentActivity'] as Activity;
return ActivityState(
currentActivity: currentActivity,
);
}
@override
Map<String, dynamic>? toJson(ActivityState state) {
return <String, dynamic>{
'currentActivity': state.currentActivity.toJson(),
};
}
}