Skip to content

Commit

Permalink
Device info in dev options. Try fixing dio options like UA and timeout.
Browse files Browse the repository at this point in the history
  • Loading branch information
liplum committed Aug 27, 2024
1 parent 3f43a73 commit 0be5f44
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 68 deletions.
30 changes: 21 additions & 9 deletions lib/init.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:mimir/entity/campus.dart';
import 'package:flutter/foundation.dart';
import 'package:mimir/credentials/init.dart';
import 'package:mimir/lifecycle.dart';
import 'package:mimir/r.dart';
import 'package:mimir/session/mimir.dart';
import 'package:mimir/settings/entity/proxy.dart';
import 'package:mimir/storage/hive/init.dart';
Expand Down Expand Up @@ -63,30 +64,41 @@ class Init {
storage: HiveCookieJar(HiveInit.cookies),
);
}
final dioOptions = BaseOptions(

dio = Dio(BaseOptions(
connectTimeout: const Duration(milliseconds: 8000),
receiveTimeout: const Duration(milliseconds: 8000),
sendTimeout: const Duration(milliseconds: 8000),
);
dio = Dio();
));
DioInit.initWith(
dio,
cookieJar: cookieJar,
config: dioOptions,
);
DioInit.initUserAgent(dio);
dioNoCookie = Dio();

dioNoCookie = Dio(BaseOptions(
connectTimeout: const Duration(milliseconds: 8000),
receiveTimeout: const Duration(milliseconds: 8000),
sendTimeout: const Duration(milliseconds: 8000),
));
DioInit.initWith(
dioNoCookie,
config: dioOptions,
);
DioInit.initUserAgent(dioNoCookie);
mimirDio = Dio();

mimirDio = Dio(BaseOptions(
connectTimeout: const Duration(milliseconds: 8000),
receiveTimeout: const Duration(milliseconds: 8000),
sendTimeout: const Duration(milliseconds: 8000),
headers: {
"User-Agent": "Flutter; ${R.meta.version}; ${R.meta.platform.name}; installer:${R.meta.installerStore}; ",
}));

DioInit.initWith(
mimirDio,
config: dioOptions,
cookieJar: cookieJar,
);
DioInit.initMimirUserAgent(mimirDio);

