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

Fix version selector #3

Merged
merged 1 commit into from
Apr 30, 2024
Merged
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
43 changes: 21 additions & 22 deletions lib/blocs/balamod_details/cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,32 @@ import 'dart:io';
import 'package:balamod/models/balatro.dart';
import 'package:balamod/services/installer.dart';
import 'package:equatable/equatable.dart';
import 'package:hydrated_bloc/hydrated_bloc.dart';
import 'package:bloc/bloc.dart';
import 'package:flutter/material.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:github/github.dart' show Release, GitHub, RepositorySlug;
import 'package:path_provider/path_provider.dart';

part 'state.dart';
part 'cubit.g.dart';

class BalamodDetailsCubit extends HydratedCubit<BalamodDetailsState> {
class BalamodDetailsCubit extends Cubit<BalamodDetailsState> {
BalamodDetailsCubit() : super(BalamodDetailsState.initial());

void loadState(Balatro balatro) async {
emit(state.copyWith(status: Status.loading, balatro: balatro));
final balamodReleases = await GitHub()
.repositories
.listReleases(RepositorySlug('balamod', 'balamod_lua'))
.toList();

final eventLogStreamController = StreamController<String>();
final progressStreamController = StreamController<double>();
final scrollController = ScrollController();
eventLogStreamController.stream.listen((event) {
emit(state.copyWith(eventLogs: [...state.eventLogs, event]));
scrollController.jumpTo(scrollController.position.maxScrollExtent);
emit(state.copyWith(scrollController: scrollController));
});
progressStreamController.stream.listen((event) {
emit(state.copyWith(progress: event));
Expand All @@ -37,37 +42,41 @@ class BalamodDetailsCubit extends HydratedCubit<BalamodDetailsState> {
eventLogStreamController: eventLogStreamController,
progressStreamController: progressStreamController,
decompileDirectory: await getApplicationDocumentsDirectory(),
selectedRelease: balamodReleases.first,
scrollController: scrollController,
));
}

Future<void> install() async {
if (state.status == Status.loading) return;
if (!state.isLoaded) return;
final installer = Installer(balatro: state.balatro!);
await installer.install(
state.selectedRelease, state.eventLogStreamController!);
state.selectedRelease!.tagName!,
state.eventLogStreamController!,
);
}

Future<void> uninstall() async {
if (state.status == Status.loading) return;
if (!state.isLoaded) return;
final installer = Installer(balatro: state.balatro!);
await installer.uninstall(
state.selectedRelease, state.eventLogStreamController!);
state.selectedRelease!.tagName!,
state.eventLogStreamController!,
);
}

void selectRelease(Release? release) {
if (release == null) {
emit(state.copyWith(selectedRelease: 'latest'));
return;
}
emit(state.copyWith(selectedRelease: release.tagName));
if (release == null) return;
emit(state.copyWith(selectedRelease: release));
}

void decompile() async {
if (state.status == Status.loading) return;
if (state.decompileDirectory == null) return;
final installer = Installer(balatro: state.balatro!);
final targetDir = Directory(
'${state.decompileDirectory!.path}/balatro-${state.balatro!.version}');
'${state.decompileDirectory!.path}/balatro-${state.balatro!.version}',
);
await installer.decompile(targetDir, state.eventLogStreamController!);
}

Expand All @@ -78,14 +87,4 @@ class BalamodDetailsCubit extends HydratedCubit<BalamodDetailsState> {
void resetLogs() {
emit(state.copyWith(eventLogs: [], progress: 0.0));
}

@override
BalamodDetailsState fromJson(Map<String, dynamic> json) {
return BalamodDetailsState.fromJson(json);
}

@override
Map<String, dynamic> toJson(BalamodDetailsState state) {
return state.toJson();
}
}
6 changes: 4 additions & 2 deletions lib/blocs/balamod_details/cubit.g.dart

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

16 changes: 11 additions & 5 deletions lib/blocs/balamod_details/state.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
part of 'cubit.dart';

enum Status { loading, ready, error }
enum Status { initial, loading, ready, error }

Directory? _dirFromJson(Map<String, dynamic>? json) => json?['path'] == null ? null : Directory(json!['path'] as String);

