Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e8b9a090e0 | ||
|
|
3ad371aa9d | ||
|
|
7463d3ee26 | ||
|
|
59fbd33059 | ||
|
|
8c1662475a | ||
|
|
7f65fe4d48 | ||
|
|
613147c20c | ||
|
|
731638b31b | ||
|
|
c4adbd0665 | ||
|
|
a833d62c50 | ||
|
|
bc01716449 | ||
|
|
12b1a11bd3 | ||
|
|
66c03ee245 | ||
|
|
a3ac007759 | ||
|
|
8ec6df2be5 | ||
|
|
5ec4db151a | ||
|
|
525f58c150 | ||
|
|
a47af8c3ad | ||
|
|
0a02620564 | ||
|
|
2ddf9ee1c6 | ||
|
|
3bc79ad8e2 | ||
|
|
78a33cd4f1 | ||
|
|
c307903c42 | ||
|
|
c5b94633f1 | ||
|
|
fd2ba0bf43 | ||
|
|
aed9ee2b01 | ||
|
|
335e5dc286 | ||
|
|
e8610b7aed | ||
|
|
2f4593a46e | ||
|
|
7509cf2ecd | ||
|
|
501bc66282 | ||
|
|
19196ceb8e | ||
|
|
9ef8eb15b0 | ||
|
|
53e8c924a6 | ||
|
|
e7399fa3fd | ||
|
|
22cc669bbe | ||
|
|
26321e3348 | ||
|
|
3590cd0c55 | ||
|
|
c311985601 | ||
|
|
4dd37b2588 | ||
|
|
543d0aadd5 | ||
|
|
d5bcda54d3 | ||
|
|
79463e65c2 | ||
|
|
ad7aba3daf | ||
|
|
47fdd4a29d | ||
|
|
fe49527dd8 | ||
|
|
4aaa5bbd60 | ||
|
|
5649e4c3d4 | ||
|
|
9f7301305e | ||
|
|
63191b4692 | ||
|
|
0f777b19f2 | ||
|
|
fb9001a02d | ||
|
|
1b80f841be | ||
|
|
3e89c71f50 | ||
|
|
2ce92aa7dd | ||
|
|
a6d6b66d31 | ||
|
|
57f84c45bd | ||
|
|
a4d08e7c1e | ||
|
|
9c9a277217 | ||
|
|
1be8ebf1f9 | ||
|
|
70be941755 | ||
|
|
351d0afd09 | ||
|
|
7d32324a2d | ||
|
|
1d4ab5e0e6 | ||
|
|
799f3d1b7d | ||
|
|
08aa6903d2 | ||
|
|
4de9417e27 | ||
|
|
e269d5dce5 | ||
|
|
78f464f2a4 | ||
|
|
9983d42015 | ||
|
|
bfe0df62dc | ||
|
|
df2654c9d6 | ||
|
|
496b1cb797 | ||
|
|
85dedf20fa | ||
|
|
43e60e29ec | ||
|
|
5346be40a9 | ||
|
|
de7da64886 | ||
|
|
709972245f | ||
|
|
d5d13549f5 | ||
|
|
c027acc91f | ||
|
|
76429628ec | ||
|
|
43f3593218 | ||
|
|
0eca7ecc31 | ||
|
|
af5d8f9d80 | ||
|
|
b84a5368d6 | ||
|
|
67e178c89d |
No files matched your search
@@ -0,0 +1,25 @@
|
|||||||
|
# Author: fruit.com.cn
|
||||||
|
# ReelShort 客户端 CI:静态分析 + 测试门禁
|
||||||
|
name: flutter-ci
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [develop, main]
|
||||||
|
pull_request:
|
||||||
|
branches: [develop, main]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- uses: subosito/flutter-action@v2
|
||||||
|
with:
|
||||||
|
channel: stable
|
||||||
|
cache: true
|
||||||
|
- name: 依赖
|
||||||
|
run: flutter pub get
|
||||||
|
- name: 静态分析(零告警门禁)
|
||||||
|
run: dart analyze --fatal-infos
|
||||||
|
- name: 单元测试(全绿门禁)
|
||||||
|
run: flutter test -r expanded
|
||||||
@@ -65,3 +65,6 @@ app.*.map.json
|
|||||||
.env
|
.env
|
||||||
.env.*
|
.env.*
|
||||||
!.env.example
|
!.env.example
|
||||||
|
|
||||||
|
# IDE 的 Flutter Widget Preview 生成目录。
|
||||||
|
.widget_preview/
|
||||||
@@ -1,18 +1,22 @@
|
|||||||
# FruitShorts(ReelShort)客户端
|
# FruitShorts(ReelShort)客户端
|
||||||
|
|
||||||
FruitShorts 是面向印尼市场的短剧 Flutter 客户端。当前版本已连接
|
FruitShorts 是面向印尼市场的短剧 Flutter 客户端,已连接 `FruitServer` 的
|
||||||
`FruitServer` 的 `/app/**` 接口,支持沉浸播放、找剧、账号、观看记录、收藏、
|
`/app/**` 接口。功能覆盖:四 tab 主页(推荐视频流 / 找剧 / 追剧 / 我的)、
|
||||||
下载授权、评论互动、站内通知和中英印尼三语界面。
|
沉浸式播放与全屏剧集播放器、清晰度切换、剧单与排行榜、新剧页、搜索、观看记录、
|
||||||
|
收藏短剧、评论与点赞、站内通知、钱包与商品、下载落盘与离线播放、中英印尼三语,
|
||||||
|
以及 Android / iOS 双端画中画(退到后台自动进小窗)。
|
||||||
|
|
||||||
## 技术栈
|
## 技术栈
|
||||||
|
|
||||||
- Flutter 3.47.7 / Dart 3.13.5
|
- Flutter 3.47.7 / Dart 3.13.5(`environment.sdk: ^3.12.2`)
|
||||||
- Provider 状态管理与依赖注入
|
- Provider 状态管理与依赖注入
|
||||||
- dio 网络请求
|
- dio 网络请求
|
||||||
- video_player 播放
|
- video_player 播放
|
||||||
- shared_preferences 与 `SharedPreferencesAsync` 本地配置
|
- shared_preferences 与 `SharedPreferencesAsync` 本地配置
|
||||||
|
- path_provider + dart:io 下载落盘(先写 `.part`,完成后原子改名)
|
||||||
- flutter_localizations,支持 `en`、`zh`、`id`
|
- flutter_localizations,支持 `en`、`zh`、`id`
|
||||||
- Android 系统画中画
|
- 画中画:Android 用 `floating` + `permission_handler`(需悬浮窗权限);
|
||||||
|
iOS 用原生 `AVPictureInPictureController` + 影子播放器(`ios/Runner/AppDelegate.swift`)
|
||||||
|
|
||||||
## 目录结构
|
## 目录结构
|
||||||
|
|
||||||
@@ -22,7 +26,7 @@ lib/
|
|||||||
config/ 地址、语言和运行时配置
|
config/ 地址、语言和运行时配置
|
||||||
data/ models、remote API、repository、controller
|
data/ models、remote API、repository、controller
|
||||||
formatting/ 文案与日期格式化
|
formatting/ 文案与日期格式化
|
||||||
platform/ 触觉等平台能力
|
platform/ 平台能力:画中画通道、设备号、触觉
|
||||||
theme/ 颜色与主题
|
theme/ 颜色与主题
|
||||||
widgets/ 跨页面复用组件
|
widgets/ 跨页面复用组件
|
||||||
l10n/ 三语 ARB 与生成代码
|
l10n/ 三语 ARB 与生成代码
|
||||||
@@ -36,20 +40,21 @@ assets/ 图标、图片和本地媒体文件
|
|||||||
|
|
||||||
## 已接接口
|
## 已接接口
|
||||||
|
|
||||||
以下接口均在 `lib/core/data/remote/` 中有实际调用,共 26 个不同路径、
|
以下接口均在 `lib/core/data/remote/` 中有实际调用,共 47 个不同路径。
|
||||||
31 个 HTTP 方法级调用。
|
|
||||||
|
|
||||||
| 分组 | 契约 | 接口 |
|
| 分组 | 接口 |
|
||||||
| --- | --- | --- |
|
| --- | --- |
|
||||||
| 账号 | §1.1 | `POST /app/auth/register`、`/login`、`/refresh`、`/logout` |
|
| 账号 | `POST /app/auth/register`、`/login`、`/refresh`、`/logout`、`/otp`;`GET /app/me`、`/app/me/devices`、`/app/me/export`;`POST /app/me/deactivate`、`/app/me/deactivate/cancel` |
|
||||||
| 内容 | §1.3、§1.4 | `GET /app/home/feed`、`/app/discover/feed`、`/app/categories`、`/app/dramas/{id}`、`/app/episodes/{id}/play` |
|
| 内容 | `GET /app/home/feed`、`/app/feed`、`/app/discover/feed`、`/app/categories`、`/app/categories/{id}/dramas`、`/app/drama-categories`、`/app/dramas/page`、`/app/dramas/{id}`、`/app/tags`、`/app/search`、`/app/playlists`、`/app/playlists/{id}` |
|
||||||
| 观看记录 | §1.5 | `POST/GET /app/watch/progress`、`GET /app/watch/recent`、`DELETE /app/watch/progress/{dramaId}`、`DELETE /app/watch/progress` |
|
| 播放 | `GET /app/episodes/{id}/play`、`/app/episodes/{id}/stats`;`POST /app/episodes/{id}/unlock` |
|
||||||
| 收藏 | §1.5 | `POST /app/favorites/toggle`、`GET /app/favorites`、`DELETE /app/favorites/{dramaId}` |
|
| 观看记录 | `POST/GET /app/watch/progress`、`GET /app/watch/recent`、`DELETE /app/watch/progress/{dramaId}`、`DELETE /app/watch/progress` |
|
||||||
| 下载授权 | §1.6 | `GET /app/downloads/policy`、`PUT /app/downloads/settings`、`POST/GET /app/downloads`、`GET /app/downloads/{episodeId}/url`、`POST /app/downloads/{episodeId}/progress`、`DELETE /app/downloads/{episodeId}`、`DELETE /app/downloads` |
|
| 收藏 | `POST /app/favorites/toggle`、`GET /app/favorites`、`DELETE /app/favorites/{dramaId}`、`POST /app/favorites/remove` |
|
||||||
| 互动 | §1.7 | `GET/POST /app/comments`、`DELETE /app/comments/{id}`、`POST /app/likes/toggle`、`POST /app/reports` |
|
| 下载 | `GET /app/downloads/policy`、`PUT /app/downloads/settings`、`POST/GET /app/downloads`、`GET /app/downloads/{id}/url`、`POST /app/downloads/{id}/progress`、`DELETE /app/downloads/{id}`、`DELETE /app/downloads` |
|
||||||
| 公告 | §1.9.2 | `GET /app/notices` |
|
| 互动 | `GET/POST /app/comments`、`DELETE /app/comments/{id}`、`GET /app/comments/count`、`POST /app/likes/toggle`、`POST /app/reports` |
|
||||||
|
| 商业化 | `GET /app/wallet`、`/app/products`、`/app/orders` |
|
||||||
|
| 公告 | `GET /app/notices` |
|
||||||
|
|
||||||
客户端没有调用 `/app/config` 或创作者入驻接口。
|
客户端没有调用 `/app/config` 和创作者入驻接口。
|
||||||
|
|
||||||
## 运行
|
## 运行
|
||||||
|
|
||||||
@@ -66,39 +71,60 @@ flutter pub get
|
|||||||
```bash
|
```bash
|
||||||
adb devices
|
adb devices
|
||||||
adb reverse tcp:8081 tcp:8081
|
adb reverse tcp:8081 tcp:8081
|
||||||
flutter run -d <device> \
|
flutter run -d <device>
|
||||||
--dart-define=API_BASE_URL=http://127.0.0.1:8081
|
|
||||||
```
|
```
|
||||||
|
|
||||||
`adb reverse` 将手机的 `127.0.0.1:8081` 转发到电脑;USB 断开或重新连接后需要
|
`adb reverse` 将手机的 `127.0.0.1:8081` 转发到电脑;USB 断开或重新连接后需要
|
||||||
重新执行。
|
重新执行。此时用调试条把地址改成 `http://127.0.0.1:8081`(见下节)。
|
||||||
|
|
||||||
Android 模拟器访问宿主机时使用:
|
Android 模拟器访问宿主机时,同样在调试条里填 `http://10.0.2.2:8081`。
|
||||||
|
|
||||||
```bash
|
|
||||||
flutter run --dart-define=API_BASE_URL=http://10.0.2.2:8081
|
|
||||||
```
|
|
||||||
|
|
||||||
### iOS
|
### iOS
|
||||||
|
|
||||||
手机与 Mac 需在同一网络,首次启动允许“本地网络”权限:
|
iOS 模拟器直接访问 Mac 本机服务,默认地址为 `http://127.0.0.1:8081`:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
flutter run -d <udid> \
|
flutter run -d <simulator-udid>
|
||||||
--dart-define=API_BASE_URL=http://<Mac局域网IP>:8081
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Debug 运行时地址
|
iPhone 真机不能用 `127.0.0.1` 访问 Mac;真机联调时需让手机与 Mac 位于同一网络,
|
||||||
|
并通过调试入口临时填写 Mac 的局域网地址。
|
||||||
|
|
||||||
debug 版本可在「我的 → 设置 → 服务器地址」修改后端地址,保存后重启 App 生效。
|
### 服务地址配置
|
||||||
地址优先级:
|
|
||||||
|
|
||||||
```text
|
地址是代码里的一个开关,**不做任何网络探测**,也不用传构建参数。两套环境各占
|
||||||
本地运行时设置 > --dart-define(API_BASE_URL) > 平台默认地址
|
一个文件:
|
||||||
|
|
||||||
|
| 文件 | 用途 | 默认值 |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `lib/core/config/dev_config.dart` | 开发 / 联调 | `http://127.0.0.1:8081` |
|
||||||
|
| `lib/core/config/prod_config.dart` | 生产 | `http://39.162.50.99:8081` |
|
||||||
|
|
||||||
|
切换环境只改 `lib/core/config/app_config.dart` 里的一个布尔值:
|
||||||
|
|
||||||
|
```dart
|
||||||
|
static const bool useDevConfig = true; // false 即用生产地址
|
||||||
```
|
```
|
||||||
|
|
||||||
因此不带 `--dart-define` 构建的 debug APK 也可在安装后改成当前电脑 IP。
|
真机或 Android 模拟器联调时,通过调试入口覆盖地址,不修改默认开发配置。打生产包前把
|
||||||
release 版本不显示入口,也不会读取本地覆盖。
|
`useDevConfig` 改成 `false`;release 构建若仍为 `true` 会在启动时直接报错,
|
||||||
|
避免线上包连到开发机。
|
||||||
|
|
||||||
|
debug 包在找剧页加载失败时会显示调试条,点「修改服务器地址」可临时改成当前
|
||||||
|
电脑 IP,保存后立即重载、不需要重新打包,该覆盖保存在本地偏好里(仅 debug
|
||||||
|
生效,release 不读取)。
|
||||||
|
|
||||||
|
## 画中画
|
||||||
|
|
||||||
|
| 平台 | 行为 |
|
||||||
|
| --- | --- |
|
||||||
|
| Android | 首次点播放页小窗按钮时申请悬浮窗权限;授权后退出 App 自动进入系统小窗。沉浸流与独立播放页共用一份引用计数,任一处活跃都算「在播」,避免互相顶掉 |
|
||||||
|
| iOS | 原生影子播放器承载 `AVPictureInPictureController`,只有播放页处于活跃状态时退到后台才自动进小窗;停在剧单等页面退后台不会触发 |
|
||||||
|
|
||||||
|
- 小窗内切集直接换源,不走页面翻页,可连续自动播放
|
||||||
|
- iOS 从系统小窗还原时用原生过渡层盖住纹理黑帧,等首帧就绪后再淡出
|
||||||
|
- 播放页「更多」面板里有「退出自动小窗」开关(仅支持自动小窗的平台显示),关掉后
|
||||||
|
退到后台不再进入小窗
|
||||||
|
|
||||||
## 环境注意
|
## 环境注意
|
||||||
|
|
||||||
@@ -131,46 +157,56 @@ xcodebuild -resolvePackageDependencies \
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
adb uninstall com.stormg.lumadrama
|
adb uninstall com.stormg.lumadrama
|
||||||
flutter run -d <device> \
|
flutter run -d <device>
|
||||||
--dart-define=API_BASE_URL=http://127.0.0.1:8081
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## 生产打包(Android)
|
## 生产打包(Android)
|
||||||
|
|
||||||
生产包只包含以下两个 ABI,**打成一个 APK**,不做 split:
|
两个渠道出两种产物,源码同一套:
|
||||||
|
|
||||||
| ABI | 适用设备 |
|
| 渠道 | 产物 | 命令 | 体积 |
|
||||||
| --- | --- |
|
| --- | --- | --- | --- |
|
||||||
| `arm64-v8a` | 现代手机(64 位) |
|
| Google Play | AAB(按设备下发) | `flutter build appbundle --release` | 上传 28MB,用户下载约 8MB |
|
||||||
| `armeabi-v7a` | 老 32 位机 |
|
| 国内各大市场 | 通用 APK(arm64 + v7) | `flutter build apk --release` | 约 13MB |
|
||||||
|
|
||||||
```bash
|
|
||||||
flutter build apk --release --target-platform android-arm64,android-arm
|
|
||||||
# 产出单个:build/app/outputs/flutter-apk/app-release.apk(约 37MB)
|
|
||||||
```
|
|
||||||
|
|
||||||
说明:
|
说明:
|
||||||
|
|
||||||
- `android/app/build.gradle.kts` 的 `release` 已配置 `abiFilters`,只保留 `arm64-v8a` 与 `armeabi-v7a`,插件也不会再带入 `x86_64` 原生库。
|
- `android/app/build.gradle.kts` 的 `release` 已配置 `abiFilters`,只保留 `arm64-v8a` 与 `armeabi-v7a`,插件也不会再带入 `x86_64` 原生库。
|
||||||
- `x86_64` 只用于模拟器,不进入生产包。
|
- `android/gradle.properties` 里固定了 `split-debug-info=build/symbols` 与 `dart-obfuscation=true`,**IDE 里点 Build APK / Build App Bundle 也会生效**:`libapp.so` 从 ~8MB/ABI 降到 ~1.1MB/ABI。
|
||||||
- 不要直接运行 `flutter build apk --release`:默认会编译三套原生库,包体约 57MB。
|
- `useLegacyPackaging = true`:原生库以压缩形式存储,这是国内 APK 从 40MB 降到 13MB 的主要原因;代价是安装时解压。
|
||||||
- 只有需要进一步压缩单机下载体积时,才考虑 `--split-per-abi` 出分架构包(arm64 约 21MB、armv7 约 19MB),默认不用。
|
- **每次发版都要归档 `build/symbols`**,线上崩溃栈需要它还原。
|
||||||
|
- release 仍在用 debug 签名(`build.gradle.kts` 的 TODO),上架前必须换成正式 keystore。
|
||||||
|
- 不要用 `--split-per-abi`:与 `abiFilters` 冲突;Play 的 AAB 已经按设备拆好。
|
||||||
|
|
||||||
|
## 生产打包(iOS)
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 正式签名(需要 iOS Distribution 证书 + App Store 描述文件)
|
||||||
|
flutter build ipa --release
|
||||||
|
|
||||||
|
# 只有 Apple Development 证书时:出开发签名 IPA,只能装已注册设备
|
||||||
|
flutter build ipa --release --export-method development
|
||||||
|
```
|
||||||
|
|
||||||
|
产物在 `build/ios/ipa/*.ipa`。画中画依赖 `UIBackgroundModes` 里的 `audio` 与
|
||||||
|
`AVPictureInPictureController`,改动 `ios/Runner/Info.plist` 或 `AppDelegate.swift`
|
||||||
|
后要重新构建,热重载不会生效。
|
||||||
|
|
||||||
## 门禁
|
## 门禁
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
dart analyze lib test
|
dart analyze
|
||||||
flutter test
|
flutter test
|
||||||
flutter build apk --debug
|
flutter build apk --debug
|
||||||
```
|
```
|
||||||
|
|
||||||
测试文件按模块放在 `test/` 下,用例名使用
|
当前基线:`dart analyze` 仅 1 条既存 info(`intl` 未在 `pubspec.yaml` 声明);
|
||||||
|
`flutter test` 全量 775 例通过。测试文件按模块放在 `test/` 下,用例名使用
|
||||||
`should_<期望>_when_<条件>`。真实后端联调测试在服务未启动时会跳过。
|
`should_<期望>_when_<条件>`。真实后端联调测试在服务未启动时会跳过。
|
||||||
|
|
||||||
## 当前边界
|
## 当前边界
|
||||||
|
|
||||||
- 下载仅管理服务端授权,不落盘、不续传、不支持离线播放。
|
- 下载会落盘并支持离线播放,但不支持断点续传:中途失败会丢弃 `.part` 重下。
|
||||||
- 没有独立搜索接口,搜索能力尚未形成完整闭环。
|
- 支付与金币:客户端已有钱包、商品、下单接口与页面,商店票据校验依赖服务端配置凭据。
|
||||||
- 站内通知没有已读/未读和红点状态。
|
- 站内通知没有已读/未读和红点状态。
|
||||||
- 支付、金币和会员权益尚未形成客户端闭环。
|
- 播放清晰度切换会保留旧画面 500ms 掩盖闪屏,切换瞬间的进度以相对位置续播。
|
||||||
- Android 支持系统画中画;iOS 画中画尚未接入。
|
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
/.gradle
|
/.gradle
|
||||||
|
/build/
|
||||||
/captures/
|
/captures/
|
||||||
/local.properties
|
/local.properties
|
||||||
GeneratedPluginRegistrant.java
|
GeneratedPluginRegistrant.java
|
||||||
|
|||||||
@@ -8,7 +8,9 @@ plugins {
|
|||||||
|
|
||||||
android {
|
android {
|
||||||
namespace = "com.stormg.lumadrama"
|
namespace = "com.stormg.lumadrama"
|
||||||
compileSdk = flutter.compileSdkVersion
|
// permission_handler_android 14 的 AAR 元数据要求依赖方 compileSdk >= 37,
|
||||||
|
// 固定为 37(比 Flutter 默认的 36 高一档,只影响编译期 API 可见性)。
|
||||||
|
compileSdk = 37
|
||||||
ndkVersion = flutter.ndkVersion
|
ndkVersion = flutter.ndkVersion
|
||||||
|
|
||||||
compileOptions {
|
compileOptions {
|
||||||
@@ -33,6 +35,13 @@ android {
|
|||||||
ndk {
|
ndk {
|
||||||
abiFilters += listOf("arm64-v8a", "armeabi-v7a")
|
abiFilters += listOf("arm64-v8a", "armeabi-v7a")
|
||||||
}
|
}
|
||||||
|
// R8 裁剪未使用的 Java/Kotlin 代码与资源,减小 dex 与 res。
|
||||||
|
isMinifyEnabled = true
|
||||||
|
isShrinkResources = true
|
||||||
|
proguardFiles(
|
||||||
|
getDefaultProguardFile("proguard-android-optimize.txt"),
|
||||||
|
"proguard-rules.pro",
|
||||||
|
)
|
||||||
// TODO: Add your own signing config for the release build.
|
// TODO: Add your own signing config for the release build.
|
||||||
// Signing with the debug keys for now, so `flutter run --release` works.
|
// Signing with the debug keys for now, so `flutter run --release` works.
|
||||||
signingConfig = signingConfigs.getByName("debug")
|
signingConfig = signingConfigs.getByName("debug")
|
||||||
@@ -50,6 +59,9 @@ kotlin {
|
|||||||
androidComponents {
|
androidComponents {
|
||||||
onVariants(selector().withBuildType("release")) { variant ->
|
onVariants(selector().withBuildType("release")) { variant ->
|
||||||
variant.packaging.jniLibs.excludes.add("lib/x86_64/**")
|
variant.packaging.jniLibs.excludes.add("lib/x86_64/**")
|
||||||
|
// 原生库随 APK 压缩存储:下载体积明显小于解压后的体积,代价是安装时解压。
|
||||||
|
// 本包 90% 体积是 so,这一项对国内商店的 APK 分发影响最大。
|
||||||
|
variant.packaging.jniLibs.useLegacyPackaging = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
# Author: fruit.com.cn
|
||||||
|
#
|
||||||
|
# R8 保号规则:Flutter 引擎与插件自带的 consumer rules 已覆盖大部分场景,
|
||||||
|
# 这里只补本项目原生层通过反射/通道访问、裁剪后容易失效的入口。
|
||||||
|
|
||||||
|
# 画中画与权限通道由 MainActivity 直接注册,保留应用包名下的入口类。
|
||||||
|
-keep class com.stormg.lumadrama.** { *; }
|
||||||
|
|
||||||
|
# Flutter 引擎在 release 下通过 JNI 回调这些类,禁止改名。
|
||||||
|
-keep class io.flutter.** { *; }
|
||||||
|
-dontwarn io.flutter.embedding.**
|
||||||
@@ -2,8 +2,11 @@
|
|||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
<uses-permission
|
<uses-permission
|
||||||
android:name="android.permission.INTERNET"/>
|
android:name="android.permission.INTERNET"/>
|
||||||
|
<!-- 画中画自动触发依赖的「显示在其他应用上层」权限(部分 ROM 用它管控 PiP) -->
|
||||||
|
<uses-permission
|
||||||
|
android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
|
||||||
<application
|
<application
|
||||||
android:label="FruitShorts"
|
android:label="ymb"
|
||||||
android:name="${applicationName}"
|
android:name="${applicationName}"
|
||||||
android:icon="@mipmap/ic_launcher"
|
android:icon="@mipmap/ic_launcher"
|
||||||
android:usesCleartextTraffic="true">
|
android:usesCleartextTraffic="true">
|
||||||
|
|||||||
@@ -284,6 +284,7 @@ class MainActivity : FlutterActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
private const val TAG = "FruitPip"
|
||||||
private const val thumbnailMaxWidth = 240
|
private const val thumbnailMaxWidth = 240
|
||||||
private const val thumbnailJpegQuality = 70
|
private const val thumbnailJpegQuality = 70
|
||||||
private const val thumbnailTimeoutMs = 1500L
|
private const val thumbnailTimeoutMs = 1500L
|
||||||
|
|||||||
|
After Width: | Height: | Size: 5.2 KiB |
|
Before Width: | Height: | Size: 7.0 KiB After Width: | Height: | Size: 3.6 KiB |
|
After Width: | Height: | Size: 3.9 KiB |
|
After Width: | Height: | Size: 3.2 KiB |
|
Before Width: | Height: | Size: 3.1 KiB After Width: | Height: | Size: 1.9 KiB |
|
After Width: | Height: | Size: 2.4 KiB |
|
Before Width: | Height: | Size: 21 KiB |
|
Before Width: | Height: | Size: 20 KiB |
|
After Width: | Height: | Size: 7.4 KiB |
|
Before Width: | Height: | Size: 7.2 KiB After Width: | Height: | Size: 3.8 KiB |
|
After Width: | Height: | Size: 5.3 KiB |
|
After Width: | Height: | Size: 13 KiB |
|
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 5.8 KiB |
|
After Width: | Height: | Size: 8.6 KiB |
|
After Width: | Height: | Size: 18 KiB |
|
Before Width: | Height: | Size: 16 KiB After Width: | Height: | Size: 8.0 KiB |
|
After Width: | Height: | Size: 12 KiB |
|
Before Width: | Height: | Size: 2.3 KiB After Width: | Height: | Size: 1.6 KiB |
|
Before Width: | Height: | Size: 6.5 KiB After Width: | Height: | Size: 156 B |
|
Before Width: | Height: | Size: 1.1 KiB After Width: | Height: | Size: 1.0 KiB |
|
Before Width: | Height: | Size: 3.6 KiB After Width: | Height: | Size: 113 B |
|
Before Width: | Height: | Size: 2.7 KiB After Width: | Height: | Size: 2.2 KiB |
|
Before Width: | Height: | Size: 9.7 KiB After Width: | Height: | Size: 216 B |
|
Before Width: | Height: | Size: 5.8 KiB After Width: | Height: | Size: 3.5 KiB |
|
Before Width: | Height: | Size: 17 KiB After Width: | Height: | Size: 384 B |
|
Before Width: | Height: | Size: 6.3 KiB After Width: | Height: | Size: 4.5 KiB |
|
Before Width: | Height: | Size: 24 KiB After Width: | Height: | Size: 623 B |
@@ -8,7 +8,7 @@
|
|||||||
<item name="android:windowDrawsSystemBarBackgrounds">false</item>
|
<item name="android:windowDrawsSystemBarBackgrounds">false</item>
|
||||||
<item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
|
<item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
|
||||||
<item name="android:windowSplashScreenBackground">#000000</item>
|
<item name="android:windowSplashScreenBackground">#000000</item>
|
||||||
<item name="android:windowSplashScreenAnimatedIcon">@drawable/splash_brand</item>
|
<item name="android:windowSplashScreenAnimatedIcon">@drawable/android12splash</item>
|
||||||
<item name="android:windowSplashScreenAnimationDuration">0</item>
|
<item name="android:windowSplashScreenAnimationDuration">0</item>
|
||||||
</style>
|
</style>
|
||||||
<style name="NormalTheme" parent="@android:style/Theme.Black.NoTitleBar">
|
<style name="NormalTheme" parent="@android:style/Theme.Black.NoTitleBar">
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
<item name="android:windowDrawsSystemBarBackgrounds">false</item>
|
<item name="android:windowDrawsSystemBarBackgrounds">false</item>
|
||||||
<item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
|
<item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
|
||||||
<item name="android:windowSplashScreenBackground">#000000</item>
|
<item name="android:windowSplashScreenBackground">#000000</item>
|
||||||
<item name="android:windowSplashScreenAnimatedIcon">@drawable/splash_brand</item>
|
<item name="android:windowSplashScreenAnimatedIcon">@drawable/android12splash</item>
|
||||||
<item name="android:windowSplashScreenAnimationDuration">0</item>
|
<item name="android:windowSplashScreenAnimationDuration">0</item>
|
||||||
</style>
|
</style>
|
||||||
<style name="NormalTheme" parent="@android:style/Theme.Light.NoTitleBar">
|
<style name="NormalTheme" parent="@android:style/Theme.Light.NoTitleBar">
|
||||||
|
|||||||
@@ -3,5 +3,5 @@
|
|||||||
|
|
||||||
<resources>
|
<resources>
|
||||||
<color name="splash_background">#000000</color>
|
<color name="splash_background">#000000</color>
|
||||||
<color name="ic_launcher_background">#FF6B2C</color>
|
<color name="ic_launcher_background">#000000</color>
|
||||||
</resources>
|
</resources>
|
||||||
@@ -6,3 +6,8 @@ android.useAndroidX=true
|
|||||||
android.newDsl=false
|
android.newDsl=false
|
||||||
# This builtInKotlin flag was added by the Flutter template
|
# This builtInKotlin flag was added by the Flutter template
|
||||||
android.builtInKotlin=false
|
android.builtInKotlin=false
|
||||||
|
|
||||||
|
# release 包默认剥离 Dart 调试符号并混淆:libapp.so 由 ~8MB 降到 ~1MB/ABI。
|
||||||
|
# 符号表输出到 build/symbols,崩溃分析时用它还原堆栈,发布前务必归档。
|
||||||
|
split-debug-info=build/symbols
|
||||||
|
dart-obfuscation=true
|
||||||
|
Before Width: | Height: | Size: 41 KiB |
|
Before Width: | Height: | Size: 24 KiB After Width: | Height: | Size: 28 KiB |
|
Before Width: | Height: | Size: 42 KiB |
|
After Width: | Height: | Size: 18 KiB |
|
After Width: | Height: | Size: 18 KiB |
|
After Width: | Height: | Size: 16 KiB |
@@ -1,6 +1,7 @@
|
|||||||
import Flutter
|
import Flutter
|
||||||
import UIKit
|
import UIKit
|
||||||
import AVFoundation
|
import AVFoundation
|
||||||
|
import AVKit
|
||||||
|
|
||||||
private let thumbnailHTTPHeaderFieldsKey = "AVURLAssetHTTPHeaderFieldsKey"
|
private let thumbnailHTTPHeaderFieldsKey = "AVURLAssetHTTPHeaderFieldsKey"
|
||||||
|
|
||||||
@@ -35,6 +36,318 @@ final class RunnerFlutterViewController: FlutterViewController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// iOS 影子播放器:独立 AVPlayer + AVPlayerLayer 专供画中画窗口。
|
||||||
|
/// Flutter 的 video_player 走纹理渲染,没有 AVPlayerLayer,无法直接创建
|
||||||
|
/// AVPictureInPictureController;小窗的画面与声音由影子接管,退出时把
|
||||||
|
/// 影子进度回传给 Dart,由 Flutter 播放器 seek 对齐后继续。
|
||||||
|
final class PipShadowCoordinator: NSObject, AVPictureInPictureControllerDelegate {
|
||||||
|
static let shared = PipShadowCoordinator()
|
||||||
|
|
||||||
|
private var player: AVPlayer?
|
||||||
|
private var playerLayer: AVPlayerLayer?
|
||||||
|
private var controller: AVPictureInPictureController?
|
||||||
|
private var endObserver: NSObjectProtocol?
|
||||||
|
private var possibleObservation: NSKeyValueObservation?
|
||||||
|
/// 画中画还原过渡层:原生窗口级红晕渐变,在系统还原动画开始时立即盖上,
|
||||||
|
/// 避免回前台瞬间 Flutter 表面未恢复显示黑帧;视频出帧后由 Dart 通知淡出。
|
||||||
|
private var restoreOverlay: UIView?
|
||||||
|
/// 系统要求 playerLayer 位于真实的 view 层级中才会判定画中画可用,
|
||||||
|
/// 因此用 1x1 透明宿主 view 承载,而不是直接挂在 window.layer 上。
|
||||||
|
private var hostView: UIView?
|
||||||
|
/// 影子图层就绪稍晚于创建,置位后由 KVO 自动拉起画中画。
|
||||||
|
private var pendingStart = false
|
||||||
|
private var startRetryWorkItems: [DispatchWorkItem] = []
|
||||||
|
|
||||||
|
weak var channel: FlutterMethodChannel?
|
||||||
|
|
||||||
|
/// active 变化回调;停止时把影子最终进度(毫秒)一并提供。
|
||||||
|
var onPipChanged: ((Bool, Int64) -> Void)?
|
||||||
|
/// 影子在画中画里播完当前集。
|
||||||
|
var onPlaybackEnded: (() -> Void)?
|
||||||
|
|
||||||
|
static var isSupported: Bool {
|
||||||
|
AVPictureInPictureController.isPictureInPictureSupported()
|
||||||
|
}
|
||||||
|
|
||||||
|
private var keyWindow: UIWindow? {
|
||||||
|
UIApplication.shared.connectedScenes
|
||||||
|
.compactMap { $0 as? UIWindowScene }
|
||||||
|
.flatMap { $0.windows }
|
||||||
|
.first { $0.isKeyWindow }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 首次创建影子:1x1 图层挂在 keyWindow 的宿主 view 里(肉眼不可见),静音待命。
|
||||||
|
/// [autoEnter] 为 true 时退后台由系统自动拉起画中画(需影子在播,见 armAutoEnter)。
|
||||||
|
func prepare(urlString: String, positionMs: Int64, autoEnter: Bool) {
|
||||||
|
teardown()
|
||||||
|
log("prepare begin supported=\(PipShadowCoordinator.isSupported) autoEnter=\(autoEnter)")
|
||||||
|
guard Self.isSupported else { return }
|
||||||
|
guard let url = URL(string: urlString) else {
|
||||||
|
log("prepare failed bad url \(urlString)")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
guard let window = keyWindow else {
|
||||||
|
log("prepare failed no key window")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
let item = AVPlayerItem(url: url)
|
||||||
|
let p = AVPlayer(playerItem: item)
|
||||||
|
p.isMuted = true
|
||||||
|
p.actionAtItemEnd = .pause
|
||||||
|
let layer = AVPlayerLayer()
|
||||||
|
layer.player = p
|
||||||
|
let host = UIView(frame: CGRect(x: 0, y: 0, width: 1, height: 1))
|
||||||
|
host.isUserInteractionEnabled = false
|
||||||
|
host.backgroundColor = .clear
|
||||||
|
window.addSubview(host)
|
||||||
|
layer.frame = host.bounds
|
||||||
|
host.layer.addSublayer(layer)
|
||||||
|
hostView = host
|
||||||
|
guard let c = AVPictureInPictureController(playerLayer: layer) else {
|
||||||
|
p.pause()
|
||||||
|
log("prepare failed no pip controller")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.canStartPictureInPictureAutomaticallyFromInline = autoEnter
|
||||||
|
c.delegate = self
|
||||||
|
player = p
|
||||||
|
playerLayer = layer
|
||||||
|
controller = c
|
||||||
|
// 静音起播让 item 完成首帧解码,isPictureInPicturePossible 才会变 true。
|
||||||
|
p.play()
|
||||||
|
possibleObservation = c.observe(
|
||||||
|
\.isPictureInPicturePossible,
|
||||||
|
options: [.new, .initial]
|
||||||
|
) { [weak self] controller, change in
|
||||||
|
let value = change.newValue ?? controller.isPictureInPicturePossible
|
||||||
|
self?.log("possible changed to \(value)")
|
||||||
|
guard let self, self.pendingStart, controller.isPictureInPicturePossible else { return }
|
||||||
|
self.pendingStart = false
|
||||||
|
// 刚翻 true 的同一 runloop 里调用会被系统静默拒绝,推到下一拍再进。
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
guard self.controller === controller, controller.isPictureInPicturePossible else {
|
||||||
|
self.log("start via kvo aborted (shadow replaced)")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
self.log("start via kvo")
|
||||||
|
controller.startPictureInPicture()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log("shadow ready, seeking to \(positionMs)ms")
|
||||||
|
if positionMs > 0 {
|
||||||
|
p.seek(
|
||||||
|
to: CMTime(value: positionMs, timescale: 1000),
|
||||||
|
toleranceBefore: .zero,
|
||||||
|
toleranceAfter: .zero
|
||||||
|
)
|
||||||
|
}
|
||||||
|
registerEndObserver(for: item)
|
||||||
|
activateAudioSession()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 退出 App 前(inactive)把影子对齐到当前进度并静音起播:
|
||||||
|
/// auto PiP 只在「player 正在播放 + 图层离屏」时由系统触发。
|
||||||
|
func armAutoEnter(positionMs: Int64) {
|
||||||
|
guard let player, let controller else {
|
||||||
|
log("arm failed, shadow missing")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
guard controller.canStartPictureInPictureAutomaticallyFromInline else {
|
||||||
|
log("arm failed, autoEnter disabled on shadow")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log("arm auto enter at \(positionMs)ms")
|
||||||
|
if positionMs > 0 {
|
||||||
|
player.seek(
|
||||||
|
to: CMTime(value: positionMs, timescale: 1000),
|
||||||
|
toleranceBefore: CMTime(value: 800, timescale: 1000),
|
||||||
|
toleranceAfter: CMTime(value: 800, timescale: 1000)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
player.play()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 回前台后取消待命:影子暂停回静音,等待下一次退后台。
|
||||||
|
func disarmAutoEnter() {
|
||||||
|
guard let player else { return }
|
||||||
|
log("disarm auto enter")
|
||||||
|
player.pause()
|
||||||
|
player.isMuted = true
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 进入画中画:图层就绪则立即进入,否则 KVO/延时重试在就绪后自动进入。
|
||||||
|
/// 仅当影子从未创建成功(prepare 失败)时返回 false。
|
||||||
|
func start() -> Bool {
|
||||||
|
guard let controller else {
|
||||||
|
log("start failed, shadow not created")
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
log("start requested possible=\(controller.isPictureInPicturePossible)")
|
||||||
|
pendingStart = true
|
||||||
|
if controller.isPictureInPicturePossible {
|
||||||
|
pendingStart = false
|
||||||
|
controller.startPictureInPicture()
|
||||||
|
}
|
||||||
|
scheduleStartRetries()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
/// KVO 偶发不触发或流加载较慢时兜底:就绪后再试两次。
|
||||||
|
private func scheduleStartRetries() {
|
||||||
|
startRetryWorkItems.forEach { $0.cancel() }
|
||||||
|
startRetryWorkItems.removeAll()
|
||||||
|
for delay in [1.0, 2.5] {
|
||||||
|
let item = DispatchWorkItem { [weak self] in
|
||||||
|
guard
|
||||||
|
let self,
|
||||||
|
self.pendingStart,
|
||||||
|
let controller = self.controller,
|
||||||
|
controller.isPictureInPicturePossible
|
||||||
|
else { return }
|
||||||
|
self.log("start via retry after \(delay)s")
|
||||||
|
self.pendingStart = false
|
||||||
|
controller.startPictureInPicture()
|
||||||
|
}
|
||||||
|
startRetryWorkItems.append(item)
|
||||||
|
DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 画中画内换集:只换 AVPlayerItem,不动 player/layer,窗口不中断。
|
||||||
|
func replaceItem(urlString: String, positionMs: Int64) {
|
||||||
|
guard let player, let url = URL(string: urlString) else { return }
|
||||||
|
let item = AVPlayerItem(url: url)
|
||||||
|
player.replaceCurrentItem(with: item)
|
||||||
|
registerEndObserver(for: item)
|
||||||
|
if positionMs > 0 {
|
||||||
|
player.seek(
|
||||||
|
to: CMTime(value: positionMs, timescale: 1000),
|
||||||
|
toleranceBefore: .zero,
|
||||||
|
toleranceAfter: .zero
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 进入画中画后解除静音并起播(此时 Flutter 播放器已被暂停)。
|
||||||
|
func unmuteAndPlay() {
|
||||||
|
player?.isMuted = false
|
||||||
|
player?.play()
|
||||||
|
}
|
||||||
|
|
||||||
|
func positionMs() -> Int64 {
|
||||||
|
Int64(max(0, player?.currentTime().seconds ?? 0) * 1000)
|
||||||
|
}
|
||||||
|
|
||||||
|
func stopPip() {
|
||||||
|
controller?.stopPictureInPicture()
|
||||||
|
}
|
||||||
|
|
||||||
|
func teardown() {
|
||||||
|
startRetryWorkItems.forEach { $0.cancel() }
|
||||||
|
startRetryWorkItems.removeAll()
|
||||||
|
possibleObservation?.invalidate()
|
||||||
|
possibleObservation = nil
|
||||||
|
pendingStart = false
|
||||||
|
if let endObserver {
|
||||||
|
NotificationCenter.default.removeObserver(endObserver)
|
||||||
|
}
|
||||||
|
endObserver = nil
|
||||||
|
player?.pause()
|
||||||
|
player?.replaceCurrentItem(with: nil)
|
||||||
|
playerLayer?.removeFromSuperlayer()
|
||||||
|
playerLayer = nil
|
||||||
|
hostView?.removeFromSuperview()
|
||||||
|
hostView = nil
|
||||||
|
controller?.delegate = nil
|
||||||
|
controller = nil
|
||||||
|
player = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 诊断日志:NSLog 之外同步转发到 Dart,便于在 flutter run 控制台观察。
|
||||||
|
private func log(_ message: String) {
|
||||||
|
NSLog("pip: %@", message)
|
||||||
|
channel?.invokeMethod("pipDebug", arguments: message)
|
||||||
|
}
|
||||||
|
|
||||||
|
private func registerEndObserver(for item: AVPlayerItem) {
|
||||||
|
if let endObserver {
|
||||||
|
NotificationCenter.default.removeObserver(endObserver)
|
||||||
|
}
|
||||||
|
endObserver = NotificationCenter.default.addObserver(
|
||||||
|
forName: .AVPlayerItemDidPlayToEndTime,
|
||||||
|
object: item,
|
||||||
|
queue: .main
|
||||||
|
) { [weak self] _ in
|
||||||
|
self?.log("item did play to end")
|
||||||
|
self?.onPlaybackEnded?()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 画中画的声音走影子播放器,需要 playback 类别的音频会话保活。
|
||||||
|
private func activateAudioSession() {
|
||||||
|
do {
|
||||||
|
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .moviePlayback)
|
||||||
|
try AVAudioSession.sharedInstance().setActive(true)
|
||||||
|
} catch {
|
||||||
|
// 会话激活失败只影响后台出声,不阻断画中画。
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func pictureInPictureControllerWillStartPictureInPicture(
|
||||||
|
_ pictureInPictureController: AVPictureInPictureController
|
||||||
|
) {
|
||||||
|
log("will start")
|
||||||
|
pendingStart = false
|
||||||
|
onPipChanged?(true, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func pictureInPictureControllerWillStopPictureInPicture(
|
||||||
|
_ pictureInPictureController: AVPictureInPictureController
|
||||||
|
) {
|
||||||
|
log("will stop, showing restore overlay")
|
||||||
|
showRestoreOverlay()
|
||||||
|
}
|
||||||
|
|
||||||
|
func pictureInPictureControllerDidStopPictureInPicture(
|
||||||
|
_ pictureInPictureController: AVPictureInPictureController
|
||||||
|
) {
|
||||||
|
log("did stop")
|
||||||
|
showRestoreOverlay()
|
||||||
|
onPipChanged?(false, positionMs())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 盖原生过渡层(幂等):主题红晕渐变,不依赖 Flutter 渲染。
|
||||||
|
func showRestoreOverlay() {
|
||||||
|
guard let window = keyWindow else { return }
|
||||||
|
restoreOverlay?.removeFromSuperview()
|
||||||
|
let overlay = UIView(frame: window.bounds)
|
||||||
|
overlay.autoresizingMask = [.flexibleWidth, .flexibleHeight]
|
||||||
|
let gradient = CAGradientLayer()
|
||||||
|
gradient.frame = overlay.bounds
|
||||||
|
gradient.colors = [
|
||||||
|
UIColor(red: 0.298, green: 0.086, blue: 0.102, alpha: 1).cgColor,
|
||||||
|
UIColor(red: 0.118, green: 0.043, blue: 0.047, alpha: 1).cgColor,
|
||||||
|
UIColor(red: 0.063, green: 0.063, blue: 0.071, alpha: 1).cgColor,
|
||||||
|
]
|
||||||
|
gradient.locations = [0, 0.22, 0.62]
|
||||||
|
gradient.startPoint = CGPoint(x: 0.5, y: 0)
|
||||||
|
gradient.endPoint = CGPoint(x: 0.5, y: 1)
|
||||||
|
overlay.layer.addSublayer(gradient)
|
||||||
|
window.addSubview(overlay)
|
||||||
|
restoreOverlay = overlay
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 淡出并移除过渡层(视频首帧已确认后由 Dart 调用)。
|
||||||
|
func hideRestoreOverlay() {
|
||||||
|
guard let overlay = restoreOverlay else { return }
|
||||||
|
restoreOverlay = nil
|
||||||
|
UIView.animate(
|
||||||
|
withDuration: 0.25,
|
||||||
|
animations: { overlay.alpha = 0 }
|
||||||
|
) { _ in overlay.removeFromSuperview() }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@main
|
@main
|
||||||
@objc class AppDelegate: FlutterAppDelegate, FlutterImplicitEngineDelegate {
|
@objc class AppDelegate: FlutterAppDelegate, FlutterImplicitEngineDelegate {
|
||||||
private let wirelessDataPrompt = WirelessDataPrompt()
|
private let wirelessDataPrompt = WirelessDataPrompt()
|
||||||
@@ -64,6 +377,7 @@ final class RunnerFlutterViewController: FlutterViewController {
|
|||||||
GeneratedPluginRegistrant.register(with: engineBridge.pluginRegistry)
|
GeneratedPluginRegistrant.register(with: engineBridge.pluginRegistry)
|
||||||
registerWirelessDataChannel(engineBridge)
|
registerWirelessDataChannel(engineBridge)
|
||||||
registerDeviceIdChannel(engineBridge)
|
registerDeviceIdChannel(engineBridge)
|
||||||
|
registerPipChannel(engineBridge)
|
||||||
let channel = FlutterMethodChannel(
|
let channel = FlutterMethodChannel(
|
||||||
name: "com.stormg.lumadrama/share",
|
name: "com.stormg.lumadrama/share",
|
||||||
binaryMessenger: engineBridge.applicationRegistrar.messenger()
|
binaryMessenger: engineBridge.applicationRegistrar.messenger()
|
||||||
@@ -72,16 +386,30 @@ final class RunnerFlutterViewController: FlutterViewController {
|
|||||||
guard call.method == "shareText",
|
guard call.method == "shareText",
|
||||||
let arguments = call.arguments as? [String: Any],
|
let arguments = call.arguments as? [String: Any],
|
||||||
let text = arguments["text"] as? String,
|
let text = arguments["text"] as? String,
|
||||||
!text.isEmpty,
|
!text.isEmpty else {
|
||||||
let controller = self?.window?.rootViewController else {
|
result(FlutterError(code: "share_unavailable", message: "Share sheet is unavailable", details: nil))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
guard let controller = self?.sharePresenter() else {
|
||||||
result(FlutterError(code: "share_unavailable", message: "Share sheet is unavailable", details: nil))
|
result(FlutterError(code: "share_unavailable", message: "Share sheet is unavailable", details: nil))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
let activity = UIActivityViewController(activityItems: [text], applicationActivities: nil)
|
let activity = UIActivityViewController(activityItems: [text], applicationActivities: nil)
|
||||||
activity.popoverPresentationController?.sourceView = controller.view
|
if let popover = activity.popoverPresentationController {
|
||||||
|
popover.sourceView = controller.view
|
||||||
|
popover.sourceRect = CGRect(
|
||||||
|
x: controller.view.bounds.midX,
|
||||||
|
y: controller.view.bounds.midY,
|
||||||
|
width: 0,
|
||||||
|
height: 0
|
||||||
|
)
|
||||||
|
popover.permittedArrowDirections = []
|
||||||
|
}
|
||||||
activity.completionWithItemsHandler = { _, _, _, _ in result(nil) }
|
activity.completionWithItemsHandler = { _, _, _, _ in result(nil) }
|
||||||
controller.present(activity, animated: true)
|
controller.present(activity, animated: true)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let thumbnailChannel = FlutterMethodChannel(
|
let thumbnailChannel = FlutterMethodChannel(
|
||||||
name: "com.stormg.lumadrama/thumbnail",
|
name: "com.stormg.lumadrama/thumbnail",
|
||||||
@@ -145,6 +473,119 @@ final class RunnerFlutterViewController: FlutterViewController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 当前前台 Scene 的根控制器之上,最适合展示系统分享面板的控制器。
|
||||||
|
private func sharePresenter() -> UIViewController? {
|
||||||
|
let keyWindow = UIApplication.shared.connectedScenes
|
||||||
|
.compactMap { $0 as? UIWindowScene }
|
||||||
|
.first { $0.activationState == .foregroundActive }?
|
||||||
|
.windows.first { $0.isKeyWindow }
|
||||||
|
guard let rootController = keyWindow?.rootViewController else { return nil }
|
||||||
|
let presenter = topPresentedController(from: rootController)
|
||||||
|
guard presenter.viewIfLoaded?.window != nil else { return nil }
|
||||||
|
return presenter
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 解析已展示页面、导航栈和标签页中的当前可见控制器。
|
||||||
|
private func topPresentedController(from controller: UIViewController) -> UIViewController {
|
||||||
|
if let presented = controller.presentedViewController {
|
||||||
|
return topPresentedController(from: presented)
|
||||||
|
}
|
||||||
|
if let navigation = controller as? UINavigationController,
|
||||||
|
let visible = navigation.visibleViewController {
|
||||||
|
return topPresentedController(from: visible)
|
||||||
|
}
|
||||||
|
if let tabs = controller as? UITabBarController,
|
||||||
|
let selected = tabs.selectedViewController {
|
||||||
|
return topPresentedController(from: selected)
|
||||||
|
}
|
||||||
|
return controller
|
||||||
|
}
|
||||||
|
|
||||||
|
/// iOS 画中画:影子播放器通道,与 Android 共用同一 channel 名。
|
||||||
|
private func registerPipChannel(_ engineBridge: FlutterImplicitEngineBridge) {
|
||||||
|
let channel = FlutterMethodChannel(
|
||||||
|
name: "com.stormg.lumadrama/pip",
|
||||||
|
binaryMessenger: engineBridge.applicationRegistrar.messenger()
|
||||||
|
)
|
||||||
|
let pip = PipShadowCoordinator.shared
|
||||||
|
pip.channel = channel
|
||||||
|
channel.setMethodCallHandler { call, result in
|
||||||
|
switch call.method {
|
||||||
|
case "isAvailable":
|
||||||
|
result(PipShadowCoordinator.isSupported)
|
||||||
|
case "enter":
|
||||||
|
guard let args = call.arguments as? [String: Any],
|
||||||
|
let url = args["url"] as? String else {
|
||||||
|
result(false)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
let positionMs = (args["positionMs"] as? NSNumber)?.int64Value ?? 0
|
||||||
|
pip.prepare(urlString: url, positionMs: positionMs, autoEnter: false)
|
||||||
|
// 等一拍让 AVPlayerLayer 完成挂载再请求进入画中画。
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
result(pip.start())
|
||||||
|
}
|
||||||
|
case "prepareShadow":
|
||||||
|
guard let args = call.arguments as? [String: Any],
|
||||||
|
let url = args["url"] as? String else {
|
||||||
|
result(nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
pip.prepare(
|
||||||
|
urlString: url,
|
||||||
|
positionMs: (args["positionMs"] as? NSNumber)?.int64Value ?? 0,
|
||||||
|
autoEnter: (args["autoEnter"] as? NSNumber)?.boolValue ?? false
|
||||||
|
)
|
||||||
|
result(nil)
|
||||||
|
case "armAutoEnter":
|
||||||
|
pip.armAutoEnter(
|
||||||
|
positionMs: (call.arguments as? [String: Any])?["positionMs"] as? Int64 ?? 0
|
||||||
|
)
|
||||||
|
result(nil)
|
||||||
|
case "showRestoreOverlay":
|
||||||
|
pip.showRestoreOverlay()
|
||||||
|
result(nil)
|
||||||
|
case "hideRestoreOverlay":
|
||||||
|
pip.hideRestoreOverlay()
|
||||||
|
result(nil)
|
||||||
|
case "disarmAutoEnter":
|
||||||
|
pip.disarmAutoEnter()
|
||||||
|
result(nil)
|
||||||
|
case "unmute":
|
||||||
|
pip.unmuteAndPlay()
|
||||||
|
result(nil)
|
||||||
|
case "replaceItem":
|
||||||
|
guard let args = call.arguments as? [String: Any],
|
||||||
|
let url = args["url"] as? String else {
|
||||||
|
result(nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
pip.replaceItem(
|
||||||
|
urlString: url,
|
||||||
|
positionMs: (args["positionMs"] as? NSNumber)?.int64Value ?? 0
|
||||||
|
)
|
||||||
|
result(nil)
|
||||||
|
case "stop":
|
||||||
|
pip.stopPip()
|
||||||
|
result(nil)
|
||||||
|
case "teardown":
|
||||||
|
pip.teardown()
|
||||||
|
result(nil)
|
||||||
|
default:
|
||||||
|
result(FlutterMethodNotImplemented)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pip.onPipChanged = { active, positionMs in
|
||||||
|
channel.invokeMethod(
|
||||||
|
"onPipChanged",
|
||||||
|
arguments: ["active": active, "positionMs": positionMs]
|
||||||
|
)
|
||||||
|
}
|
||||||
|
pip.onPlaybackEnded = {
|
||||||
|
channel.invokeMethod("onPipPlaybackEnded", arguments: nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// 设备标识:IDFV 免权限,卸载本 App 不变,避免客户端自生成的 id 每次重装都变。
|
/// 设备标识:IDFV 免权限,卸载本 App 不变,避免客户端自生成的 id 每次重装都变。
|
||||||
/// 同通道顺带提供设备区域(ISO-2),注册时作为服务端 IP 解析失败后的兜底。
|
/// 同通道顺带提供设备区域(ISO-2),注册时作为服务端 IP 解析失败后的兜底。
|
||||||
private func registerDeviceIdChannel(_ engineBridge: FlutterImplicitEngineBridge) {
|
private func registerDeviceIdChannel(_ engineBridge: FlutterImplicitEngineBridge) {
|
||||||
|
|||||||
|
Before Width: | Height: | Size: 43 KiB After Width: | Height: | Size: 24 KiB |
|
Before Width: | Height: | Size: 412 B After Width: | Height: | Size: 361 B |
|
Before Width: | Height: | Size: 907 B After Width: | Height: | Size: 799 B |
|
Before Width: | Height: | Size: 1.5 KiB After Width: | Height: | Size: 1.3 KiB |
|
Before Width: | Height: | Size: 593 B After Width: | Height: | Size: 587 B |
|
Before Width: | Height: | Size: 1.4 KiB After Width: | Height: | Size: 1.2 KiB |
|
Before Width: | Height: | Size: 2.3 KiB After Width: | Height: | Size: 2.0 KiB |
|
Before Width: | Height: | Size: 907 B After Width: | Height: | Size: 799 B |
|
Before Width: | Height: | Size: 2.1 KiB After Width: | Height: | Size: 1.8 KiB |
|
Before Width: | Height: | Size: 3.6 KiB After Width: | Height: | Size: 2.8 KiB |
|
Before Width: | Height: | Size: 1.1 KiB After Width: | Height: | Size: 1.0 KiB |
|
Before Width: | Height: | Size: 2.8 KiB After Width: | Height: | Size: 2.3 KiB |
|
Before Width: | Height: | Size: 1.3 KiB After Width: | Height: | Size: 1.2 KiB |
|
Before Width: | Height: | Size: 3.3 KiB After Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 3.6 KiB After Width: | Height: | Size: 2.8 KiB |
|
Before Width: | Height: | Size: 6.0 KiB After Width: | Height: | Size: 4.2 KiB |
|
Before Width: | Height: | Size: 2.3 KiB After Width: | Height: | Size: 1.6 KiB |
|
Before Width: | Height: | Size: 5.8 KiB After Width: | Height: | Size: 3.5 KiB |
|
Before Width: | Height: | Size: 2.5 KiB After Width: | Height: | Size: 1.7 KiB |
|
Before Width: | Height: | Size: 7.1 KiB After Width: | Height: | Size: 3.8 KiB |
|
Before Width: | Height: | Size: 6.3 KiB After Width: | Height: | Size: 4.0 KiB |
|
Before Width: | Height: | Size: 6.0 KiB After Width: | Height: | Size: 3.7 KiB |
|
Before Width: | Height: | Size: 14 KiB After Width: | Height: | Size: 9.9 KiB |
|
Before Width: | Height: | Size: 24 KiB After Width: | Height: | Size: 20 KiB |
@@ -7,7 +7,7 @@
|
|||||||
<key>CFBundleDevelopmentRegion</key>
|
<key>CFBundleDevelopmentRegion</key>
|
||||||
<string>$(DEVELOPMENT_LANGUAGE)</string>
|
<string>$(DEVELOPMENT_LANGUAGE)</string>
|
||||||
<key>CFBundleDisplayName</key>
|
<key>CFBundleDisplayName</key>
|
||||||
<string>FruitShorts</string>
|
<string>ymb</string>
|
||||||
<key>CFBundleExecutable</key>
|
<key>CFBundleExecutable</key>
|
||||||
<string>$(EXECUTABLE_NAME)</string>
|
<string>$(EXECUTABLE_NAME)</string>
|
||||||
<key>CFBundleIdentifier</key>
|
<key>CFBundleIdentifier</key>
|
||||||
@@ -15,7 +15,7 @@
|
|||||||
<key>CFBundleInfoDictionaryVersion</key>
|
<key>CFBundleInfoDictionaryVersion</key>
|
||||||
<string>6.0</string>
|
<string>6.0</string>
|
||||||
<key>CFBundleName</key>
|
<key>CFBundleName</key>
|
||||||
<string>FruitShorts</string>
|
<string>ymb</string>
|
||||||
<key>CFBundlePackageType</key>
|
<key>CFBundlePackageType</key>
|
||||||
<string>APPL</string>
|
<string>APPL</string>
|
||||||
<key>CFBundleShortVersionString</key>
|
<key>CFBundleShortVersionString</key>
|
||||||
@@ -41,13 +41,6 @@
|
|||||||
<key>NSIncludesSubdomains</key>
|
<key>NSIncludesSubdomains</key>
|
||||||
<true/>
|
<true/>
|
||||||
</dict>
|
</dict>
|
||||||
<key>localhost</key>
|
|
||||||
<dict>
|
|
||||||
<key>NSExceptionAllowsInsecureHTTPLoads</key>
|
|
||||||
<true/>
|
|
||||||
<key>NSIncludesSubdomains</key>
|
|
||||||
<true/>
|
|
||||||
</dict>
|
|
||||||
<key>192.168.31.243</key>
|
<key>192.168.31.243</key>
|
||||||
<dict>
|
<dict>
|
||||||
<key>NSExceptionAllowsInsecureHTTPLoads</key>
|
<key>NSExceptionAllowsInsecureHTTPLoads</key>
|
||||||
@@ -62,6 +55,13 @@
|
|||||||
<key>NSIncludesSubdomains</key>
|
<key>NSIncludesSubdomains</key>
|
||||||
<true/>
|
<true/>
|
||||||
</dict>
|
</dict>
|
||||||
|
<key>localhost</key>
|
||||||
|
<dict>
|
||||||
|
<key>NSExceptionAllowsInsecureHTTPLoads</key>
|
||||||
|
<true/>
|
||||||
|
<key>NSIncludesSubdomains</key>
|
||||||
|
<true/>
|
||||||
|
</dict>
|
||||||
</dict>
|
</dict>
|
||||||
</dict>
|
</dict>
|
||||||
<key>NSLocalNetworkUsageDescription</key>
|
<key>NSLocalNetworkUsageDescription</key>
|
||||||
@@ -89,10 +89,16 @@
|
|||||||
</dict>
|
</dict>
|
||||||
<key>UIApplicationSupportsIndirectInputEvents</key>
|
<key>UIApplicationSupportsIndirectInputEvents</key>
|
||||||
<true/>
|
<true/>
|
||||||
|
<key>UIBackgroundModes</key>
|
||||||
|
<array>
|
||||||
|
<string>audio</string>
|
||||||
|
</array>
|
||||||
<key>UILaunchStoryboardName</key>
|
<key>UILaunchStoryboardName</key>
|
||||||
<string>LaunchScreen</string>
|
<string>LaunchScreen</string>
|
||||||
<key>UIMainStoryboardFile</key>
|
<key>UIMainStoryboardFile</key>
|
||||||
<string>Main</string>
|
<string>Main</string>
|
||||||
|
<key>UIStatusBarHidden</key>
|
||||||
|
<false/>
|
||||||
<key>UISupportedInterfaceOrientations</key>
|
<key>UISupportedInterfaceOrientations</key>
|
||||||
<array>
|
<array>
|
||||||
<string>UIInterfaceOrientationPortrait</string>
|
<string>UIInterfaceOrientationPortrait</string>
|
||||||
@@ -105,7 +111,5 @@
|
|||||||
<string>UIInterfaceOrientationLandscapeLeft</string>
|
<string>UIInterfaceOrientationLandscapeLeft</string>
|
||||||
<string>UIInterfaceOrientationLandscapeRight</string>
|
<string>UIInterfaceOrientationLandscapeRight</string>
|
||||||
</array>
|
</array>
|
||||||
<key>UIStatusBarHidden</key>
|
|
||||||
<false/>
|
|
||||||
</dict>
|
</dict>
|
||||||
</plist>
|
</plist>
|
||||||
@@ -14,6 +14,7 @@ import 'core/data/remote/catalog_api.dart';
|
|||||||
import 'core/data/remote/category_api.dart';
|
import 'core/data/remote/category_api.dart';
|
||||||
import 'core/data/remote/comment_api.dart';
|
import 'core/data/remote/comment_api.dart';
|
||||||
import 'core/data/remote/commerce_api.dart';
|
import 'core/data/remote/commerce_api.dart';
|
||||||
|
import 'core/data/remote/config_api.dart';
|
||||||
import 'core/data/remote/device_profile.dart';
|
import 'core/data/remote/device_profile.dart';
|
||||||
import 'core/data/remote/download_api.dart';
|
import 'core/data/remote/download_api.dart';
|
||||||
import 'core/data/remote/interaction_api.dart';
|
import 'core/data/remote/interaction_api.dart';
|
||||||
@@ -35,6 +36,7 @@ import 'core/data/repositories/playlist_repository.dart';
|
|||||||
import 'core/data/repositories/search_repository.dart';
|
import 'core/data/repositories/search_repository.dart';
|
||||||
import 'core/data/repositories/store_repository.dart';
|
import 'core/data/repositories/store_repository.dart';
|
||||||
import 'core/data/repositories/watch_history_repository.dart';
|
import 'core/data/repositories/watch_history_repository.dart';
|
||||||
|
import 'core/data/services/app_config_controller.dart';
|
||||||
import 'core/data/services/auth_controller.dart';
|
import 'core/data/services/auth_controller.dart';
|
||||||
import 'core/data/services/auth_session_store.dart';
|
import 'core/data/services/auth_session_store.dart';
|
||||||
import 'core/data/services/catalog_controller.dart';
|
import 'core/data/services/catalog_controller.dart';
|
||||||
@@ -47,6 +49,7 @@ import 'core/data/services/playlist_controller.dart';
|
|||||||
import 'core/data/services/event_tracker.dart';
|
import 'core/data/services/event_tracker.dart';
|
||||||
import 'core/data/services/playback_position_cache.dart';
|
import 'core/data/services/playback_position_cache.dart';
|
||||||
import 'core/data/services/playback_session.dart';
|
import 'core/data/services/playback_session.dart';
|
||||||
|
import 'core/data/services/unlocked_episode_registry.dart';
|
||||||
import 'core/data/services/video_player_port.dart';
|
import 'core/data/services/video_player_port.dart';
|
||||||
import 'core/data/services/wallet_controller.dart';
|
import 'core/data/services/wallet_controller.dart';
|
||||||
import 'core/navigation/app_route_observer.dart';
|
import 'core/navigation/app_route_observer.dart';
|
||||||
@@ -207,12 +210,22 @@ class _LumaDramaAppState extends State<LumaDramaApp> {
|
|||||||
|
|
||||||
late final LocaleController _localeController = LocaleController();
|
late final LocaleController _localeController = LocaleController();
|
||||||
|
|
||||||
|
/// 后台下发的启动配置(登录页背景等);拉不到就用本地默认。
|
||||||
|
late final AppConfigController _appConfigController = AppConfigController(
|
||||||
|
ConfigApi(_apiClient),
|
||||||
|
);
|
||||||
|
|
||||||
|
/// 登录期内共享的「已解锁收费集」:播放页进出、feed 与全屏切换都要沿用。
|
||||||
|
late final UnlockedEpisodeRegistry _unlockedEpisodes =
|
||||||
|
UnlockedEpisodeRegistry();
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
// 恢复本地令牌与设备号;未登录也能正常浏览,不阻塞首屏
|
// 恢复本地令牌与设备号;未登录也能正常浏览,不阻塞首屏
|
||||||
_auth.load();
|
_auth.load();
|
||||||
unawaited(_localeController.load());
|
unawaited(_localeController.load());
|
||||||
|
unawaited(_appConfigController.load());
|
||||||
// 登录/登出时同步收藏状态:收藏属于账号,不能跨账号串联
|
// 登录/登出时同步收藏状态:收藏属于账号,不能跨账号串联
|
||||||
_auth.addListener(_onAuthChanged);
|
_auth.addListener(_onAuthChanged);
|
||||||
unawaited(_favoriteController.load());
|
unawaited(_favoriteController.load());
|
||||||
@@ -226,6 +239,8 @@ class _LumaDramaAppState extends State<LumaDramaApp> {
|
|||||||
if (_auth.isSignedIn == _wasSignedIn) return;
|
if (_auth.isSignedIn == _wasSignedIn) return;
|
||||||
_wasSignedIn = _auth.isSignedIn;
|
_wasSignedIn = _auth.isSignedIn;
|
||||||
_interactionController.clear();
|
_interactionController.clear();
|
||||||
|
// 解锁结论属于账号:换人登录后不能沿用上一个人的可播判断。
|
||||||
|
_unlockedEpisodes.clear();
|
||||||
if (_auth.isSignedIn) {
|
if (_auth.isSignedIn) {
|
||||||
unawaited(_favoriteController.load());
|
unawaited(_favoriteController.load());
|
||||||
unawaited(_walletController.load());
|
unawaited(_walletController.load());
|
||||||
@@ -245,6 +260,7 @@ class _LumaDramaAppState extends State<LumaDramaApp> {
|
|||||||
_walletController.dispose();
|
_walletController.dispose();
|
||||||
_interactionController.dispose();
|
_interactionController.dispose();
|
||||||
_localeController.dispose();
|
_localeController.dispose();
|
||||||
|
_appConfigController.dispose();
|
||||||
_apiClient.close();
|
_apiClient.close();
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
@@ -252,7 +268,9 @@ class _LumaDramaAppState extends State<LumaDramaApp> {
|
|||||||
CategoryController _createCategories() {
|
CategoryController _createCategories() {
|
||||||
final controller = CategoryController(_categoryRepository);
|
final controller = CategoryController(_categoryRepository);
|
||||||
if (widget.catalogRepository == null) {
|
if (widget.catalogRepository == null) {
|
||||||
controller.dramaPageLoader = CategoryApi(_apiClient).dramas;
|
final api = CategoryApi(_apiClient);
|
||||||
|
controller.dramaPageLoader = api.dramas;
|
||||||
|
controller.rankingLoader = api.ranking;
|
||||||
}
|
}
|
||||||
controller.load();
|
controller.load();
|
||||||
return controller;
|
return controller;
|
||||||
@@ -271,6 +289,7 @@ class _LumaDramaAppState extends State<LumaDramaApp> {
|
|||||||
Provider<CatalogApi>.value(value: _catalogApi),
|
Provider<CatalogApi>.value(value: _catalogApi),
|
||||||
Provider<PlaybackRepository>.value(value: _playback),
|
Provider<PlaybackRepository>.value(value: _playback),
|
||||||
Provider<PlaybackPositionCache>.value(value: _playbackPositionCache),
|
Provider<PlaybackPositionCache>.value(value: _playbackPositionCache),
|
||||||
|
Provider<UnlockedEpisodeRegistry>.value(value: _unlockedEpisodes),
|
||||||
Provider<WatchHistoryRepository>.value(value: _watchHistory),
|
Provider<WatchHistoryRepository>.value(value: _watchHistory),
|
||||||
Provider<FavoriteRepository>.value(value: _favorites),
|
Provider<FavoriteRepository>.value(value: _favorites),
|
||||||
Provider<CommentRepository>.value(value: _commentRepository),
|
Provider<CommentRepository>.value(value: _commentRepository),
|
||||||
@@ -296,6 +315,7 @@ class _LumaDramaAppState extends State<LumaDramaApp> {
|
|||||||
ChangeNotifierProvider.value(value: _walletController),
|
ChangeNotifierProvider.value(value: _walletController),
|
||||||
ChangeNotifierProvider.value(value: _interactionController),
|
ChangeNotifierProvider.value(value: _interactionController),
|
||||||
ChangeNotifierProvider.value(value: _localeController),
|
ChangeNotifierProvider.value(value: _localeController),
|
||||||
|
ChangeNotifierProvider.value(value: _appConfigController),
|
||||||
ChangeNotifierProvider.value(value: _auth),
|
ChangeNotifierProvider.value(value: _auth),
|
||||||
],
|
],
|
||||||
child: Consumer<LocaleController>(
|
child: Consumer<LocaleController>(
|
||||||
|
|||||||
@@ -1,165 +1,51 @@
|
|||||||
// Author: fruit.com.cn
|
// Author: fruit.com.cn
|
||||||
|
|
||||||
import 'dart:io';
|
|
||||||
|
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
|
|
||||||
/// 客户端与后端约定的运行时配置。
|
import 'dev_config.dart';
|
||||||
|
import 'prod_config.dart';
|
||||||
|
|
||||||
|
/// 客户端运行时配置。
|
||||||
///
|
///
|
||||||
/// 接口地址优先级:运行时设置 > `--dart-define=API_BASE_URL=...` > 平台默认值。
|
/// 两套环境各一个文件:
|
||||||
/// 开发机的局域网地址会随网络变化,换网络后改 [devHostBaseUrl] 即可。
|
/// - `dev_config.dart`:[DevConfig](本机联调)
|
||||||
class AppConfig {
|
/// - `prod_config.dart`:[ProdConfig](生产)
|
||||||
const AppConfig._();
|
|
||||||
|
|
||||||
/// 开发调试默认服务地址,当前指向本机局域网 IP。
|
|
||||||
///
|
///
|
||||||
/// 换网络时只改这一行,地址不要带结尾斜杠。
|
/// Debug 使用开发环境,Release 自动使用生产环境,不做网络探测。
|
||||||
static const String devHostBaseUrl = 'http://192.168.31.243:8081';
|
abstract final class AppConfig {
|
||||||
|
/// **环境开关**:Debug 使用开发环境,Release 使用生产环境。
|
||||||
|
static const bool useDevConfig = !kReleaseMode;
|
||||||
|
|
||||||
/// 本机后端地址:通过 `adb reverse tcp:8081 tcp:8081` 映射到开发机,
|
/// 当前选中的环境地址。
|
||||||
/// 探测可达时 App 直连本机服务;不可达时回落到公网地址。
|
static String get baseUrl =>
|
||||||
static const String lanBaseUrl = 'http://127.0.0.1:8081';
|
useDevConfig ? DevConfig.baseUrl : ProdConfig.baseUrl;
|
||||||
|
|
||||||
/// 公网地址(远程/移动网络访问时使用)。
|
|
||||||
static const String publicBaseUrl = 'http://39.162.50.99:8081';
|
|
||||||
|
|
||||||
static const String _overrideBaseUrl = String.fromEnvironment('API_BASE_URL');
|
|
||||||
|
|
||||||
|
/// 调试面板手改的地址(见 `ServerEndpoint`);为空表示用上面的环境地址。
|
||||||
static String _runtimeBaseUrl = '';
|
static String _runtimeBaseUrl = '';
|
||||||
|
|
||||||
|
/// 启动期异步准备(iOS 无线数据权限弹窗),目录请求前等待它。
|
||||||
static Future<void>? _ready;
|
static Future<void>? _ready;
|
||||||
|
|
||||||
/// 启动地址探测完成信号;默认已完成,避免影响既有测试与调用方。
|
/// 启动准备完成信号;默认已完成,避免影响既有测试与调用方。
|
||||||
static Future<void> get ready => _ready ?? Future<void>.value();
|
static Future<void> get ready => _ready ?? Future<void>.value();
|
||||||
static set ready(Future<void> value) => _ready = value;
|
static set ready(Future<void> value) => _ready = value;
|
||||||
|
|
||||||
static const Duration _probeTimeout = Duration(seconds: 2);
|
static const Duration requestTimeout = Duration(seconds: 8);
|
||||||
static const List<String> _probeCandidates = [
|
|
||||||
lanBaseUrl,
|
|
||||||
devHostBaseUrl,
|
|
||||||
if (devHostBaseUrl != publicBaseUrl) publicBaseUrl,
|
|
||||||
];
|
|
||||||
|
|
||||||
/// Build-time server URL supplied through `--dart-define=API_BASE_URL`.
|
/// 实际使用的服务地址:运行时手改优先,否则用环境开关选中的地址。
|
||||||
static String get compiledBaseUrl => _overrideBaseUrl;
|
static String get apiBaseUrl =>
|
||||||
|
_runtimeBaseUrl.isNotEmpty ? _runtimeBaseUrl : baseUrl;
|
||||||
|
|
||||||
/// Existing platform-aware fallback used when no runtime or build override exists.
|
/// 应用一个已经规范化的运行时地址;传 null 表示清除。
|
||||||
static String get defaultBaseUrl => _defaultBaseUrl;
|
|
||||||
|
|
||||||
/// Effective server URL, including the debug-only runtime override.
|
|
||||||
static String get apiBaseUrl => _runtimeBaseUrl.isNotEmpty
|
|
||||||
? _runtimeBaseUrl
|
|
||||||
: (compiledBaseUrl.isNotEmpty ? compiledBaseUrl : defaultBaseUrl);
|
|
||||||
|
|
||||||
static final String _defaultBaseUrl = _runsOnDevMachine
|
|
||||||
? lanBaseUrl
|
|
||||||
: devHostBaseUrl;
|
|
||||||
|
|
||||||
/// 模拟器、桌面端与开发机同机,环回地址可用;iOS 真机不满足。
|
|
||||||
/// Android 没有等价的探测方式,统一按真机处理(模拟器用
|
|
||||||
/// `--dart-define=API_BASE_URL=http://10.0.2.2:8081` 覆盖)。
|
|
||||||
static final bool _runsOnDevMachine =
|
|
||||||
kIsWeb ||
|
|
||||||
defaultTargetPlatform == TargetPlatform.macOS ||
|
|
||||||
defaultTargetPlatform == TargetPlatform.windows ||
|
|
||||||
defaultTargetPlatform == TargetPlatform.linux ||
|
|
||||||
(defaultTargetPlatform == TargetPlatform.iOS &&
|
|
||||||
Platform.environment.containsKey('SIMULATOR_DEVICE_NAME'));
|
|
||||||
|
|
||||||
/// Applies an already normalized runtime URL. Passing null clears it.
|
|
||||||
static void applyRuntimeBaseUrl(String? value) {
|
static void applyRuntimeBaseUrl(String? value) {
|
||||||
_runtimeBaseUrl = value ?? '';
|
_runtimeBaseUrl = value ?? '';
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Clears the in-memory runtime override, mainly for tests and restarts.
|
/// 清除运行时地址,主要给测试与重启流程用。
|
||||||
static void clearRuntimeBaseUrl() {
|
static void clearRuntimeBaseUrl() {
|
||||||
_runtimeBaseUrl = '';
|
_runtimeBaseUrl = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
static const Duration requestTimeout = Duration(seconds: 8);
|
|
||||||
|
|
||||||
/// 按环回、开发机、公网顺序探测,写入首个返回 200 的地址。
|
|
||||||
///
|
|
||||||
/// 全部不可达时使用平台默认地址,真机保留开发机地址,避免退回公网。
|
|
||||||
static Future<void> probeAndApplyBaseUrl({
|
|
||||||
Future<bool> Function(String baseUrl)? probe,
|
|
||||||
String? fallbackBaseUrl,
|
|
||||||
|
|
||||||
/// 编译期地址测试注入;未传时读取 `API_BASE_URL`。
|
|
||||||
String? compiledOverride,
|
|
||||||
}) async {
|
|
||||||
final compiled = compiledOverride ?? compiledBaseUrl;
|
|
||||||
if (compiled.isNotEmpty) {
|
|
||||||
if (_runtimeBaseUrl.isEmpty) applyRuntimeBaseUrl(compiled);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
final candidates = <String>[
|
|
||||||
if (_runtimeBaseUrl.isNotEmpty) _runtimeBaseUrl,
|
|
||||||
for (final candidate in _probeCandidates)
|
|
||||||
if (candidate != _runtimeBaseUrl) candidate,
|
|
||||||
];
|
|
||||||
final results = await Future.wait([
|
|
||||||
for (final candidate in candidates)
|
|
||||||
probe == null
|
|
||||||
? _probeBaseUrl(candidate)
|
|
||||||
: _runInjectedProbe(candidate, probe),
|
|
||||||
]);
|
|
||||||
for (var index = 0; index < candidates.length; index++) {
|
|
||||||
final candidate = candidates[index];
|
|
||||||
final result = results[index];
|
|
||||||
if (kDebugMode) {
|
|
||||||
final status = result.ok
|
|
||||||
? 'ok'
|
|
||||||
: 'fail(${result.reason ?? 'request failed'})';
|
|
||||||
debugPrint('[probe] $candidate -> $status');
|
|
||||||
}
|
|
||||||
if (result.ok) {
|
|
||||||
applyRuntimeBaseUrl(candidate);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
applyRuntimeBaseUrl(fallbackBaseUrl ?? defaultBaseUrl);
|
|
||||||
}
|
|
||||||
|
|
||||||
static Future<_ProbeResult> _runInjectedProbe(
|
|
||||||
String baseUrl,
|
|
||||||
Future<bool> Function(String baseUrl) probe,
|
|
||||||
) async {
|
|
||||||
try {
|
|
||||||
return _ProbeResult(ok: await probe(baseUrl));
|
|
||||||
} catch (error) {
|
|
||||||
return _ProbeResult(ok: false, reason: error.toString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static Future<_ProbeResult> _probeBaseUrl(String baseUrl) async {
|
|
||||||
final client = HttpClient()..connectionTimeout = _probeTimeout;
|
|
||||||
try {
|
|
||||||
return await _requestCategories(
|
|
||||||
client,
|
|
||||||
Uri.parse('$baseUrl/app/categories'),
|
|
||||||
).timeout(_probeTimeout);
|
|
||||||
} catch (error) {
|
|
||||||
return _ProbeResult(ok: false, reason: error.toString());
|
|
||||||
} finally {
|
|
||||||
client.close(force: true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static Future<_ProbeResult> _requestCategories(
|
|
||||||
HttpClient client,
|
|
||||||
Uri uri,
|
|
||||||
) async {
|
|
||||||
final request = await client.getUrl(uri);
|
|
||||||
final response = await request.close();
|
|
||||||
await response.drain<void>();
|
|
||||||
final ok = response.statusCode == 200;
|
|
||||||
return _ProbeResult(
|
|
||||||
ok: ok,
|
|
||||||
reason: ok ? null : 'HTTP ${response.statusCode}',
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// 后端返回的是相对路径(/files/...),这里补上服务地址。
|
/// 后端返回的是相对路径(/files/...),这里补上服务地址。
|
||||||
static String mediaUrl(String path) {
|
static String mediaUrl(String path) {
|
||||||
if (path.isEmpty) return '';
|
if (path.isEmpty) return '';
|
||||||
@@ -167,10 +53,3 @@ class AppConfig {
|
|||||||
return '$apiBaseUrl$path';
|
return '$apiBaseUrl$path';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class _ProbeResult {
|
|
||||||
const _ProbeResult({required this.ok, this.reason});
|
|
||||||
|
|
||||||
final bool ok;
|
|
||||||
final String? reason;
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
// Author: fruit.com.cn
|
||||||
|
|
||||||
|
/// 开发环境配置。
|
||||||
|
///
|
||||||
|
/// 本机联调用:模拟器与桌面端可直接用回环地址,但 iOS/Android 真机必须用
|
||||||
|
/// 开发机的局域网 IP(真机上的 127.0.0.1 指向手机自己)。地址不要带结尾斜杠。
|
||||||
|
abstract final class DevConfig {
|
||||||
|
/// 开发机后端地址(Mac 当前 Wi-Fi 地址,换网络后需要同步修改)。
|
||||||
|
static const String baseUrl = 'http://192.168.31.243:8081';
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
// Author: fruit.com.cn
|
||||||
|
|
||||||
|
/// 生产环境配置。
|
||||||
|
///
|
||||||
|
/// 发版前确认这里的 [baseUrl],地址不要带结尾斜杠。
|
||||||
|
abstract final class ProdConfig {
|
||||||
|
/// 生产后端地址。
|
||||||
|
static const String baseUrl = 'http://39.162.50.99:8081';
|
||||||
|
}
|
||||||
@@ -22,16 +22,9 @@ abstract final class ServerEndpoint {
|
|||||||
return value.replaceFirst(RegExp(r'/+$'), '');
|
return value.replaceFirst(RegExp(r'/+$'), '');
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Resolves runtime > dart-define > default using normalized valid values.
|
/// 运行时手改优先,其次构建期默认地址;两者都必须先通过规范校验。
|
||||||
static String resolve({
|
static String resolve({required String? stored, required String fallback}) {
|
||||||
required String? stored,
|
return normalize(stored) ?? normalize(fallback) ?? fallback;
|
||||||
required String compiled,
|
|
||||||
required String fallback,
|
|
||||||
}) {
|
|
||||||
return normalize(stored) ??
|
|
||||||
normalize(compiled) ??
|
|
||||||
normalize(fallback) ??
|
|
||||||
fallback;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Loads the persisted override before the app creates its ApiClient.
|
/// Loads the persisted override before the app creates its ApiClient.
|
||||||
@@ -46,11 +39,7 @@ abstract final class ServerEndpoint {
|
|||||||
final prefs = preferences ?? SharedPreferencesAsync();
|
final prefs = preferences ?? SharedPreferencesAsync();
|
||||||
final stored = await prefs.getString(preferenceKey);
|
final stored = await prefs.getString(preferenceKey);
|
||||||
AppConfig.applyRuntimeBaseUrl(
|
AppConfig.applyRuntimeBaseUrl(
|
||||||
resolve(
|
resolve(stored: stored, fallback: AppConfig.baseUrl),
|
||||||
stored: stored,
|
|
||||||
compiled: AppConfig.compiledBaseUrl,
|
|
||||||
fallback: AppConfig.defaultBaseUrl,
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
AppConfig.clearRuntimeBaseUrl();
|
AppConfig.clearRuntimeBaseUrl();
|
||||||
|
|||||||
@@ -0,0 +1,67 @@
|
|||||||
|
// Author: fruit.com.cn
|
||||||
|
|
||||||
|
import 'drama.dart';
|
||||||
|
import 'locale_text.dart';
|
||||||
|
|
||||||
|
/// 服务端推荐榜快照;顺序和排名均不可在客户端重算。
|
||||||
|
class CategoryRanking {
|
||||||
|
const CategoryRanking({
|
||||||
|
required this.id,
|
||||||
|
required this.code,
|
||||||
|
required this.revision,
|
||||||
|
required this.entries,
|
||||||
|
this.locales = const {},
|
||||||
|
this.generatedAt,
|
||||||
|
});
|
||||||
|
|
||||||
|
final String id;
|
||||||
|
final String code;
|
||||||
|
final String revision;
|
||||||
|
final Map<String, String> locales;
|
||||||
|
final DateTime? generatedAt;
|
||||||
|
final List<CategoryRankingEntry> entries;
|
||||||
|
|
||||||
|
/// 解析 `GET /app/categories/{id}/ranking`。
|
||||||
|
factory CategoryRanking.fromJson(Map<String, dynamic> json) {
|
||||||
|
final rawList = json['list'];
|
||||||
|
final entries = rawList is List
|
||||||
|
? rawList
|
||||||
|
.whereType<Map>()
|
||||||
|
.map(
|
||||||
|
(item) =>
|
||||||
|
CategoryRankingEntry.fromJson(item.cast<String, dynamic>()),
|
||||||
|
)
|
||||||
|
.toList(growable: false)
|
||||||
|
: const <CategoryRankingEntry>[];
|
||||||
|
return CategoryRanking(
|
||||||
|
id: (json['id'] ?? '').toString(),
|
||||||
|
code: json['code'] as String? ?? '',
|
||||||
|
revision: (json['revision'] ?? '1').toString(),
|
||||||
|
locales: localeMap(json['locales']),
|
||||||
|
generatedAt: DateTime.tryParse(json['generatedAt'] as String? ?? ''),
|
||||||
|
entries: entries,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 推荐榜中的一部短剧及其服务端排名。
|
||||||
|
class CategoryRankingEntry {
|
||||||
|
const CategoryRankingEntry({required this.rank, required this.drama});
|
||||||
|
|
||||||
|
final int rank;
|
||||||
|
final Drama drama;
|
||||||
|
|
||||||
|
factory CategoryRankingEntry.fromJson(Map<String, dynamic> json) {
|
||||||
|
final rawDrama = json['drama'];
|
||||||
|
return CategoryRankingEntry(
|
||||||
|
rank: (json['rank'] as num?)?.toInt() ?? 0,
|
||||||
|
drama: Drama.fromCardJson(
|
||||||
|
rawDrama is Map<String, dynamic>
|
||||||
|
? rawDrama
|
||||||
|
: rawDrama is Map
|
||||||
|
? rawDrama.cast<String, dynamic>()
|
||||||
|
: const <String, dynamic>{},
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,10 +7,19 @@ class ContentCategory {
|
|||||||
const ContentCategory({
|
const ContentCategory({
|
||||||
required this.id,
|
required this.id,
|
||||||
required this.name,
|
required this.name,
|
||||||
|
this.code = '',
|
||||||
|
this.layoutType = gridLayout,
|
||||||
|
this.revision = '1',
|
||||||
this.locales = const {},
|
this.locales = const {},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
static const String gridLayout = 'GRID';
|
||||||
|
static const String rankingLayout = 'RANKING';
|
||||||
|
|
||||||
final String id;
|
final String id;
|
||||||
|
final String code;
|
||||||
|
final String layoutType;
|
||||||
|
final String revision;
|
||||||
|
|
||||||
/// 中文名称,与 locales 里的 zh 相同。
|
/// 中文名称,与 locales 里的 zh 相同。
|
||||||
final String name;
|
final String name;
|
||||||
@@ -20,9 +29,18 @@ class ContentCategory {
|
|||||||
factory ContentCategory.fromJson(Map<String, dynamic> json) {
|
factory ContentCategory.fromJson(Map<String, dynamic> json) {
|
||||||
final locales = localeMap(json['locales']);
|
final locales = localeMap(json['locales']);
|
||||||
final name = json['name'] as String? ?? locales['zh'] ?? '';
|
final name = json['name'] as String? ?? locales['zh'] ?? '';
|
||||||
return ContentCategory(id: (json['id'] ?? '').toString(), name: name, locales: locales);
|
return ContentCategory(
|
||||||
|
id: (json['id'] ?? '').toString(),
|
||||||
|
name: name,
|
||||||
|
code: json['code'] as String? ?? '',
|
||||||
|
layoutType: json['layoutType'] as String? ?? gridLayout,
|
||||||
|
revision: (json['revision'] ?? '1').toString(),
|
||||||
|
locales: locales,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool get isRanking => layoutType == rankingLayout;
|
||||||
|
|
||||||
/// 按界面语言取名称。缺对应语言时回落到中文。
|
/// 按界面语言取名称。缺对应语言时回落到中文。
|
||||||
String localizedName(String languageCode) {
|
String localizedName(String languageCode) {
|
||||||
return localizedText(languageCode, name, locales);
|
return localizedText(languageCode, name, locales);
|
||||||
|
|||||||
@@ -97,11 +97,13 @@ class Drama {
|
|||||||
required this.title,
|
required this.title,
|
||||||
required this.synopsis,
|
required this.synopsis,
|
||||||
required this.posterUrl,
|
required this.posterUrl,
|
||||||
|
this.coverLandscape = '',
|
||||||
this.episodes = const [],
|
this.episodes = const [],
|
||||||
this.titleLocales = const {},
|
this.titleLocales = const {},
|
||||||
this.synopsisLocales = const {},
|
this.synopsisLocales = const {},
|
||||||
this.tag = '',
|
this.tag = '',
|
||||||
this.tagLocales = const {},
|
this.tagLocales = const {},
|
||||||
|
this.tagCode = '',
|
||||||
this.tagColor = '',
|
this.tagColor = '',
|
||||||
this.category = '',
|
this.category = '',
|
||||||
this.categoryLocales = const {},
|
this.categoryLocales = const {},
|
||||||
@@ -127,8 +129,14 @@ class Drama {
|
|||||||
final Map<String, String> synopsisLocales;
|
final Map<String, String> synopsisLocales;
|
||||||
final String tag;
|
final String tag;
|
||||||
final Map<String, String> tagLocales;
|
final Map<String, String> tagLocales;
|
||||||
|
|
||||||
|
/// 标签语义码(如 `NEW_ARRIVAL`)。文案会随语言变,语义判断只认这个码。
|
||||||
|
final String tagCode;
|
||||||
final String tagColor;
|
final String tagColor;
|
||||||
final String posterUrl;
|
final String posterUrl;
|
||||||
|
|
||||||
|
/// 横版封面,用于继续观看横幅等横向展示位;为空时回退竖版 `posterUrl`。
|
||||||
|
final String coverLandscape;
|
||||||
final List<Episode> episodes;
|
final List<Episode> episodes;
|
||||||
final String category;
|
final String category;
|
||||||
final Map<String, String> categoryLocales;
|
final Map<String, String> categoryLocales;
|
||||||
@@ -172,8 +180,10 @@ class Drama {
|
|||||||
synopsisLocales: text.synopsisLocales,
|
synopsisLocales: text.synopsisLocales,
|
||||||
tag: text.tag,
|
tag: text.tag,
|
||||||
tagLocales: text.tagLocales,
|
tagLocales: text.tagLocales,
|
||||||
|
tagCode: (json['tagCode'] as String? ?? '').trim(),
|
||||||
tagColor: json['tagColor'] as String? ?? '',
|
tagColor: json['tagColor'] as String? ?? '',
|
||||||
posterUrl: json['cover'] as String? ?? '',
|
posterUrl: json['cover'] as String? ?? '',
|
||||||
|
coverLandscape: json['coverLandscape'] as String? ?? '',
|
||||||
episodes: preview is Map<String, dynamic>
|
episodes: preview is Map<String, dynamic>
|
||||||
? [Episode.fromJson(preview)]
|
? [Episode.fromJson(preview)]
|
||||||
: const [],
|
: const [],
|
||||||
@@ -204,8 +214,10 @@ class Drama {
|
|||||||
synopsisLocales: text.synopsisLocales,
|
synopsisLocales: text.synopsisLocales,
|
||||||
tag: text.tag,
|
tag: text.tag,
|
||||||
tagLocales: text.tagLocales,
|
tagLocales: text.tagLocales,
|
||||||
|
tagCode: (json['tagCode'] as String? ?? '').trim(),
|
||||||
tagColor: json['tagColor'] as String? ?? '',
|
tagColor: json['tagColor'] as String? ?? '',
|
||||||
posterUrl: json['cover'] as String? ?? '',
|
posterUrl: json['cover'] as String? ?? '',
|
||||||
|
coverLandscape: json['coverLandscape'] as String? ?? '',
|
||||||
category: text.category,
|
category: text.category,
|
||||||
categoryLocales: text.categoryLocales,
|
categoryLocales: text.categoryLocales,
|
||||||
types: text.types,
|
types: text.types,
|
||||||
@@ -315,7 +327,10 @@ class _DramaText {
|
|||||||
final category = _firstText(categoryLocales, json['category'], types);
|
final category = _firstText(categoryLocales, json['category'], types);
|
||||||
return _DramaText(
|
return _DramaText(
|
||||||
title: _firstText(titleLocales, json['title']),
|
title: _firstText(titleLocales, json['title']),
|
||||||
synopsis: _firstText(synopsisLocales, json['synopsis'] ?? json['description']),
|
synopsis: _firstText(
|
||||||
|
synopsisLocales,
|
||||||
|
json['synopsis'] ?? json['description'],
|
||||||
|
),
|
||||||
titleLocales: titleLocales,
|
titleLocales: titleLocales,
|
||||||
synopsisLocales: synopsisLocales,
|
synopsisLocales: synopsisLocales,
|
||||||
tag: _firstText(tagLocales, json['tag']),
|
tag: _firstText(tagLocales, json['tag']),
|
||||||
@@ -328,7 +343,11 @@ class _DramaText {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
String _firstText(Map<String, String> locales, Object? fallback, [List<Map<String, String>>? types]) {
|
String _firstText(
|
||||||
|
Map<String, String> locales,
|
||||||
|
Object? fallback, [
|
||||||
|
List<Map<String, String>>? types,
|
||||||
|
]) {
|
||||||
final chinese = locales['zh'] ?? '';
|
final chinese = locales['zh'] ?? '';
|
||||||
if (chinese.isNotEmpty) return chinese;
|
if (chinese.isNotEmpty) return chinese;
|
||||||
if (types != null && types.isNotEmpty) {
|
if (types != null && types.isNotEmpty) {
|
||||||
@@ -339,7 +358,9 @@ String _firstText(Map<String, String> locales, Object? fallback, [List<Map<Strin
|
|||||||
}
|
}
|
||||||
|
|
||||||
String _genreText(List<Map<String, String>> types, Object? fallback) {
|
String _genreText(List<Map<String, String>> types, Object? fallback) {
|
||||||
final names = types.map((item) => item['zh'] ?? '').where((value) => value.isNotEmpty);
|
final names = types
|
||||||
|
.map((item) => item['zh'] ?? '')
|
||||||
|
.where((value) => value.isNotEmpty);
|
||||||
final joined = names.join(' / ');
|
final joined = names.join(' / ');
|
||||||
if (joined.isNotEmpty) return joined;
|
if (joined.isNotEmpty) return joined;
|
||||||
return fallback is String ? fallback : '';
|
return fallback is String ? fallback : '';
|
||||||
|
|||||||
@@ -7,7 +7,11 @@ Map<String, String> localeMap(Object? raw) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// 按当前语言取文案。没有对应语言时用中文,中文也没有时用 fallback。
|
/// 按当前语言取文案。没有对应语言时用中文,中文也没有时用 fallback。
|
||||||
String localizedText(String languageCode, String fallback, Map<String, String> locales) {
|
String localizedText(
|
||||||
|
String languageCode,
|
||||||
|
String fallback,
|
||||||
|
Map<String, String> locales,
|
||||||
|
) {
|
||||||
final selected = locales[languageCode] ?? '';
|
final selected = locales[languageCode] ?? '';
|
||||||
if (selected.isNotEmpty) return selected;
|
if (selected.isNotEmpty) return selected;
|
||||||
final chinese = locales['zh'] ?? '';
|
final chinese = locales['zh'] ?? '';
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ class WatchProgress {
|
|||||||
this.dramaTitle,
|
this.dramaTitle,
|
||||||
this.titleLocales = const {},
|
this.titleLocales = const {},
|
||||||
this.dramaCover,
|
this.dramaCover,
|
||||||
|
this.dramaCoverLandscape,
|
||||||
this.episodeNumber,
|
this.episodeNumber,
|
||||||
this.totalEpisodes,
|
this.totalEpisodes,
|
||||||
this.finished = false,
|
this.finished = false,
|
||||||
@@ -29,6 +30,9 @@ class WatchProgress {
|
|||||||
final String? dramaTitle;
|
final String? dramaTitle;
|
||||||
final Map<String, String> titleLocales;
|
final Map<String, String> titleLocales;
|
||||||
final String? dramaCover;
|
final String? dramaCover;
|
||||||
|
|
||||||
|
/// 横版封面,用于继续观看横幅等横向位;为空时回退 [dramaCover]。
|
||||||
|
final String? dramaCoverLandscape;
|
||||||
final int? episodeNumber;
|
final int? episodeNumber;
|
||||||
final int? totalEpisodes;
|
final int? totalEpisodes;
|
||||||
final Duration position;
|
final Duration position;
|
||||||
@@ -60,6 +64,7 @@ class WatchProgress {
|
|||||||
dramaTitle: json['dramaTitle']?.toString(),
|
dramaTitle: json['dramaTitle']?.toString(),
|
||||||
titleLocales: localeMap(json['titleLocales']),
|
titleLocales: localeMap(json['titleLocales']),
|
||||||
dramaCover: json['dramaCover']?.toString(),
|
dramaCover: json['dramaCover']?.toString(),
|
||||||
|
dramaCoverLandscape: json['dramaCoverLandscape']?.toString(),
|
||||||
episodeNumber: json['episodeNumber'] == null
|
episodeNumber: json['episodeNumber'] == null
|
||||||
? null
|
? null
|
||||||
: jsonToInt(json['episodeNumber']),
|
: jsonToInt(json['episodeNumber']),
|
||||||
@@ -83,6 +88,7 @@ class WatchProgress {
|
|||||||
if (dramaTitle != null) 'dramaTitle': dramaTitle,
|
if (dramaTitle != null) 'dramaTitle': dramaTitle,
|
||||||
if (titleLocales.isNotEmpty) 'titleLocales': titleLocales,
|
if (titleLocales.isNotEmpty) 'titleLocales': titleLocales,
|
||||||
if (dramaCover != null) 'dramaCover': dramaCover,
|
if (dramaCover != null) 'dramaCover': dramaCover,
|
||||||
|
if (dramaCoverLandscape != null) 'dramaCoverLandscape': dramaCoverLandscape,
|
||||||
if (episodeNumber != null) 'episodeNumber': episodeNumber,
|
if (episodeNumber != null) 'episodeNumber': episodeNumber,
|
||||||
if (totalEpisodes != null) 'totalEpisodes': totalEpisodes,
|
if (totalEpisodes != null) 'totalEpisodes': totalEpisodes,
|
||||||
'positionMs': position.inMilliseconds,
|
'positionMs': position.inMilliseconds,
|
||||||
|
|||||||
@@ -46,6 +46,14 @@ class WatchRecord {
|
|||||||
return pageCover.isNotEmpty ? pageCover : drama?.posterUrl ?? '';
|
return pageCover.isNotEmpty ? pageCover : drama?.posterUrl ?? '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 横向展示位(继续观看横幅)优先用横版封面,缺省回退竖版。
|
||||||
|
String get displayLandscapeCover {
|
||||||
|
final pageLandscape = progress.dramaCoverLandscape?.trim() ?? '';
|
||||||
|
if (pageLandscape.isNotEmpty) return pageLandscape;
|
||||||
|
final landscape = drama?.coverLandscape.trim() ?? '';
|
||||||
|
return landscape.isNotEmpty ? landscape : displayCover;
|
||||||
|
}
|
||||||
|
|
||||||
/// 分页接口已带展示字段时无需等待详情加载。
|
/// 分页接口已带展示字段时无需等待详情加载。
|
||||||
bool get hasDisplayMetadata =>
|
bool get hasDisplayMetadata =>
|
||||||
displayTitle.isNotEmpty ||
|
displayTitle.isNotEmpty ||
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import '../models/account_device.dart';
|
|||||||
import '../models/account_export.dart';
|
import '../models/account_export.dart';
|
||||||
import '../models/account_profile.dart';
|
import '../models/account_profile.dart';
|
||||||
import 'api_client.dart';
|
import 'api_client.dart';
|
||||||
|
import 'response_decoders.dart';
|
||||||
|
|
||||||
/// 个人中心接口:`/app/me*`。
|
/// 个人中心接口:`/app/me*`。
|
||||||
class AccountApi {
|
class AccountApi {
|
||||||
@@ -23,11 +24,9 @@ class AccountApi {
|
|||||||
|
|
||||||
Future<List<AccountDevice>> devices() async {
|
Future<List<AccountDevice>> devices() async {
|
||||||
final data = await _client.get('/app/me/devices');
|
final data = await _client.get('/app/me/devices');
|
||||||
if (data is! List) return const [];
|
return ResponseDecoders.asMapList(
|
||||||
return [
|
data,
|
||||||
for (final item in data)
|
).map(AccountDevice.fromJson).toList();
|
||||||
if (item is Map) AccountDevice.fromJson(item.cast<String, dynamic>()),
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<AccountExport> export() async {
|
Future<AccountExport> export() async {
|
||||||
@@ -47,6 +46,7 @@ class AccountApi {
|
|||||||
await _client.post('/app/me/deactivate/cancel');
|
await _client.post('/app/me/deactivate/cancel');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 个人资料允许缺失:后端未返回对象时按空对象解析,页面用默认值兜底。
|
||||||
Map<String, dynamic> _map(Object? data) =>
|
Map<String, dynamic> _map(Object? data) =>
|
||||||
data is Map ? data.cast<String, dynamic>() : const {};
|
ResponseDecoders.tryMap(data) ?? const {};
|
||||||
}
|
}
|
||||||
@@ -6,6 +6,7 @@ import '../models/page_result.dart';
|
|||||||
import '../models/playlist_detail.dart';
|
import '../models/playlist_detail.dart';
|
||||||
import '../models/search_result.dart';
|
import '../models/search_result.dart';
|
||||||
import 'api_client.dart';
|
import 'api_client.dart';
|
||||||
|
import 'response_decoders.dart';
|
||||||
|
|
||||||
/// 客户端目录接口,对应后端 `/app/**`。
|
/// 客户端目录接口,对应后端 `/app/**`。
|
||||||
class CatalogApi {
|
class CatalogApi {
|
||||||
@@ -14,11 +15,15 @@ class CatalogApi {
|
|||||||
final ApiClient _client;
|
final ApiClient _client;
|
||||||
|
|
||||||
Future<HomeFeed> homeFeed() async {
|
Future<HomeFeed> homeFeed() async {
|
||||||
return HomeFeed.fromJson(_asMap(await _client.get('/app/home/feed')));
|
return HomeFeed.fromJson(
|
||||||
|
ResponseDecoders.asMap(await _client.get('/app/home/feed')),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<HomeFeed> discoverFeed() async {
|
Future<HomeFeed> discoverFeed() async {
|
||||||
return HomeFeed.fromJson(_asMap(await _client.get('/app/discover/feed')));
|
return HomeFeed.fromJson(
|
||||||
|
ResponseDecoders.asMap(await _client.get('/app/discover/feed')),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 推荐流;types/categoryId 为空表示不过滤,cursor 为上一页末条。
|
/// 推荐流;types/categoryId 为空表示不过滤,cursor 为上一页末条。
|
||||||
@@ -29,7 +34,7 @@ class CatalogApi {
|
|||||||
int size = 10,
|
int size = 10,
|
||||||
}) async {
|
}) async {
|
||||||
return FeedPage.fromJson(
|
return FeedPage.fromJson(
|
||||||
_asMap(
|
ResponseDecoders.asMap(
|
||||||
await _client.get(
|
await _client.get(
|
||||||
'/app/feed',
|
'/app/feed',
|
||||||
query: {
|
query: {
|
||||||
@@ -58,7 +63,7 @@ class CatalogApi {
|
|||||||
'pageSize': '$pageSize',
|
'pageSize': '$pageSize',
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
return SearchResult.fromJson(_asMap(data));
|
return SearchResult.fromJson(ResponseDecoders.asMap(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
static const int playlistPageSize = 10;
|
static const int playlistPageSize = 10;
|
||||||
@@ -86,7 +91,7 @@ class CatalogApi {
|
|||||||
int pageSize = playlistDramaPageSize,
|
int pageSize = playlistDramaPageSize,
|
||||||
}) async {
|
}) async {
|
||||||
return PlaylistDetail.fromJson(
|
return PlaylistDetail.fromJson(
|
||||||
_asMap(
|
ResponseDecoders.asMap(
|
||||||
await _client.get(
|
await _client.get(
|
||||||
'/app/playlists/$id',
|
'/app/playlists/$id',
|
||||||
query: {
|
query: {
|
||||||
@@ -138,20 +143,13 @@ class CatalogApi {
|
|||||||
|
|
||||||
Future<Drama> dramaDetail(String dramaId) async {
|
Future<Drama> dramaDetail(String dramaId) async {
|
||||||
return Drama.fromDetailJson(
|
return Drama.fromDetailJson(
|
||||||
_asMap(await _client.get('/app/dramas/$dramaId')),
|
ResponseDecoders.asMap(await _client.get('/app/dramas/$dramaId')),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
List<ContentCategory> _categories(Object? data) {
|
List<ContentCategory> _categories(Object? data) {
|
||||||
if (data is! List) return const [];
|
return ResponseDecoders.asMapList(
|
||||||
return [
|
data,
|
||||||
for (final item in data)
|
).map(ContentCategory.fromJson).toList();
|
||||||
if (item is Map<String, dynamic>) ContentCategory.fromJson(item),
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
Map<String, dynamic> _asMap(Object? data) {
|
|
||||||
if (data is Map<String, dynamic>) return data;
|
|
||||||
throw const ApiException(-1, '返回数据格式异常');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,9 +1,11 @@
|
|||||||
// Author: fruit.com.cn
|
// Author: fruit.com.cn
|
||||||
|
|
||||||
import '../models/content_category.dart';
|
import '../models/content_category.dart';
|
||||||
|
import '../models/category_ranking.dart';
|
||||||
import '../models/drama.dart';
|
import '../models/drama.dart';
|
||||||
import '../models/page_result.dart';
|
import '../models/page_result.dart';
|
||||||
import 'api_client.dart';
|
import 'api_client.dart';
|
||||||
|
import 'response_decoders.dart';
|
||||||
|
|
||||||
/// 内容分类接口,对应契约 `GET /app/categories`。
|
/// 内容分类接口,对应契约 `GET /app/categories`。
|
||||||
class CategoryApi {
|
class CategoryApi {
|
||||||
@@ -16,11 +18,9 @@ class CategoryApi {
|
|||||||
/// 拉取已启用分类,id 全程按字符串解析。
|
/// 拉取已启用分类,id 全程按字符串解析。
|
||||||
Future<List<ContentCategory>> list() async {
|
Future<List<ContentCategory>> list() async {
|
||||||
final data = await _client.get('/app/categories');
|
final data = await _client.get('/app/categories');
|
||||||
if (data is! List) return const [];
|
return ResponseDecoders.asMapList(
|
||||||
return [
|
data,
|
||||||
for (final item in data)
|
).map(ContentCategory.fromJson).toList();
|
||||||
if (item is Map<String, dynamic>) ContentCategory.fromJson(item),
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 分类下已上线短剧,pageIndex 从 1 开始。
|
/// 分类下已上线短剧,pageIndex 从 1 开始。
|
||||||
@@ -31,11 +31,14 @@ class CategoryApi {
|
|||||||
}) async {
|
}) async {
|
||||||
final data = await _client.get(
|
final data = await _client.get(
|
||||||
'/app/categories/$categoryId/dramas',
|
'/app/categories/$categoryId/dramas',
|
||||||
query: {
|
query: {'pageIndex': '$pageIndex', 'pageSize': '$pageSize'},
|
||||||
'pageIndex': '$pageIndex',
|
|
||||||
'pageSize': '$pageSize',
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
return PageResult.parse(data, Drama.fromCardJson);
|
return PageResult.parse(data, Drama.fromCardJson);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 推荐榜快照,服务端已确定内容顺序和连续排名。
|
||||||
|
Future<CategoryRanking> ranking(String categoryId) async {
|
||||||
|
final data = await _client.get('/app/categories/$categoryId/ranking');
|
||||||
|
return CategoryRanking.fromJson(ResponseDecoders.asMap(data));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -4,6 +4,7 @@ import '../models/order.dart';
|
|||||||
import '../models/product.dart';
|
import '../models/product.dart';
|
||||||
import '../models/wallet.dart';
|
import '../models/wallet.dart';
|
||||||
import 'api_client.dart';
|
import 'api_client.dart';
|
||||||
|
import 'response_decoders.dart';
|
||||||
|
|
||||||
/// 金币与解锁接口,对应契约 §1.8。
|
/// 金币与解锁接口,对应契约 §1.8。
|
||||||
class CommerceApi {
|
class CommerceApi {
|
||||||
@@ -13,8 +14,7 @@ class CommerceApi {
|
|||||||
|
|
||||||
Future<WalletSnapshot> wallet() async {
|
Future<WalletSnapshot> wallet() async {
|
||||||
final data = await _client.get('/app/wallet');
|
final data = await _client.get('/app/wallet');
|
||||||
if (data is Map<String, dynamic>) return WalletSnapshot.fromJson(data);
|
return WalletSnapshot.fromJson(ResponseDecoders.asMap(data));
|
||||||
throw const ApiException(-1, '返回数据格式异常');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 上架商品列表;`type` 传 `COIN_PACK` 或 `VIP`。
|
/// 上架商品列表;`type` 传 `COIN_PACK` 或 `VIP`。
|
||||||
@@ -23,13 +23,9 @@ class CommerceApi {
|
|||||||
'/app/products',
|
'/app/products',
|
||||||
query: type == null ? null : {'type': type},
|
query: type == null ? null : {'type': type},
|
||||||
);
|
);
|
||||||
if (data is List) {
|
return ResponseDecoders.requiredMapList(
|
||||||
return data
|
data,
|
||||||
.whereType<Map<String, dynamic>>()
|
).map(AppProduct.fromJson).toList();
|
||||||
.map(AppProduct.fromJson)
|
|
||||||
.toList();
|
|
||||||
}
|
|
||||||
throw const ApiException(-1, '返回数据格式异常');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 创建充值订单,返回 `CREATED` 状态订单。
|
/// 创建充值订单,返回 `CREATED` 状态订单。
|
||||||
@@ -38,13 +34,11 @@ class CommerceApi {
|
|||||||
'/app/orders',
|
'/app/orders',
|
||||||
body: {'productId': productId},
|
body: {'productId': productId},
|
||||||
);
|
);
|
||||||
if (data is Map<String, dynamic>) return AppOrder.fromJson(data);
|
return AppOrder.fromJson(ResponseDecoders.asMap(data));
|
||||||
throw const ApiException(-1, '返回数据格式异常');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<UnlockResult> unlock(String episodeId) async {
|
Future<UnlockResult> unlock(String episodeId) async {
|
||||||
final data = await _client.post('/app/episodes/$episodeId/unlock');
|
final data = await _client.post('/app/episodes/$episodeId/unlock');
|
||||||
if (data is Map<String, dynamic>) return UnlockResult.fromJson(data);
|
return UnlockResult.fromJson(ResponseDecoders.asMap(data));
|
||||||
throw const ApiException(-1, '返回数据格式异常');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
// Author: fruit.com.cn
|
||||||
|
|
||||||
|
import 'api_client.dart';
|
||||||
|
|
||||||
|
/// 启动配置:`GET /app/config`(匿名,后台只下发白名单键)。
|
||||||
|
class ConfigApi {
|
||||||
|
const ConfigApi(this._client);
|
||||||
|
|
||||||
|
final ApiClient _client;
|
||||||
|
|
||||||
|
/// 白名单配置表;结构异常时返回空表,调用方按本地默认处理。
|
||||||
|
Future<Map<String, dynamic>> appConfig() async {
|
||||||
|
final data = await _client.get('/app/config');
|
||||||
|
return data is Map<String, dynamic> ? data : const <String, dynamic>{};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@
|
|||||||
import '../models/download_license.dart';
|
import '../models/download_license.dart';
|
||||||
import '../models/download_policy.dart';
|
import '../models/download_policy.dart';
|
||||||
import 'api_client.dart';
|
import 'api_client.dart';
|
||||||
|
import 'response_decoders.dart';
|
||||||
|
|
||||||
/// 离线下载授权接口,对应契约 §1.6 的全部 8 个接口。
|
/// 离线下载授权接口,对应契约 §1.6 的全部 8 个接口。
|
||||||
///
|
///
|
||||||
@@ -15,7 +16,7 @@ class DownloadApi {
|
|||||||
/// 拉取配额和偏好策略。
|
/// 拉取配额和偏好策略。
|
||||||
Future<DownloadPolicy> policy() async {
|
Future<DownloadPolicy> policy() async {
|
||||||
return DownloadPolicy.fromJson(
|
return DownloadPolicy.fromJson(
|
||||||
_asMap(await _client.get('/app/downloads/policy')),
|
ResponseDecoders.asMap(await _client.get('/app/downloads/policy')),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -30,7 +31,7 @@ class DownloadApi {
|
|||||||
/// 申请下载授权;同集重复申请由服务端保证幂等。
|
/// 申请下载授权;同集重复申请由服务端保证幂等。
|
||||||
Future<DownloadLicense> grant(String episodeId, {String? quality}) async {
|
Future<DownloadLicense> grant(String episodeId, {String? quality}) async {
|
||||||
return DownloadLicense.fromJson(
|
return DownloadLicense.fromJson(
|
||||||
_asMap(
|
ResponseDecoders.asMap(
|
||||||
await _client.post(
|
await _client.post(
|
||||||
'/app/downloads',
|
'/app/downloads',
|
||||||
body: {'episodeId': episodeId, 'quality': ?quality},
|
body: {'episodeId': episodeId, 'quality': ?quality},
|
||||||
@@ -42,17 +43,17 @@ class DownloadApi {
|
|||||||
/// 拉取当前设备的授权列表。
|
/// 拉取当前设备的授权列表。
|
||||||
Future<List<DownloadLicense>> list() async {
|
Future<List<DownloadLicense>> list() async {
|
||||||
final data = await _client.get('/app/downloads');
|
final data = await _client.get('/app/downloads');
|
||||||
if (data is! List) return const [];
|
return ResponseDecoders.asMapList(
|
||||||
return [
|
data,
|
||||||
for (final item in data)
|
).map(DownloadLicense.fromJson).toList();
|
||||||
if (item is Map<String, dynamic>) DownloadLicense.fromJson(item),
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 续传前刷新短时效下载地址。
|
/// 续传前刷新短时效下载地址。
|
||||||
Future<DownloadLicense> refreshUrl(String episodeId) async {
|
Future<DownloadLicense> refreshUrl(String episodeId) async {
|
||||||
return DownloadLicense.fromJson(
|
return DownloadLicense.fromJson(
|
||||||
_asMap(await _client.get('/app/downloads/$episodeId/url')),
|
ResponseDecoders.asMap(
|
||||||
|
await _client.get('/app/downloads/$episodeId/url'),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,7 +68,7 @@ class DownloadApi {
|
|||||||
String? errorMsg,
|
String? errorMsg,
|
||||||
}) async {
|
}) async {
|
||||||
return DownloadLicense.fromJson(
|
return DownloadLicense.fromJson(
|
||||||
_asMap(
|
ResponseDecoders.asMap(
|
||||||
await _client.post(
|
await _client.post(
|
||||||
'/app/downloads/$episodeId/progress',
|
'/app/downloads/$episodeId/progress',
|
||||||
body: {
|
body: {
|
||||||
@@ -93,9 +94,4 @@ class DownloadApi {
|
|||||||
final data = await _client.delete('/app/downloads');
|
final data = await _client.delete('/app/downloads');
|
||||||
return data is int ? data : 0;
|
return data is int ? data : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<String, dynamic> _asMap(Object? data) {
|
|
||||||
if (data is Map<String, dynamic>) return data;
|
|
||||||
throw const ApiException(-1, '');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -3,6 +3,7 @@
|
|||||||
import '../models/episode_stats.dart';
|
import '../models/episode_stats.dart';
|
||||||
import '../models/play_ticket.dart';
|
import '../models/play_ticket.dart';
|
||||||
import 'api_client.dart';
|
import 'api_client.dart';
|
||||||
|
import 'response_decoders.dart';
|
||||||
|
|
||||||
/// 播放票据接口:`GET /app/episodes/{id}/play`(契约 §1.4)。
|
/// 播放票据接口:`GET /app/episodes/{id}/play`(契约 §1.4)。
|
||||||
///
|
///
|
||||||
@@ -15,14 +16,12 @@ class PlaybackApi {
|
|||||||
|
|
||||||
Future<PlayTicket> ticket(String episodeId) async {
|
Future<PlayTicket> ticket(String episodeId) async {
|
||||||
final data = await _client.get('/app/episodes/$episodeId/play');
|
final data = await _client.get('/app/episodes/$episodeId/play');
|
||||||
if (data is Map<String, dynamic>) return PlayTicket.fromJson(data);
|
return PlayTicket.fromJson(ResponseDecoders.asMap(data));
|
||||||
throw const ApiException(-1, '返回数据格式异常');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 轻量刷新互动计数:不签发地址,也不占用播放票据缓存。
|
/// 轻量刷新互动计数:不签发地址,也不占用播放票据缓存。
|
||||||
Future<EpisodeStats> episodeStats(String episodeId) async {
|
Future<EpisodeStats> episodeStats(String episodeId) async {
|
||||||
final data = await _client.get('/app/episodes/$episodeId/stats');
|
final data = await _client.get('/app/episodes/$episodeId/stats');
|
||||||
if (data is Map<String, dynamic>) return EpisodeStats.fromJson(data);
|
return EpisodeStats.fromJson(ResponseDecoders.asMap(data));
|
||||||
throw const ApiException(-1, '返回数据格式异常');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
// Author: fruit.com.cn
|
||||||
|
|
||||||
|
import 'api_exception.dart';
|
||||||
|
|
||||||
|
/// 接口响应 `data` 字段的统一解析。
|
||||||
|
///
|
||||||
|
/// 此前每个 `*_api.dart` 各写一份 `is Map` 判断与错误文案,还出现过抛空
|
||||||
|
/// message 导致界面显示空白的情况;统一收口到这里,语义只有三种:
|
||||||
|
/// 必须为对象、可为空对象、对象数组。
|
||||||
|
abstract final class ResponseDecoders {
|
||||||
|
/// 业务失败码:HTTP 成功但 `data` 结构不符合约定。
|
||||||
|
static const int malformedCode = -1;
|
||||||
|
|
||||||
|
/// `data` 结构异常时的提示,可直接展示给用户。
|
||||||
|
static const String malformedMessage = '返回数据格式异常';
|
||||||
|
|
||||||
|
/// 要求 `data` 是对象,否则抛 [ApiException]。
|
||||||
|
static Map<String, dynamic> asMap(Object? data) {
|
||||||
|
final map = tryMap(data);
|
||||||
|
if (map == null) {
|
||||||
|
throw const ApiException(malformedCode, malformedMessage);
|
||||||
|
}
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 允许缺失:`data` 为空或不是对象时返回 null。
|
||||||
|
static Map<String, dynamic>? tryMap(Object? data) {
|
||||||
|
if (data is Map<String, dynamic>) return data;
|
||||||
|
if (data is Map) return data.cast<String, dynamic>();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 对象数组;不是数组时按空列表处理,避免列表页因结构异常白屏。
|
||||||
|
static List<Map<String, dynamic>> asMapList(Object? data) {
|
||||||
|
if (data is! List) return const [];
|
||||||
|
return data
|
||||||
|
.whereType<Map>()
|
||||||
|
.map((item) => item.cast<String, dynamic>())
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 对象数组;不是数组时抛 [ApiException],用于不能静默为空的数据(如商品)。
|
||||||
|
static List<Map<String, dynamic>> requiredMapList(Object? data) {
|
||||||
|
if (data is! List) {
|
||||||
|
throw const ApiException(malformedCode, malformedMessage);
|
||||||
|
}
|
||||||
|
return asMapList(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@
|
|||||||
import '../models/page_result.dart';
|
import '../models/page_result.dart';
|
||||||
import '../models/watch_progress.dart';
|
import '../models/watch_progress.dart';
|
||||||
import 'api_client.dart';
|
import 'api_client.dart';
|
||||||
|
import 'response_decoders.dart';
|
||||||
|
|
||||||
/// 观看进度接口:`/app/watch/*`(契约 §1.5)。支持账号或设备身份。
|
/// 观看进度接口:`/app/watch/*`(契约 §1.5)。支持账号或设备身份。
|
||||||
class WatchApi {
|
class WatchApi {
|
||||||
@@ -28,7 +29,8 @@ class WatchApi {
|
|||||||
'source': source,
|
'source': source,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
return data is Map<String, dynamic> ? WatchProgress.fromJson(data) : null;
|
final map = ResponseDecoders.tryMap(data);
|
||||||
|
return map == null ? null : WatchProgress.fromJson(map);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 某部剧的续看位置;没有记录时后端返回空 data。
|
/// 某部剧的续看位置;没有记录时后端返回空 data。
|
||||||
@@ -37,7 +39,8 @@ class WatchApi {
|
|||||||
'/app/watch/progress',
|
'/app/watch/progress',
|
||||||
query: {'dramaId': dramaId},
|
query: {'dramaId': dramaId},
|
||||||
);
|
);
|
||||||
return data is Map<String, dynamic> ? WatchProgress.fromJson(data) : null;
|
final map = ResponseDecoders.tryMap(data);
|
||||||
|
return map == null ? null : WatchProgress.fromJson(map);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 最近观看分页:`pageIndex`/`pageSize`,解包 `PageResult`。
|
/// 最近观看分页:`pageIndex`/`pageSize`,解包 `PageResult`。
|
||||||
@@ -49,13 +52,9 @@ class WatchApi {
|
|||||||
'/app/watch/recent',
|
'/app/watch/recent',
|
||||||
query: {'pageIndex': '$pageIndex', 'pageSize': '$pageSize'},
|
query: {'pageIndex': '$pageIndex', 'pageSize': '$pageSize'},
|
||||||
);
|
);
|
||||||
if (data is! Map) {
|
final map = ResponseDecoders.tryMap(data);
|
||||||
return const PageResult();
|
if (map == null) return const PageResult();
|
||||||
}
|
return PageResult.fromJson(map, WatchProgress.fromJson);
|
||||||
return PageResult.fromJson(
|
|
||||||
Map<String, dynamic>.from(data),
|
|
||||||
WatchProgress.fromJson,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 删除某部剧的观看记录(记录页清单条用)。
|
/// 删除某部剧的观看记录(记录页清单条用)。
|
||||||
|
|||||||
@@ -3,7 +3,8 @@
|
|||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
|
|
||||||
/// Native Android activity PiP; the Flutter playback session keeps the player.
|
/// 原生活动级画中画桥:Android 是 Activity PiP(画面即 Flutter 渲染),
|
||||||
|
/// iOS 是影子播放器接力(画面/声音来自原生 AVPlayer,退出时进度回传)。
|
||||||
class AndroidPictureInPicture {
|
class AndroidPictureInPicture {
|
||||||
AndroidPictureInPicture({MethodChannel? channel})
|
AndroidPictureInPicture({MethodChannel? channel})
|
||||||
: _channel = channel ?? const MethodChannel('com.stormg.lumadrama/pip');
|
: _channel = channel ?? const MethodChannel('com.stormg.lumadrama/pip');
|
||||||
@@ -11,22 +12,147 @@ class AndroidPictureInPicture {
|
|||||||
final MethodChannel _channel;
|
final MethodChannel _channel;
|
||||||
|
|
||||||
bool get isPlatformSupported =>
|
bool get isPlatformSupported =>
|
||||||
!kIsWeb && defaultTargetPlatform == TargetPlatform.android;
|
!kIsWeb &&
|
||||||
|
(defaultTargetPlatform == TargetPlatform.android ||
|
||||||
|
defaultTargetPlatform == TargetPlatform.iOS);
|
||||||
|
|
||||||
void setStateListener(ValueChanged<bool>? listener) {
|
void Function(bool active, int finalPositionMs)? _pipChangedListener;
|
||||||
_channel.setMethodCallHandler(
|
VoidCallback? _playbackEndedListener;
|
||||||
listener == null
|
|
||||||
? null
|
/// PiP 状态回调:iOS 停止时 [finalPositionMs] 为影子最终进度(毫秒)。
|
||||||
: (call) async {
|
void setStateListener(void Function(bool active, int finalPositionMs)? listener) {
|
||||||
if (call.method == 'onPipChanged') {
|
_pipChangedListener = listener;
|
||||||
listener(call.arguments == true);
|
_refreshHandler();
|
||||||
}
|
}
|
||||||
},
|
|
||||||
|
/// iOS 画中画内影子播完当前集(Android 不会触发)。
|
||||||
|
void setPlaybackEndedListener(VoidCallback? listener) {
|
||||||
|
_playbackEndedListener = listener;
|
||||||
|
_refreshHandler();
|
||||||
|
}
|
||||||
|
|
||||||
|
void _refreshHandler() {
|
||||||
|
if (_pipChangedListener == null && _playbackEndedListener == null) {
|
||||||
|
_channel.setMethodCallHandler(null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_channel.setMethodCallHandler((call) async {
|
||||||
|
switch (call.method) {
|
||||||
|
case 'pipDebug':
|
||||||
|
debugPrint('pip[native]: ${call.arguments}');
|
||||||
|
case 'onPipChanged':
|
||||||
|
final listener = _pipChangedListener;
|
||||||
|
if (listener == null) return;
|
||||||
|
final args = call.arguments;
|
||||||
|
if (args is Map) {
|
||||||
|
listener(
|
||||||
|
args['active'] == true,
|
||||||
|
(args['positionMs'] as num?)?.toInt() ?? 0,
|
||||||
);
|
);
|
||||||
|
} else {
|
||||||
|
// Android 只回传 bool。
|
||||||
|
listener(args == true, 0);
|
||||||
|
}
|
||||||
|
case 'onPipPlaybackEnded':
|
||||||
|
_playbackEndedListener?.call();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<bool> enter() async {
|
/// 进入画中画。Android 忽略片源参数;iOS 用 [url] 与 [positionMs]
|
||||||
|
/// 初始化影子播放器(对齐到当前进度、静音待命)。
|
||||||
|
Future<bool> enter({required String url, required int positionMs}) async {
|
||||||
if (!isPlatformSupported) return false;
|
if (!isPlatformSupported) return false;
|
||||||
return await _channel.invokeMethod<bool>('enter') ?? false;
|
return await _channel.invokeMethod<bool>('enter', {
|
||||||
|
'url': url,
|
||||||
|
'positionMs': positionMs,
|
||||||
|
}) ??
|
||||||
|
false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 创建影子播放器但不起播(仅 iOS):
|
||||||
|
/// 自动小窗待命形态——静音暂停,退后台由 [armAutoEnter] 拉起。
|
||||||
|
Future<void> prepareShadow({
|
||||||
|
required String url,
|
||||||
|
required int positionMs,
|
||||||
|
bool autoEnter = false,
|
||||||
|
}) async {
|
||||||
|
if (defaultTargetPlatform != TargetPlatform.iOS || kIsWeb) return;
|
||||||
|
try {
|
||||||
|
await _channel.invokeMethod<void>('prepareShadow', {
|
||||||
|
'url': url,
|
||||||
|
'positionMs': positionMs,
|
||||||
|
'autoEnter': autoEnter,
|
||||||
|
});
|
||||||
|
} catch (_) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 退后台瞬间把影子对齐到当前进度并静音起播,触发系统自动画中画(仅 iOS)。
|
||||||
|
Future<void> armAutoEnter(int positionMs) async {
|
||||||
|
if (defaultTargetPlatform != TargetPlatform.iOS || kIsWeb) return;
|
||||||
|
try {
|
||||||
|
await _channel.invokeMethod<void>('armAutoEnter', {
|
||||||
|
'positionMs': positionMs,
|
||||||
|
});
|
||||||
|
} catch (_) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 回前台后影子回待命(暂停、静音)(仅 iOS)。
|
||||||
|
Future<void> disarmAutoEnter() async {
|
||||||
|
if (defaultTargetPlatform != TargetPlatform.iOS || kIsWeb) return;
|
||||||
|
try {
|
||||||
|
await _channel.invokeMethod<void>('disarmAutoEnter');
|
||||||
|
} catch (_) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 显示原生还原过渡层(仅 iOS;还原动画开始时原生侧也会自动盖上)。
|
||||||
|
Future<void> showRestoreOverlay() async {
|
||||||
|
if (defaultTargetPlatform != TargetPlatform.iOS || kIsWeb) return;
|
||||||
|
try {
|
||||||
|
await _channel.invokeMethod<void>('showRestoreOverlay');
|
||||||
|
} catch (_) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 淡出原生还原过渡层(仅 iOS;视频首帧确认后调用)。
|
||||||
|
Future<void> hideRestoreOverlay() async {
|
||||||
|
if (defaultTargetPlatform != TargetPlatform.iOS || kIsWeb) return;
|
||||||
|
try {
|
||||||
|
await _channel.invokeMethod<void>('hideRestoreOverlay');
|
||||||
|
} catch (_) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 解除影子静音并起播(仅 iOS 有意义,进入 PiP 后声音交给影子)。
|
||||||
|
Future<void> unmute() async {
|
||||||
|
if (defaultTargetPlatform != TargetPlatform.iOS || kIsWeb) return;
|
||||||
|
try {
|
||||||
|
await _channel.invokeMethod<void>('unmute');
|
||||||
|
} catch (_) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// iOS 画中画内换集:影子只换播放项,窗口不中断。
|
||||||
|
Future<void> replaceItem({required String url, required int positionMs}) async {
|
||||||
|
if (defaultTargetPlatform != TargetPlatform.iOS || kIsWeb) return;
|
||||||
|
try {
|
||||||
|
await _channel.invokeMethod<void>('replaceItem', {
|
||||||
|
'url': url,
|
||||||
|
'positionMs': positionMs,
|
||||||
|
});
|
||||||
|
} catch (_) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 请求退出画中画(iOS;触发 onPipChanged(false) 后由 Dart 收尾)。
|
||||||
|
Future<void> stop() async {
|
||||||
|
if (defaultTargetPlatform != TargetPlatform.iOS || kIsWeb) return;
|
||||||
|
try {
|
||||||
|
await _channel.invokeMethod<void>('stop');
|
||||||
|
} catch (_) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 销毁影子播放器(仅 iOS)。
|
||||||
|
Future<void> teardown() async {
|
||||||
|
if (defaultTargetPlatform != TargetPlatform.iOS || kIsWeb) return;
|
||||||
|
try {
|
||||||
|
await _channel.invokeMethod<void>('teardown');
|
||||||
|
} catch (_) {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
// Author: fruit.com.cn
|
||||||
|
|
||||||
|
import 'package:flutter/foundation.dart';
|
||||||
|
|
||||||
|
import '../remote/config_api.dart';
|
||||||
|
|
||||||
|
/// 后台下发的启动配置。
|
||||||
|
///
|
||||||
|
/// 失败一律静默:拿不到就用 App 内置默认,不阻塞启动、不弹错。
|
||||||
|
class AppConfigController extends ChangeNotifier {
|
||||||
|
AppConfigController(this._api);
|
||||||
|
|
||||||
|
final ConfigApi _api;
|
||||||
|
|
||||||
|
String _loginBackgroundUrl = '';
|
||||||
|
|
||||||
|
/// 登录页整屏背景图;空串表示用本地默认图。
|
||||||
|
String get loginBackgroundUrl => _loginBackgroundUrl;
|
||||||
|
|
||||||
|
Future<void> load() async {
|
||||||
|
try {
|
||||||
|
final config = await _api.appConfig();
|
||||||
|
final url = (config['loginBackgroundUrl'] as String? ?? '').trim();
|
||||||
|
if (url == _loginBackgroundUrl) return;
|
||||||
|
_loginBackgroundUrl = url;
|
||||||
|
notifyListeners();
|
||||||
|
} catch (error) {
|
||||||
|
debugPrint('[config] load failed: $error');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
// Author: fruit.com.cn
|
|
||||||
|
|
||||||
import '../../config/app_config.dart';
|
|
||||||
|
|
||||||
/// 重新探测服务地址后再加载指定目录数据。
|
|
||||||
Future<void> retryCatalogLoad({
|
|
||||||
required Future<void> Function() reload,
|
|
||||||
Future<void> Function()? probe,
|
|
||||||
}) async {
|
|
||||||
await (probe ?? AppConfig.probeAndApplyBaseUrl)();
|
|
||||||
await reload();
|
|
||||||
}
|
|
||||||
@@ -4,6 +4,7 @@ import 'package:flutter/foundation.dart';
|
|||||||
|
|
||||||
import '../../config/app_config.dart';
|
import '../../config/app_config.dart';
|
||||||
import '../models/content_category.dart';
|
import '../models/content_category.dart';
|
||||||
|
import '../models/category_ranking.dart';
|
||||||
import '../models/drama.dart';
|
import '../models/drama.dart';
|
||||||
import '../models/home_feed.dart';
|
import '../models/home_feed.dart';
|
||||||
import '../models/page_result.dart';
|
import '../models/page_result.dart';
|
||||||
@@ -21,10 +22,15 @@ class CategoryController extends ChangeNotifier {
|
|||||||
String categoryId, {
|
String categoryId, {
|
||||||
int pageIndex,
|
int pageIndex,
|
||||||
int pageSize,
|
int pageSize,
|
||||||
})? dramaPageLoader;
|
})?
|
||||||
|
dramaPageLoader;
|
||||||
|
|
||||||
|
/// 生产环境推荐榜加载器,测试与本地数据模式可不注入。
|
||||||
|
Future<CategoryRanking> Function(String categoryId)? rankingLoader;
|
||||||
|
|
||||||
List<ContentCategory> categories = const [];
|
List<ContentCategory> categories = const [];
|
||||||
final Map<String, CategoryDramaSlot> _slots = {};
|
final Map<String, CategoryDramaSlot> _slots = {};
|
||||||
|
final Map<String, CategoryRankingSlot> _rankingSlots = {};
|
||||||
HomeFeed feed = const HomeFeed();
|
HomeFeed feed = const HomeFeed();
|
||||||
bool loading = false;
|
bool loading = false;
|
||||||
String? error;
|
String? error;
|
||||||
@@ -36,6 +42,29 @@ class CategoryController extends ChangeNotifier {
|
|||||||
return _slots.putIfAbsent(categoryId, CategoryDramaSlot.new);
|
return _slots.putIfAbsent(categoryId, CategoryDramaSlot.new);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CategoryRankingSlot rankingSlotOf(String categoryId) {
|
||||||
|
return _rankingSlots.putIfAbsent(categoryId, CategoryRankingSlot.new);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 打开推荐榜并获取当前服务端快照。
|
||||||
|
Future<void> openRanking(String categoryId) async {
|
||||||
|
final loader = rankingLoader;
|
||||||
|
if (loader == null) return;
|
||||||
|
final slot = rankingSlotOf(categoryId);
|
||||||
|
slot.loading = true;
|
||||||
|
slot.error = null;
|
||||||
|
_notify();
|
||||||
|
try {
|
||||||
|
slot.ranking = await loader(categoryId);
|
||||||
|
} catch (exception) {
|
||||||
|
slot.error = exception;
|
||||||
|
if (kDebugMode) debugPrint('[category-ranking] $exception');
|
||||||
|
} finally {
|
||||||
|
slot.loading = false;
|
||||||
|
_notify();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// 打开分类并加载第一页短剧。
|
/// 打开分类并加载第一页短剧。
|
||||||
Future<void> openCategory(String categoryId) async {
|
Future<void> openCategory(String categoryId) async {
|
||||||
final loader = dramaPageLoader;
|
final loader = dramaPageLoader;
|
||||||
@@ -126,8 +155,14 @@ class CategoryController extends ChangeNotifier {
|
|||||||
loading = false;
|
loading = false;
|
||||||
_notify();
|
_notify();
|
||||||
}
|
}
|
||||||
if (error == null && dramaPageLoader != null && categories.isNotEmpty) {
|
final hasRemoteLoader = dramaPageLoader != null || rankingLoader != null;
|
||||||
await openCategory(categories.first.id);
|
if (error == null && hasRemoteLoader && categories.isNotEmpty) {
|
||||||
|
final first = categories.first;
|
||||||
|
if (first.isRanking) {
|
||||||
|
await openRanking(first.id);
|
||||||
|
} else {
|
||||||
|
await openCategory(first.id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -152,3 +187,10 @@ class CategoryDramaSlot {
|
|||||||
|
|
||||||
bool get hasMore => dramas.length < total;
|
bool get hasMore => dramas.length < total;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 单个推荐榜的快照状态。
|
||||||
|
class CategoryRankingSlot {
|
||||||
|
CategoryRanking? ranking;
|
||||||
|
Object? error;
|
||||||
|
bool loading = false;
|
||||||
|
}
|
||||||
@@ -0,0 +1,233 @@
|
|||||||
|
// Author: fruit.com.cn
|
||||||
|
|
||||||
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:flutter/foundation.dart';
|
||||||
|
|
||||||
|
import '../../data/models/drama.dart';
|
||||||
|
import '../../data/models/feed_page.dart';
|
||||||
|
|
||||||
|
/// 单个分桶(频道/分类)的游标分页状态。
|
||||||
|
class ChannelFeedState {
|
||||||
|
List<Drama> dramas = const [];
|
||||||
|
String? cursor;
|
||||||
|
bool hasMore = true;
|
||||||
|
bool loaded = false;
|
||||||
|
bool loading = false;
|
||||||
|
bool loadingMore = false;
|
||||||
|
String? lastError;
|
||||||
|
int requestSeq = 0;
|
||||||
|
Future<bool>? inFlight;
|
||||||
|
bool skipNextRefresh = false;
|
||||||
|
|
||||||
|
/// 正在持有 [loading] 的那次刷新请求的序号;被抢占时据此只由原请求复位。
|
||||||
|
int? loadingOwner;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 按 key 分桶的游标分页控制器基类。
|
||||||
|
///
|
||||||
|
/// 首页频道与剧场分类的逻辑完全同构:缓存优先渲染、请求序号防串台、
|
||||||
|
/// 分页去重追加。两者此前各写一份,改动容易只改一边,故收口到这里,
|
||||||
|
/// 子类只需提供分桶 key 与「如何发一次请求」。
|
||||||
|
abstract class ChannelFeedController<K> extends ChangeNotifier {
|
||||||
|
ChannelFeedController(
|
||||||
|
K initialKey, {
|
||||||
|
bool initiallySelected = true,
|
||||||
|
this.requestTimeout = defaultRequestTimeout,
|
||||||
|
}) : _current = initialKey,
|
||||||
|
_selected = initiallySelected;
|
||||||
|
|
||||||
|
static const int pageSize = 20;
|
||||||
|
static const Duration defaultRequestTimeout = Duration(seconds: 10);
|
||||||
|
|
||||||
|
final Duration requestTimeout;
|
||||||
|
final Map<K, ChannelFeedState> _states = {};
|
||||||
|
K _current;
|
||||||
|
bool _selected;
|
||||||
|
bool _disposed = false;
|
||||||
|
|
||||||
|
K get currentKey => _current;
|
||||||
|
String? get lastError => _state(_current).lastError;
|
||||||
|
List<Drama> get dramas => _state(_current).dramas;
|
||||||
|
String? get cursor => _state(_current).cursor;
|
||||||
|
bool get hasMore => _state(_current).hasMore;
|
||||||
|
bool get loaded => _state(_current).loaded;
|
||||||
|
bool get isLoading => _state(_current).loading;
|
||||||
|
bool get loadingMore => _state(_current).loadingMore;
|
||||||
|
bool get loadingFirstPage => !loaded && _state(_current).loading;
|
||||||
|
|
||||||
|
/// 子类实现:按 key 拉一页;[cursor] 为空表示首屏/刷新。
|
||||||
|
@protected
|
||||||
|
Future<FeedPage> fetchPage(K key, {String? cursor});
|
||||||
|
|
||||||
|
/// 同步切换分桶;有缓存先渲染,无缓存时复用或等待在飞请求。
|
||||||
|
@protected
|
||||||
|
Future<void> selectKey(K next) async {
|
||||||
|
if (_selected && next == _current) return;
|
||||||
|
_current = next;
|
||||||
|
_selected = true;
|
||||||
|
_notify();
|
||||||
|
final state = _state(next);
|
||||||
|
if (state.dramas.isNotEmpty) {
|
||||||
|
if (state.skipNextRefresh) {
|
||||||
|
state.skipNextRefresh = false;
|
||||||
|
} else if (!state.loading) {
|
||||||
|
unawaited(_refresh(next, keepData: true));
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (state.loading) {
|
||||||
|
final inFlight = state.inFlight;
|
||||||
|
if (inFlight != null) await inFlight;
|
||||||
|
state.skipNextRefresh = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await _refresh(next, keepData: false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 用已有数据填充某个分桶,不发起网络请求。
|
||||||
|
@protected
|
||||||
|
void seedKey(K key, List<Drama> dramas) {
|
||||||
|
final state = _state(key);
|
||||||
|
state
|
||||||
|
..requestSeq = state.requestSeq + 1
|
||||||
|
..dramas = List.unmodifiable(dramas)
|
||||||
|
..cursor = null
|
||||||
|
..hasMore = false
|
||||||
|
..loaded = true
|
||||||
|
..loading = false
|
||||||
|
..loadingOwner = null
|
||||||
|
..loadingMore = false
|
||||||
|
..lastError = null
|
||||||
|
..inFlight = null;
|
||||||
|
_notifyIfCurrent(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 后台预取某个分桶首屏;成功后标记「切过去不必再刷一次」。
|
||||||
|
@protected
|
||||||
|
Future<void> prefetchKey(K key) async {
|
||||||
|
final state = _state(key);
|
||||||
|
if (state.loaded || state.loading) return;
|
||||||
|
final success = await _refresh(key, keepData: false);
|
||||||
|
if (success && !_disposed) state.skipNextRefresh = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 刷新当前分桶,失败时保留旧数据。
|
||||||
|
Future<bool> refreshCurrent() async {
|
||||||
|
final state = _state(_current);
|
||||||
|
if (state.loading) {
|
||||||
|
return await state.inFlight ?? false;
|
||||||
|
}
|
||||||
|
return _refresh(_current, keepData: true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 加载当前分桶下一页并去重追加。
|
||||||
|
Future<void> loadMore() async {
|
||||||
|
final key = _current;
|
||||||
|
final state = _state(key);
|
||||||
|
if (!state.hasMore || state.loadingMore || state.cursor == null) return;
|
||||||
|
final seq = ++state.requestSeq;
|
||||||
|
state.loadingMore = true;
|
||||||
|
_notifyIfCurrent(key);
|
||||||
|
try {
|
||||||
|
final page = await fetchPage(
|
||||||
|
key,
|
||||||
|
cursor: state.cursor,
|
||||||
|
).timeout(requestTimeout);
|
||||||
|
if (!_acceptSequence(key, seq)) return;
|
||||||
|
final seen = state.dramas.map((drama) => drama.id).toSet();
|
||||||
|
final appended = page.dramas
|
||||||
|
.where((drama) => seen.add(drama.id))
|
||||||
|
.toList();
|
||||||
|
state
|
||||||
|
..dramas = List.unmodifiable([...state.dramas, ...appended])
|
||||||
|
..cursor = page.nextCursor
|
||||||
|
..hasMore = page.hasMore
|
||||||
|
..loaded = true
|
||||||
|
..lastError = null;
|
||||||
|
} catch (error) {
|
||||||
|
if (_acceptSequence(key, seq)) {
|
||||||
|
state.lastError = error.toString();
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
state.loadingMore = false;
|
||||||
|
_notifyIfCurrent(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<bool> _refresh(K key, {required bool keepData}) {
|
||||||
|
final state = _state(key);
|
||||||
|
final operation = _performRefresh(key, keepData: keepData);
|
||||||
|
state.inFlight = operation;
|
||||||
|
unawaited(_clearInFlight(state, operation));
|
||||||
|
return operation;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<bool> _performRefresh(K key, {required bool keepData}) async {
|
||||||
|
final state = _state(key);
|
||||||
|
final seq = ++state.requestSeq;
|
||||||
|
state
|
||||||
|
..loading = true
|
||||||
|
..loadingOwner = seq
|
||||||
|
..lastError = null;
|
||||||
|
_notifyIfCurrent(key);
|
||||||
|
try {
|
||||||
|
final page = await fetchPage(key).timeout(requestTimeout);
|
||||||
|
if (!_acceptSequence(key, seq)) return false;
|
||||||
|
state
|
||||||
|
..dramas = List.unmodifiable(page.dramas)
|
||||||
|
..cursor = page.nextCursor
|
||||||
|
..hasMore = page.hasMore
|
||||||
|
..loaded = true
|
||||||
|
..lastError = null;
|
||||||
|
_notifyIfCurrent(key);
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
if (!_acceptSequence(key, seq)) return false;
|
||||||
|
if (!keepData) state.dramas = const [];
|
||||||
|
state
|
||||||
|
..loaded = true
|
||||||
|
..lastError = error.toString();
|
||||||
|
_notifyIfCurrent(key);
|
||||||
|
return false;
|
||||||
|
} finally {
|
||||||
|
// 只由「真正拥有 loading 的那次请求」复位:被 loadMore 抢占时它不再
|
||||||
|
// 更新数据,但必须收掉自己打开的加载态,否则该分桶会一直显示加载中,
|
||||||
|
// 之后 refreshCurrent 也只会读到已清空的 inFlight 而永远返回 false。
|
||||||
|
if (!_disposed && state.loadingOwner == seq) {
|
||||||
|
state
|
||||||
|
..loading = false
|
||||||
|
..loadingOwner = null;
|
||||||
|
_notifyIfCurrent(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _clearInFlight(
|
||||||
|
ChannelFeedState state,
|
||||||
|
Future<bool> operation,
|
||||||
|
) async {
|
||||||
|
await operation;
|
||||||
|
if (identical(state.inFlight, operation)) state.inFlight = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
ChannelFeedState _state(K key) =>
|
||||||
|
_states.putIfAbsent(key, ChannelFeedState.new);
|
||||||
|
|
||||||
|
bool _acceptSequence(K key, int seq) =>
|
||||||
|
!_disposed && _state(key).requestSeq == seq;
|
||||||
|
|
||||||
|
void _notifyIfCurrent(K key) {
|
||||||
|
if (!_disposed && key == _current) _notify();
|
||||||
|
}
|
||||||
|
|
||||||
|
void _notify() {
|
||||||
|
if (!_disposed) notifyListeners();
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void dispose() {
|
||||||
|
_disposed = true;
|
||||||
|
super.dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -38,6 +38,9 @@ class DeviceIdProvider {
|
|||||||
/// Android 依次取网络运营商 / SIM 卡 / 系统语言的国家,iOS 取系统 region。
|
/// Android 依次取网络运营商 / SIM 卡 / 系统语言的国家,iOS 取系统 region。
|
||||||
Future<String?> deviceRegion() async {
|
Future<String?> deviceRegion() async {
|
||||||
if (kIsWeb) return null;
|
if (kIsWeb) return null;
|
||||||
|
// 只有两端原生侧实现了 region;其它平台(测试、桌面)上通道没有 handler,
|
||||||
|
// 调用会一直挂着,注册流程会卡在"转圈"。
|
||||||
|
if (!Platform.isAndroid && !Platform.isIOS) return null;
|
||||||
try {
|
try {
|
||||||
final region = await _channel.invokeMethod<String>('region');
|
final region = await _channel.invokeMethod<String>('region');
|
||||||
final value = region?.trim().toUpperCase() ?? '';
|
final value = region?.trim().toUpperCase() ?? '';
|
||||||
|
|||||||
@@ -120,7 +120,9 @@ class DownloadController extends ChangeNotifier {
|
|||||||
String episodeId, {
|
String episodeId, {
|
||||||
DownloadLicense? license,
|
DownloadLicense? license,
|
||||||
}) async {
|
}) async {
|
||||||
if (_downloading.contains(episodeId)) return const DownloadActionResult(ok: true);
|
if (_downloading.contains(episodeId)) {
|
||||||
|
return const DownloadActionResult(ok: true);
|
||||||
|
}
|
||||||
final source = license ?? _licenseFor(episodeId);
|
final source = license ?? _licenseFor(episodeId);
|
||||||
if (source == null) return const DownloadActionResult(ok: false);
|
if (source == null) return const DownloadActionResult(ok: false);
|
||||||
_beginDownload(episodeId);
|
_beginDownload(episodeId);
|
||||||
@@ -129,7 +131,8 @@ class DownloadController extends ChangeNotifier {
|
|||||||
final media = await _storage.download(
|
final media = await _storage.download(
|
||||||
episodeId: episodeId,
|
episodeId: episodeId,
|
||||||
url: resolved,
|
url: resolved,
|
||||||
onProgress: (received, total) => _onProgress(episodeId, received, total),
|
onProgress: (received, total) =>
|
||||||
|
_onProgress(episodeId, received, total),
|
||||||
);
|
);
|
||||||
_localPaths[episodeId] = media.filePath;
|
_localPaths[episodeId] = media.filePath;
|
||||||
_progress[episodeId] = 1;
|
_progress[episodeId] = 1;
|
||||||
|
|||||||
@@ -1,17 +1,18 @@
|
|||||||
// Author: fruit.com.cn
|
// Author: fruit.com.cn
|
||||||
|
|
||||||
import 'package:flutter/foundation.dart';
|
|
||||||
|
|
||||||
import '../models/drama.dart';
|
import '../models/drama.dart';
|
||||||
|
import '../models/page_result.dart';
|
||||||
import '../remote/api_client.dart';
|
import '../remote/api_client.dart';
|
||||||
import '../repositories/favorite_repository.dart';
|
import '../repositories/favorite_repository.dart';
|
||||||
|
import 'paged_list_controller.dart';
|
||||||
|
|
||||||
/// 收藏状态:**以服务端为准**。
|
/// 收藏状态:**以服务端为准**。
|
||||||
///
|
///
|
||||||
/// 点击收藏先乐观更新(立刻变色),失败再回滚并给出提示 —— 网络抖动时不至于
|
/// 点击收藏先乐观更新(立刻变色),失败再回滚并给出提示 —— 网络抖动时不至于
|
||||||
/// "点了没反应"或"看起来收藏了其实没有"。
|
/// "点了没反应"或"看起来收藏了其实没有"。
|
||||||
class FavoriteController extends ChangeNotifier {
|
class FavoriteController extends PagedListController<Drama> {
|
||||||
FavoriteController(this._repository, {this.pageSize = defaultPageSize});
|
FavoriteController(this._repository, {this.pageSize = defaultPageSize})
|
||||||
|
: super(fallbackErrorKey: 'favoritesError');
|
||||||
|
|
||||||
static const int defaultPageSize = 20;
|
static const int defaultPageSize = 20;
|
||||||
|
|
||||||
@@ -19,17 +20,9 @@ class FavoriteController extends ChangeNotifier {
|
|||||||
final int pageSize;
|
final int pageSize;
|
||||||
final Set<String> _saved = {};
|
final Set<String> _saved = {};
|
||||||
final Map<String, int> _favoriteCounts = {};
|
final Map<String, int> _favoriteCounts = {};
|
||||||
List<Drama> cards = const [];
|
|
||||||
int total = 0;
|
/// 收藏列表卡片(按服务端顺序)。
|
||||||
bool loading = false;
|
List<Drama> get cards => items;
|
||||||
bool loadingMore = false;
|
|
||||||
bool hasMore = false;
|
|
||||||
String? error;
|
|
||||||
String? loadMoreError;
|
|
||||||
int? lastErrorCode;
|
|
||||||
int _pageIndex = 0;
|
|
||||||
int _requestSeq = 0;
|
|
||||||
bool _disposed = false;
|
|
||||||
|
|
||||||
Set<String> get savedIds => Set.unmodifiable(_saved);
|
Set<String> get savedIds => Set.unmodifiable(_saved);
|
||||||
|
|
||||||
@@ -38,6 +31,33 @@ class FavoriteController extends ChangeNotifier {
|
|||||||
/// 当前剧收藏数;包含本地 toggle 增量。
|
/// 当前剧收藏数;包含本地 toggle 增量。
|
||||||
int? favoriteCount(String dramaId) => _favoriteCounts[dramaId];
|
int? favoriteCount(String dramaId) => _favoriteCounts[dramaId];
|
||||||
|
|
||||||
|
/// 页面统一把收藏失败翻译成固定 key。
|
||||||
|
@override
|
||||||
|
String? errorKeyFor(Object failure) => 'favoritesError';
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<PageResult<Drama>> fetchPage(int pageIndex) {
|
||||||
|
return _repository.page(pageIndex: pageIndex, pageSize: pageSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
String itemKey(Drama item) => item.id;
|
||||||
|
|
||||||
|
/// 第一页到位后,用服务端列表校准收藏集合与计数。
|
||||||
|
@override
|
||||||
|
void onItemsReplaced() {
|
||||||
|
_saved
|
||||||
|
..clear()
|
||||||
|
..addAll(items.map((drama) => drama.id));
|
||||||
|
_rememberCardCounts(items);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> onPageLoaded(List<Drama> added) async {
|
||||||
|
_saved.addAll(added.map((drama) => drama.id));
|
||||||
|
_rememberCardCounts(added);
|
||||||
|
}
|
||||||
|
|
||||||
/// 首次展示时缓存卡片基数;之后统一由本控制器维护,避免页面各存一份。
|
/// 首次展示时缓存卡片基数;之后统一由本控制器维护,避免页面各存一份。
|
||||||
int? favoriteCountFor(Drama drama) {
|
int? favoriteCountFor(Drama drama) {
|
||||||
final current = _favoriteCounts[drama.id];
|
final current = _favoriteCounts[drama.id];
|
||||||
@@ -82,81 +102,6 @@ class FavoriteController extends ChangeNotifier {
|
|||||||
if (notify) this.notify();
|
if (notify) this.notify();
|
||||||
}
|
}
|
||||||
|
|
||||||
void notify() {
|
|
||||||
if (!_disposed) notifyListeners();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// 拉收藏列表:列表既用于"我的"页展示,也用于校准 [savedIds]。
|
|
||||||
Future<void> load() async {
|
|
||||||
final request = ++_requestSeq;
|
|
||||||
loading = true;
|
|
||||||
loadingMore = false;
|
|
||||||
error = null;
|
|
||||||
loadMoreError = null;
|
|
||||||
lastErrorCode = null;
|
|
||||||
notify();
|
|
||||||
try {
|
|
||||||
final page = await _repository.page(pageIndex: 1, pageSize: pageSize);
|
|
||||||
if (_disposed || request != _requestSeq) return;
|
|
||||||
final list = _uniqueCards(page.list);
|
|
||||||
cards = list;
|
|
||||||
_saved
|
|
||||||
..clear()
|
|
||||||
..addAll(list.map((drama) => drama.id));
|
|
||||||
_rememberCardCounts(list);
|
|
||||||
total = page.total;
|
|
||||||
_pageIndex = 1;
|
|
||||||
hasMore = page.list.isNotEmpty && cards.length < total;
|
|
||||||
} catch (failure) {
|
|
||||||
if (!_disposed && request == _requestSeq) {
|
|
||||||
error = 'favoritesError';
|
|
||||||
if (failure is ApiException) lastErrorCode = failure.code;
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
if (!_disposed && request == _requestSeq) {
|
|
||||||
loading = false;
|
|
||||||
notify();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// 追加下一页;正在刷新、已在加载或无更多时直接忽略。
|
|
||||||
Future<void> loadMore() async {
|
|
||||||
if (loading || loadingMore || !hasMore) return;
|
|
||||||
final request = ++_requestSeq;
|
|
||||||
loadingMore = true;
|
|
||||||
loadMoreError = null;
|
|
||||||
lastErrorCode = null;
|
|
||||||
notify();
|
|
||||||
try {
|
|
||||||
final page = await _repository.page(
|
|
||||||
pageIndex: _pageIndex + 1,
|
|
||||||
pageSize: pageSize,
|
|
||||||
);
|
|
||||||
if (_disposed || request != _requestSeq) return;
|
|
||||||
final known = cards.map((drama) => drama.id).toSet();
|
|
||||||
final appended = _uniqueCards(
|
|
||||||
page.list,
|
|
||||||
).where((drama) => known.add(drama.id)).toList(growable: false);
|
|
||||||
cards = [...cards, ...appended];
|
|
||||||
_saved.addAll(appended.map((drama) => drama.id));
|
|
||||||
_rememberCardCounts(appended);
|
|
||||||
total = page.total;
|
|
||||||
_pageIndex += 1;
|
|
||||||
hasMore = page.list.isNotEmpty && cards.length < total;
|
|
||||||
} catch (failure) {
|
|
||||||
if (!_disposed && request == _requestSeq) {
|
|
||||||
loadMoreError = 'favoritesError';
|
|
||||||
if (failure is ApiException) lastErrorCode = failure.code;
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
if (!_disposed && request == _requestSeq) {
|
|
||||||
loadingMore = false;
|
|
||||||
notify();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// 每次切换都以接口状态为准;匿名请求由设备号归属。
|
/// 每次切换都以接口状态为准;匿名请求由设备号归属。
|
||||||
///
|
///
|
||||||
/// [card] 是当前剧的卡片(播放页/详情页手上就有):收藏成功时直接放进列表,
|
/// [card] 是当前剧的卡片(播放页/详情页手上就有):收藏成功时直接放进列表,
|
||||||
@@ -169,15 +114,13 @@ class FavoriteController extends ChangeNotifier {
|
|||||||
: favoriteCountFor(card);
|
: favoriteCountFor(card);
|
||||||
final previousCards = cards;
|
final previousCards = cards;
|
||||||
final previousTotal = total;
|
final previousTotal = total;
|
||||||
final previousHasMore = hasMore;
|
|
||||||
// 乐观更新
|
// 乐观更新
|
||||||
if (wasSaved) {
|
if (wasSaved) {
|
||||||
_saved.remove(dramaId);
|
_saved.remove(dramaId);
|
||||||
_stepCount(dramaId, -1);
|
_stepCount(dramaId, -1);
|
||||||
if (cards.any((drama) => drama.id == dramaId)) {
|
if (cards.any((drama) => drama.id == dramaId)) {
|
||||||
cards = cards.where((drama) => drama.id != dramaId).toList();
|
setItems(cards.where((drama) => drama.id != dramaId).toList());
|
||||||
total = total > 0 ? total - 1 : 0;
|
setTotal(total - 1);
|
||||||
hasMore = cards.length < total;
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
_saved.add(dramaId);
|
_saved.add(dramaId);
|
||||||
@@ -191,13 +134,11 @@ class FavoriteController extends ChangeNotifier {
|
|||||||
_saved.add(dramaId);
|
_saved.add(dramaId);
|
||||||
if (wasSaved) {
|
if (wasSaved) {
|
||||||
_restoreCount(dramaId, previousCount);
|
_restoreCount(dramaId, previousCount);
|
||||||
cards = previousCards;
|
setItems(previousCards);
|
||||||
total = previousTotal;
|
setTotal(previousTotal);
|
||||||
hasMore = previousHasMore;
|
|
||||||
} else if (card != null && !cards.any((drama) => drama.id == dramaId)) {
|
} else if (card != null && !cards.any((drama) => drama.id == dramaId)) {
|
||||||
cards = [card, ...cards];
|
setItems([card, ...cards]);
|
||||||
total += 1;
|
setTotal(total + 1);
|
||||||
hasMore = cards.length < total;
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
_saved.remove(dramaId);
|
_saved.remove(dramaId);
|
||||||
@@ -205,18 +146,16 @@ class FavoriteController extends ChangeNotifier {
|
|||||||
_restoreCount(dramaId, previousCount);
|
_restoreCount(dramaId, previousCount);
|
||||||
}
|
}
|
||||||
if (cards.any((drama) => drama.id == dramaId)) {
|
if (cards.any((drama) => drama.id == dramaId)) {
|
||||||
cards = cards.where((drama) => drama.id != dramaId).toList();
|
setItems(cards.where((drama) => drama.id != dramaId).toList());
|
||||||
total = total > 0 ? total - 1 : 0;
|
setTotal(total - 1);
|
||||||
hasMore = cards.length < total;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
notify();
|
notify();
|
||||||
return true;
|
return true;
|
||||||
} catch (failure) {
|
} catch (failure) {
|
||||||
// 回滚
|
// 回滚
|
||||||
cards = previousCards;
|
setItems(previousCards);
|
||||||
total = previousTotal;
|
setTotal(previousTotal);
|
||||||
hasMore = previousHasMore;
|
|
||||||
if (wasSaved) {
|
if (wasSaved) {
|
||||||
_saved.add(dramaId);
|
_saved.add(dramaId);
|
||||||
} else {
|
} else {
|
||||||
@@ -235,27 +174,24 @@ class FavoriteController extends ChangeNotifier {
|
|||||||
if (ids.isEmpty) return true;
|
if (ids.isEmpty) return true;
|
||||||
final previousCards = cards;
|
final previousCards = cards;
|
||||||
final previousTotal = total;
|
final previousTotal = total;
|
||||||
final previousHasMore = hasMore;
|
|
||||||
final removed = cards.where((drama) => ids.contains(drama.id)).toList();
|
final removed = cards.where((drama) => ids.contains(drama.id)).toList();
|
||||||
if (removed.isEmpty) return true;
|
if (removed.isEmpty) return true;
|
||||||
final previousCounts = {
|
final previousCounts = {
|
||||||
for (final drama in removed) drama.id: _favoriteCounts[drama.id],
|
for (final drama in removed) drama.id: _favoriteCounts[drama.id],
|
||||||
};
|
};
|
||||||
cards = cards.where((drama) => !ids.contains(drama.id)).toList();
|
setItems(cards.where((drama) => !ids.contains(drama.id)).toList());
|
||||||
for (final id in ids) {
|
for (final id in ids) {
|
||||||
_saved.remove(id);
|
_saved.remove(id);
|
||||||
_stepCount(id, -1);
|
_stepCount(id, -1);
|
||||||
}
|
}
|
||||||
total = total > removed.length ? total - removed.length : 0;
|
setTotal(total - removed.length);
|
||||||
hasMore = cards.length < total;
|
|
||||||
notify();
|
notify();
|
||||||
try {
|
try {
|
||||||
await _repository.removeMany(dramaIds);
|
await _repository.removeMany(dramaIds);
|
||||||
return true;
|
return true;
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
cards = previousCards;
|
setItems(previousCards);
|
||||||
total = previousTotal;
|
setTotal(previousTotal);
|
||||||
hasMore = previousHasMore;
|
|
||||||
for (final drama in removed) {
|
for (final drama in removed) {
|
||||||
_saved.add(drama.id);
|
_saved.add(drama.id);
|
||||||
}
|
}
|
||||||
@@ -276,9 +212,8 @@ class FavoriteController extends ChangeNotifier {
|
|||||||
_saved.remove(dramaId);
|
_saved.remove(dramaId);
|
||||||
_stepCount(dramaId, -1);
|
_stepCount(dramaId, -1);
|
||||||
final next = [...cards]..removeAt(index);
|
final next = [...cards]..removeAt(index);
|
||||||
cards = next;
|
setItems(next);
|
||||||
total = total > 0 ? total - 1 : 0;
|
setTotal(total - 1);
|
||||||
hasMore = cards.length < total;
|
|
||||||
notify();
|
notify();
|
||||||
try {
|
try {
|
||||||
await _repository.remove(dramaId);
|
await _repository.remove(dramaId);
|
||||||
@@ -289,11 +224,10 @@ class FavoriteController extends ChangeNotifier {
|
|||||||
index > restored.length ? restored.length : index,
|
index > restored.length ? restored.length : index,
|
||||||
removed,
|
removed,
|
||||||
);
|
);
|
||||||
cards = restored;
|
setItems(restored);
|
||||||
_saved.add(dramaId);
|
_saved.add(dramaId);
|
||||||
_restoreCount(dramaId, previousCount);
|
_restoreCount(dramaId, previousCount);
|
||||||
total += 1;
|
setTotal(total + 1);
|
||||||
hasMore = cards.length < total;
|
|
||||||
notify();
|
notify();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -301,27 +235,11 @@ class FavoriteController extends ChangeNotifier {
|
|||||||
|
|
||||||
/// 退出登录时清空:收藏状态属于账号,不能跨账号串联。
|
/// 退出登录时清空:收藏状态属于账号,不能跨账号串联。
|
||||||
void clear() {
|
void clear() {
|
||||||
_requestSeq += 1;
|
resetPaging();
|
||||||
_saved.clear();
|
_saved.clear();
|
||||||
cards = const [];
|
|
||||||
total = 0;
|
|
||||||
loading = false;
|
|
||||||
hasMore = false;
|
|
||||||
loadingMore = false;
|
|
||||||
loadMoreError = null;
|
|
||||||
error = null;
|
|
||||||
_pageIndex = 0;
|
|
||||||
notify();
|
notify();
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Drama> _uniqueCards(List<Drama> source) {
|
|
||||||
final seen = <String>{};
|
|
||||||
return [
|
|
||||||
for (final drama in source)
|
|
||||||
if (seen.add(drama.id)) drama,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
void _rememberCardCounts(List<Drama> dramas) {
|
void _rememberCardCounts(List<Drama> dramas) {
|
||||||
for (final drama in dramas) {
|
for (final drama in dramas) {
|
||||||
final count = drama.favoriteCount;
|
final count = drama.favoriteCount;
|
||||||
@@ -345,10 +263,4 @@ class FavoriteController extends ChangeNotifier {
|
|||||||
_favoriteCounts[dramaId] = count;
|
_favoriteCounts[dramaId] = count;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
|
||||||
void dispose() {
|
|
||||||
_disposed = true;
|
|
||||||
super.dispose();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,223 +1,41 @@
|
|||||||
// Author: fruit.com.cn
|
// Author: fruit.com.cn
|
||||||
|
|
||||||
import 'dart:async';
|
|
||||||
|
|
||||||
import 'package:flutter/foundation.dart';
|
|
||||||
|
|
||||||
import '../../data/models/drama.dart';
|
import '../../data/models/drama.dart';
|
||||||
|
import '../../data/models/feed_page.dart';
|
||||||
import '../../data/repositories/catalog_repository.dart';
|
import '../../data/repositories/catalog_repository.dart';
|
||||||
import '../../../pages/home/home_feed_channel.dart';
|
import '../../../pages/home/home_feed_channel.dart';
|
||||||
|
import 'channel_feed_controller.dart';
|
||||||
|
|
||||||
/// 首页内容类型频道状态:每频道独立缓存、刷新和游标分页。
|
/// 首页内容类型频道状态:每频道独立缓存、刷新和游标分页。
|
||||||
class HomeFeedController extends ChangeNotifier {
|
class HomeFeedController extends ChannelFeedController<HomeFeedChannel> {
|
||||||
HomeFeedController(
|
HomeFeedController(this._repository, {super.requestTimeout})
|
||||||
this._repository, {
|
: super(HomeFeedChannel.recommend);
|
||||||
this.requestTimeout = defaultRequestTimeout,
|
|
||||||
}) : _states = {
|
|
||||||
for (final channel in HomeFeedChannel.values)
|
|
||||||
channel: _ChannelFeedState(),
|
|
||||||
};
|
|
||||||
|
|
||||||
static const int pageSize = 20;
|
|
||||||
static const Duration defaultRequestTimeout = Duration(seconds: 10);
|
|
||||||
|
|
||||||
final CatalogRepository _repository;
|
final CatalogRepository _repository;
|
||||||
final Duration requestTimeout;
|
|
||||||
final Map<HomeFeedChannel, _ChannelFeedState> _states;
|
|
||||||
HomeFeedChannel _channel = HomeFeedChannel.recommend;
|
|
||||||
bool _disposed = false;
|
|
||||||
|
|
||||||
HomeFeedChannel get channel => _channel;
|
HomeFeedChannel get channel => currentKey;
|
||||||
String? get lastError => _state(_channel).lastError;
|
|
||||||
List<Drama> get dramas => _state(_channel).dramas;
|
|
||||||
String? get cursor => _state(_channel).cursor;
|
|
||||||
bool get hasMore => _state(_channel).hasMore;
|
|
||||||
bool get loaded => _state(_channel).loaded;
|
|
||||||
bool get isLoading => _state(_channel).loading;
|
|
||||||
bool get loadingMore => _state(_channel).loadingMore;
|
|
||||||
bool get loadingFirstPage => !loaded && _state(_channel).loading;
|
|
||||||
|
|
||||||
/// 用首页聚合接口打平的数据填充推荐首屏,不发起网络请求。
|
/// 用首页聚合接口打平的数据填充推荐首屏,不发起网络请求。
|
||||||
void seedRecommend(List<Drama> dramas) {
|
void seedRecommend(List<Drama> dramas) =>
|
||||||
final state = _state(HomeFeedChannel.recommend);
|
seedKey(HomeFeedChannel.recommend, dramas);
|
||||||
state
|
|
||||||
..requestSeq = state.requestSeq + 1
|
|
||||||
..dramas = List.unmodifiable(dramas)
|
|
||||||
..cursor = null
|
|
||||||
..hasMore = false
|
|
||||||
..loaded = true
|
|
||||||
..loading = false
|
|
||||||
..loadingMore = false
|
|
||||||
..lastError = null
|
|
||||||
..inFlight = null;
|
|
||||||
_notifyIfCurrent(HomeFeedChannel.recommend);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// 后台预取漫剧与真人剧首屏,不切换频道,也不影响当前列表。
|
/// 后台预取漫剧与真人剧首屏,不切换频道,也不影响当前列表。
|
||||||
Future<void> prefetchChannels() async {
|
Future<void> prefetchChannels() async {
|
||||||
await Future.wait([
|
await Future.wait([
|
||||||
_prefetchChannel(HomeFeedChannel.anime),
|
prefetchKey(HomeFeedChannel.anime),
|
||||||
_prefetchChannel(HomeFeedChannel.live),
|
prefetchKey(HomeFeedChannel.live),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 同步切到目标频道;有缓存先渲染,无缓存时复用或等待在飞请求。
|
/// 同步切到目标频道;有缓存先渲染,无缓存时复用或等待在飞请求。
|
||||||
Future<void> selectChannel(HomeFeedChannel next) async {
|
Future<void> selectChannel(HomeFeedChannel next) => selectKey(next);
|
||||||
if (next == _channel) return;
|
|
||||||
_channel = next;
|
|
||||||
_notify();
|
|
||||||
final state = _state(next);
|
|
||||||
if (state.dramas.isNotEmpty) {
|
|
||||||
if (state.skipNextRefresh) {
|
|
||||||
state.skipNextRefresh = false;
|
|
||||||
} else if (!state.loading) {
|
|
||||||
unawaited(_refresh(next, keepData: true));
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (state.loading) {
|
|
||||||
final inFlight = state.inFlight;
|
|
||||||
if (inFlight != null) await inFlight;
|
|
||||||
state.skipNextRefresh = false;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
await _refresh(next, keepData: false);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// 刷新当前频道,失败时保留旧数据。
|
|
||||||
Future<bool> refreshCurrent() async {
|
|
||||||
final state = _state(_channel);
|
|
||||||
if (state.loading) {
|
|
||||||
return await state.inFlight ?? false;
|
|
||||||
}
|
|
||||||
return _refresh(_channel, keepData: true);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// 加载当前频道下一页并去重追加。
|
|
||||||
Future<void> loadMore() async {
|
|
||||||
final channel = _channel;
|
|
||||||
final state = _state(channel);
|
|
||||||
if (!state.hasMore || state.loadingMore || state.cursor == null) return;
|
|
||||||
final seq = ++state.requestSeq;
|
|
||||||
state.loadingMore = true;
|
|
||||||
_notifyIfCurrent(channel);
|
|
||||||
try {
|
|
||||||
final page = await _repository
|
|
||||||
.fetchFeed(
|
|
||||||
types: channel.apiTypes,
|
|
||||||
cursor: state.cursor,
|
|
||||||
size: pageSize,
|
|
||||||
)
|
|
||||||
.timeout(requestTimeout);
|
|
||||||
if (!_acceptSequence(channel, seq)) return;
|
|
||||||
final seen = state.dramas.map((drama) => drama.id).toSet();
|
|
||||||
final appended = page.dramas
|
|
||||||
.where((drama) => seen.add(drama.id))
|
|
||||||
.toList();
|
|
||||||
state
|
|
||||||
..dramas = List.unmodifiable([...state.dramas, ...appended])
|
|
||||||
..cursor = page.nextCursor
|
|
||||||
..hasMore = page.hasMore
|
|
||||||
..loaded = true
|
|
||||||
..lastError = null;
|
|
||||||
} catch (error) {
|
|
||||||
if (_acceptSequence(channel, seq)) {
|
|
||||||
state.lastError = error.toString();
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
state.loadingMore = false;
|
|
||||||
_notifyIfCurrent(channel);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<bool> _refresh(HomeFeedChannel channel, {required bool keepData}) {
|
|
||||||
final state = _state(channel);
|
|
||||||
final operation = _performRefresh(channel, keepData: keepData);
|
|
||||||
state.inFlight = operation;
|
|
||||||
unawaited(_clearInFlight(state, operation));
|
|
||||||
return operation;
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> _prefetchChannel(HomeFeedChannel channel) async {
|
|
||||||
final state = _state(channel);
|
|
||||||
if (state.loaded || state.loading) return;
|
|
||||||
final success = await _refresh(channel, keepData: false);
|
|
||||||
if (success && !_disposed) state.skipNextRefresh = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<bool> _performRefresh(
|
|
||||||
HomeFeedChannel channel, {
|
|
||||||
required bool keepData,
|
|
||||||
}) async {
|
|
||||||
final state = _state(channel);
|
|
||||||
final seq = ++state.requestSeq;
|
|
||||||
state
|
|
||||||
..loading = true
|
|
||||||
..lastError = null;
|
|
||||||
_notifyIfCurrent(channel);
|
|
||||||
try {
|
|
||||||
final page = await _repository
|
|
||||||
.fetchFeed(types: channel.apiTypes, size: pageSize)
|
|
||||||
.timeout(requestTimeout);
|
|
||||||
if (!_acceptSequence(channel, seq)) return false;
|
|
||||||
state
|
|
||||||
..dramas = List.unmodifiable(page.dramas)
|
|
||||||
..cursor = page.nextCursor
|
|
||||||
..hasMore = page.hasMore
|
|
||||||
..loaded = true
|
|
||||||
..loading = false
|
|
||||||
..lastError = null;
|
|
||||||
_notifyIfCurrent(channel);
|
|
||||||
return true;
|
|
||||||
} catch (error) {
|
|
||||||
if (!_acceptSequence(channel, seq)) return false;
|
|
||||||
if (!keepData) state.dramas = const [];
|
|
||||||
state
|
|
||||||
..loaded = true
|
|
||||||
..loading = false
|
|
||||||
..lastError = error.toString();
|
|
||||||
_notifyIfCurrent(channel);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> _clearInFlight(
|
|
||||||
_ChannelFeedState state,
|
|
||||||
Future<bool> operation,
|
|
||||||
) async {
|
|
||||||
await operation;
|
|
||||||
if (identical(state.inFlight, operation)) state.inFlight = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
_ChannelFeedState _state(HomeFeedChannel channel) => _states[channel]!;
|
|
||||||
|
|
||||||
bool _acceptSequence(HomeFeedChannel channel, int seq) =>
|
|
||||||
!_disposed && _state(channel).requestSeq == seq;
|
|
||||||
|
|
||||||
void _notifyIfCurrent(HomeFeedChannel channel) {
|
|
||||||
if (!_disposed && channel == _channel) _notify();
|
|
||||||
}
|
|
||||||
|
|
||||||
void _notify() {
|
|
||||||
if (!_disposed) notifyListeners();
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
Future<FeedPage> fetchPage(HomeFeedChannel key, {String? cursor}) {
|
||||||
_disposed = true;
|
return _repository.fetchFeed(
|
||||||
super.dispose();
|
types: key.apiTypes,
|
||||||
|
cursor: cursor,
|
||||||
|
size: ChannelFeedController.pageSize,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class _ChannelFeedState {
|
|
||||||
List<Drama> dramas = const [];
|
|
||||||
String? cursor;
|
|
||||||
bool hasMore = true;
|
|
||||||
bool loaded = false;
|
|
||||||
bool loading = false;
|
|
||||||
bool loadingMore = false;
|
|
||||||
String? lastError;
|
|
||||||
int requestSeq = 0;
|
|
||||||
Future<bool>? inFlight;
|
|
||||||
bool skipNextRefresh = false;
|
|
||||||
}
|
|
||||||