-
-
Notifications
You must be signed in to change notification settings - Fork 252
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
Plate 46 calc #688
Open
AyushJagaty
wants to merge
6
commits into
wger-project:master
Choose a base branch
from
AyushJagaty:plate-46-calc
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Plate 46 calc #688
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b919e62
User can now customize bar and plate weights
debd328
User can customise bar and plate weights
AyushJagaty 2395284
Ayush Sourav Jagaty - <https://github.com/AyushJagaty>
AyushJagaty 9cfa6c1
configurable plates
AyushJagaty 3c3d245
Merge branch 'wger-project:master' into plate-46-calc
AyushJagaty 78eb620
having issue in shared Preferences
AyushJagaty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{ | ||
"dart.lineLength": 100, | ||
"diffEditor.ignoreTrimWhitespace": true, | ||
"cmake.sourceDirectory": "/Users/ayush/flutter/linux", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import 'package:flutter/material.dart'; | ||
|
||
class PlateConfiguration extends ChangeNotifier { | ||
//olympic standard weights | ||
List<double> _plateWeights = [1.25, 2.5, 5, 10, 15, 20, 25]; | ||
|
||
List<double> get plateWeights => _plateWeights; | ||
|
||
void setPlateWeights(List<double> weights) { | ||
_plateWeights = weights; | ||
notifyListeners(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import 'dart:convert'; | ||
import 'package:flutter/widgets.dart'; | ||
import 'package:shared_preferences/shared_preferences.dart'; | ||
import 'package:wger/helpers/gym_mode.dart'; | ||
|
||
class PlateWeights extends ChangeNotifier{ | ||
bool isMetric = true; | ||
bool plateChoiceExists = false; | ||
bool loadedFromSharedPref = false; | ||
num totalWeight = 0; | ||
num barWeight = 20; | ||
num convertTolbs = 2.205; | ||
num totalWeightInKg = 0; | ||
num barWeightInKg = 20; | ||
List<num> selectedWeights = []; | ||
List<num> kgWeights = [1.25, 2.5, 5, 10, 15, 20, 25]; | ||
List<num> lbsWeights = [2.5, 5, 10, 25, 35, 45]; | ||
List<num> customPlates = []; | ||
late Map<num,int> grouped; | ||
List<num> get data => selectedWeights; | ||
set data(List<num> newData){ | ||
selectedWeights = newData; | ||
//saving data to shared preference | ||
saveIntoSharedPrefs(); | ||
notifyListeners(); | ||
} | ||
Future<void> saveIntoSharedPrefs() async{ | ||
final pref = await SharedPreferences.getInstance(); | ||
//converting List Weights to String | ||
final String selectedPlates = jsonEncode(selectedWeights); | ||
pref.setString('selectedPlates', selectedPlates); | ||
notifyListeners(); | ||
} | ||
|
||
void readPlates() async{ | ||
final pref = await SharedPreferences.getInstance(); | ||
final platePrefData = pref.getString('selectedPlates'); | ||
if(platePrefData != null){ | ||
try{ | ||
final plateData = json.decode(platePrefData); | ||
if(plateData is List){ | ||
selectedWeights = plateData.cast<num>(); | ||
}else{ | ||
throw const FormatException('Not a List'); | ||
} | ||
}catch(e){ | ||
selectedWeights = []; | ||
} | ||
} | ||
print('loaded'); | ||
notifyListeners(); | ||
} | ||
|
||
Future<void> toggleSelection(num x) async{ | ||
if(selectedWeights.contains(x)) { | ||
selectedWeights.remove(x); | ||
}else { | ||
selectedWeights.add(x); | ||
} | ||
final prefs = await SharedPreferences.getInstance(); | ||
prefs.setString('selectedPlates',jsonEncode(selectedWeights)); | ||
notifyListeners(); | ||
} | ||
|
||
void unitChange() { | ||
if(isMetric==false) { | ||
totalWeight = totalWeightInKg; | ||
isMetric = true; | ||
barWeight = barWeightInKg; | ||
} else { | ||
isMetric = false; | ||
totalWeight = totalWeightInKg*2.205; | ||
barWeight = barWeightInKg*2.205; | ||
} | ||
notifyListeners(); | ||
} | ||
|
||
void clear() async{ | ||
selectedWeights.clear(); | ||
final prefs = await SharedPreferences.getInstance(); | ||
prefs.setString('selectedPlates',jsonEncode(selectedWeights)); | ||
notifyListeners(); | ||
} | ||
|
||
void setWeight(num x) { | ||
totalWeight = x; | ||
totalWeightInKg=x; | ||
notifyListeners(); | ||
} | ||
|
||
void calculatePlates() { | ||
selectedWeights.sort(); | ||
customPlates = plateCalculator(totalWeight,barWeight,selectedWeights); | ||
grouped = groupPlates(customPlates); | ||
notifyListeners(); | ||
} | ||
|
||
void resetPlates() async{ | ||
selectedWeights = []; | ||
final prefs = await SharedPreferences.getInstance(); | ||
prefs.setString('selectedPlates',jsonEncode(selectedWeights)); | ||
notifyListeners(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_gen/gen_l10n/app_localizations.dart'; | ||
import 'package:flutter_html/flutter_html.dart'; | ||
import 'package:provider/provider.dart'; | ||
import 'package:wger/providers/plate_weights.dart'; | ||
import 'package:wger/providers/user.dart'; | ||
|
||
class AddPlateWeights extends StatefulWidget { | ||
const AddPlateWeights({super.key}); | ||
|
||
@override | ||
State<AddPlateWeights> createState() => _AddPlateWeightsState(); | ||
} | ||
|
||
class _AddPlateWeightsState extends State<AddPlateWeights> with SingleTickerProviderStateMixin{ | ||
late AnimationController _controller; | ||
late Animation<Offset> _animation; | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
WidgetsBinding.instance.addPostFrameCallback((_){ | ||
Provider.of<PlateWeights>(context,listen: false).readPlates(); | ||
}); | ||
_controller = AnimationController( | ||
vsync: this, | ||
duration: Duration(seconds: 1), | ||
); | ||
_animation = Tween<Offset>( | ||
begin: const Offset(-1.0, 0.0), // Start off-screen | ||
end: const Offset(0.0, 0.0), // End at original position | ||
).animate(CurvedAnimation( | ||
parent: _controller, | ||
curve: Curves.easeInOut, | ||
)); | ||
|
||
_controller.forward(); // Start the animation | ||
} | ||
|
||
@override | ||
void dispose() { | ||
_controller.dispose(); | ||
super.dispose(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
bool unit = true; | ||
return Consumer2<PlateWeights,UserProvider> ( | ||
builder:(context,plateProvider,userProvider,child)=> Scaffold ( | ||
appBar: AppBar ( | ||
title: const Text('Select Available Plates'), | ||
), | ||
body: Column ( | ||
children: [ | ||
Row ( | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
children: [ | ||
const Text('Preferred Unit'), | ||
DropdownButton ( | ||
onChanged: (newValue){ | ||
plateProvider.clear(); | ||
if(newValue=='kg') { | ||
unit = true; | ||
} else { | ||
unit = false; | ||
} | ||
print(unit); | ||
if(unit != userProvider.profile?.isMetric) { | ||
userProvider.unitChange(); | ||
//plateProvider.unitChange(); | ||
_controller.reset(); | ||
_controller.forward(); | ||
} | ||
}, | ||
items: ['kg','lbs'].map((unit){ | ||
return DropdownMenuItem<String> ( | ||
value: unit, | ||
child: Text(unit), | ||
); | ||
}).toList(), | ||
), | ||
], | ||
), | ||
SingleChildScrollView ( | ||
scrollDirection: Axis.horizontal, | ||
child: Row ( | ||
children: (userProvider.profile?.weightUnitStr == 'kg') | ||
? plateProvider.kgWeights.map((number) { | ||
return SlideTransition( | ||
position: _animation, | ||
child: GestureDetector( | ||
onTap: () => plateProvider.toggleSelection(number), | ||
child: Padding( | ||
padding: const EdgeInsets.all(8.0), | ||
child: Container ( | ||
margin: const EdgeInsets.all(8), | ||
padding: const EdgeInsets.all(16), | ||
decoration: BoxDecoration ( | ||
color: plateProvider.selectedWeights.contains(number) | ||
? const Color.fromARGB(255, 82, 226, 236) | ||
: const Color.fromARGB(255, 97, 105, 101), | ||
borderRadius: BorderRadius.circular(10), | ||
), | ||
child: Text ( | ||
'$number kg', // Add unit to text | ||
style: const TextStyle( | ||
color: Colors.white, | ||
fontWeight: FontWeight.bold, | ||
), | ||
), | ||
), | ||
), | ||
), | ||
); | ||
}).toList() | ||
: plateProvider.lbsWeights.map((number) { | ||
return SlideTransition( | ||
position: _animation, | ||
child: GestureDetector( | ||
onTap: () => plateProvider.toggleSelection(number), | ||
child: Padding( | ||
padding: const EdgeInsets.all(8.0), | ||
child: Container( | ||
margin: const EdgeInsets.all(8), | ||
padding: const EdgeInsets.all(16), | ||
decoration: BoxDecoration ( | ||
color: plateProvider.selectedWeights.contains(number) | ||
? const Color.fromARGB(255, 82, 226, 236) | ||
: const Color.fromARGB(255, 97, 105, 101), | ||
borderRadius: BorderRadius.circular(10), | ||
), | ||
child: Text ( | ||
'$number lbs', // Add unit to text | ||
style: const TextStyle( | ||
color: Colors.white, | ||
fontWeight: FontWeight.bold, | ||
), | ||
), | ||
), | ||
), | ||
), | ||
); | ||
}).toList(), | ||
), | ||
), | ||
Row( | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
children: [ | ||
TextButton( | ||
onPressed: (){ | ||
plateProvider.saveIntoSharedPrefs(); | ||
if(plateProvider.selectedWeights.isNotEmpty){ | ||
plateProvider.plateChoiceExists=true; | ||
plateProvider.calculatePlates(); | ||
} | ||
Navigator.pop(context); | ||
}, | ||
child: const Text('Done'), | ||
), | ||
ElevatedButton( | ||
onPressed: (){ | ||
plateProvider.resetPlates(); | ||
}, | ||
child: const Text('Reset',style: TextStyle(color: Colors.red,fontWeight: FontWeight.bold)) | ||
), | ||
], | ||
), | ||
] | ||
), | ||
), | ||
); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make sure there is enough margin on the right side of the screen as well (or wrap around if there's not enough space). Also I would name the dropdown something like "preferred unit". If you want, you can extract this info from the profile from the UserProvider (the isMetric).