mimirSession = MimirSession(
dio: mimirDio,
);
Expand Down
8 changes: 0 additions & 8 deletions lib/network/dio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class DioInit {
static void initWith(
Dio dio, {
CookieJar? cookieJar,
BaseOptions? config,
}) async {
if (!kIsWeb && cookieJar != null) {
dio.interceptors.add(CookieManager(cookieJar));
Expand All @@ -25,13 +24,6 @@ class DioInit {
if (kDebugMode && R.debugNetwork && R.poorNetworkSimulation) {
dio.interceptors.add(PoorNetworkDioInterceptor());
}
if (config != null) {
dio.options = config;
}
}

static Future<void> initMimirUserAgent(Dio dio) async {
dio.options.headers["User-Agent"] = "Mimir ${R.meta.version} ${R.meta.platform}";
}

static Future<void> initUserAgent(Dio dio) async {
Expand Down
165 changes: 114 additions & 51 deletions lib/settings/page/developer.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'dart:async';
import 'dart:convert';

import 'package:collection/collection.dart';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
Expand Down Expand Up @@ -97,33 +99,21 @@ class _DeveloperOptionsPageState extends ConsumerState<DeveloperOptionsPage> {
const DebugWebViewTile(),
const DebugDeepLinkTile(),
if (UniversalPlatform.isAndroid || UniversalPlatform.isIOS) const GoInAppWebviewTile(),
ListTile(
title: "Device info".text(),
trailing: Icon(context.icons.rightChevron),
onTap: () {
context.showSheet((ctx) => const DeviceInfoPage());
},
),
if (!kIsWeb)
DebugFetchVersionTile(
title: "Official".text(),
fetch: () async {
final info = await BackendInit.update.getLatestVersionFromOfficial();
return info.version.toString();
},
),
if (!kIsWeb)
DebugFetchVersionTile(
leading: const Icon(SimpleIcons.apple),
title: "App Store CN".text(),
fetch: () async {
final info = await BackendInit.update.getLatestVersionFromAppStore();
return "${info!}";
},
),
if (!kIsWeb)
DebugFetchVersionTile(
leading: const Icon(SimpleIcons.apple),
title: "App Store".text(),
fetch: () async {
final info = await BackendInit.update.getLatestVersionFromAppStore(iosAppStoreRegion: null);
return "${info!}";
ListTile(
title: "Check version".text(),
trailing: Icon(context.icons.rightChevron),
onTap: () {
context.showSheet((ctx) => const VersionCheckerPage());
},
),
// const DebugDeviceInfoTile(),
buildPartyPopper(),
]),
),
Expand All @@ -137,6 +127,7 @@ class _DeveloperOptionsPageState extends ConsumerState<DeveloperOptionsPage> {
leading: "🎉".text(style: context.textTheme.headlineLarge),
title: "Party popper 🎉".text(),
subtitle: "Tap me!".text(),
trailing: Icon(context.icons.rightChevron),
onTap: () {
context.showSheet((ctx) => Scaffold(
body: [
Expand Down Expand Up @@ -483,6 +474,34 @@ class DebugExpenseUserOverrideTile extends ConsumerWidget {
}
}

class GoInAppWebviewTile extends ConsumerWidget {
const GoInAppWebviewTile({super.key});

@override
Widget build(BuildContext context, WidgetRef ref) {
return TextInputActionTile(
leading: const Icon(Icons.route_outlined),
title: "In-app Webview".text(),
canSubmit: (url) => url.isEmpty || Uri.tryParse(url) != null,
hintText: R.websiteUri.toString(),
onSubmit: (url) {
if (url.isEmpty) {
url = R.websiteUri.toString();
}
var uri = Uri.tryParse(url);
if (uri == null) return false;
if (uri.scheme.isEmpty) {
uri = uri.replace(scheme: "https");
}
context.navigator.push(MaterialPageRoute(
builder: (ctx) => InAppWebViewPage(initialUri: WebUri.uri(uri!)),
));
return true;
},
);
}
}

class DebugFetchVersionTile extends StatefulWidget {
final Widget? title;
final Widget? leading;
Expand Down Expand Up @@ -532,42 +551,86 @@ class _DebugFetchVersionTileState extends State<DebugFetchVersionTile> {
}
}

class DebugDeviceInfoTile extends StatelessWidget {
const DebugDeviceInfoTile({super.key});
class VersionCheckerPage extends ConsumerStatefulWidget {
const VersionCheckerPage({super.key});

@override
ConsumerState createState() => _VersionCheckerPageState();
}

class _VersionCheckerPageState extends ConsumerState<VersionCheckerPage> {
@override
Widget build(BuildContext context) {
return ListTile(
title: "Device info".text(),
subtitle: R.meta.deviceInfo.toString().text(),
return Scaffold(
body: CustomScrollView(
slivers: [
SliverList.list(children: [
DebugFetchVersionTile(
title: "Official".text(),
fetch: () async {
final info = await BackendInit.update.getLatestVersionFromOfficial();
return info.version.toString();
},
),
DebugFetchVersionTile(
leading: const Icon(SimpleIcons.apple),
title: "App Store CN".text(),
fetch: () async {
final info = await BackendInit.update.getLatestVersionFromAppStore();
return "${info!}";
},
),
DebugFetchVersionTile(
leading: const Icon(SimpleIcons.apple),
title: "App Store".text(),
fetch: () async {
final info = await BackendInit.update.getLatestVersionFromAppStore(iosAppStoreRegion: null);
return "${info!}";
},
),
])
],
),
);
}
}

class GoInAppWebviewTile extends ConsumerWidget {
const GoInAppWebviewTile({super.key});
class DeviceInfoPage extends ConsumerStatefulWidget {
const DeviceInfoPage({super.key});

@override
Widget build(BuildContext context, WidgetRef ref) {
return TextInputActionTile(
leading: const Icon(Icons.route_outlined),
title: "In-app Webview".text(),
canSubmit: (url) => url.isEmpty || Uri.tryParse(url) != null,
hintText: R.websiteUri.toString(),
onSubmit: (url) {
if (url.isEmpty) {
url = R.websiteUri.toString();
}
var uri = Uri.tryParse(url);
if (uri == null) return false;
if (uri.scheme.isEmpty) {
uri = uri.replace(scheme: "https");
}
context.navigator.push(MaterialPageRoute(
builder: (ctx) => InAppWebViewPage(initialUri: WebUri.uri(uri!)),
));
return true;
},
ConsumerState createState() => _DeviceInfoPageState();
}

class _DeviceInfoPageState extends ConsumerState<DeviceInfoPage> {
@override
Widget build(BuildContext context) {
const encoder = JsonEncoder.withIndent(" ");
final json = encoder.convert(R.meta.deviceInfo.data);
return Scaffold(
body: CustomScrollView(
slivers: [
json.text().scrolled().padAll(8).sliver(),
],
),
);
}

List<Widget> buildWidgets() {
final info = R.meta.deviceInfo;
if (info is AndroidDeviceInfo) {
info.device;
} else if (info is IosDeviceInfo) {
info.model;
}else if(info is WindowsDeviceInfo){
info.productName;
}else if(info is MacOsDeviceInfo){
info.model;
}else if(info is WebBrowserInfo){

}else if(info is LinuxDeviceInfo){
info.name;
}
return [];
}
}

0 comments on commit 0be5f44

Please sign in to comment.