Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Toggle challenge time difference days vs minutes #710

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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