Skip to content

Commit

Permalink
Merge branch 'guozhigq:main' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
KoolShow authored Dec 24, 2023
2 parents 1e95632 + 0a08d34 commit 6d2a6e8
Show file tree
Hide file tree
Showing 10 changed files with 327 additions and 325 deletions.
4 changes: 2 additions & 2 deletions lib/http/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ class Api {
// 操作用户关系
static const String relationMod = '/x/relation/modify';

// 相互关系查询
static const String relationSearch = '/x/space/wbi/acc/relation';
// 相互关系查询 // 失效
// static const String relationSearch = '/x/space/wbi/acc/relation';

// 评论列表
// https://api.bilibili.com/x/v2/reply/main?csrf=6e22efc1a47225ea25f901f922b5cfdd&mode=3&oid=254175381&pagination_str=%7B%22offset%22:%22%22%7D&plat=1&seek_rpid=0&type=11
Expand Down
42 changes: 27 additions & 15 deletions lib/http/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -250,31 +250,43 @@ class UserHttp {
return {'status': false, 'msg': res.data['message']};
}
}

// 相互关系查询
static Future relationSearch(int mid) async {
Map params = await WbiSign().makSign({
'mid': mid,
'token': '',
'platform': 'web',
'web_location': 1550101,
});
static Future hasFollow(int mid) async {
var res = await Request().get(
Api.relationSearch,
Api.hasFollow,
data: {
'mid': mid,
'w_rid': params['w_rid'],
'wts': params['wts'],
'fid': mid,
},
);
if (res.data['code'] == 0) {
// relation 主动状态
// 被动状态
return {'status': true, 'data': res.data['data']};
} else {
return {'status': false, 'msg': res.data['message']};
}
}
// // 相互关系查询
// static Future relationSearch(int mid) async {
// Map params = await WbiSign().makSign({
// 'mid': mid,
// 'token': '',
// 'platform': 'web',
// 'web_location': 1550101,
// });
// var res = await Request().get(
// Api.relationSearch,
// data: {
// 'mid': mid,
// 'w_rid': params['w_rid'],
// 'wts': params['wts'],
// },
// );
// if (res.data['code'] == 0) {
// // relation 主动状态
// // 被动状态
// return {'status': true, 'data': res.data['data']};
// } else {
// return {'status': false, 'msg': res.data['message']};
// }
// }

// 搜索历史记录
static Future searchHistory(
Expand Down
17 changes: 12 additions & 5 deletions lib/pages/bangumi/introduction/view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,14 @@ class _BangumiInfoState extends State<BangumiInfo> {
late final BangumiInfoModel? bangumiItem;
late double sheetHeight;
int? cid;

bool isProcessing = false;
void Function()? handleState(Future Function() action) {
return isProcessing ? null : () async {
setState(() => isProcessing = true);
await action();
setState(() => isProcessing = false);
};
}
@override
void initState() {
super.initState();
Expand Down Expand Up @@ -395,7 +402,7 @@ class _BangumiInfoState extends State<BangumiInfo> {
() => ActionItem(
icon: const Icon(FontAwesomeIcons.thumbsUp),
selectIcon: const Icon(FontAwesomeIcons.solidThumbsUp),
onTap: () => bangumiIntroController.actionLikeVideo(),
onTap: handleState(bangumiIntroController.actionLikeVideo),
selectStatus: bangumiIntroController.hasLike.value,
loadingStatus: false,
text: !widget.loadingStatus
Expand All @@ -406,7 +413,7 @@ class _BangumiInfoState extends State<BangumiInfo> {
() => ActionItem(
icon: const Icon(FontAwesomeIcons.b),
selectIcon: const Icon(FontAwesomeIcons.b),
onTap: () => bangumiIntroController.actionCoinVideo(),
onTap: handleState(bangumiIntroController.actionCoinVideo),
selectStatus: bangumiIntroController.hasCoin.value,
loadingStatus: false,
text: !widget.loadingStatus
Expand Down Expand Up @@ -455,7 +462,7 @@ class _BangumiInfoState extends State<BangumiInfo> {
Obx(
() => ActionRowItem(
icon: const Icon(FontAwesomeIcons.thumbsUp),
onTap: () => videoIntroController.actionLikeVideo(),
onTap: handleState(videoIntroController.actionLikeVideo),
selectStatus: videoIntroController.hasLike.value,
loadingStatus: widget.loadingStatus,
text: !widget.loadingStatus
Expand All @@ -467,7 +474,7 @@ class _BangumiInfoState extends State<BangumiInfo> {
Obx(
() => ActionRowItem(
icon: const Icon(FontAwesomeIcons.b),
onTap: () => videoIntroController.actionCoinVideo(),
onTap: handleState(videoIntroController.actionCoinVideo),
selectStatus: videoIntroController.hasCoin.value,
loadingStatus: widget.loadingStatus,
text: !widget.loadingStatus
Expand Down
94 changes: 38 additions & 56 deletions lib/pages/danmaku/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,74 +3,56 @@ import 'package:pilipala/models/danmaku/dm.pb.dart';
import 'package:pilipala/plugin/pl_player/index.dart';

class PlDanmakuController {
PlDanmakuController(this.cid, this.playerController);
PlDanmakuController(this.cid);
final int cid;
final PlPlayerController playerController;
late Duration videoDuration;
// 按 6min 分段
int segCount = 0;
List<DmSegMobileReply> dmSegList = [];
Map<int,List<DanmakuElem>> dmSegMap = {};
// 已请求的段落标记
List<int> hasrequestSeg = [];
int currentSegIndex = 1;
int currentDmIndex = 0;
List<bool> requestedSeg = [];

void calcSegment() {
dmSegList.clear();
// 视频分段数
segCount = (videoDuration.inSeconds / (60 * 6)).ceil();
dmSegList = List<DmSegMobileReply>.generate(
segCount < 1 ? 1 : segCount, (index) => DmSegMobileReply());
// 当前分段
try {
currentSegIndex =
(playerController.position.value.inSeconds / (60 * 6)).ceil();
currentSegIndex = currentSegIndex < 1 ? 1 : currentSegIndex;
} catch (_) {}
}
bool get initiated => requestedSeg.isNotEmpty;

Future<List<DmSegMobileReply>> queryDanmaku() async {
// dmSegList.clear();
DmSegMobileReply result =
await DanmakaHttp.queryDanmaku(cid: cid, segmentIndex: currentSegIndex);
if (result.elems.isNotEmpty) {
result.elems.sort((a, b) => (a.progress).compareTo(b.progress));
// dmSegList.add(result);
currentSegIndex = currentSegIndex < 1 ? 1 : currentSegIndex;
dmSegList[currentSegIndex - 1] = result;
}
if (dmSegList.isNotEmpty) {
findClosestPositionIndex(playerController.position.value.inMilliseconds);
static int SEGMENT_LENGTH = 60 * 6 * 1000;

void initiate(int videoDuration, int progress) {
if (requestedSeg.isEmpty) {
int segCount = (videoDuration / SEGMENT_LENGTH).ceil();
requestedSeg = List<bool>.generate(segCount, (index) => false);
}
return dmSegList;
queryDanmaku(
calcSegment(progress)
);
}

/// 查询当前最接近的弹幕
void findClosestPositionIndex(int position) {
int segIndex = (position / (6 * 60 * 1000)).ceil() - 1;
if (segIndex < 0) segIndex = 0;
List elems = dmSegList[segIndex].elems;

if (segIndex < dmSegList.length) {
int left = 0;
int right = elems.length;
void dispose() {
dmSegMap.clear();
requestedSeg.clear();
}

while (left < right) {
int mid = (right + left) ~/ 2;
var midPosition = elems[mid].progress;
int calcSegment(int progress) {
return progress ~/ SEGMENT_LENGTH;
}

if (midPosition >= position) {
right = mid;
} else {
left = mid + 1;
void queryDanmaku(int segmentIndex) async {
assert(requestedSeg[segmentIndex] == false);
requestedSeg[segmentIndex] = true;
DmSegMobileReply result =
await DanmakaHttp.queryDanmaku(cid: cid, segmentIndex: segmentIndex + 1);
if (result.elems.isNotEmpty) {
for (var element in result.elems) {
int pos = element.progress ~/ 100;//每0.1秒存储一次
if (dmSegMap[pos] == null) {
dmSegMap[pos] = [];
}
dmSegMap[pos]!.add(element);
}
}
}

currentSegIndex = segIndex;
currentDmIndex = right;
} else {
currentSegIndex = segIndex;
currentDmIndex = 0;
List<DanmakuElem>? getCurrentDanmaku(int progress) {
int segmentIndex = calcSegment(progress);
if (!requestedSeg[segmentIndex]) {
queryDanmaku(segmentIndex);
}
return dmSegMap[progress ~/ 100];
}
}
99 changes: 32 additions & 67 deletions lib/pages/danmaku/view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hive/hive.dart';
import 'package:ns_danmaku/ns_danmaku.dart';
import 'package:pilipala/models/danmaku/dm.pb.dart';
import 'package:pilipala/pages/danmaku/index.dart';
import 'package:pilipala/plugin/pl_player/index.dart';
import 'package:pilipala/utils/danmaku.dart';
Expand All @@ -27,41 +28,41 @@ class _PlDanmakuState extends State<PlDanmaku> {
late PlPlayerController playerController;
late PlDanmakuController _plDanmakuController;
DanmakuController? _controller;
bool danmuPlayStatus = true;
// bool danmuPlayStatus = true;
Box setting = GStrorage.setting;
late bool enableShowDanmaku;
late List blockTypes;
late double showArea;
late double opacityVal;
late double fontSizeVal;
late double danmakuDurationVal;
int latestAddedPosition = -1;

@override
void initState() {
super.initState();
enableShowDanmaku =
setting.get(SettingBoxKey.enableShowDanmaku, defaultValue: false);
_plDanmakuController =
PlDanmakuController(widget.cid, widget.playerController);
PlDanmakuController(widget.cid);
if (mounted) {
playerController = widget.playerController;
_plDanmakuController.videoDuration = playerController.duration.value;
if (enableShowDanmaku || playerController.isOpenDanmu.value) {
_plDanmakuController
..calcSegment()
..queryDanmaku();
_plDanmakuController.initiate(
playerController.duration.value.inMilliseconds,
playerController.position.value.inMilliseconds
);
}
playerController
..addStatusLister(playerListener)
..addPositionListener(videoPositionListen);
}
playerController.isOpenDanmu.listen((p0) {
if (p0) {
if (_plDanmakuController.dmSegList.isEmpty) {
_plDanmakuController
..calcSegment()
..queryDanmaku();
}
if (p0 && !_plDanmakuController.initiated) {
_plDanmakuController.initiate(
playerController.duration.value.inMilliseconds,
playerController.position.value.inMilliseconds
);
}
});
blockTypes = playerController.blockTypes;
Expand All @@ -82,68 +83,32 @@ class _PlDanmakuState extends State<PlDanmaku> {
}

void videoPositionListen(Duration position) {
if (!danmuPlayStatus) {
_controller!.onResume();
danmuPlayStatus = true;
}
if (!playerController.isOpenDanmu.value) {
return;
}
PlDanmakuController ctr = _plDanmakuController;
int currentPosition = position.inMilliseconds;
blockTypes = playerController.blockTypes;
// 根据position判断是否有已缓存弹幕。没有则请求对应段
int segIndex = (currentPosition / (6 * 60 * 1000)).ceil();
segIndex = segIndex < 1 ? 1 : segIndex;
// print('🌹🌹: ${segIndex}');
// print('🌹🌹: ${ctr.dmSegList.length}');
// print('🌹🌹: ${ctr.hasrequestSeg.contains(segIndex - 1)}');
if (segIndex - 1 >= ctr.dmSegList.length ||
(ctr.dmSegList[segIndex - 1].elems.isEmpty &&
!ctr.hasrequestSeg.contains(segIndex - 1))) {
ctr.hasrequestSeg.add(segIndex - 1);
ctr.currentSegIndex = segIndex;
EasyThrottle.throttle('follow', const Duration(seconds: 1), () {
ctr.queryDanmaku();
});
}
// 超出分段数返回
if (ctr.currentSegIndex >= ctr.dmSegList.length) {
return;
}
if (ctr.dmSegList.isEmpty ||
ctr.dmSegList[ctr.currentSegIndex].elems.isEmpty) {
return;
}
// 超出当前分段的弹幕总数返回
if (ctr.currentDmIndex >= ctr.dmSegList[ctr.currentSegIndex].elems.length) {
ctr.currentDmIndex = 0;
ctr.currentSegIndex++;
currentPosition -= currentPosition % 100;//取整百的毫秒数

if (currentPosition == latestAddedPosition) {
return;
}
var element = ctr.dmSegList[ctr.currentSegIndex].elems[ctr.currentDmIndex];
var delta = currentPosition - element.progress;
latestAddedPosition = currentPosition;

if (delta >= 0 && delta < 200) {
// 屏蔽彩色弹幕
if (blockTypes.contains(6) ? element.color == 16777215 : true) {
_controller!.addItems([
DanmakuItem(
element.content,
color: DmUtils.decimalToColor(element.color),
time: element.progress,
type: DmUtils.getPosition(element.mode),
)
]);
}
ctr.currentDmIndex++;
} else {
if (!playerController.isOpenDanmu.value) {
_controller!.pause();
danmuPlayStatus = false;
return;
}
ctr.findClosestPositionIndex(position.inMilliseconds);
List<DanmakuElem>? currentDanmakuList =
_plDanmakuController.getCurrentDanmaku(currentPosition);

if (currentDanmakuList != null) {
Color? defaultColor = playerController.blockTypes.contains(6) ?
DmUtils.decimalToColor(16777215) : null;

_controller!.addItems(
currentDanmakuList.map((e) => DanmakuItem(
e.content,
color: defaultColor ?? DmUtils.decimalToColor(e.color),
time: e.progress,
type: DmUtils.getPosition(e.mode),
)).toList()
);
}
}

Expand Down
Loading

0 comments on commit 6d2a6e8

Please sign in to comment.