83 lines
2.3 KiB
Dart
83 lines
2.3 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:device_info_plus/device_info_plus.dart';
|
|
import 'package:injectable/injectable.dart';
|
|
import 'package:package_info_plus/package_info_plus.dart';
|
|
|
|
class DeviceInfo {
|
|
final String deviceId;
|
|
final String deviceName;
|
|
final String deviceType;
|
|
final String platform;
|
|
final String osVersion;
|
|
final String appVersion;
|
|
|
|
const DeviceInfo({
|
|
required this.deviceId,
|
|
required this.deviceName,
|
|
required this.deviceType,
|
|
required this.platform,
|
|
required this.osVersion,
|
|
required this.appVersion,
|
|
});
|
|
|
|
Map<String, dynamic> toJson() => {
|
|
'device_id': deviceId,
|
|
'device_name': deviceName,
|
|
'device_type': deviceType,
|
|
'platform': platform,
|
|
'os_version': osVersion,
|
|
'app_version': appVersion,
|
|
};
|
|
}
|
|
|
|
@lazySingleton
|
|
class DeviceInfoService {
|
|
final DeviceInfoPlugin _deviceInfo = DeviceInfoPlugin();
|
|
|
|
Future<DeviceInfo> getDeviceInfo() async {
|
|
final packageInfo = await PackageInfo.fromPlatform();
|
|
final appVersion = packageInfo.version;
|
|
|
|
if (Platform.isAndroid) {
|
|
final info = await _deviceInfo.androidInfo;
|
|
return DeviceInfo(
|
|
deviceId: info.id,
|
|
deviceName: '${info.manufacturer} ${info.model}',
|
|
deviceType: _resolveDeviceType(info.model),
|
|
platform: 'android',
|
|
osVersion: 'Android ${info.version.release}',
|
|
appVersion: appVersion,
|
|
);
|
|
} else if (Platform.isIOS) {
|
|
final info = await _deviceInfo.iosInfo;
|
|
return DeviceInfo(
|
|
deviceId: info.identifierForVendor ?? '',
|
|
deviceName: info.name,
|
|
deviceType: _resolveDeviceType(info.model),
|
|
platform: 'ios',
|
|
osVersion: '${info.systemName} ${info.systemVersion}',
|
|
appVersion: appVersion,
|
|
);
|
|
}
|
|
|
|
// Fallback (web/desktop — tidak dipakai tapi aman)
|
|
return DeviceInfo(
|
|
deviceId: 'unknown',
|
|
deviceName: 'unknown',
|
|
deviceType: 'desktop',
|
|
platform: 'web',
|
|
osVersion: 'unknown',
|
|
appVersion: appVersion,
|
|
);
|
|
}
|
|
|
|
/// Tentukan device_type berdasarkan model name.
|
|
/// Nilai valid: 'mobile' | 'tablet' | 'desktop'
|
|
String _resolveDeviceType(String model) {
|
|
final lower = model.toLowerCase();
|
|
if (lower.contains('ipad') || lower.contains('tablet')) return 'tablet';
|
|
return 'mobile';
|
|
}
|
|
}
|