Skip to content

Commit

Permalink
Refactor: views code and project style (#158)
Browse files Browse the repository at this point in the history
- reorganize views code
- make fields and locals final
- remove unnecessary `sync*`
  - see: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo#avoid-syncasync
- remove unnecessary post frame callback
- add analysis options on `analysis_options.yaml`
  • Loading branch information
FriesI23 authored Apr 8, 2024
1 parent 68d59ed commit 6c4b686
Show file tree
Hide file tree
Showing 98 changed files with 1,008 additions and 999 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ documentation and project management.
If you have relevant knowledge and are willing to contribute to this project,
you can help me improve the documentation, e.g `README.md` file.

When contribute code to this project, please try to follow
[this][style-guide-for-flutter] guideline.

## Donate

[!["Buy Me A Coffee"][buymeacoffee-badge]](https://www.buymeacoffee.com/d49cb87qgww)
Expand Down Expand Up @@ -146,3 +149,4 @@ limitations under the License.
[eth-addr]: https://etherscan.io/address/0x35FC877Ef0234FbeABc51ad7fC64D9c1bE161f8F
[btc-badge]: https://img.shields.io/badge/Bitcoin-000000?style=for-the-badge&logo=bitcoin&logoColor=white
[btc-addr]: https://blockchair.com/bitcoin/address/bc1qz2vjews2fcscmvmcm5ctv47mj6236x9p26zk49
[style-guide-for-flutter]: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo
47 changes: 46 additions & 1 deletion analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,54 @@ linter:
rules:
# avoid_print: false # Uncomment to disable the `avoid_print` rule
# prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule
# FIXME: ignore with 3.13.x's linter bug, see https://github.com/dart-lang/linter/issues/4753
# FIXME: ignore with 3.13.x's linter bug.
# see https://github.com/dart-lang/linter/issues/4753
use_build_context_synchronously: false
# Prefer relative imports for files in lib/.
# see: https://dart.dev/tools/linter-rules/prefer_relative_imports
prefer_relative_imports: true
# Private field could be final.
# see: https://dart.dev/tools/linter-rules/prefer_final_fields
prefer_final_fields: true
# Prefer final for variable declarations if they are not reassigned.
# see: https://dart.dev/tools/linter-rules/prefer_final_locals
prefer_final_locals: true
# Don't use the Null type, unless you are positive that you don't want void.
# see: https://dart.dev/tools/linter-rules/prefer_void_to_null
prefer_void_to_null: true
# Test type arguments in operator ==(Object other).
# see: https://dart.dev/tools/linter-rules/test_types_in_equals
test_types_in_equals: true
# Declare method return types.
# see: https://dart.dev/tools/linter-rules/always_declare_return_types
always_declare_return_types: true
# Avoid annotating types for function expression parameters.
# see: https://dart.dev/tools/linter-rules/avoid_types_on_closure_parameters
avoid_types_on_closure_parameters: true
# Sort combinator names alphabetically.
# see: https://dart.dev/tools/linter-rules/combinators_ordering
combinators_ordering: true
# Adhere to Effective Dart Guide directives sorting conventions.
# see: https://dart.dev/tools/linter-rules/directives_ordering
directives_ordering: true
# Put a single newline at end of file.
# see: https://dart.dev/tools/linter-rules/eol_at_end_of_file
eol_at_end_of_file: true
# Missing whitespace between adjacent strings.
# see: https://dart.dev/tools/linter-rules/missing_whitespace_between_adjacent_strings
missing_whitespace_between_adjacent_strings: true
# Don't create a lambda when a tear-off will do.
# see: https://dart.dev/tools/linter-rules/unnecessary_lambdas
unnecessary_lambdas: true
# Use enums rather than classes that behave like enums.
# see: https://dart.dev/tools/linter-rules/use_enums
use_enums: true
# Use string buffers to compose strings.
# see: https://dart.dev/tools/linter-rules/use_string_buffers
use_string_buffers: true
# Start the name of the method with to/_to or as/_as if applicable.
# see: https://dart.dev/tools/linter-rules/use_to_and_as_if_applicable
use_to_and_as_if_applicable: true

# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options
4 changes: 1 addition & 3 deletions lib/annotation/contributor_converter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ class ContributorCollectionConverter
const ContributorCollectionConverter();

Iterable<ContributorInfo> buildDefinedContributors(Iterable rawData) =>
rawData
.whereType<Map<String, dynamic>>()
.map((e) => ContributorInfo.fromJson(e));
rawData.whereType<Map<String, dynamic>>().map(ContributorInfo.fromJson);

Iterable<ContributorInfo> buildContributors(Iterable rawData,
[Map<int, ContributorInfo> definedContributors = const {}]) sync* {
Expand Down
28 changes: 14 additions & 14 deletions lib/common/math.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@ num intervalTrans(

num habitGrowCurve(
{int days = 30, num x = 1, Tuple2<num, num> interv = _defualtInterv}) {
num x0 = 0;
num k = 0.4;
num minX = -10;
num maxX = 10;
const num x0 = 0;
const num k = 0.4;
const num minX = -10;
const num maxX = 10;

var newX = intervalTrans(
final newX = intervalTrans(
x,
itervFrom: Tuple2(0, days),
itervTo: Tuple2(minX, maxX),
itervTo: const Tuple2(minX, maxX),
);

var y = 1 / (1 + math.exp(-k * (newX - x0)));
final y = 1 / (1 + math.exp(-k * (newX - x0)));
return intervalTrans(
y,
itervFrom: interv,
Expand All @@ -53,21 +53,21 @@ num habitGrowCurve(

num habitCrowCurveInverse(
{int days = 30, num y = 0, Tuple2<num, num> interv = _defualtInterv}) {
num x0 = 0;
num k = 0.4;
num minX = -10;
num maxX = 10;
const num x0 = 0;
const num k = 0.4;
const num minX = -10;
const num maxX = 10;

var newY = intervalTrans(
final newY = intervalTrans(
y,
itervFrom: _defualtInterv,
itervTo: interv,
);

var x = x0 - math.log(1 / newY - 1) / k;
final x = x0 - math.log(1 / newY - 1) / k;
return intervalTrans(
x,
itervFrom: Tuple2(minX, maxX),
itervFrom: const Tuple2(minX, maxX),
itervTo: Tuple2(0, days),
);
}
Expand Down
26 changes: 13 additions & 13 deletions lib/common/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ ThemeMode transToMaterialThemeType(AppThemeType themeType) {

Iterable<T> combineIterables<T>(Iterable<T> first, Iterable<T> second,
{required int Function(T a, T b) compare}) sync* {
var firstIterator = first.iterator;
var secondIterator = second.iterator;
final firstIterator = first.iterator;
final secondIterator = second.iterator;

var firstHasNext = firstIterator.moveNext();
var secondHasNext = secondIterator.moveNext();

while (firstHasNext && secondHasNext) {
var firstElement = firstIterator.current;
var secondElement = secondIterator.current;
final firstElement = firstIterator.current;
final secondElement = secondIterator.current;

if (compare(firstElement, secondElement) <= 0) {
yield firstElement;
Expand Down Expand Up @@ -113,18 +113,18 @@ class TemplateString {
}

String format(Map<String, dynamic> params) {
String result = '';
final result = StringBuffer();

int fixedComponent = 0;
for (int i = 0; i < totalComponents; i++) {
for (var i = 0; i < totalComponents; i++) {
if (genericComponents.containsKey(i)) {
result += params[genericComponents[i]].toString();
continue;
result.write(params[genericComponents[i]]);
} else {
result.write(fixedComponents[fixedComponent++]);
}
result += fixedComponents[fixedComponent++];
}

return result;
return result.toString();
}
}

Expand All @@ -136,12 +136,12 @@ String truncateString(String s, int x, int l, int r, {String midStr = "..."}) {
}

String genHabitUUID() {
var uuid = const Uuid();
const uuid = Uuid();
return uuid.v4();
}

String genRecordUUID() {
var uuid = const Uuid();
const uuid = Uuid();
return uuid.v4();
}

Expand Down Expand Up @@ -178,7 +178,7 @@ Iterable<Tuple2<int, int>> getContinuousRanges(List<int> input) sync* {

String? encodeUrlQueryParameters(Map<String, String> params) {
return params.entries
.map((MapEntry<String, String> e) =>
.map((e) =>
'${Uri.encodeComponent(e.key)}=${Uri.encodeComponent(e.value)}')
.join('&');
}
Expand Down
2 changes: 1 addition & 1 deletion lib/component/widgets/color_display_chip.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ColorDisplayChip extends StatelessWidget {

@override
Widget build(BuildContext context) {
CustomColors? colorData = Theme.of(context).extension<CustomColors>();
final CustomColors? colorData = Theme.of(context).extension<CustomColors>();
return IntrinsicHeight(
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
Expand Down
7 changes: 3 additions & 4 deletions lib/component/widgets/crypto_donate_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,9 @@ class CryptoDonateButton extends StatelessWidget {
backgroundColor: color,
iconColor: const MaterialStatePropertyAll(Colors.white),
overlayColor: MaterialStateProperty.resolveWith<Color?>(
(Set<MaterialState> states) =>
states.contains(MaterialState.pressed)
? color.value.darken(0.1)
: null,
(states) => states.contains(MaterialState.pressed)
? color.value.darken(0.1)
: null,
),
);

Expand Down
10 changes: 5 additions & 5 deletions lib/component/widgets/data_container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ class DateContainer extends StatelessWidget {

@override
Widget build(BuildContext context) {
ThemeData theme = Theme.of(context);
final ThemeData theme = Theme.of(context);
var labelStyle = theme.textTheme.labelMedium
?.copyWith(color: theme.colorScheme.onSurface);

var today = DateTime.now();
var showDate = date ?? today;
var content = <Widget>[];
final today = DateTime.now();
final showDate = date ?? today;
final content = <Widget>[];
if (showDate.isSameDate(today)) {
labelStyle = labelStyle?.copyWith(color: theme.colorScheme.primary);
} else if (showDate.difference(today).inDays.abs() == 1) {
labelStyle = labelStyle?.copyWith(color: theme.colorScheme.secondary);
}
var localeString = Localizations.localeOf(context).toLanguageTag();
final localeString = Localizations.localeOf(context).toLanguageTag();
content.addAll([
Text(DateFormat("MM/dd", localeString).format(showDate),
style: labelStyle),
Expand Down
10 changes: 5 additions & 5 deletions lib/component/widgets/date_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ class _HabitDatetimePickerDialog extends State<HabitDatetimePickerDialog>
entryModeButton: entryModeButton,
);

bool isTodaySelected = _selectedDate.value == widget.currentDate;
final bool isTodaySelected = _selectedDate.value == widget.currentDate;
final Widget todayChip = ChoiceChip(
iconTheme: IconThemeData(color: colorScheme.primary),
avatar:
Expand All @@ -320,7 +320,7 @@ class _HabitDatetimePickerDialog extends State<HabitDatetimePickerDialog>
onSelected: (value) => _handleDateChanged(widget.currentDate),
);

bool isTomorrowSelected = _selectedDate.value == tomorrowDate;
final bool isTomorrowSelected = _selectedDate.value == tomorrowDate;
final Widget tomorrowChip = ChoiceChip(
iconTheme: IconThemeData(color: colorScheme.primary),
avatar: isTomorrowSelected
Expand All @@ -334,7 +334,7 @@ class _HabitDatetimePickerDialog extends State<HabitDatetimePickerDialog>
onSelected: (value) => _handleDateChanged(tomorrowDate),
);

bool isNextDateSelected = _selectedDate.value == nextDate;
final bool isNextDateSelected = _selectedDate.value == nextDate;
final Widget nextDateChip = ChoiceChip(
iconTheme: IconThemeData(color: colorScheme.primary),
avatar: isNextDateSelected
Expand All @@ -357,7 +357,7 @@ class _HabitDatetimePickerDialog extends State<HabitDatetimePickerDialog>

Widget? otherDateChip;
if (otherDate != null) {
bool isOtherDateSelected = _selectedDate.value == otherDate;
final bool isOtherDateSelected = _selectedDate.value == otherDate;
otherDateChip = ChoiceChip(
iconTheme: IconThemeData(color: colorScheme.primary),
avatar: isOtherDateSelected
Expand Down Expand Up @@ -404,7 +404,7 @@ class _HabitDatetimePickerDialog extends State<HabitDatetimePickerDialog>
data: MediaQuery.of(context).copyWith(
textScaleFactor: textScaleFactor,
),
child: Builder(builder: (BuildContext context) {
child: Builder(builder: (context) {
switch (orientation) {
case Orientation.portrait:
return Column(
Expand Down
2 changes: 1 addition & 1 deletion lib/component/widgets/habit_calendar_space_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class HabitCalendarSpaceBar extends StatelessWidget {

@override
Widget build(BuildContext context) {
DateTime crtDate = startDate ?? DateTime.now();
final DateTime crtDate = startDate ?? DateTime.now();
int? limitItemCount;

if (endDate == null) {
Expand Down
10 changes: 5 additions & 5 deletions lib/component/widgets/habit_daily_status_container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ class HabitDailyStatusContainer extends StatelessWidget {

@override
Widget build(BuildContext context) {
ThemeData themeData = Theme.of(context);
var habitListTileColor = themeData.extension<HabitSummaryListTileColor>();
var globalColor = themeData.extension<HabitSummaryDailyStatusColor>();
var defaultColor = _getDefaultDailyStatusColor(themeData);
final ThemeData themeData = Theme.of(context);
final habitListTileColor = themeData.extension<HabitSummaryListTileColor>();
final globalColor = themeData.extension<HabitSummaryDailyStatusColor>();
final defaultColor = _getDefaultDailyStatusColor(themeData);

Widget withAutoMarkStatus() {
return Icon(
Expand Down Expand Up @@ -198,7 +198,7 @@ class HabitDailyStatusContainer extends StatelessWidget {

HabitSummaryDailyStatusColor _getDefaultDailyStatusColor(
ThemeData themeData) {
CustomColors? colorData = themeData.extension<CustomColors>();
final CustomColors? colorData = themeData.extension<CustomColors>();
return HabitSummaryDailyStatusColor(
autoMark: colorData?.getColor(colorType),
unknown: themeData.colorScheme.outlineOpacity48,
Expand Down
27 changes: 11 additions & 16 deletions lib/component/widgets/habit_freq_chart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ class HabitFreqChart extends StatelessWidget {
}

List<BarChartGroupData> _buildBarGroupListWithValue() {
var result = <BarChartGroupData>[];
final result = <BarChartGroupData>[];
data.forEachIndexed((i, e) {
var record = e.value, index = data.length - i - 1;
final record = e.value, index = data.length - i - 1;

var includeList = [];
final includeList = [];
var currentHeight = 0.0;
includeList.add({
'fromY': currentHeight,
Expand All @@ -160,12 +160,12 @@ class HabitFreqChart extends StatelessWidget {
});
currentHeight += record.overfulfilTotalValue;

var barRods = <BarChartRodData>[];
final barRods = <BarChartRodData>[];
includeList.forEachIndexed((index, element) {
var radius = index == includeList.length - 1
final radius = index == includeList.length - 1
? kHabitFreqChartTopRadius
: BorderRadius.zero;
var rodData = BarChartRodData(
final rodData = BarChartRodData(
fromY: element["fromY"],
toY: element["toY"],
color: element["color"],
Expand All @@ -175,7 +175,7 @@ class HabitFreqChart extends StatelessWidget {
barRods.add(rodData);
});

var cell = BarChartGroupData(
final cell = BarChartGroupData(
x: index,
groupVertically: true,
barRods: barRods,
Expand Down Expand Up @@ -240,8 +240,8 @@ class HabitFreqChart extends StatelessWidget {
}

Widget _buildBottomTitle(BuildContext context, double value, TitleMeta meta) {
var index = math.max(0, data.length - value - 1).toInt();
var date = data[index].key;
final index = math.max(0, data.length - value - 1).toInt();
final date = data[index].key;
return SideTitleWidget(
axisSide: meta.axisSide,
child: _buildBottomTitleCell(context, date, value),
Expand Down Expand Up @@ -312,13 +312,8 @@ class HabitFreqChart extends StatelessWidget {
tooltipBgColor: Colors.transparent,
tooltipPadding: EdgeInsets.zero,
tooltipMargin: 2,
getTooltipItem: (
BarChartGroupData group,
int groupIndex,
BarChartRodData rod,
int rodIndex,
) {
var value = rod.toY.round();
getTooltipItem: (group, groupIndex, rod, rodIndex) {
final value = rod.toY.round();
if (value == 0) return null;
return BarTooltipItem(
NumberFormat.compact().format(value),
Expand Down
Loading

0 comments on commit 6c4b686

Please sign in to comment.