diff --git a/lib/main.dart b/lib/main.dart index c82393f..9b0410a 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -66,6 +66,9 @@ void main(List 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'); diff --git a/lib/other/backend.dart b/lib/other/backend.dart index 5d6a41d..4514a2d 100644 --- a/lib/other/backend.dart +++ b/lib/other/backend.dart @@ -392,9 +392,6 @@ Future getMod( thumbnailUrl: screenshots, id: resJSON["id"].toString(), setAreParentButtonsActive: setAreParentButtonsActive, - getAreParentButtonsActive: () { - return true; - }, slug: resJSON["slug"].toString(), rawMod: resJSON, modClass: modClass, @@ -484,9 +481,6 @@ Future getMod( slug: resJSON["slug"], modClass: modClass, downloadable: downloadable, - getAreParentButtonsActive: () { - return true; - }, preVersion: preVersion, showPreVersion: versionShow, versionTarget: versionTarget, @@ -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 vrs = json.decode((await http.get( - uri, - headers: { - "User-Agent": await generateUserAgent(), - }, - )) - .body); - List versions = []; - for (var v in vrs) { - if (v["version_type"] == "release") { - versions.add(v["version"].toString()); - } - } - List versionItems = []; - List modpackItems = []; - for (var version in versions) { - versionItems.add( - DropdownMenuEntry(label: version.toString(), value: version), - ); - } + List versionItems = await getVersionsEntries(); + List modpackItems = []; List modpacks = getModpacks(hideFree: false); @@ -758,25 +728,41 @@ Future getMachineIdAndOs() async { machineId: base64Url.encode(utf8.encode(machineIdUnencoded)), os: os); } -Future> getVersions() async { - List items = []; - Uri uri = Uri.parse( - 'https://api.modrinth.com/v2/tag/game_version', - ); - List vrs = json.decode((await http.get( - uri, - headers: { - "User-Agent": await generateUserAgent(), - }, - )) - .body); +Future> getVersions({bool onInit = false}) async { List versions = []; - for (var v in vrs) { - if (v["version_type"] == "release") { - versions.add(v["version"].toString()); + + if (onInit) { + List 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> getVersionsEntries() async { + List items = []; + List versions = await getVersions(); for (var version in versions) { items.add( DropdownMenuEntry(label: version.toString(), value: version), diff --git a/lib/pages/modpack_creator/modpack_creator.dart b/lib/pages/modpack_creator/modpack_creator.dart index 2669d2e..8ce90a4 100644 --- a/lib/pages/modpack_creator/modpack_creator.dart +++ b/lib/pages/modpack_creator/modpack_creator.dart @@ -50,7 +50,7 @@ class _ModpackCreatorState extends State { @override Widget build(BuildContext context) { - getVersions(); + getVersionsEntries(); return Scaffold( appBar: DraggableAppBar( appBar: AppBar( @@ -83,7 +83,7 @@ class _ModpackCreatorState extends State { Container( margin: const EdgeInsets.symmetric(vertical: 12), child: FutureBuilder( - future: getVersions(), + future: getVersionsEntries(), builder: ((BuildContext context, snapshot) { if (snapshot.hasError) { return DropdownMenu( diff --git a/lib/pages/web/filter_mods.dart b/lib/pages/web/filter_mods.dart index 1e106da..d47f1bc 100644 --- a/lib/pages/web/filter_mods.dart +++ b/lib/pages/web/filter_mods.dart @@ -86,7 +86,7 @@ class _FilterModsState extends State { Container( margin: const EdgeInsets.symmetric(vertical: 12), child: FutureBuilder( - future: getVersions(), + future: getVersionsEntries(), builder: ((BuildContext context, snapshot) { if (snapshot.hasError) { return DropdownMenu( diff --git a/lib/pages/web/mod/loading_mod.dart b/lib/pages/web/mod/loading_mod.dart index 744cbb0..4c5592a 100644 --- a/lib/pages/web/mod/loading_mod.dart +++ b/lib/pages/web/mod/loading_mod.dart @@ -41,11 +41,7 @@ class _LoadingModState extends State { future: getMod( widget.modId, widget.source, - (val) => { - setState( - () {}, - ) - }, + widget.setAreParentButtonsActive, deletable: widget.deletable, preVersion: widget.preVersion, versionShow: widget.showPreVersion, diff --git a/lib/pages/web/mod/mod.dart b/lib/pages/web/mod/mod.dart index 2ebc45f..514490e 100644 --- a/lib/pages/web/mod/mod.dart +++ b/lib/pages/web/mod/mod.dart @@ -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'; @@ -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, @@ -85,7 +83,6 @@ class Mod extends StatefulWidget { final List thumbnailUrl; final Map rawMod; Function(bool) setAreParentButtonsActive; - bool Function() getAreParentButtonsActive; void install( BuildContext context, @@ -487,28 +484,9 @@ class _ModState extends State with AutomaticKeepAliveClientMixin { void updateModpackInfo() async { try { - Uri uri = Uri.parse( - 'https://api.modrinth.com/v2/tag/game_version', - ); - List vrs = json.decode((await http.get( - uri, - headers: { - "User-Agent": await generateUserAgent(), - }, - )) - .body); - List versions = []; - for (var v in vrs) { - if (v["version_type"] == "release") { - versions.add(v["version"].toString()); - } - } + List versions = await getVersionsEntries(); - for (var version in versions) { - versionItems.add( - DropdownMenuEntry(label: version.toString(), value: version), - ); - } + versionItems = versions; List modpacks = getModpacks(hideFree: false); @@ -720,9 +698,7 @@ class _ModState extends State 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")) { @@ -796,6 +772,7 @@ class _ModState extends State with AutomaticKeepAliveClientMixin { child: widget.deletable ? FilledButton.icon( onPressed: () async { + widget.setAreParentButtonsActive(false); try { String fileName = Uri.decodeComponent(widget.preVersion); @@ -841,7 +818,6 @@ class _ModState extends State with AutomaticKeepAliveClientMixin { context, newModConfigRaw, false); } widget.setAreParentButtonsActive(true); - RestartWidget.restartApp(context); }, icon: const Icon(Icons.delete), label: Text(AppLocalizations.of(context)!.delete), diff --git a/lib/pages/web/modpack_update_page/modpack_update_page.dart/update_modpack.dart b/lib/pages/web/modpack_update_page/modpack_update_page.dart/update_modpack.dart index d428fa7..d23f22a 100644 --- a/lib/pages/web/modpack_update_page/modpack_update_page.dart/update_modpack.dart +++ b/lib/pages/web/modpack_update_page/modpack_update_page.dart/update_modpack.dart @@ -78,7 +78,7 @@ class _UpdateModpackState extends State { Container( margin: const EdgeInsets.symmetric(vertical: 12), child: FutureBuilder( - future: getVersions(), + future: getVersionsEntries(), builder: ((BuildContext context, snapshot) { if (snapshot.hasError) { return DropdownMenu( @@ -113,7 +113,8 @@ class _UpdateModpackState extends State { 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; diff --git a/lib/pages/web/web_sources.dart b/lib/pages/web/web_sources.dart index 58ec06c..52bd462 100644 --- a/lib/pages/web/web_sources.dart +++ b/lib/pages/web/web_sources.dart @@ -84,10 +84,6 @@ class _WebSourcesPageState extends State { }); } - bool getAreButtonsActive() { - return areButtonsEnabled; - } - Future> searchMods( String query, ModClass modsClass, ModSource modSource) async { List widgets = []; @@ -146,7 +142,6 @@ class _WebSourcesPageState extends State { slug: slug, rawMod: mod, setAreParentButtonsActive: setAreButtonsEnabled, - getAreParentButtonsActive: getAreButtonsActive, downloadCount: downloadCount, source: ModSource.curseForge, modClass: modsClass, @@ -217,7 +212,6 @@ class _WebSourcesPageState extends State { slug: slug, modIconUrl: icon, setAreParentButtonsActive: setAreButtonsEnabled, - getAreParentButtonsActive: getAreButtonsActive, downloadCount: downloadCount, source: ModSource.modRinth, modClass: modsClass,