Skip to content

Commit

Permalink
improve getting versions, current modpack page
Browse files Browse the repository at this point in the history
  • Loading branch information
mrquantumoff committed Jul 18, 2024
1 parent e47d44c commit 69cafe1
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 91 deletions.
3 changes: 3 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ void main(List<String> args) async {
);
await trayManager.setContextMenu(menu);

// Initialize all the versions
await getVersions(onInit: true);

// The protocol handler package only works on Windows/MacOS. Linux has a stupider implementation of protocol handling
if (Platform.isWindows || Platform.isMacOS) {
await protocolHandler.register('curseforge');
Expand Down
80 changes: 33 additions & 47 deletions lib/other/backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,6 @@ Future<Mod> getMod(
thumbnailUrl: screenshots,
id: resJSON["id"].toString(),
setAreParentButtonsActive: setAreParentButtonsActive,
getAreParentButtonsActive: () {
return true;
},
slug: resJSON["slug"].toString(),
rawMod: resJSON,
modClass: modClass,
Expand Down Expand Up @@ -484,9 +481,6 @@ Future<Mod> getMod(
slug: resJSON["slug"],
modClass: modClass,
downloadable: downloadable,
getAreParentButtonsActive: () {
return true;
},
preVersion: preVersion,
showPreVersion: versionShow,
versionTarget: versionTarget,
Expand Down Expand Up @@ -655,34 +649,10 @@ void installModByProtocol(int modId, int fileId, Function() fail) async {
rawMod: mod,
modClass: modClass,
thumbnailUrl: screenshots,
getAreParentButtonsActive: () {
return true;
},
);
Uri uri = Uri.parse(
'https://api.modrinth.com/v2/tag/game_version',
);
List<dynamic> vrs = json.decode((await http.get(
uri,
headers: {
"User-Agent": await generateUserAgent(),
},
))
.body);
List<String> versions = [];
for (var v in vrs) {
if (v["version_type"] == "release") {
versions.add(v["version"].toString());
}
}
List<DropdownMenuEntry> versionItems = [];
List<DropdownMenuEntry> modpackItems = [];

for (var version in versions) {
versionItems.add(
DropdownMenuEntry(label: version.toString(), value: version),
);
}
List<DropdownMenuEntry> versionItems = await getVersionsEntries();
List<DropdownMenuEntry> modpackItems = [];

List<String> modpacks = getModpacks(hideFree: false);

Expand Down Expand Up @@ -758,25 +728,41 @@ Future<MachineIdAndOS> getMachineIdAndOs() async {
machineId: base64Url.encode(utf8.encode(machineIdUnencoded)), os: os);
}

Future<List<DropdownMenuEntry>> getVersions() async {
List<DropdownMenuEntry> items = [];
Uri uri = Uri.parse(
'https://api.modrinth.com/v2/tag/game_version',
);
List<dynamic> vrs = json.decode((await http.get(
uri,
headers: {
"User-Agent": await generateUserAgent(),
},
))
.body);
Future<List<String>> getVersions({bool onInit = false}) async {
List<String> versions = [];
for (var v in vrs) {
if (v["version_type"] == "release") {
versions.add(v["version"].toString());

if (onInit) {
List<String> newVersions = [];

Uri uri = Uri.parse(
'https://api.modrinth.com/v2/tag/game_version',
);
http.Response res = await http.get(
uri,
headers: {
"User-Agent": await generateUserAgent(),
},
);
debugPrint("Minecraft versions: ${res.body}");
dynamic vrs = json.decode(res.body);

for (var v in vrs) {
if (v["version_type"] == "release") {
newVersions.add(v["version"].toString());
}
}
versions = newVersions;
GetStorage().write("mcVersions", newVersions);
}

versions = GetStorage().read("mcVersions") ?? versions;

return versions;
}

Future<List<DropdownMenuEntry>> getVersionsEntries() async {
List<DropdownMenuEntry> items = [];
List<String> versions = await getVersions();
for (var version in versions) {
items.add(
DropdownMenuEntry(label: version.toString(), value: version),
Expand Down
4 changes: 2 additions & 2 deletions lib/pages/modpack_creator/modpack_creator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class _ModpackCreatorState extends State<ModpackCreator> {

@override
Widget build(BuildContext context) {
getVersions();
getVersionsEntries();
return Scaffold(
appBar: DraggableAppBar(
appBar: AppBar(
Expand Down Expand Up @@ -83,7 +83,7 @@ class _ModpackCreatorState extends State<ModpackCreator> {
Container(
margin: const EdgeInsets.symmetric(vertical: 12),
child: FutureBuilder(
future: getVersions(),
future: getVersionsEntries(),
builder: ((BuildContext context, snapshot) {
if (snapshot.hasError) {
return DropdownMenu(
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/web/filter_mods.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class _FilterModsState extends State<FilterMods> {
Container(
margin: const EdgeInsets.symmetric(vertical: 12),
child: FutureBuilder(
future: getVersions(),
future: getVersionsEntries(),
builder: ((BuildContext context, snapshot) {
if (snapshot.hasError) {
return DropdownMenu(
Expand Down
6 changes: 1 addition & 5 deletions lib/pages/web/mod/loading_mod.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ class _LoadingModState extends State<LoadingMod> {
future: getMod(
widget.modId,
widget.source,
(val) => {
setState(
() {},
)
},
widget.setAreParentButtonsActive,
deletable: widget.deletable,
preVersion: widget.preVersion,
versionShow: widget.showPreVersion,
Expand Down
32 changes: 4 additions & 28 deletions lib/pages/web/mod/mod.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import 'package:get_storage_qnt/get_storage.dart';
import 'package:http/http.dart' as http;
import 'package:intl/intl.dart';
import 'package:quadrant/other/backend.dart';
import 'package:quadrant/other/restart_app.dart';
import 'package:quadrant/pages/web/generate_user_agent.dart';
import 'package:quadrant/pages/web/mod/install_mod_page.dart';
import 'package:quadrant/pages/web/web_sources.dart';
Expand Down Expand Up @@ -50,7 +49,6 @@ class Mod extends StatefulWidget {
required this.id,
required this.downloadCount,
required this.setAreParentButtonsActive,
required this.getAreParentButtonsActive,
required this.source,
required this.modClass,
required this.slug,
Expand Down Expand Up @@ -85,7 +83,6 @@ class Mod extends StatefulWidget {
final List<String> thumbnailUrl;
final Map rawMod;
Function(bool) setAreParentButtonsActive;
bool Function() getAreParentButtonsActive;

void install(
BuildContext context,
Expand Down Expand Up @@ -487,28 +484,9 @@ class _ModState extends State<Mod> with AutomaticKeepAliveClientMixin {

void updateModpackInfo() async {
try {
Uri uri = Uri.parse(
'https://api.modrinth.com/v2/tag/game_version',
);
List<dynamic> vrs = json.decode((await http.get(
uri,
headers: {
"User-Agent": await generateUserAgent(),
},
))
.body);
List<String> versions = [];
for (var v in vrs) {
if (v["version_type"] == "release") {
versions.add(v["version"].toString());
}
}
List<DropdownMenuEntry> versions = await getVersionsEntries();

for (var version in versions) {
versionItems.add(
DropdownMenuEntry(label: version.toString(), value: version),
);
}
versionItems = versions;

List<String> modpacks = getModpacks(hideFree: false);

Expand Down Expand Up @@ -720,9 +698,7 @@ class _ModState extends State<Mod> with AutomaticKeepAliveClientMixin {
Container(
margin:
const EdgeInsets.only(top: 8, bottom: 8, right: 8),
child: widget.downloadable &&
!widget.autoInstall &&
widget.getAreParentButtonsActive()
child: widget.downloadable && !widget.autoInstall
? FilledButton.icon(
onPressed: () {
if (GetStorage().read("experimentalFeatures")) {
Expand Down Expand Up @@ -796,6 +772,7 @@ class _ModState extends State<Mod> with AutomaticKeepAliveClientMixin {
child: widget.deletable
? FilledButton.icon(
onPressed: () async {
widget.setAreParentButtonsActive(false);
try {
String fileName =
Uri.decodeComponent(widget.preVersion);
Expand Down Expand Up @@ -841,7 +818,6 @@ class _ModState extends State<Mod> with AutomaticKeepAliveClientMixin {
context, newModConfigRaw, false);
}
widget.setAreParentButtonsActive(true);
RestartWidget.restartApp(context);
},
icon: const Icon(Icons.delete),
label: Text(AppLocalizations.of(context)!.delete),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class _UpdateModpackState extends State<UpdateModpack> {
Container(
margin: const EdgeInsets.symmetric(vertical: 12),
child: FutureBuilder(
future: getVersions(),
future: getVersionsEntries(),
builder: ((BuildContext context, snapshot) {
if (snapshot.hasError) {
return DropdownMenu(
Expand Down Expand Up @@ -113,7 +113,8 @@ class _UpdateModpackState extends State<UpdateModpack> {
label: Text(AppLocalizations.of(context)!.chooseModpack),
width: 840,
onSelected: (dynamic newValue) async {
String latestVersion = (await getVersions())[0].value;
String latestVersion =
(await getVersionsEntries())[0].value;
setState(() {
versionFieldController.text = latestVersion;
apiFieldEnabled = false;
Expand Down
6 changes: 0 additions & 6 deletions lib/pages/web/web_sources.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@ class _WebSourcesPageState extends State<WebSourcesPage> {
});
}

bool getAreButtonsActive() {
return areButtonsEnabled;
}

Future<List<Mod>> searchMods(
String query, ModClass modsClass, ModSource modSource) async {
List<Mod> widgets = [];
Expand Down Expand Up @@ -146,7 +142,6 @@ class _WebSourcesPageState extends State<WebSourcesPage> {
slug: slug,
rawMod: mod,
setAreParentButtonsActive: setAreButtonsEnabled,
getAreParentButtonsActive: getAreButtonsActive,
downloadCount: downloadCount,
source: ModSource.curseForge,
modClass: modsClass,
Expand Down Expand Up @@ -217,7 +212,6 @@ class _WebSourcesPageState extends State<WebSourcesPage> {
slug: slug,
modIconUrl: icon,
setAreParentButtonsActive: setAreButtonsEnabled,
getAreParentButtonsActive: getAreButtonsActive,
downloadCount: downloadCount,
source: ModSource.modRinth,
modClass: modsClass,
Expand Down

0 comments on commit 69cafe1

Please sign in to comment.