Expand All @@ -15,36 +15,41 @@ class BalamodDetailsState extends Equatable {
final StreamController<String>? eventLogStreamController;
@JsonKey(includeFromJson: false, includeToJson: false)
final StreamController<double>? progressStreamController;
final String selectedRelease;
final Release? selectedRelease;
final List<String> eventLogs;
@JsonKey(fromJson: _dirFromJson, toJson: _dirToJson)
final Directory? decompileDirectory;
final double progress;
final ScrollController? scrollController;

const BalamodDetailsState({
this.balatro,
this.releases = const [],
this.status = Status.loading,
this.status = Status.initial,
this.eventLogStreamController,
this.progressStreamController,
this.selectedRelease = 'latest',
this.selectedRelease,
this.eventLogs = const [],
this.decompileDirectory,
this.progress = 0.0,
this.scrollController,
});

bool get isLoaded => !(status == Status.initial || status == Status.loading);

factory BalamodDetailsState.initial() => const BalamodDetailsState();

BalamodDetailsState copyWith({
Balatro? balatro,
Status? status,
List<Release>? releases,
String? selectedRelease,
Release? selectedRelease,
StreamController<String>? eventLogStreamController,
StreamController<double>? progressStreamController,
List<String>? eventLogs,
Directory? decompileDirectory,
double? progress,
ScrollController? scrollController,
}) {
return BalamodDetailsState(
balatro: balatro ?? this.balatro,
Expand All @@ -57,6 +62,7 @@ class BalamodDetailsState extends Equatable {
eventLogs: eventLogs ?? this.eventLogs,
decompileDirectory: decompileDirectory ?? this.decompileDirectory,
progress: progress ?? this.progress,
scrollController: scrollController ?? this.scrollController,
);
}

Expand Down
61 changes: 24 additions & 37 deletions lib/pages/balatro.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import 'package:balamod/models/balatro.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:github/github.dart' show Release;
import 'package:go_router/go_router.dart';
import 'package:google_fonts/google_fonts.dart';

class BalatroPage extends StatefulWidget {
class BalatroPage extends StatelessWidget {
final String path;
final String version;
final String balamodVersion;
Expand All @@ -22,23 +23,6 @@ class BalatroPage extends StatefulWidget {
required this.executable,
});

@override
State<BalatroPage> createState() => _BalatroPageState();
}

class _BalatroPageState extends State<BalatroPage> {
@override
void didChangeDependencies() {
super.didChangeDependencies();
final cubit = context.read<BalamodDetailsCubit>();
cubit.loadState(Balatro(
path: widget.path,
executable: widget.executable,
version: widget.version,
balamodVersion: widget.balamodVersion,
));
}

Widget _buildPage(BuildContext context, BalamodDetailsState state) {
final cubit = context.read<BalamodDetailsCubit>();
return Scaffold(
Expand All @@ -49,7 +33,7 @@ class _BalatroPageState extends State<BalatroPage> {
},
icon: const Icon(Icons.arrow_back),
),
title: Text('Balatro ${widget.version}'),
title: Text('Balatro $version'),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.start,
Expand All @@ -65,25 +49,19 @@ class _BalatroPageState extends State<BalatroPage> {
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: DropdownButton(
items: [
const DropdownMenuItem(
value: null,
child: Text('latest'),
),
...state.releases.map(
(release) {
return DropdownMenuItem(
value: release,
child: Text(release.tagName ?? 'latest'),
);
},
)
],
value: null,
child: DropdownButton<Release>(
items: state.releases.map(
(release) {
return DropdownMenuItem(
value: release,
child: Text(release.tagName!),
);
},
).toList(),
value: state.selectedRelease,
hint: Padding(
padding: const EdgeInsets.symmetric(horizontal: 2.0),
child: Text(state.selectedRelease),
child: Text(state.selectedRelease!.tagName!),
),
onChanged: (release) => cubit.selectRelease(release),
),
Expand Down Expand Up @@ -131,6 +109,7 @@ class _BalatroPageState extends State<BalatroPage> {
child: Padding(
padding: const EdgeInsets.all(8.0),
child: ListView.builder(
controller: state.scrollController!,
itemCount: state.eventLogs.length,
itemBuilder: (ctx, index) {
return ListTile(
Expand All @@ -154,7 +133,15 @@ class _BalatroPageState extends State<BalatroPage> {
return BlocConsumer<BalamodDetailsCubit, BalamodDetailsState>(
listener: (context, state) {},
builder: (context, state) {
if (state.status == Status.loading) {
if (!state.isLoaded) {
if (state.status == Status.initial) {
context.read<BalamodDetailsCubit>().loadState(Balatro(
path: path,
executable: executable,
version: version,
balamodVersion: balamodVersion,
),);
}
return const Center(
child: CircularProgressIndicator(),
);
Expand Down
10 changes: 9 additions & 1 deletion lib/pages/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,15 @@ class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return BlocConsumer<BalamodCubit, BalamodState>(
listener: (context, state) {},
listener: (context, state) {
if (state.status == BalamodStatus.ready) {
if (state.balamods.singleOrNull != null) {
context.go(
'/balatro?path=${state.balamods.single.path}&version=${state.balamods.single.version}&balamodVersion=${state.balamods.single.balamodVersion}&executable=${state.balamods.single.executable}',
);
}
}
},
builder: (context, state) {
if (state.status == BalamodStatus.loading) {
return const Center(
Expand Down