Skip to content
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

[Feature request] Fetching and Grouping Album Assets by Date #1244

Open
HenrikH96 opened this issue Dec 11, 2024 · 1 comment
Open

[Feature request] Fetching and Grouping Album Assets by Date #1244

HenrikH96 opened this issue Dec 11, 2024 · 1 comment

Comments

@HenrikH96
Copy link

Platforms

dart, Android, iOS

Description

Currently, the albumAssetCount method provides only the total count of assets within an album. While useful, it is insufficient for scenarios where grouping assets by date (e.g., by month) is required — a common feature in many modern image pickers.

I was able to achieve this functionality by combining multiple SQL requests and leveraging asset counts to create custom groupings. However, this approach is not very efficient and may result in performance issues for larger datasets.

Any suggestion on a better way ?

My workaround:

var firstAsset = await album.getAssetListRange(start: 0, end: 1);
var lastAsset =
    await album.getAssetListRange(start: assetCount - 1, end: assetCount);

var startDateTime = firstAsset.first.modifiedDateTime;
var endDateTime = lastAsset.first.modifiedDateTime;

int differenceInMonths = (startDateTime.year * 12 + startDateTime.month) -
    (endDateTime.year * 12 + endDateTime.month);
DateTime currentDate = startDateTime;

int lastIndex = 0;
for (int i = 0; i <= differenceInMonths; i++) {
  DateTime startDate = DateTime(currentDate.year, currentDate.month, 1);
  DateTime endDate = DateTime(currentDate.year, currentDate.month + 1, 0,
      23, 59, 59);

  var filter = CustomFilter.sql(
    where: '''
      ${CustomColumns.android.width} IS NOT NULL
      AND ${CustomColumns.base.modifiedDate} >= ${CustomColumns.utils.convertDateTimeToSql(startDate)}
      AND ${CustomColumns.base.modifiedDate} <= ${CustomColumns.utils.convertDateTimeToSql(endDate)}
    ''',
    orderBy: [OrderByItem.desc(CustomColumns.android.modifiedDate)],
  );

  var newAlbum = album.copyWith(filterOption: filter);
  var monthlyAssetCount = await newAlbum.assetCountAsync;
  String monthKey =
      "${startDate.year}-${startDate.month.toString().padLeft(2, '0')}";
  albumStatistics[monthKey] = AlbumStatistics(
      startIndex: lastIndex, endIndex: lastIndex + monthlyAssetCount);

  lastIndex += monthlyAssetCount;

  currentDate = DateTime(currentDate.year, currentDate.month - 1);
}

Why

Reason:
This feature would make it easier to rebuild common image pickers like those found in WhatsApp or built-in photo libraries. Many popular apps use this kind of grouping to improve user experience. Adding this capability directly to the library would align it with such widely adopted use cases.

Feature Request:
Introduce a built-in way to fetch assets grouped by timeframes (e.g., months), reducing the need for multiple SQL queries and manual calculations. This feature would enhance the library's usability for developers implementing custom image pickers and similar features.

@fluttercandies-dev
Copy link

AI Summary: The user requests a built-in feature to group album assets by date (e.g., monthly), as the current method of combining multiple SQL requests is inefficient for large datasets and common image picker UIs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants