Skip to content

Commit

Permalink
调整功能和UI
Browse files Browse the repository at this point in the history
  • Loading branch information
EaniaHuui committed May 27, 2022
1 parent 98db0d7 commit 4d983b4
Show file tree
Hide file tree
Showing 10 changed files with 287 additions and 311 deletions.
2 changes: 1 addition & 1 deletion images/ic_quick_future.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
69 changes: 46 additions & 23 deletions lib/page/android_log/android_log_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ import 'package:substring_highlight/substring_highlight.dart';

class AndroidLogPage extends StatefulWidget {
final String deviceId;
final String packageName;

const AndroidLogPage(
{Key? key, required this.deviceId, required this.packageName})
: super(key: key);
const AndroidLogPage({Key? key, required this.deviceId}) : super(key: key);

@override
State<AndroidLogPage> createState() => _AndroidLogPageState();
Expand Down Expand Up @@ -60,7 +57,7 @@ class _AndroidLogPageState
),
),
const SizedBox(width: 12),
const TextView("筛选级别:"),
const TextView("级别:"),
Container(
height: 33,
decoration: BoxDecoration(
Expand All @@ -73,6 +70,9 @@ class _AndroidLogPageState
),
),
const SizedBox(width: 12),
const TextView("应用:"),
packageNameView(context),
const SizedBox(width: 12),
Selector<AndroidLogViewModel, bool>(
selector: (context, viewModel) => viewModel.isFilterPackage,
builder: (context, isFilter, child) {
Expand All @@ -84,20 +84,7 @@ class _AndroidLogPageState
);
},
),
const TextView("只显示当前应用Log"),
const SizedBox(width: 12),
Selector<AndroidLogViewModel, bool>(
selector: (context, viewModel) => viewModel.isColorLog,
builder: (context, isColorLog, child) {
return Checkbox(
value: isColorLog,
onChanged: (value) {
viewModel.setColorLog(value ?? false);
},
);
},
),
const TextView("显示彩色Log"),
const TextView("筛选应用"),
const SizedBox(width: 16),
],
),
Expand Down Expand Up @@ -189,9 +176,7 @@ class _AndroidLogPageState
delegate: FlutterListViewDelegate(
(context, index) {
var log = viewModel.logList[index];
Color textColor = viewModel.isColorLog
? viewModel.getLogColor(log)
: const Color(0xFF383838);
Color textColor = viewModel.getLogColor(log);
return Listener(
onPointerDown: (event) {
if (event.kind == PointerDeviceKind.mouse &&
Expand Down Expand Up @@ -236,12 +221,50 @@ class _AndroidLogPageState
);
}

Widget packageNameView(BuildContext context) {
return InkWell(
onTap: () {
viewModel.selectPackageName(context);
},
child: Container(
alignment: Alignment.center,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5),
border: Border.all(color: Colors.black.withOpacity(0.5)),
),
height: 33,
child: Row(
children: [
const SizedBox(width: 10),
Selector<AndroidLogViewModel, String>(
selector: (context, viewModel) => viewModel.packageName,
builder: (context, packageName, child) {
return TextView(
packageName.isEmpty ? "未选择筛选应用" : packageName,
color: const Color(0xFF666666),
fontSize: 12,
);
},
),
const SizedBox(
width: 5,
),
const Icon(
Icons.arrow_drop_down,
color: Color(0xFF666666),
),
const SizedBox(width: 5),
],
),
),
);
}

@override
createViewModel() {
return AndroidLogViewModel(
context,
widget.deviceId,
widget.packageName,
);
}

Expand Down
59 changes: 29 additions & 30 deletions lib/page/android_log/android_log_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ import 'dart:io';

import 'package:android_tool/page/common/app.dart';
import 'package:android_tool/page/common/base_view_model.dart';
import 'package:android_tool/page/common/package_help_mixin.dart';
import 'package:android_tool/widget/pop_up_menu_button.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_list_view/flutter_list_view.dart';
import 'package:process_run/shell.dart';
import 'package:shared_preferences/shared_preferences.dart';

class AndroidLogViewModel extends BaseViewModel {
class AndroidLogViewModel extends BaseViewModel with PackageHelpMixin {
static const String colorLogKey = 'colorLog';
static const String filterPackageKey = 'filterPackage';
static const String caseSensitiveKey = 'caseSensitive';

String deviceId;
String packageName;

bool isFilterPackage = false;
String filterContent = "";
Expand All @@ -31,8 +31,6 @@ class AndroidLogViewModel extends BaseViewModel {

bool isShowLast = true;

bool isColorLog = true;

String pid = "";

int findIndex = -1;
Expand All @@ -52,30 +50,25 @@ class AndroidLogViewModel extends BaseViewModel {
AndroidLogViewModel(
BuildContext context,
this.deviceId,
this.packageName,
) : super(context) {
App().getAdbPath().then((value) => adbPath = value);
App().eventBus.on<DeviceIdEvent>().listen((event) {
App().eventBus.on<DeviceIdEvent>().listen((event) async {
logList.clear();
deviceId = event.deviceId;
shell.kill();
listenerLog();
});
App().eventBus.on<PackageNameEvent>().listen((event) async {
packageName = event.packageName;
if (isFilterPackage) {
logList.clear();
pid = await getPid();
kill();
if (deviceId.isEmpty) {
resetPackage();
return;
}
await getInstalledApp(deviceId);
listenerLog();
});
App().eventBus.on<AdbPathEvent>().listen((event) {
logList.clear();
adbPath = event.path;
shell.kill();
kill();
listenerLog();
});
SharedPreferences.getInstance().then((preferences) {
isColorLog = preferences.getBool(colorLogKey) ?? true;
isFilterPackage = preferences.getBool(filterPackageKey) ?? false;
isCaseSensitive = preferences.getBool(caseSensitiveKey) ?? false;
});
Expand All @@ -90,17 +83,34 @@ class AndroidLogViewModel extends BaseViewModel {
filterLevelViewModel.list = filterLevel;
filterLevelViewModel.selectValue = filterLevel.first;
filterLevelViewModel.addListener(() {
shell.kill();
kill();
listenerLog();
});
}

void init() async {
adbPath = await App().getAdbPath();
await getInstalledApp(deviceId);
pid = await getPid();
execAdb(["-s", deviceId, "logcat", "-c"]);
listenerLog();
}

void selectPackageName(BuildContext context) async {
var package = await showPackageSelect(context, deviceId);
if (packageName == package || package.isEmpty) {
return;
}
packageName = package;
if (isFilterPackage) {
logList.clear();
pid = await getPid();
kill();
listenerLog();
notifyListeners();
}
}

void listenerLog() {
String level = filterLevelViewModel.selectValue?.value ?? "";
var list = ["-s", deviceId, "logcat", "$level"];
Expand Down Expand Up @@ -138,9 +148,6 @@ class AndroidLogViewModel extends BaseViewModel {
}

Color getLogColor(String log) {
if (!isColorLog) {
return const Color(0xFF383838);
}
var split = log.split(" ");
split.removeWhere((element) => element.isEmpty);
String type = "";
Expand All @@ -167,7 +174,7 @@ class AndroidLogViewModel extends BaseViewModel {

/// 根据包名获取进程应用进程id
Future<String> getPid() async {
var result = await exec("adb", [
var result = await execAdb([
"-s",
deviceId,
"shell",
Expand Down Expand Up @@ -198,14 +205,6 @@ class AndroidLogViewModel extends BaseViewModel {
notifyListeners();
}

void setColorLog(bool bool) {
isColorLog = bool;
SharedPreferences.getInstance().then((preferences) {
preferences.setBool(caseSensitiveKey, bool);
});
notifyListeners();
}

void setCaseSensitive(bool bool) {
isCaseSensitive = bool;
SharedPreferences.getInstance().then((preferences) {
Expand Down
7 changes: 0 additions & 7 deletions lib/page/common/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class App {
/// 保存包名
Future<void> setPackageName(String packageName) async {
_packageName = packageName;
eventBus.fire(PackageNameEvent(packageName));
final prefs = await SharedPreferences.getInstance();
await prefs.setString(packageNameKey, packageName);
}
Expand Down Expand Up @@ -78,12 +77,6 @@ class DeviceIdEvent {
DeviceIdEvent(this.deviceId);
}

class PackageNameEvent {
String packageName;

PackageNameEvent(this.packageName);
}

class AdbPathEvent {
String path;

Expand Down
66 changes: 66 additions & 0 deletions lib/page/common/package_help_mixin.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import 'package:android_tool/page/common/app.dart';
import 'package:android_tool/page/common/base_view_model.dart';
import 'package:android_tool/widget/list_filter_dialog.dart';
import 'package:flutter/material.dart';
import 'package:process_run/shell.dart';

mixin PackageHelpMixin implements BaseViewModel {
String packageName = "";

ListFilterController<ListFilterItem> packageNameController =
ListFilterController();

List<ListFilterItem> packageNameList = [];

/// 选择调试应用
Future<String> showPackageSelect(BuildContext context, String deviceId) async {
if (packageNameList.isEmpty) {
return "";
}
var value = await packageNameController.show(
context,
packageNameList,
ListFilterItem(packageName),
refreshCallback: () {
getInstalledApp(deviceId);
},
);
if (value != null) {
return value.itemTitle;
}
return "";
}

/// 获取已安装的应用
Future<void> getInstalledApp(String deviceId) async {
var installedApp = await execAdb(
['-s', deviceId, 'shell', 'pm', 'list', 'packages', '-3']);
if (installedApp == null) {
resetPackage();
return;
}
var outLines = installedApp.outLines;
packageNameList = outLines.map((e) {
return ListFilterItem(e.replaceAll("package:", ""));
}).toList();
packageNameList.sort((a, b) => a.itemTitle.compareTo(b.itemTitle));
packageNameController.setData(packageNameList);
if (packageNameList.isNotEmpty) {
var package = await App().getPackageName();
if (package.isNotEmpty) {
packageName = packageNameList
.firstWhere((element) => element.itemTitle == package,
orElse: () => packageNameList.first)
.itemTitle;
} else {
packageName = packageNameList.first.itemTitle;
}
}
}

void resetPackage() {
packageName = "";
packageNameList.clear();
notifyListeners();
}
}
2 changes: 1 addition & 1 deletion lib/page/devices_info/devices_info_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class DevicesInfoViewModel extends FeatureViewModel {
BuildContext context,
String deviceId,
String packageName,
) : super(context, deviceId, packageName);
) : super(context, deviceId);

Future<void> getScreenshot() async {
PaintingBinding.instance?.imageCache?.clear();
Expand Down
Loading

0 comments on commit 4d983b4

Please sign in to comment.