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

feat: migrate to dart 3 #101

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 12 additions & 18 deletions example/lib/apps_events.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'dart:async';

import 'package:device_apps/device_apps.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class AppsEventsScreen extends StatefulWidget {
Expand Down Expand Up @@ -86,7 +85,7 @@ class _AppEventItem extends StatelessWidget {
children: <Widget>[
ListTile(
title: Text(event.packageName),
subtitle: _AppEventItemType(event.event),
subtitle: _AppEventItemType(event),
leading: Text('${event.time.hour}:${event.time.minute}'),
),
const Divider()
Expand All @@ -98,22 +97,17 @@ class _AppEventItem extends StatelessWidget {
class _AppEventItemType extends StatelessWidget {
final String _type;

_AppEventItemType(ApplicationEventType type)
: _type = _extractEventTypeName(type);

static String _extractEventTypeName(ApplicationEventType type) {
switch (type) {
case ApplicationEventType.installed:
return 'Installed';
case ApplicationEventType.updated:
return 'Updated';
case ApplicationEventType.uninstalled:
return 'Uninstalled';
case ApplicationEventType.enabled:
return 'Enabled';
case ApplicationEventType.disabled:
return 'Disabled';
}
_AppEventItemType(ApplicationEvent event)
: _type = _extractEventTypeName(event);

static String _extractEventTypeName(ApplicationEvent event) {
return switch (event) {
ApplicationEventInstalled() => 'Installed',
ApplicationEventUpdated() => 'Updated',
ApplicationEventUninstalled() => 'Uninstalled',
ApplicationEventEnabled() => 'Enabled',
ApplicationEventDisabled() => 'Disabled',
};
}

@override
Expand Down
8 changes: 4 additions & 4 deletions example/lib/apps_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ class _AppsListScreenContent extends StatelessWidget {

const _AppsListScreenContent(
{Key? key,
this.includeSystemApps: false,
this.onlyAppsWithLaunchIntent: false})
this.includeSystemApps = false,
this.onlyAppsWithLaunchIntent = false})
: super(key: key);

@override
Expand All @@ -80,9 +80,9 @@ class _AppsListScreenContent extends StatelessWidget {
return Column(
children: <Widget>[
ListTile(
leading: app is ApplicationWithIcon
leading: app.icon != null
? CircleAvatar(
backgroundImage: MemoryImage(app.icon),
backgroundImage: MemoryImage(app.icon!),
backgroundColor: Colors.white,
)
: null,
Expand Down
4 changes: 2 additions & 2 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ flutter:
uses-material-design: true

environment:
sdk: ">=2.12.0 <3.0.0"
flutter: ">=1.10.0"
sdk: ">=3.0.0 <4.0.0"
flutter: ">=3.0.0"
1 change: 0 additions & 1 deletion lib/device_apps.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export 'src/model/application_category.dart';
export 'src/model/application_event.dart';
export 'src/plugin.dart';
15 changes: 0 additions & 15 deletions lib/src/model/application_event.dart

This file was deleted.

85 changes: 21 additions & 64 deletions lib/src/plugin.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import 'dart:async';
import 'dart:convert';
import 'dart:typed_data';

import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';

import 'model/application_category.dart';
import 'model/application_event.dart';

/// Plugin to list applications installed on an Android device
/// iOS is not supported
Expand All @@ -25,9 +23,9 @@ class DeviceApps {
/// [onlyAppsWithLaunchIntent] will only list applications when an entrypoint.
/// It is similar to what a launcher will display
static Future<List<Application>> getInstalledApplications({
bool includeSystemApps: false,
bool includeAppIcons: false,
bool onlyAppsWithLaunchIntent: false,
bool includeSystemApps = false,
bool includeAppIcons = false,
bool onlyAppsWithLaunchIntent = false,
}) async {
try {
final Object apps =
Expand Down Expand Up @@ -182,6 +180,7 @@ class _BaseApplication {

/// An application installed on the device
/// Depending on the Android version, some attributes may not be available
@immutable
class Application extends _BaseApplication {
/// Displayable name of the application
final String appName;
Expand Down Expand Up @@ -219,15 +218,16 @@ class Application extends _BaseApplication {
/// or disabled (installed, but not visible)
final bool enabled;

final String? _icon;

/// Icon of the application to use in conjunction with [Image.memory]
Uint8List? get icon => _icon == null ? null : base64.decode(_icon!);

factory Application._(Map<dynamic, dynamic> map) {
if (map.length == 0) {
throw Exception('The map can not be null!');
}
if (map.containsKey('app_icon')) {
return ApplicationWithIcon._fromMap(map);
} else {
return Application._fromMap(map);
}
return Application._fromMap(map);
}

Application._fromMap(Map<dynamic, dynamic> map)
Expand All @@ -241,6 +241,7 @@ class Application extends _BaseApplication {
updateTimeMillis = map['update_time'] as int,
enabled = map['is_enabled'] as bool,
category = _parseCategory(map['category']),
_icon = map['app_icon'] as String?,
super._fromMap(map);

/// Mapping of Android categories
Expand Down Expand Up @@ -305,6 +306,7 @@ class Application extends _BaseApplication {
'updateTimeMillis: $updateTimeMillis, '
'category: $category, '
'enabled: $enabled'
'icon: $icon'
'}';
}

Expand All @@ -323,7 +325,8 @@ class Application extends _BaseApplication {
installTimeMillis == other.installTimeMillis &&
updateTimeMillis == other.updateTimeMillis &&
category == other.category &&
enabled == other.enabled;
enabled == other.enabled &&
icon == other.icon;

@override
int get hashCode =>
Expand All @@ -337,36 +340,8 @@ class Application extends _BaseApplication {
installTimeMillis.hashCode ^
updateTimeMillis.hashCode ^
category.hashCode ^
enabled.hashCode;
}

/// If the [includeAppIcons] attribute is provided, this class will be used.
/// To display an image simply use the [Image.memory] widget.
/// Example:
///
/// ```
/// Image.memory(app.icon)
/// ```
class ApplicationWithIcon extends Application {
final String _icon;

ApplicationWithIcon._fromMap(Map<dynamic, dynamic> map)
: _icon = map['app_icon'] as String,
super._fromMap(map);

/// Icon of the application to use in conjunction with [Image.memory]
Uint8List get icon => base64.decode(_icon);

@override
bool operator ==(Object other) =>
identical(this, other) ||
super == other &&
other is ApplicationWithIcon &&
runtimeType == other.runtimeType &&
_icon == other._icon;

@override
int get hashCode => super.hashCode ^ _icon.hashCode;
enabled.hashCode ^
icon.hashCode;
}

/// Represent an event relative to an application, which can be:
Expand All @@ -378,7 +353,7 @@ class ApplicationWithIcon extends Application {
///
/// Note: an [Application] is not available directly in this object, as it would
/// be null in the case of an uninstallation
abstract class ApplicationEvent {
sealed class ApplicationEvent {
final DateTime time;

factory ApplicationEvent._(Map<dynamic, dynamic> map) {
Expand Down Expand Up @@ -410,23 +385,20 @@ abstract class ApplicationEvent {
/// The package name of the application related to this event
String get packageName;

/// The event type will help check if the app is installed or not
ApplicationEventType get event;

@override
String toString() {
return 'event: $event, time: $time';
return 'event: ${this.runtimeType}, time: $time';
}

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is ApplicationEvent &&
runtimeType == other.runtimeType &&
event == other.event;
packageName == other.packageName;

@override
int get hashCode => event.hashCode;
int get hashCode => runtimeType.hashCode ^ packageName.hashCode;
}

class ApplicationEventInstalled extends ApplicationEvent {
Expand All @@ -436,9 +408,6 @@ class ApplicationEventInstalled extends ApplicationEvent {
: application = Application._(map),
super._fromMap(map);

@override
ApplicationEventType get event => ApplicationEventType.installed;

@override
String get packageName => application.packageName;

Expand Down Expand Up @@ -466,9 +435,6 @@ class ApplicationEventUpdated extends ApplicationEvent {
: application = Application._(map),
super._fromMap(map);

@override
ApplicationEventType get event => ApplicationEventType.updated;

@override
String get packageName => application.packageName;

Expand Down Expand Up @@ -499,9 +465,6 @@ class ApplicationEventUninstalled extends ApplicationEvent {
@override
String get packageName => _application.packageName;

@override
ApplicationEventType get event => ApplicationEventType.uninstalled;

@override
bool operator ==(Object other) =>
identical(this, other) ||
Expand All @@ -526,9 +489,6 @@ class ApplicationEventEnabled extends ApplicationEvent {
: application = Application._(map),
super._fromMap(map);

@override
ApplicationEventType get event => ApplicationEventType.enabled;

@override
String get packageName => application.packageName;

Expand Down Expand Up @@ -556,9 +516,6 @@ class ApplicationEventDisabled extends ApplicationEvent {
: application = Application._(map),
super._fromMap(map);

@override
ApplicationEventType get event => ApplicationEventType.disabled;

@override
String get packageName => application.packageName;

Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ version: 2.2.0
homepage: https://github.com/g123k/flutter_plugin_device_apps

environment:
sdk: '>=2.12.0 <3.0.0'
flutter: ">=1.10.0"
sdk: '>=3.0.0 <4.0.0'
flutter: ">=3.0.0"

dependencies:
flutter:
Expand Down