Skip to content

Commit

Permalink
feat: use ReadPosition to record current progress for extensibility
Browse files Browse the repository at this point in the history
  • Loading branch information
qixiaoo committed Oct 12, 2024
1 parent 738247c commit bfe7306
Show file tree
Hide file tree
Showing 14 changed files with 508 additions and 47 deletions.
13 changes: 7 additions & 6 deletions lib/bloc/screen/reader/reader_screen_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:masiro/data/repository/masiro_repository.dart';
import 'package:masiro/data/repository/model/chapter_detail.dart';
import 'package:masiro/data/repository/model/chapter_record.dart';
import 'package:masiro/data/repository/model/loading_status.dart';
import 'package:masiro/data/repository/model/read_position.dart';
import 'package:masiro/data/repository/model/reading_mode.dart';
import 'package:masiro/data/repository/novel_record_repository.dart';
import 'package:masiro/di/get_it.dart';
Expand All @@ -22,8 +23,8 @@ class ReaderScreenBloc extends Bloc<ReaderScreenEvent, ReaderScreenState> {
}) : super(ReaderScreenInitialState()) {
on<ReaderScreenChapterDetailRequested>(_onRequestReaderScreenChapterDetail);
on<ReaderScreenHudToggled>(_onToggleReaderScreenHud);
on<ReaderScreenProgressChanged>(
_onChangeReaderScreenProgress,
on<ReaderScreenPositionChanged>(
_onChangeReaderScreenPosition,
transformer: debounce(const Duration(milliseconds: 500)),
);
on<ReaderScreenChapterNavigated>(_onReaderScreenChapterNavigated);
Expand All @@ -46,7 +47,7 @@ class ReaderScreenBloc extends Bloc<ReaderScreenEvent, ReaderScreenState> {
emit(
ReaderScreenLoadedState(
chapterDetail: chapterDetail,
progress: chapterRecord?.progress ?? 0,
position: chapterRecord?.position ?? startPosition,
),
);
} catch (e) {
Expand All @@ -66,8 +67,8 @@ class ReaderScreenBloc extends Bloc<ReaderScreenEvent, ReaderScreenState> {
emit(loadedState.copyWith(isHudVisible: !isHudVisible));
}

Future<void> _onChangeReaderScreenProgress(
ReaderScreenProgressChanged event,
Future<void> _onChangeReaderScreenPosition(
ReaderScreenPositionChanged event,
Emitter<ReaderScreenState> emit,
) async {
if (state is! ReaderScreenLoadedState) {
Expand All @@ -85,7 +86,7 @@ class ReaderScreenBloc extends Bloc<ReaderScreenEvent, ReaderScreenState> {
id: chapterRecord?.id ?? Isar.autoIncrement,
chapterId: chapterId,
novelId: novelId,
progress: event.progress,
position: event.position,
readingMode: readingMode,
),
);
Expand Down
9 changes: 5 additions & 4 deletions lib/bloc/screen/reader/reader_screen_event.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:equatable/equatable.dart';
import 'package:masiro/data/repository/model/read_position.dart';

sealed class ReaderScreenEvent extends Equatable {
@override
Expand All @@ -16,13 +17,13 @@ final class ReaderScreenChapterDetailRequested extends ReaderScreenEvent {

final class ReaderScreenHudToggled extends ReaderScreenEvent {}

final class ReaderScreenProgressChanged extends ReaderScreenEvent {
final int progress;
final class ReaderScreenPositionChanged extends ReaderScreenEvent {
final ReadPosition position;

ReaderScreenProgressChanged({required this.progress});
ReaderScreenPositionChanged({required this.position});

@override
List<Object> get props => [progress];
List<Object> get props => [position];
}

final class ReaderScreenChapterNavigated extends ReaderScreenEvent {
Expand Down
11 changes: 6 additions & 5 deletions lib/bloc/screen/reader/reader_screen_state.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:equatable/equatable.dart';
import 'package:masiro/data/repository/model/chapter_detail.dart';
import 'package:masiro/data/repository/model/loading_status.dart';
import 'package:masiro/data/repository/model/read_position.dart';
import 'package:masiro/data/repository/model/reading_mode.dart';

sealed class ReaderScreenState extends Equatable {
Expand All @@ -23,29 +24,29 @@ class ReaderScreenLoadedState extends ReaderScreenState {
final ChapterDetail chapterDetail;
final bool isHudVisible;
final ReadingMode readingMode;
final int progress;
final ReadPosition position;
final LoadingStatus loadingStatus;

ReaderScreenLoadedState({
required this.chapterDetail,
this.isHudVisible = false,
this.readingMode = ReadingMode.scroll,
required this.progress,
required this.position,
this.loadingStatus = LoadingStatus.success,
});

ReaderScreenLoadedState copyWith({
ChapterDetail? chapterDetail,
bool? isHudVisible,
ReadingMode? readingMode,
int? progress,
ReadPosition? position,
LoadingStatus? loadingStatus,
}) {
return ReaderScreenLoadedState(
chapterDetail: chapterDetail ?? this.chapterDetail,
isHudVisible: isHudVisible ?? this.isHudVisible,
readingMode: readingMode ?? this.readingMode,
progress: progress ?? this.progress,
position: position ?? this.position,
loadingStatus: loadingStatus ?? this.loadingStatus,
);
}
Expand All @@ -55,7 +56,7 @@ class ReaderScreenLoadedState extends ReaderScreenState {
chapterDetail,
isHudVisible,
readingMode,
progress,
position,
loadingStatus,
];
}
4 changes: 4 additions & 0 deletions lib/data/database/entity/chapter_record_entity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,21 @@ class ChapterRecordEntity {
@Index()
int novelId;

@Deprecated('Use `positionJson` instead.')
int progress;

@Enumerated(EnumType.name)
late ChapterReadingMode readingMode;

String? positionJson;

ChapterRecordEntity({
this.id = Isar.autoIncrement,
required this.chapterId,
required this.novelId,
required this.progress,
required this.readingMode,
this.positionJson,
});
}

Expand Down
Loading

0 comments on commit bfe7306

Please sign in to comment.