65 lines
2.0 KiB
Dart
65 lines
2.0 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';
|
|
|
|
@lazySingleton
|
|
class DeviceInfoService {
|
|
DeviceInfoService(this._deviceInfo, this._packageInfo);
|
|
|
|
final DeviceInfoPlugin _deviceInfo;
|
|
final PackageInfo _packageInfo;
|
|
|
|
String get appVersion => '${_packageInfo.version}+${_packageInfo.buildNumber}';
|
|
|
|
Future<Map<String, String>> getDevicePayload() async {
|
|
final info = await _getDeviceInfo();
|
|
return {
|
|
'device_id': info['device_id'] ?? '',
|
|
'device_name': info['device_name'] ?? '',
|
|
'device_type': info['device_type'] ?? 'mobile',
|
|
'platform': _getPlatform(),
|
|
'app_version': appVersion,
|
|
'os_version': info['os_version'] ?? '',
|
|
};
|
|
}
|
|
|
|
String _getPlatform() {
|
|
if (Platform.isAndroid) return 'android';
|
|
if (Platform.isIOS) return 'ios';
|
|
return 'web';
|
|
}
|
|
|
|
Future<Map<String, String>> _getDeviceInfo() async {
|
|
if (Platform.isAndroid) {
|
|
final android = await _deviceInfo.androidInfo;
|
|
return {
|
|
'device_id': android.id,
|
|
'device_name': '${android.manufacturer} ${android.model}',
|
|
'device_type': _resolveDeviceType(android.model),
|
|
'os_version': 'Android ${android.version.release} (SDK ${android.version.sdkInt})',
|
|
};
|
|
} else if (Platform.isIOS) {
|
|
final ios = await _deviceInfo.iosInfo;
|
|
return {
|
|
'device_id': ios.identifierForVendor ?? '',
|
|
'device_name': ios.name,
|
|
'device_type': _resolveDeviceType(ios.model),
|
|
'os_version': '${ios.systemName} ${ios.systemVersion}',
|
|
};
|
|
}
|
|
return {'device_type': 'desktop'};
|
|
}
|
|
|
|
/// Deteksi device_type berdasarkan nama model
|
|
/// Nilai valid: mobile | tablet | desktop
|
|
String _resolveDeviceType(String model) {
|
|
final lower = model.toLowerCase();
|
|
if (lower.contains('tablet') || lower.contains('tab') || lower.contains('ipad')) {
|
|
return 'tablet';
|
|
}
|
|
return 'mobile';
|
|
}
|
|
}
|