Skip to content

Commit

Permalink
opt: sponsor block
Browse files Browse the repository at this point in the history
Signed-off-by: bggRGjQaUbCoE <[email protected]>
  • Loading branch information
bggRGjQaUbCoE committed Nov 12, 2024
1 parent 60bdf07 commit 81c8845
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/http/init.dart
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ class Request {
/*
* get请求
*/
Future get(url, {data, options, cancelToken, extra}) async {
Future<Response> get(url, {data, options, cancelToken, extra}) async {
Response response;
if (extra != null) {
if (extra['ua'] != null) {
Expand Down
2 changes: 1 addition & 1 deletion lib/http/login.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class LoginHttp {
'data': CaptchaDataModel.fromJson(res.data['data']),
};
} else {
return {'status': false, 'data': res.message};
return {'status': false, 'data': res.data['message']};
}
}

Expand Down
97 changes: 86 additions & 11 deletions lib/pages/setting/sponsor_block_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'dart:math';

import 'package:PiliPalaX/common/widgets/pair.dart';
import 'package:PiliPalaX/http/constants.dart';
import 'package:PiliPalaX/pages/setting/widgets/select_item.dart';
import 'package:PiliPalaX/http/index.dart';
import 'package:PiliPalaX/pages/video/detail/controller.dart'
show SegmentType, SegmentTypeExt, SkipType, SkipTypeExt;
import 'package:PiliPalaX/utils/storage.dart';
Expand All @@ -28,6 +28,8 @@ class _SponsorBlockPageState extends State<SponsorBlockPage> {
late String _userId;
late bool _blockToast;
late String _blockServer;
late bool _blockTrack;
bool? _serverStatus;

@override
void initState() {
Expand All @@ -38,6 +40,8 @@ class _SponsorBlockPageState extends State<SponsorBlockPage> {
_userId = GStorage.blockUserID;
_blockToast = GStorage.blockToast;
_blockServer = GStorage.blockServer;
_blockTrack = GStorage.blockTrack;
_checkServerStatus();
}

@override
Expand All @@ -46,9 +50,21 @@ class _SponsorBlockPageState extends State<SponsorBlockPage> {
super.dispose();
}

TextStyle get _titleStyle => TextStyle(fontSize: 15);
TextStyle get _subTitleStyle =>
TextStyle(color: Theme.of(context).colorScheme.outline);
TextStyle get _titleStyle => TextStyle(fontSize: 14);
TextStyle get _subTitleStyle => TextStyle(
fontSize: 12,
color: Theme.of(context).colorScheme.outline,
);

_checkServerStatus() {
Request().get('$_blockServer/api/status').then((res) {
if (res.data is Map) {
setState(() {
_serverStatus = res.data['uptime'] != null;
});
}
});
}

Widget get _blockLimitItem => ListTile(
dense: true,
Expand Down Expand Up @@ -184,6 +200,12 @@ class _SponsorBlockPageState extends State<SponsorBlockPage> {
setState(() {});
}

void _updateBlockTrack() async {
_blockTrack = !_blockTrack;
await GStorage.setting.put(SettingBoxKey.blockTrack, _blockTrack);
setState(() {});
}

Widget get _blockToastItem => ListTile(
dense: true,
onTap: _updateBlockToast,
Expand All @@ -208,6 +230,30 @@ class _SponsorBlockPageState extends State<SponsorBlockPage> {
),
));

Widget get _blockTrackItem => ListTile(
dense: true,
onTap: _updateBlockTrack,
title: Text(
'记录跳过片段',
style: _titleStyle,
),
trailing: Transform.scale(
alignment: Alignment.centerRight,
scale: 0.8,
child: Switch(
thumbIcon: WidgetStateProperty.resolveWith<Icon?>((states) {
if (states.isNotEmpty && states.first == WidgetState.selected) {
return const Icon(Icons.done);
}
return null;
}),
value: _blockTrack,
onChanged: (val) {
_updateBlockTrack();
},
),
));

Widget get _blockServerItem => ListTile(
dense: true,
onTap: () {
Expand Down Expand Up @@ -267,6 +313,30 @@ class _SponsorBlockPageState extends State<SponsorBlockPage> {
),
);

Widget get _serverStatusItem => ListTile(
dense: true,
onTap: _checkServerStatus,
title: Text(
'服务器状态',
style: _titleStyle,
),
trailing: Text(
_serverStatus == null
? '-'
: _serverStatus == true
? '正常'
: '错误',
style: TextStyle(
fontSize: 13,
color: _serverStatus == null
? null
: _serverStatus == true
? Theme.of(context).colorScheme.primary
: Theme.of(context).colorScheme.error,
),
),
);

Widget get _divider => SliverToBoxAdapter(
child: Divider(
height: 1,
Expand Down Expand Up @@ -294,10 +364,14 @@ class _SponsorBlockPageState extends State<SponsorBlockPage> {
),
body: CustomScrollView(
slivers: [
_dividerL,
SliverToBoxAdapter(child: _serverStatusItem),
_dividerL,
SliverToBoxAdapter(child: _blockLimitItem),
_divider,
SliverToBoxAdapter(child: _blockToastItem),
_divider,
SliverToBoxAdapter(child: _blockTrackItem),
_dividerL,
SliverList.separated(
itemCount: _blockSettings.length,
Expand Down Expand Up @@ -377,22 +451,23 @@ class _SponsorBlockPageState extends State<SponsorBlockPage> {
),
),
),
style: TextStyle(fontSize: 15),
style: TextStyle(fontSize: 14),
),
TextSpan(
text: ' ${_blockSettings[index].first.title}',
style: TextStyle(fontSize: 15),
style: TextStyle(fontSize: 14),
),
],
),
),
subtitle: Text(
_blockSettings[index].first.description,
style: _blockSettings[index].second == SkipType.disable
? null
: TextStyle(
color: Theme.of(context).colorScheme.outline,
),
style: TextStyle(
fontSize: 12,
color: _blockSettings[index].second == SkipType.disable
? null
: Theme.of(context).colorScheme.outline,
),
),
trailing: PopupMenuButton(
initialValue: _blockSettings[index].second,
Expand Down
8 changes: 7 additions & 1 deletion lib/pages/video/detail/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ extension SegmentTypeExt on SegmentType {
'订阅提醒', //interaction
'开场', //intro
'片尾', //outro
'回顾', //preview
'预览', //preview
'非音乐', //music_offtopic
'精彩时刻', //poi_highlight
'闲聊', //filler
Expand Down Expand Up @@ -604,6 +604,12 @@ class VideoDetailController extends GetxController
if (GStorage.blockToast) {
_showBlockToast('已跳过${item.segmentType.shortTitle}片段');
}
if (GStorage.blockTrack) {
Request().post(
'${GStorage.blockServer}/api/viewedVideoSponsorTime',
queryParameters: {'UUID': item.UUID},
);
}
} catch (e) {
debugPrint('failed to skip: $e');
_showBlockToast('${item.segmentType.shortTitle}片段跳过失败');
Expand Down
4 changes: 4 additions & 0 deletions lib/utils/storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ class GStorage {
static String get blockServer => setting.get(SettingBoxKey.blockServer,
defaultValue: HttpString.sponsorBlockBaseUrl);

static bool get blockTrack =>
setting.get(SettingBoxKey.blockTrack, defaultValue: true);

static ThemeMode get themeMode {
switch (setting.get(SettingBoxKey.themeMode,
defaultValue: ThemeType.system.code)) {
Expand Down Expand Up @@ -246,6 +249,7 @@ class SettingBoxKey {
blockUserID = 'blockUserID',
blockToast = 'blockToast',
blockServer = 'blockServer',
blockTrack = 'blockTrack',

// 弹幕相关设置 权重(云屏蔽) 屏蔽类型 显示区域 透明度 字体大小 弹幕时间 描边粗细 字体粗细
danmakuWeight = 'danmakuWeight',
Expand Down

0 comments on commit 81c8845

Please sign in to comment.