Skip to content

Commit

Permalink
Merge branch 'master' into fix/initdataslowly
Browse files Browse the repository at this point in the history
  • Loading branch information
Hao-yiwen authored Nov 29, 2024
2 parents c2049fb + 9d16c2e commit b6a3ecc
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 20 deletions.
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
MIT License

Copyright (c) 2024 yw.hao
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
4 changes: 2 additions & 2 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ android {
ndkVersion = flutter.ndkVersion

compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}

defaultConfig {
Expand Down
26 changes: 19 additions & 7 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:label="@string/app_name"
android:name="${applicationName}"
android:usesCleartextTraffic="true"
android:enableOnBackInvokedCallback="true"
android:icon="@mipmap/ic_launcher">
<service android:name="com.ryanheise.audioservice.AudioService"
android:foregroundServiceType="mediaPlayback"
android:exported="true">
<intent-filter>
<action android:name="android.media.browse.MediaBrowserService" />
</intent-filter>
</service>

<receiver android:name="com.ryanheise.audioservice.MediaButtonReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:exported="true"
Expand All @@ -25,17 +44,10 @@
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>
<!-- Required to query activities that can process text, see:
https://developer.android.com/training/package-visibility and
https://developer.android.com/reference/android/content/Intent#ACTION_PROCESS_TEXT.
In particular, this is used by the Flutter engine in io.flutter.plugin.text.ProcessTextPlugin. -->
<queries>
<intent>
<action android:name="android.intent.action.PROCESS_TEXT"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.example.netease_cloud_music_app

import io.flutter.embedding.android.FlutterActivity
import com.ryanheise.audioservice.AudioServiceActivity

class MainActivity: FlutterActivity()
class MainActivity: AudioServiceActivity()
49 changes: 43 additions & 6 deletions lib/http/http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'package:dio_cache_interceptor/dio_cache_interceptor.dart';

import 'error_interceptor.dart';
import 'api/login/dto/login_status_dto.dart';
import 'package:path/path.dart' as path;

class Http {
///超时时间
Expand Down Expand Up @@ -248,7 +249,20 @@ class UserLoginStateController {
}

File _saveFile() {
return File(Http.pathProvider.getDataSavedPath() + "_accountInfo.json");
final String dirPath = Http.pathProvider.getDataSavedPath();
final Directory directory = Directory(dirPath);

// 确保目录存在
if (!directory.existsSync()) {
directory.createSync(recursive: true);
}

final String filePath = path.join(dirPath, "_accountInfo.json");
final File file = File(filePath);

LogBox.info("Account file path: $filePath"); // 用于调试

return file;
}

void onLogined(LoginStatusDto info) {
Expand All @@ -264,14 +278,37 @@ class UserLoginStateController {
Future<void> _readAccountInfo() async {
try {
File file = _saveFile();
// 检查文件是否存在
if (!file.existsSync()) {
throw Exception("Account file not found");
LogBox.info("账户文件不存在,可能是首次登录");
return; // 直接返回,不抛出异常
}

// 读取文件内容
String accountInfo = file.readAsStringSync();
_accountInfo = LoginStatusDto.fromJson(jsonDecode(accountInfo));
} catch (e) {
LogBox.error(e);
await onLogout();
if (accountInfo.isEmpty) {
LogBox.info("账户文件为空");
return;
}

// 解析 JSON
final Map<String, dynamic> jsonMap = jsonDecode(accountInfo);
if (jsonMap == null || jsonMap.isEmpty) {
LogBox.info("账户数据解析为空");
return;
}

// 转换为对象
_accountInfo = LoginStatusDto.fromJson(jsonMap);
LogBox.info("账户信息读取成功");

} catch (e, stackTrace) {
LogBox.error("读取账户信息失败: $e");
LogBox.error(stackTrace); // 打印堆栈信息以便调试
// 只有在确实需要登出时才调用 onLogout
if (e is! FileSystemException) { // 如果不是文件系统错误,则登出
await onLogout();
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/pages/main/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ class Main extends GetView<MainController> {
context,
'为你精选的音乐播客',
controller
.personalizedDjprogramDto.value.result!),
.personalizedDjprogramDto.value?.result ?? []),
if (controller.recommendResourceDto.value != null &&
controller.recommendResourceDto.value!
.recommend !=
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/search/search_detail.dart
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class _SearchDetailState extends State<SearchDetail>
itemBuilder: (context, index) {
return GestureDetector(
key: ValueKey(controller.songList.value.result?.playlists?[index]
.id),
.id ?? index),
onTap: () async {
final lists = await MainController.to
.getPlayListDetail(controller.songList.value.result?.playlists?[index].id ?? 0);
Expand Down
6 changes: 5 additions & 1 deletion lib/routes/routes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ abstract class _Paths {
@AutoRouterConfig(replaceInRouteName: 'Page,Route')
class AppRouter extends RootStackRouter {
@override
RouteType get defaultRouteType => const RouteType.material();
RouteType get defaultRouteType => const RouteType.custom(
transitionsBuilder: TransitionsBuilders.slideLeft,
durationInMilliseconds: 100,
reverseDurationInMilliseconds: 200
);

@override
List<AutoRoute> get routes => [
Expand Down
1 change: 1 addition & 0 deletions lib/widgets/custom_tag.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class CustomTag extends StatelessWidget{
@override
Widget build(BuildContext context) {
return Container(
margin: const EdgeInsets.only(right: 10),
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
decoration: BoxDecoration(
color: Colors.grey[200],
Expand Down

0 comments on commit b6a3ecc

Please sign in to comment.