PrfPeriodicCounter class

A persistent counter that automatically resets at the start of each aligned time period.

PrfPeriodicCounter is designed to track integer counters that reset periodically based on a specified TrackerPeriod. It ensures thread-safe operations using a lock and provides methods to check expiration, reset the counter, and calculate time-related metrics for the current period. This is ideal for scenarios where counters need to be reset at regular intervals, such as daily or weekly counters.

Inheritance

Constructors

PrfPeriodicCounter.new(String key, {required TrackerPeriod period, bool useCache = false})
Constructs a PrfPeriodicCounter with the specified key, period, and optional useCache.

Properties

currentPeriodStart DateTime
Returns the aligned start of the current period (e.g., today at 00:00).
no setter
elapsedInCurrentPeriod Duration
Returns the duration that has elapsed in the current period.
no setter
hashCode int
The hash code for this object.
no setterinherited
lastUpdate BasePrfObject<DateTime>
Provides access to the last update timestamp, considering the caching strategy.
no setterinherited
nextPeriodStart DateTime
Returns the DateTime of the next period start.
no setter
percentElapsed double
Returns the percentage of the period that has elapsed, as a value between 0.0 and 1.0.
no setter
period TrackerPeriod
The period for which the counter is valid.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
timeUntilNextPeriod Duration
Returns the duration until the next period begins.
no setter
useCache bool
Whether to use in-memory cache for performance.
finalinherited
value BasePrfObject<int>
Provides access to the tracked value, considering the caching strategy.
no setterinherited

Methods

clear() Future<void>
Clears both the value and last update timestamp from storage.
inherited
clearValueOnly() Future<void>
Resets the counter value to its fallback value but retains the last update timestamp.
inherited
fallbackValue() int
Provides the fallback value for the counter, which is zero.
inherited
get() Future<int>
Retrieves the tracked value, resetting it if expired.
inherited
getLastUpdateTime() Future<DateTime?>
Retrieves the last update time, or null if never updated.
inherited
hasState() Future<bool>
Checks if either the value or timestamp exists in SharedPreferences.
inherited
increment([int amount = 1]) Future<int>
Increments the counter by amount (default is 1).
inherited
isCurrentlyExpired() Future<bool>
Determines if the tracker is currently expired.
inherited
isExpired(DateTime now, DateTime? last) bool
Determines if the counter is expired based on the current time now and the last update time.
override
isNonZero() Future<bool>
Checks if the counter value is greater than zero.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
peek() Future<int>
Retrieves the value without resetting or updating it.
inherited
raw() Future<int>
Retrieves the current raw counter value without checking expiration.
inherited
reset() Future<void>
Resets the counter value to zero and updates the last update timestamp to the start of the current period.
override
timeSinceLastUpdate() Future<Duration?>
Calculates the duration since the last update, or null if never updated.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited