Skip to content

Commit

Permalink
Merge pull request #406 from lamarios/feature/freezed
Browse files Browse the repository at this point in the history
Freezed migration
  • Loading branch information
lamarios authored Dec 13, 2023
2 parents 35b5d9c + 827deb8 commit e3e5a05
Show file tree
Hide file tree
Showing 160 changed files with 9,950 additions and 5,776 deletions.
42 changes: 20 additions & 22 deletions lib/app/states/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,21 @@ import '../../database.dart';
import '../../globals.dart';
import '../../home/models/db/home_layout.dart';
import '../../settings/models/db/server.dart';
import '../../videos/models/db/progress.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

part 'app.g.dart';
part 'app.freezed.dart';

final log = Logger('HomeState');

class AppCubit extends Cubit<AppState> {
late final StreamSubscription intentDataStreamSubscription;
AppCubit(super.initialState) {
onReady();
}


onReady() {
state.intentDataStreamSubscription = ReceiveSharingIntent.getTextStream().listen((String value) {
intentDataStreamSubscription = ReceiveSharingIntent.getTextStream().listen((String value) {
openAppLink(value);
}, onError: (err) {
log.warning("getLinkStream error: $err");
Expand All @@ -33,18 +35,13 @@ class AppCubit extends Cubit<AppState> {
openAppLink((value ?? ''));
});

var selectedIndex = int.parse(db.getSettings(ON_OPEN)?.value ?? '0');
if (!isLoggedIn && selectedIndex > 1 || selectedIndex < 0) {
selectedIndex = 0;
}
selectIndex(selectedIndex);

service.syncHistory();
}

@override
close() async {
state.intentDataStreamSubscription.cancel();
intentDataStreamSubscription.cancel();
super.close();
}

Expand Down Expand Up @@ -86,23 +83,24 @@ class AppCubit extends Cubit<AppState> {
bool get isLoggedIn => (state.server?.authToken?.isNotEmpty ?? false) || (state.server?.sidCookie?.isNotEmpty ?? false);
}

@CopyWith(constructor: "_")
class AppState {
late int selectedIndex;

late Server? server;

late StreamSubscription intentDataStreamSubscription;

HomeLayout homeLayout = db.getHomeLayout();

AppState() {
@freezed
class AppState with _$AppState {
static AppState init(){
late Server? server;
try {
server = db.getCurrentlySelectedServer();
} catch (e) {
server = null;
}
}
HomeLayout homeLayout = db.getHomeLayout();
bool isLoggedIn = (server?.authToken?.isNotEmpty ?? false) || (server?.sidCookie?.isNotEmpty ?? false);

var selectedIndex = int.parse(db.getSettings(ON_OPEN)?.value ?? '0');
if (!isLoggedIn && selectedIndex > 1 || selectedIndex < 0) {
selectedIndex = 0;
}

AppState._(this.selectedIndex, this.server, this.intentDataStreamSubscription, this.homeLayout);
return AppState(selectedIndex, server, homeLayout);
}
factory AppState(int selectedIndex, Server? server, HomeLayout homeLayout ) = _AppState;
}
167 changes: 167 additions & 0 deletions lib/app/states/app.freezed.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
// coverage:ignore-file
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint
// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target, unnecessary_question_mark

part of 'app.dart';

// **************************************************************************
// FreezedGenerator
// **************************************************************************

T _$identity<T>(T value) => value;

final _privateConstructorUsedError = UnsupportedError(
'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#custom-getters-and-methods');

/// @nodoc
mixin _$AppState {
int get selectedIndex => throw _privateConstructorUsedError;
Server? get server => throw _privateConstructorUsedError;
HomeLayout get homeLayout => throw _privateConstructorUsedError;

@JsonKey(ignore: true)
$AppStateCopyWith<AppState> get copyWith =>
throw _privateConstructorUsedError;
}

/// @nodoc
abstract class $AppStateCopyWith<$Res> {
factory $AppStateCopyWith(AppState value, $Res Function(AppState) then) =
_$AppStateCopyWithImpl<$Res, AppState>;
@useResult
$Res call({int selectedIndex, Server? server, HomeLayout homeLayout});
}

/// @nodoc
class _$AppStateCopyWithImpl<$Res, $Val extends AppState>
implements $AppStateCopyWith<$Res> {
_$AppStateCopyWithImpl(this._value, this._then);

// ignore: unused_field
final $Val _value;
// ignore: unused_field
final $Res Function($Val) _then;

@pragma('vm:prefer-inline')
@override
$Res call({
Object? selectedIndex = null,
Object? server = freezed,
Object? homeLayout = null,
}) {
return _then(_value.copyWith(
selectedIndex: null == selectedIndex
? _value.selectedIndex
: selectedIndex // ignore: cast_nullable_to_non_nullable
as int,
server: freezed == server
? _value.server
: server // ignore: cast_nullable_to_non_nullable
as Server?,
homeLayout: null == homeLayout
? _value.homeLayout
: homeLayout // ignore: cast_nullable_to_non_nullable
as HomeLayout,
) as $Val);
}
}

/// @nodoc
abstract class _$$AppStateImplCopyWith<$Res>
implements $AppStateCopyWith<$Res> {
factory _$$AppStateImplCopyWith(
_$AppStateImpl value, $Res Function(_$AppStateImpl) then) =
__$$AppStateImplCopyWithImpl<$Res>;
@override
@useResult
$Res call({int selectedIndex, Server? server, HomeLayout homeLayout});
}

/// @nodoc
class __$$AppStateImplCopyWithImpl<$Res>
extends _$AppStateCopyWithImpl<$Res, _$AppStateImpl>
implements _$$AppStateImplCopyWith<$Res> {
__$$AppStateImplCopyWithImpl(
_$AppStateImpl _value, $Res Function(_$AppStateImpl) _then)
: super(_value, _then);

@pragma('vm:prefer-inline')
@override
$Res call({
Object? selectedIndex = null,
Object? server = freezed,
Object? homeLayout = null,
}) {
return _then(_$AppStateImpl(
null == selectedIndex
? _value.selectedIndex
: selectedIndex // ignore: cast_nullable_to_non_nullable
as int,
freezed == server
? _value.server
: server // ignore: cast_nullable_to_non_nullable
as Server?,
null == homeLayout
? _value.homeLayout
: homeLayout // ignore: cast_nullable_to_non_nullable
as HomeLayout,
));
}
}

/// @nodoc
class _$AppStateImpl implements _AppState {
_$AppStateImpl(this.selectedIndex, this.server, this.homeLayout);

@override
final int selectedIndex;
@override
final Server? server;
@override
final HomeLayout homeLayout;

@override
String toString() {
return 'AppState(selectedIndex: $selectedIndex, server: $server, homeLayout: $homeLayout)';
}

@override
bool operator ==(dynamic other) {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is _$AppStateImpl &&
(identical(other.selectedIndex, selectedIndex) ||
other.selectedIndex == selectedIndex) &&
(identical(other.server, server) || other.server == server) &&
(identical(other.homeLayout, homeLayout) ||
other.homeLayout == homeLayout));
}

@override
int get hashCode =>
Object.hash(runtimeType, selectedIndex, server, homeLayout);

@JsonKey(ignore: true)
@override
@pragma('vm:prefer-inline')
_$$AppStateImplCopyWith<_$AppStateImpl> get copyWith =>
__$$AppStateImplCopyWithImpl<_$AppStateImpl>(this, _$identity);
}

abstract class _AppState implements AppState {
factory _AppState(final int selectedIndex, final Server? server,
final HomeLayout homeLayout) = _$AppStateImpl;

@override
int get selectedIndex;
@override
Server? get server;
@override
HomeLayout get homeLayout;
@override
@JsonKey(ignore: true)
_$$AppStateImplCopyWith<_$AppStateImpl> get copyWith =>
throw _privateConstructorUsedError;
}
94 changes: 0 additions & 94 deletions lib/app/states/app.g.dart

This file was deleted.

25 changes: 11 additions & 14 deletions lib/app/states/tv_home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,30 @@ import 'package:easy_debounce/easy_debounce.dart';
import 'package:flutter/cupertino.dart';
import 'package:invidious/globals.dart';

part 'tv_home.g.dart';

class TvHomeCubit extends Cubit<TvHomeState> {
class TvHomeCubit extends Cubit<bool> {
ScrollController scrollController = ScrollController();
TvHomeCubit(super.initialState);

menuItemFocusChanged(bool focus) {
if (focus) {
EasyDebounce.cancel('expand-home-menu');
emit(state.copyWith(expandMenu: true));
emit(true);
} else {
EasyDebounce.debounce('expand-home-menu', const Duration(milliseconds: 50), () {
emit(state.copyWith(expandMenu: false));
emit(false);
});
}
}

@override
close()async {
scrollController.dispose();
super.close();
}

scrollToTop() {
state.scrollController.animateTo(0, duration: animationDuration, curve: Curves.easeInOutQuad);
scrollController.animateTo(0, duration: animationDuration, curve: Curves.easeInOutQuad);
}
}

@CopyWith(constructor: "_")
class TvHomeState {
bool expandMenu = false;
ScrollController scrollController = ScrollController();

TvHomeState();

TvHomeState._(this.expandMenu, this.scrollController);
}
Loading

0 comments on commit e3e5a05

Please sign in to comment.