Skip to content

Commit

Permalink
Merge pull request #710 from givtnl/improvement/debug-toggle-challeng…
Browse files Browse the repository at this point in the history
…e-difference-days-minutes

Toggle challenge time difference days vs minutes
  • Loading branch information
Daniela510 committed May 2, 2024
2 parents 0d326ce + 43e13f3 commit df88c0a
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ class GenerosityChallengeCubit extends Cubit<GenerosityChallengeState> {
final lastCompletedDateTime =
DateTime.parse(days[lastCompletedDayIndex].dateCompleted);
final diff = lastCompletedDateTime.difference(DateTime.now());
if (diff.inDays != 0) {
final timeDifference =
state.unlockDayTimeDifference == UnlockDayTimeDifference.days
? diff.inDays
: diff.inMinutes;
if (timeDifference != 0) {
return nextActiveDayIndex;
} else {
//no active day yet
Expand Down Expand Up @@ -99,4 +103,14 @@ class GenerosityChallengeCubit extends Cubit<GenerosityChallengeState> {
),
);
}

Future<void> toggleTimeDifference(int index) async {
final newTimeDifference = index == 0
? UnlockDayTimeDifference.days
: UnlockDayTimeDifference.minutes;

emit(state.copyWith(unlockDayTimeDifference: newTimeDifference));
final days = await _generosityChallengeRepository.loadFromCache();
_refreshState(days);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ enum GenerosityChallengeStatus {
completed,
}

enum UnlockDayTimeDifference { minutes, days }

class GenerosityChallengeState extends Equatable {
const GenerosityChallengeState({
this.status = GenerosityChallengeStatus.initial,
this.unlockDayTimeDifference = UnlockDayTimeDifference.days,
this.activeDayIndex = -1,
this.detailedDayIndex = -1,
this.days = const [],
Expand All @@ -19,6 +22,7 @@ class GenerosityChallengeState extends Equatable {
final List<Day> days;
final int activeDayIndex;
final int detailedDayIndex;
final UnlockDayTimeDifference unlockDayTimeDifference;
final GenerosityChallengeStatus status;

bool get hasActiveDay => activeDayIndex != -1;
Expand All @@ -31,15 +35,19 @@ class GenerosityChallengeState extends Equatable {
int? activeDayIndex,
int? detailedDayIndex,
GenerosityChallengeStatus? status,
UnlockDayTimeDifference? unlockDayTimeDifference,
}) {
return GenerosityChallengeState(
days: days ?? this.days,
activeDayIndex: activeDayIndex ?? this.activeDayIndex,
detailedDayIndex: detailedDayIndex ?? this.detailedDayIndex,
status: status ?? this.status,
unlockDayTimeDifference:
unlockDayTimeDifference ?? this.unlockDayTimeDifference,
);
}

@override
List<Object> get props => [days, activeDayIndex, detailedDayIndex, status];
List<Object> get props =>
[days, activeDayIndex, detailedDayIndex, status, unlockDayTimeDifference];
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,25 @@ import 'package:givt_app/features/children/generosity_challenge/utils/generosity
import 'package:givt_app/features/children/generosity_challenge/widgets/day_button.dart';
import 'package:givt_app/features/children/generosity_challenge/widgets/generosity_app_bar.dart';
import 'package:givt_app/utils/utils.dart';
import 'package:package_info_plus/package_info_plus.dart';

class GenerosityChallengeOverview extends StatelessWidget {
class GenerosityChallengeOverview extends StatefulWidget {
const GenerosityChallengeOverview({super.key});

@override
State<GenerosityChallengeOverview> createState() =>
_GenerosityChallengeOverviewState();
}

class _GenerosityChallengeOverviewState
extends State<GenerosityChallengeOverview> {
bool isDebug = false;
@override
void initState() {
super.initState();
_isDebug().then((value) => isDebug = value);
}

@override
Widget build(BuildContext context) {
final challenge = context.watch<GenerosityChallengeCubit>();
Expand Down Expand Up @@ -69,6 +84,30 @@ class GenerosityChallengeOverview extends StatelessWidget {
);
},
),
const Spacer(),
if (isDebug)
ToggleButtons(
borderRadius: const BorderRadius.all(Radius.circular(8)),
selectedBorderColor: Colors.blue[700],
selectedColor: Colors.white,
fillColor: Colors.blue[200],
color: Colors.blue[400],
constraints: const BoxConstraints(
minHeight: 40,
minWidth: 80,
),
isSelected: [
challenge.state.unlockDayTimeDifference ==
UnlockDayTimeDifference.days,
challenge.state.unlockDayTimeDifference ==
UnlockDayTimeDifference.minutes,
],
onPressed: challenge.toggleTimeDifference,
children: [
Text(UnlockDayTimeDifference.days.name),
Text(UnlockDayTimeDifference.minutes.name),
],
),
],
),
],
Expand All @@ -77,6 +116,11 @@ class GenerosityChallengeOverview extends StatelessWidget {
);
}

Future<bool> _isDebug() async {
final info = await PackageInfo.fromPlatform();
return info.packageName.contains('test');
}

Widget _buildGenerosityHeader(String name) => Column(
mainAxisSize: MainAxisSize.min,
children: [
Expand Down

0 comments on commit df88c0a

Please sign in to comment.