Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加用户字段,优化关注页面排序,更新信息 #273

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions simple_live_app/lib/models/db/follow_user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class FollowUser {
required this.userName,
required this.face,
required this.addTime,
required this.special,
required this.lastWatchTime,
required this.lastPlayTime,
required this.watchSecond,
});

///id=siteId_roomId
Expand All @@ -33,6 +37,18 @@ class FollowUser {
@HiveField(5)
DateTime addTime;

@HiveField(6)
bool special;

@HiveField(7)
DateTime lastWatchTime;

@HiveField(8)
DateTime lastPlayTime;

@HiveField(9)
int watchSecond;

/// 直播状态
/// 0=未知(加载中) 1=未开播 2=直播中
Rx<int> liveStatus = 0.obs;
Expand All @@ -44,5 +60,13 @@ class FollowUser {
userName: json['userName'],
face: json['face'],
addTime: DateTime.parse(json['addTime']),
special: json.containsKey('special') ? json['special'] : false,
lastWatchTime: json.containsKey('lastWatchTime')
? DateTime.parse(json['lastWatchTime'])
: DateTime(2000),
lastPlayTime: json.containsKey('lastPlayTime')
? DateTime.parse(json['lastPlayTime'])
: DateTime(2000),
watchSecond: json['watchSecond'] ?? 0,
);
}
16 changes: 14 additions & 2 deletions simple_live_app/lib/models/db/follow_user.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 68 additions & 0 deletions simple_live_app/lib/modules/live_room/live_room_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class LiveRoomController extends PlayerController with WidgetsBindingObserver {
var online = 0.obs;
var followed = false.obs;
var liveStatus = false.obs;
var specialFollowed = false.obs;
RxList<LiveSuperChatMessage> superChats = RxList<LiveSuperChatMessage>();

/// 滚动控制
Expand Down Expand Up @@ -107,6 +108,9 @@ class LiveRoomController extends PlayerController with WidgetsBindingObserver {
initAutoExit();
showDanmakuState.value = AppSettingsController.instance.danmuEnable.value;
followed.value = DBService.instance.getFollowExist("${site.id}_$roomId");
specialFollowed.value =
DBService.instance.getFollowUser("${site.id}_$roomId")?.special ??
false;
loadData();

scrollController.addListener(scrollListener);
Expand Down Expand Up @@ -263,6 +267,7 @@ class LiveRoomController extends PlayerController with WidgetsBindingObserver {
getSuperChatMessage();

addHistory();
updateFollowUser();
online.value = detail.value!.online;
liveStatus.value = detail.value!.status || detail.value!.isRecord;
if (liveStatus.value) {
Expand Down Expand Up @@ -446,6 +451,33 @@ class LiveRoomController extends PlayerController with WidgetsBindingObserver {
.toList();
}

Timer? updateTimer;

/// 更新自定义数据
void updateFollowUser() {
if (detail.value == null) {
return;
}
updateTimer?.cancel();

var id = "${site.id}_$roomId";
var user = DBService.instance.getFollowUser(id);
if (user == null) {
return;
}

user.userName = detail.value?.userName ?? user.userName;
user.face = detail.value?.userAvatar ?? user.face;
DBService.instance.addFollow(user);

updateTimer = Timer.periodic(const Duration(seconds: 10), (timer) {
user.watchSecond += 10;
user.lastWatchTime = DateTime.now();
user.lastPlayTime = DateTime.now();
DBService.instance.addFollow(user);
});
}

/// 添加历史记录
void addHistory() {
if (detail.value == null) {
Expand All @@ -468,6 +500,37 @@ class LiveRoomController extends PlayerController with WidgetsBindingObserver {
DBService.instance.addOrUpdateHistory(history);
}

/// 特别关注
void followUserSpecial() {
if (detail.value == null) {
return;
}
var id = "${site.id}_$roomId";
if (!followed.value) {
followUser();
}
FollowUser? user = DBService.instance.getFollowUser(id);
if (user != null) {
user.special = true;
DBService.instance.addFollow(user);
}
specialFollowed.value = true;
}

/// 取消特别关注
void removeFollowUserSpecial() {
if (detail.value == null) {
return;
}
var id = "${site.id}_$roomId";
FollowUser? user = DBService.instance.getFollowUser(id);
if (user != null) {
user.special = false;
DBService.instance.addFollow(user);
}
specialFollowed.value = false;
}

/// 关注用户
void followUser() {
if (detail.value == null) {
Expand All @@ -482,6 +545,10 @@ class LiveRoomController extends PlayerController with WidgetsBindingObserver {
userName: detail.value?.userName ?? "",
face: detail.value?.userAvatar ?? "",
addTime: DateTime.now(),
watchSecond: 0,
lastWatchTime: DateTime.now(),
lastPlayTime: DateTime.now(),
special: false,
),
);
followed.value = true;
Expand Down Expand Up @@ -890,6 +957,7 @@ class LiveRoomController extends PlayerController with WidgetsBindingObserver {
WidgetsBinding.instance.removeObserver(this);
scrollController.removeListener(scrollListener);
autoExitTimer?.cancel();
updateTimer?.cancel();

liveDanmaku.stop();
danmakuController = null;
Expand Down
19 changes: 19 additions & 0 deletions simple_live_app/lib/modules/live_room/live_room_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,25 @@ class LiveRoomPage extends GetView<LiveRoomController> {
label: const Text("关注"),
),
),
Obx(
() => controller.specialFollowed.value
? TextButton.icon(
style: TextButton.styleFrom(
textStyle: const TextStyle(fontSize: 14),
),
onPressed: controller.removeFollowUserSpecial,
icon: const Icon(Remix.heart_fill),
label: const Text("取消特别关注"),
)
: TextButton.icon(
style: TextButton.styleFrom(
textStyle: const TextStyle(fontSize: 14),
),
onPressed: controller.followUserSpecial,
icon: const Icon(Remix.heart_line),
label: const Text("特别关注"),
),
),
const Expanded(child: Center()),
TextButton.icon(
style: TextButton.styleFrom(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,19 @@ class FollowUserController extends BasePageController<FollowUser> {
} else if (filterMode.value == 2) {
list.assignAll(allList.where((x) => x.liveStatus.value == 1));
}
allList.sort((a, b) => b.liveStatus.value.compareTo(a.liveStatus.value));

allList.sort((a, b) {
if (b.liveStatus.value != a.liveStatus.value) {
return b.liveStatus.value.compareTo(a.liveStatus.value);
}
if (a.special == b.special) {
return b.lastWatchTime.compareTo(a.lastWatchTime);
}
if (b.special) {
return 1;
}
return -1;
});
}

void setFilterMode(int mode) {
Expand All @@ -80,6 +92,11 @@ class FollowUserController extends BasePageController<FollowUser> {
item.liveStatus.value =
(await site.liveSite.getLiveStatus(roomId: item.roomId)) ? 2 : 1;

if (item.liveStatus.value == 2) {
item.lastPlayTime = DateTime.now();
DBService.instance.addFollow(item);
}

filterData();
} catch (e) {
Log.logPrint(e);
Expand Down Expand Up @@ -260,6 +277,10 @@ class FollowUserController extends BasePageController<FollowUser> {
"userName": item.userName,
"face": item.face,
"addTime": item.addTime.toString(),
"special": item.special,
"lastWatchTime": item.lastWatchTime.toString(),
"lastPlayTime": item.lastPlayTime.toString(),
"watchSecond": item.watchSecond,
},
)
.toList();
Expand Down
7 changes: 7 additions & 0 deletions simple_live_app/lib/services/db_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ class DBService extends GetxService {
followBox = await Hive.openBox("FollowUser");
}

FollowUser? getFollowUser(String id) {
if (followBox.containsKey(id)) {
return followBox.get(id);
}
return null;
}

bool getFollowExist(String id) {
return followBox.containsKey(id);
}
Expand Down