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

Provider #170

Merged
merged 7 commits into from
Sep 27, 2024
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
28 changes: 13 additions & 15 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:logger/logger.dart';
// import 'package:logger/logger.dart';
dhruv282 marked this conversation as resolved.
Show resolved Hide resolved
import 'package:openhiit/pages/home/home.dart';
import 'package:openhiit/providers/workout_provider.dart';
import 'package:permission_handler/permission_handler.dart';

// Global logger instance for logging messages
var logger = Logger(
printer: PrettyPrinter(methodCount: 0),
);
import 'package:provider/provider.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();
Expand Down Expand Up @@ -39,16 +36,17 @@ void main() async {
class WorkoutTimer extends StatelessWidget {
const WorkoutTimer({super.key});

/// Application root.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'OpenHIIT',
debugShowCheckedModeBanner: false,
theme: ThemeData(),
darkTheme: ThemeData.dark(), // standard dark theme
themeMode: ThemeMode.system,
home: const MyHomePage(),
);
return MultiProvider(
providers: [ChangeNotifierProvider(create: (_) => WorkoutProvider())],
child: MaterialApp(
title: 'OpenHIIT',
debugShowCheckedModeBanner: false,
theme: ThemeData(),
darkTheme: ThemeData.dark(), // standard dark theme
themeMode: ThemeMode.system,
home: const MyHomePage(),
));
}
}
56 changes: 29 additions & 27 deletions lib/pages/active_timer/workout.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,24 @@ import '../../models/lists/list_model_animated.dart';
import '../../models/lists/list_tile_model.dart';

