PrfActivityCounter class

A utility class for tracking user activity over time across various spans such as hour, day, month, and year. It provides persistent storage and retrieval of activity data, making it suitable for usage statistics, trend analysis, and generating long-term activity reports.

The PrfActivityCounter is designed to be isolate-safe and supports automatic time-based bucketing using the current date and time.

Example usage:

final activityCounter = PrfActivityCounter('user_activity');
await activityCounter.add(5);
final todayCount = await activityCounter.today;
print('Activities today: $todayCount');
Inheritance

Constructors

PrfActivityCounter.new(String key, {bool useCache = false, DateTime clock()?})
Creates a new instance of PrfActivityCounter with the given key.

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
thisHour Future<int>
Returns the activity count for the current hour.
no setter
thisMonth Future<int>
Returns the activity count for the current month.
no setter
thisYear Future<int>
Returns the activity count for the current year.
no setter
today Future<int>
Returns the activity count for the current day.
no setter
useCache bool
Whether to use in-memory cache for performance.
finalinherited

Methods

activeDates(ActivitySpan span) Future<List<DateTime>>
Returns a list of DateTime objects where any activity was tracked for the specified span.
add(int amount) Future<void>
Adds the specified amount to the current time bucket across all spans.
all(ActivitySpan span) Future<Map<int, int>>
Returns a map of non-zero entries for the specified span.
amountFor(ActivitySpan span, DateTime date) Future<int>
Returns the activity count for the specified date in the given span.
amountThis(ActivitySpan span) Future<int>
Returns the activity count for the current time in the specified span.
clear(ActivitySpan span) Future<void>
Clears all data in the specified span.
clearAllKnown(List<ActivitySpan> spans) Future<void>
Clears data for multiple spans specified in spans.
hasAnyData() Future<bool>
Returns true if any activity has ever been recorded.
increment() Future<void>
Increments the activity count by 1 for the current time bucket.
maxValue(ActivitySpan span) Future<int>
Returns the largest value ever recorded for the specified span.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
removeAll() Future<void>
Permanently deletes all stored data for this counter.
reset() Future<void>
Clears all data across all spans.
summary() Future<Map<ActivitySpan, int>>
Returns a summary map of activity counts for all spans at the current time.
toString() String
A string representation of this object.
inherited
total(ActivitySpan span) Future<int>
Returns the total sum of all recorded entries in the specified span.

Operators

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