Skip to content
This repository has been archived by the owner on Jul 11, 2024. It is now read-only.

Support lazy loading through DeviceApps.streamInstalledApplications() #90

Closed
wants to merge 3 commits into from
Closed

Conversation

alexrintt
Copy link

Lazy loading through new API streamInstalledApplications

With this PR there's an way to use this package to list all packages individually; how and why was explained on #89.

Take this for instance:

final apps = <Application>[];
final appsStream = DeviceApps.streamInstalledApplications(
  includeAppIcons: true,
  includeSystemApps: true,
);

appsStream.listen(
  (app) {
    apps.add(app);
    setState(() {});
  },
  onDone: () {
    isLoading = false;
    setState(() {});
  },
);

The example above will updated the Flutter UI every time a new package is loaded in the native side, instead of:

// This call takes 5~10 seconds in real devices with large apps data set
// Meanwhile a boring loading spinner should be shown
final apps = await DeviceApps.getInstalledApplications(
  includeAppIcons: true,
  includeSystemApps: true,
);
setState(() {}); 

getInstalledApplications propose

We can also re-implement the getInstalledApplications API since the Stream<T>s can be converted to Future<T>s (but not vice-versa).

static Future<List<Application>> getInstalledApplications({
  bool includeSystemApps: false,
  bool includeAppIcons: false,
  bool onlyAppsWithLaunchIntent: false,
}) async {
  return streamInstalledApplications().toList(); // Re-use the logic here and convert as Future<T>
}

This PR is a scratch, I would to know what we can change here to improve even more this feature.

@alexrintt alexrintt changed the title Add DeviceApps.streamInstalledApplications() Support lazy loading through DeviceApps.streamInstalledApplications() Jul 1, 2022
@Djihanegh
Copy link

Hello, i need this feature to improve my app performance, any updates @g123k @alexrintt ?

@alexrintt
Copy link
Author

i think this repository is no longer maintained, i am using this feature directly from my fork:

dependencies:
  # ...
  device_apps:
    git:
      url: https://github.com/alexrintt/flutter_plugin_device_apps
      ref: 6051e51fd78778f848eca870ec51a0367b5d2fd9 # use this hash instead 'master' because i can push a new commit in the future maybe, and i don't wanna break anyone else app.

there are no docs for this feature, but you can see how i am using this at:

https://github.com/alexrintt/kanade/blob/f149d373a214d59804b26a50fee703eeb28404ca/lib/stores/device_apps.dart#L187-L214

class Store {
  // ...
  Future<void> loadPackages() async {
    isLoading = true;

    notifyListeners();

    totalPackagesCount = await DeviceApps.getInstalledPackagesCount();

    final appsStream = DeviceApps.streamInstalledApplications(
      includeAppIcons: true,
      includeSystemApps: true,
    );

    appsStream.listen(
      (app) {
        apps.add(app);

        throttle(() {
          if (!isDisposed) {
            notifyListeners();
          }
        });
      },
      onDone: () {
        isLoading = false;
        notifyListeners();
      },
    );
  // ...
}

@Djihanegh
Copy link

Thank you, it works !

@alexrintt alexrintt closed this Feb 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants