Skip to content

Commit

Permalink
feat: remove device_info_plus and implement getting the android versi…
Browse files Browse the repository at this point in the history
…on directly in the app
  • Loading branch information
user5522 committed Mar 22, 2024
1 parent 7f567e6 commit 0a6eae1
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 39 deletions.
29 changes: 29 additions & 0 deletions android/app/src/main/kotlin/tk/user5522/timetable/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,35 @@
package tk.user5522.timetable

import io.flutter.embedding.android.FlutterActivity
import android.os.Build
import androidx.annotation.NonNull
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel


class MainActivity: FlutterActivity() {
private val CHANNEL = "tk.user5522.timetable/androidVersion"

fun getAndroidVersion(): Int {
return Build.VERSION.SDK_INT
}

override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {
call, result ->
if (call.method == "getAndroidVersion") {
val androidVersion = getAndroidVersion()

if (androidVersion != -1) {
result.success(androidVersion)
} else {
// result.error("UNAVAILABLE", 0, null)
}
} else {
result.notImplemented()
}
}
}

}
14 changes: 7 additions & 7 deletions lib/components/settings/general.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import 'package:device_info_plus/device_info_plus.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:timetable/components/settings/language_options.dart';
import 'package:timetable/components/settings/theme_options.dart';
import 'package:timetable/helpers/get_os_version.dart';
import 'package:timetable/provider/language.dart';
import 'package:timetable/provider/settings.dart';
import 'package:timetable/provider/themes.dart';
Expand Down Expand Up @@ -32,16 +32,16 @@ class GeneralOptions extends ConsumerWidget {
),
onTap: () {},
),
FutureBuilder<AndroidDeviceInfo>(
future: DeviceInfoPlugin().androidInfo,
FutureBuilder<int>(
future: getAndroidVersion(),
builder: (
BuildContext context,
AsyncSnapshot<AndroidDeviceInfo> snapshot,
AsyncSnapshot<int> snapshot,
) {
final androidDeviceInfo = snapshot.data;
final version = snapshot.data;

if (androidDeviceInfo != null &&
androidDeviceInfo.version.sdkInt >= 31) {
if (version != null &&
version >= 31) {
return SwitchListTile(
title: const Text("monet_theming").tr(),
subtitle: const Text("Android 12+"),
Expand Down
17 changes: 17 additions & 0 deletions lib/helpers/get_os_version.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'dart:async';
import 'package:flutter/services.dart';

const platform = MethodChannel('tk.user5522.timetable/androidVersion');

Future<int> getAndroidVersion() async {
int version;

try {
final result = await platform.invokeMethod<int>('getAndroidVersion');
version = result!;
} on PlatformException {
version = 0;
}

return version;
}
7 changes: 0 additions & 7 deletions lib/screens/settings_screen.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:device_info_plus/device_info_plus.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
Expand All @@ -17,12 +16,6 @@ class SettingsScreen extends ConsumerWidget {
super.key,
});

Future<int> getAndroidVersion() async {
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
return androidInfo.version.sdkInt;
}

@override
Widget build(BuildContext context, WidgetRef ref) {
final navbarToggle = ref.watch(settingsProvider).navbarVisible;
Expand Down
24 changes: 0 additions & 24 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -209,22 +209,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.3.6"
device_info_plus:
dependency: "direct main"
description:
name: device_info_plus
sha256: "77f757b789ff68e4eaf9c56d1752309bd9f7ad557cb105b938a7f8eb89e59110"
url: "https://pub.dev"
source: hosted
version: "9.1.2"
device_info_plus_platform_interface:
dependency: transitive
description:
name: device_info_plus_platform_interface
sha256: d3b01d5868b50ae571cd1dc6e502fc94d956b665756180f7b16ead09e836fd64
url: "https://pub.dev"
source: hosted
version: "7.0.0"
drift:
dependency: "direct main"
description:
Expand Down Expand Up @@ -882,14 +866,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "5.2.0"
win32_registry:
dependency: transitive
description:
name: win32_registry
sha256: "41fd8a189940d8696b1b810efb9abcf60827b6cbfab90b0c43e8439e3a39d85a"
url: "https://pub.dev"
source: hosted
version: "1.1.2"
xdg_directories:
dependency: transitive
description:
Expand Down
1 change: 0 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ dependencies:
flutter_hooks: ^0.20.5
hooks_riverpod: ^2.5.1
non_uniform_border: ^1.0.0
device_info_plus: ^9.0.3
drift: ^2.16.0
sqlite3_flutter_libs: ^0.5.20
path_provider: ^2.1.2
Expand Down

0 comments on commit 0a6eae1

Please sign in to comment.