Skip to content

Commit

Permalink
docs: Add Periodicity enum docs
Browse files Browse the repository at this point in the history
  • Loading branch information
enrique-lozano committed Nov 9, 2024
1 parent 6706e55 commit ca313b2
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions lib/core/models/date-utils/periodicity.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import 'package:flutter/widgets.dart';
import 'package:monekin/i18n/translations.g.dart';

/// Enum representing different time periods or intervals.
enum Periodicity {
day,
week,
month,
year;

/// Returns a localized string representing the current period,
/// with support for singular or plural form based on [isPlural].
///
/// [context] - The [BuildContext] for localization access.
/// [isPlural] - Boolean indicating whether to return the plural form.
///
/// For example, if the enum value is [Periodicity.day] and [isPlural] is true,
/// it might return "days" in the appropriate language.
String periodText(BuildContext context, {required bool isPlural}) {
final t = Translations.of(context);
final n = isPlural ? 2 : 1;
Expand All @@ -24,6 +33,12 @@ enum Periodicity {
return '';
}

/// Returns a localized string for describing "all periods" of the current [Periodicity] value.
///
/// [context] - The [BuildContext] for localization access.
///
/// For example, if the enum value is [Periodicity.week], this method might return
/// "weekly" in the appropriate language.
String allThePeriodsText(BuildContext context) {
final t = Translations.of(context);

Expand All @@ -40,6 +55,11 @@ enum Periodicity {
return '';
}

/// Conversion factors between different periodicity units.
///
/// This map stores conversion factors between two [Periodicity] values, with the key
/// being a tuple (from, to) and the value being the factor. If a conversion isn't found
/// in this map, it may be calculated by inverting an existing factor.
static const Map<(Periodicity, Periodicity), double> _conversionFactors = {
(Periodicity.day, Periodicity.week): 7,
(Periodicity.day, Periodicity.month): 30,
Expand All @@ -49,15 +69,22 @@ enum Periodicity {
(Periodicity.month, Periodicity.year): 12,
};

/// Retrieves the conversion factor to convert from one [Periodicity] unit to another.
///
/// [from] - The source [Periodicity] unit.
/// [to] - The target [Periodicity] unit.
///
/// Returns a [double] representing the conversion factor, or 1 if the units
/// are the same or an appropriate conversion factor cannot be found.
static double getConversionFactor(Periodicity from, Periodicity to) {
if (from == to) return 1;

final direct = _conversionFactors[(from, to)];
if (direct != null) return direct;

final inverse = _conversionFactors[(to, from)];
if (inverse != null) return 1 / inverse;

return 1; // Default case
}
}

0 comments on commit ca313b2

Please sign in to comment.