class StartWorkout extends StatelessWidget {
const StartWorkout({super.key});
const StartWorkout({super.key, required this.workout});

final Workout workout;

@override
Widget build(BuildContext context) {
return const Scaffold(
return Scaffold(
body: Center(
child: CountDownTimer(),
child: CountDownTimer(workout: workout),
),
);
}
}

class CountDownTimer extends StatefulWidget {
const CountDownTimer({super.key});
const CountDownTimer({super.key, required this.workout});

final Workout workout;

@override
CountDownTimerState createState() => CountDownTimerState();
Expand Down Expand Up @@ -160,12 +164,10 @@ class CountDownTimerState extends State<CountDownTimer>
WidgetsBinding.instance.renderViews.first.automaticSystemUiAdjustment =
false;

Workout workoutArgument =
ModalRoute.of(context)!.settings.arguments as Workout;
Workout workout = widget.workout;

List<dynamic> exercises = workoutArgument.exercises != ""
? jsonDecode(workoutArgument.exercises)
: [];
List<dynamic> exercises =
workout.exercises != "" ? jsonDecode(workout.exercises) : [];

final GlobalKey<AnimatedListState> listKey = GlobalKey<AnimatedListState>();

Expand All @@ -177,7 +179,7 @@ class CountDownTimerState extends State<CountDownTimer>
shouldReset = false;
intervalInfo = ListModel<ListTileModel>(
listKey: listKey,
initialItems: listItems(exercises, workoutArgument),
initialItems: listItems(exercises, workout),
removedItemBuilder: _buildRemovedItem,
);
intervalTotal = intervalInfo.length;
Expand Down Expand Up @@ -296,19 +298,19 @@ class CountDownTimerState extends State<CountDownTimer>

return Countdown(
controller: _workoutController,
iterations: workoutArgument.iterations,
workSeconds: workoutArgument.workTime,
restSeconds: workoutArgument.restTime,
breakSeconds: workoutArgument.breakTime,
getreadySeconds: workoutArgument.getReadyTime,
warmupSeconds: workoutArgument.warmupTime,
cooldownSeconds: workoutArgument.cooldownTime,
workSound: workoutArgument.workSound,
restSound: workoutArgument.restSound,
completeSound: workoutArgument.completeSound,
countdownSound: workoutArgument.countdownSound,
halfwaySound: workoutArgument.halfwaySound,
numberOfWorkIntervals: workoutArgument.numExercises,
iterations: workout.iterations,
workSeconds: workout.workTime,
restSeconds: workout.restTime,
breakSeconds: workout.breakTime,
getreadySeconds: workout.getReadyTime,
warmupSeconds: workout.warmupTime,
cooldownSeconds: workout.cooldownTime,
workSound: workout.workSound,
restSound: workout.restSound,
completeSound: workout.completeSound,
countdownSound: workout.countdownSound,
halfwaySound: workout.halfwaySound,
numberOfWorkIntervals: workout.numExercises,
onFinished: () {
WidgetsBinding.instance.addPostFrameCallback((_) {
if (intervalInfo.length == 1) {
Expand All @@ -333,11 +335,11 @@ class CountDownTimerState extends State<CountDownTimer>
if (timerData.status == "complete" && restart == false) {
done = true;
} else if (timerData.status == "start" &&
timerData.iterations == workoutArgument.iterations) {
timerData.iterations == workout.iterations) {
currentWorkInterval = 0;
ListModel<ListTileModel> intervalList = ListModel<ListTileModel>(
listKey: listKey,
initialItems: listItems(exercises, workoutArgument),
initialItems: listItems(exercises, workout),
removedItemBuilder: _buildRemovedItem,
);

Expand Down Expand Up @@ -437,7 +439,7 @@ class CountDownTimerState extends State<CountDownTimer>
timerText(
timerData.currentMicroSeconds
.toString(),
workoutArgument),
workout),
maxLines: 1,
minFontSize: 20,
maxFontSize: 20000,
Expand Down Expand Up @@ -712,7 +714,7 @@ class CountDownTimerState extends State<CountDownTimer>
child: AutoSizeText(
timerText(
timerData.currentMicroSeconds.toString(),
workoutArgument),
workout),
maxLines: 1,
minFontSize: 20,
maxFontSize: 20000,
Expand Down
44 changes: 13 additions & 31 deletions lib/pages/home/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import 'package:openhiit/constants/snackbars.dart';
import 'package:openhiit/models/workout_type.dart';
import 'package:openhiit/pages/select_timer/select_timer.dart';
import 'package:openhiit/pages/view_workout/view_workout.dart';
import 'package:openhiit/pages/view_workout/widgets/fab_column.dart';
import 'package:openhiit/pages/home/widgets/fab_column.dart';
import 'package:openhiit/providers/workout_provider.dart';
import 'package:openhiit/utils/database/database_manager.dart';
import 'package:openhiit/utils/functions.dart';
import 'package:openhiit/utils/import_export/local_file_util.dart';
import 'package:openhiit/widgets/home/export_bottom_sheet.dart';
import 'package:openhiit/widgets/home/timer_list_tile.dart';
import 'package:openhiit/widgets/loader.dart';
import 'package:provider/provider.dart';
import 'package:share_plus/share_plus.dart';
import 'package:sqflite/sqflite.dart';

Expand All @@ -31,16 +33,14 @@ class _MyHomePageState extends State<MyHomePage> {
///
List<Workout> reorderableWorkoutList = [];

/// The initial list of workouts to be loaded fresh
/// from the DB.
///
late Future<List<Workout>> workouts;
late WorkoutProvider workoutProvider;

/// Initialize...
@override
void initState() {
super.initState();
workouts = DatabaseManager().lists(DatabaseManager().initDB());

workoutProvider = Provider.of<WorkoutProvider>(context, listen: false);
}
// ---

Expand Down Expand Up @@ -95,22 +95,11 @@ class _MyHomePageState extends State<MyHomePage> {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const ViewWorkout(),

/// Pass the [tappedWorkout] as an argument to
/// the ViewWorkout page.
settings: RouteSettings(
arguments: tappedWorkout,
builder: (context) => ViewWorkout(
workout: tappedWorkout,
),
),
).then((value) {
/// When we come back to the hompage, refresh the
/// list of workouts by reloading from the DB.
///
setState(() {
workouts = DatabaseManager().lists(DatabaseManager().initDB());
});
});
);
}
// ---

Expand Down Expand Up @@ -225,14 +214,7 @@ class _MyHomePageState extends State<MyHomePage> {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const SelectTimer()),
).then((value) {
/// When we come back to the hompage, refresh the
/// list of workouts by reloading from the DB.
///
setState(() {
workouts = DatabaseManager().lists(DatabaseManager().initDB());
});
});
);
}
// ---

Expand All @@ -253,7 +235,7 @@ class _MyHomePageState extends State<MyHomePage> {
exporting = true;
});

List<Workout> loadedWorkouts = await workouts;
List<Workout> loadedWorkouts = workoutProvider.workouts;

LocalFileUtil fileUtil = LocalFileUtil();

Expand Down Expand Up @@ -297,7 +279,7 @@ class _MyHomePageState extends State<MyHomePage> {
setState(() {
exporting = true;
});
List<Workout> loadedWorkouts = await workouts;
List<Workout> loadedWorkouts = workoutProvider.workouts;

LocalFileUtil fileUtil = LocalFileUtil();

Expand Down Expand Up @@ -398,7 +380,7 @@ class _MyHomePageState extends State<MyHomePage> {
padding: const EdgeInsets.all(8.0),
child: SizedBox(
child: FutureBuilder(
future: workouts,
future: workoutProvider.loadWorkoutData(),
builder:
(BuildContext context, AsyncSnapshot snapshot) {
/// When [workouts] has successfully loaded.
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/set_sounds.dart/widgets/sound_dropdown.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:flutter/material.dart';
import 'package:soundpool/soundpool.dart';
import '../constants/sound_name_map.dart';
import '../constants/sounds.dart';
a-mabe marked this conversation as resolved.
Show resolved Hide resolved

/// Possible interval states
// enum IntervalStates { start, work, rest, complete }
Expand Down
20 changes: 13 additions & 7 deletions lib/pages/view_workout/view_workout.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:convert';
import 'dart:io';
import 'package:openhiit/pages/home/home.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:uuid/uuid.dart';
import 'package:flutter/material.dart';
Expand All @@ -15,7 +16,8 @@ import '../../models/lists/list_tile_model.dart';
import '../active_timer/workout.dart';

class ViewWorkout extends StatefulWidget {
const ViewWorkout({super.key});
final Workout workout;
const ViewWorkout({super.key, required this.workout});
@override
ViewWorkoutState createState() => ViewWorkoutState();
}
Expand Down Expand Up @@ -65,9 +67,7 @@ class ViewWorkoutState extends State<ViewWorkout> {

@override
Widget build(BuildContext context) {
/// Extracting the Workout object from the route arguments.
///
Workout workout = ModalRoute.of(context)!.settings.arguments as Workout;
Workout workout = widget.workout;

/// Parsing the exercises data from the Workout object.
///
Expand Down Expand Up @@ -111,7 +111,7 @@ class ViewWorkoutState extends State<ViewWorkout> {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const CountDownTimer(),
builder: (context) => CountDownTimer(workout: workout),
settings: RouteSettings(
arguments: workout,
),
Expand All @@ -128,8 +128,14 @@ class ViewWorkoutState extends State<ViewWorkout> {
? 40
: 80,
onDelete: () {
deleteList(workout).then((value) => Navigator.pop(context));
Navigator.of(context).pop();
deleteList(workout).then((value) {
if (context.mounted) {
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (_) => const MyHomePage()),
(route) => false);
}
});
},
onEdit: () {
Workout workoutCopy = workout.copy();
Expand Down
Loading