Skip to content

Commit

Permalink
feat: allow user to adjust font size on reader screen
Browse files Browse the repository at this point in the history
  • Loading branch information
qixiaoo committed Oct 13, 2024
1 parent d56a240 commit b82b9b2
Show file tree
Hide file tree
Showing 15 changed files with 277 additions and 19 deletions.
19 changes: 19 additions & 0 deletions lib/bloc/screen/reader/reader_screen_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:isar/isar.dart';
import 'package:masiro/bloc/screen/reader/reader_screen_event.dart';
import 'package:masiro/bloc/screen/reader/reader_screen_state.dart';
import 'package:masiro/bloc/util/event_transformer.dart';
import 'package:masiro/data/repository/app_configuration_repository.dart';
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';
Expand All @@ -15,6 +16,7 @@ import 'package:masiro/di/get_it.dart';
class ReaderScreenBloc extends Bloc<ReaderScreenEvent, ReaderScreenState> {
final masiroRepository = getIt<MasiroRepository>();
final novelRecordRepository = getIt<NovelRecordRepository>();
final appConfigurationRepository = getIt<AppConfigurationRepository>();

final int novelId;

Expand All @@ -28,6 +30,7 @@ class ReaderScreenBloc extends Bloc<ReaderScreenEvent, ReaderScreenState> {
transformer: debounce(const Duration(milliseconds: 500)),
);
on<ReaderScreenChapterNavigated>(_onReaderScreenChapterNavigated);
on<ReaderScreenFontSizeChanged>(_onReaderScreenFontSizeChanged);
}

Future<void> _onRequestReaderScreenChapterDetail(
Expand All @@ -44,10 +47,12 @@ class ReaderScreenBloc extends Bloc<ReaderScreenEvent, ReaderScreenState> {
chapterId,
ReadingMode.scroll,
);
final appConfig = await appConfigurationRepository.getAppConfiguration();
emit(
ReaderScreenLoadedState(
chapterDetail: chapterDetail,
position: chapterRecord?.position ?? startPosition,
fontSize: appConfig.fontSize,
),
);
} catch (e) {
Expand Down Expand Up @@ -104,6 +109,20 @@ class ReaderScreenBloc extends Bloc<ReaderScreenEvent, ReaderScreenState> {
add(ReaderScreenChapterDetailRequested(chapterId: event.chapterId));
}

Future<void> _onReaderScreenFontSizeChanged(
ReaderScreenFontSizeChanged event,
Emitter<ReaderScreenState> emit,
) async {
if (state is! ReaderScreenLoadedState) {
return;
}
final appConfig = await appConfigurationRepository.getAppConfiguration();
final nextAppConfig = appConfig.copyWith(fontSize: event.fontSize);
await appConfigurationRepository.putAppConfiguration(nextAppConfig);
final loadedState = state as ReaderScreenLoadedState;
emit(loadedState.copyWith(fontSize: event.fontSize));
}

Future<String> purchasePaidChapter(PaymentInfo paymentInfo) async {
if (state is! ReaderScreenLoadedState) {
throw Exception(
Expand Down
9 changes: 9 additions & 0 deletions lib/bloc/screen/reader/reader_screen_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,12 @@ final class ReaderScreenChapterNavigated extends ReaderScreenEvent {
@override
List<Object> get props => [chapterId];
}

final class ReaderScreenFontSizeChanged extends ReaderScreenEvent {
final int fontSize;

ReaderScreenFontSizeChanged({required this.fontSize});

@override
List<Object> get props => [fontSize];
}
5 changes: 5 additions & 0 deletions lib/bloc/screen/reader/reader_screen_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ class ReaderScreenLoadedState extends ReaderScreenState {
final ReadingMode readingMode;
final ReadPosition position;
final LoadingStatus loadingStatus;
final int fontSize;

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

ReaderScreenLoadedState copyWith({
Expand All @@ -41,13 +43,15 @@ class ReaderScreenLoadedState extends ReaderScreenState {
ReadingMode? readingMode,
ReadPosition? position,
LoadingStatus? loadingStatus,
int? fontSize,
}) {
return ReaderScreenLoadedState(
chapterDetail: chapterDetail ?? this.chapterDetail,
isHudVisible: isHudVisible ?? this.isHudVisible,
readingMode: readingMode ?? this.readingMode,
position: position ?? this.position,
loadingStatus: loadingStatus ?? this.loadingStatus,
fontSize: fontSize ?? this.fontSize,
);
}

Expand All @@ -58,5 +62,6 @@ class ReaderScreenLoadedState extends ReaderScreenState {
readingMode,
position,
loadingStatus,
fontSize,
];
}
5 changes: 5 additions & 0 deletions lib/data/database/entity/app_configuration_entity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ class AppConfigurationEntity {

int themeColor;

int fontSize;

AppConfigurationEntity({
this.id = Isar.autoIncrement,
required this.dbVersion,
this.lastSignInTime = 0,
this.themeMode = ThemeMode.system,
this.themeColor = defaultThemeColor,
this.fontSize = defaultFontSize,
});

AppConfigurationEntity copyWith({
Expand All @@ -31,13 +34,15 @@ class AppConfigurationEntity {
int? lastSignInTime,
ThemeMode? themeMode,
int? themeColor,
int? fontSize,
}) {
return AppConfigurationEntity(
id: id ?? this.id,
dbVersion: dbVersion ?? this.dbVersion,
lastSignInTime: lastSignInTime ?? this.lastSignInTime,
themeMode: themeMode ?? this.themeMode,
themeColor: themeColor ?? this.themeColor,
fontSize: fontSize ?? this.fontSize,
);
}
}
129 changes: 118 additions & 11 deletions lib/data/database/entity/app_configuration_entity.g.dart

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

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ AppConfiguration appConfigurationEntityToModel(AppConfigurationEntity entity) {
lastSignInTime: entity.lastSignInTime,
themeMode: entity.themeMode,
themeColor: entity.themeColor,
fontSize: entity.fontSize,
);
}

Expand All @@ -18,5 +19,6 @@ AppConfigurationEntity appConfigurationToEntity(AppConfiguration config) {
lastSignInTime: config.lastSignInTime,
themeMode: config.themeMode,
themeColor: config.themeColor,
fontSize: config.fontSize,
);
}
Loading

0 comments on commit b82b9b2

Please sign in to comment.