mirror of
https://github.com/AngelAuraMC/Amethyst-Android.git
synced 2025-09-15 07:39:00 -04:00
Refactor: Make the interface filters easier to extend
This commit is contained in:
parent
1c21646165
commit
6299a9a4e4
@ -3,11 +3,9 @@ package net.kdt.pojavlaunch.fragments;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
@ -22,6 +20,7 @@ import net.kdt.pojavlaunch.modloaders.modpacks.ModItemAdapter;
|
||||
import net.kdt.pojavlaunch.modloaders.modpacks.api.ModpackApi;
|
||||
import net.kdt.pojavlaunch.modloaders.modpacks.api.ModrinthApi;
|
||||
import net.kdt.pojavlaunch.modloaders.modpacks.models.ModItem;
|
||||
import net.kdt.pojavlaunch.modloaders.modpacks.models.SearchFilters;
|
||||
import net.kdt.pojavlaunch.profiles.VersionSelectorDialog;
|
||||
|
||||
import java.util.Arrays;
|
||||
@ -37,11 +36,15 @@ public class SearchModFragment extends Fragment {
|
||||
|
||||
private ModpackApi modpackApi;
|
||||
|
||||
private SearchFilters mSearchFilters;
|
||||
|
||||
|
||||
public SearchModFragment(){
|
||||
super(R.layout.fragment_mod_search);
|
||||
modpackApi = new ModrinthApi();
|
||||
mModItemAdapter = new ModItemAdapter(modpackApi);
|
||||
mSearchFilters = new SearchFilters();
|
||||
mSearchFilters.isModpack = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -54,18 +57,18 @@ public class SearchModFragment extends Fragment {
|
||||
mRecyclerview.setLayoutManager(new LinearLayoutManager(getContext()));
|
||||
mRecyclerview.setAdapter(mModItemAdapter);
|
||||
|
||||
|
||||
mSelectedVersion.setText("1.12.2");
|
||||
// Setup the expendable list behavior
|
||||
mSelectVersionButton.setOnClickListener(v -> VersionSelectorDialog.open(v.getContext(), true, (id, snapshot)->{
|
||||
mSelectedVersion.setText(id);
|
||||
mSearchFilters.mcVersion = id;
|
||||
}));
|
||||
|
||||
mSearchEditText.setOnEditorActionListener((v, actionId, event) -> {
|
||||
PojavApplication.sExecutorService.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
ModItem[] items = modpackApi.searchMod(true, mSelectedVersion.getText().toString(), mSearchEditText.getText().toString());
|
||||
mSearchFilters.name = mSearchEditText.getText().toString();
|
||||
ModItem[] items = modpackApi.searchMod(mSearchFilters);
|
||||
Log.d(SearchModFragment.class.toString(), Arrays.toString(items));
|
||||
Tools.runOnUiThread(() -> mModItemAdapter.setModItems(items, mSelectedVersion.getText().toString()));
|
||||
}
|
||||
|
@ -55,8 +55,7 @@ public class ModItemAdapter extends RecyclerView.Adapter<ModItemAdapter.ViewHold
|
||||
public void onClick(View v) {
|
||||
mModpackApi.handleInstallation(
|
||||
mModDetail,
|
||||
mModDetail.versionUrls[mExtendedSpinner.getSelectedItemPosition()],
|
||||
mTargetMcVersion);
|
||||
mExtendedSpinner.getSelectedItemPosition());
|
||||
|
||||
//TODO do something !
|
||||
}
|
||||
@ -68,7 +67,7 @@ public class ModItemAdapter extends RecyclerView.Adapter<ModItemAdapter.ViewHold
|
||||
|
||||
if(isExtended() && mModDetail == null) {
|
||||
PojavApplication.sExecutorService.execute(() -> {
|
||||
mModDetail = mModpackApi.getModDetails(mModItem, mTargetMcVersion);
|
||||
mModDetail = mModpackApi.getModDetails(mModItem);
|
||||
System.out.println(mModDetail);
|
||||
Tools.runOnUiThread(() -> setStateDetailed(mModDetail));
|
||||
});
|
||||
|
@ -4,38 +4,37 @@ package net.kdt.pojavlaunch.modloaders.modpacks.api;
|
||||
import net.kdt.pojavlaunch.PojavApplication;
|
||||
import net.kdt.pojavlaunch.modloaders.modpacks.models.ModDetail;
|
||||
import net.kdt.pojavlaunch.modloaders.modpacks.models.ModItem;
|
||||
import net.kdt.pojavlaunch.modloaders.modpacks.models.SearchFilters;
|
||||
import net.kdt.pojavlaunch.progresskeeper.ProgressKeeper;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public interface ModpackApi {
|
||||
|
||||
/**
|
||||
* @param searchModpack Whether we are searching mod or modpacks
|
||||
* @param minecraftVersion The minecraft version we want
|
||||
* @param name The name of the mod(pack)
|
||||
* @param searchFilters Filters
|
||||
* @return A list of mod items
|
||||
*/
|
||||
ModItem[] searchMod(boolean searchModpack, String minecraftVersion, String name);
|
||||
ModItem[] searchMod(SearchFilters searchFilters);
|
||||
|
||||
/**
|
||||
* Fetch the mod details
|
||||
* @param item The moditem that was selected
|
||||
* @param targetMcVersion The desired version
|
||||
* @return Detailed data about a mod(pack)
|
||||
*/
|
||||
ModDetail getModDetails(ModItem item, String targetMcVersion);
|
||||
ModDetail getModDetails(ModItem item);
|
||||
|
||||
/**
|
||||
* Download and install the mod(pack)
|
||||
* @param modDetail The mod detail data
|
||||
* @param versionUrl The version that has been selected by the user
|
||||
* @param mcVersion The selected mc version
|
||||
* @param selectedVersion The selected version
|
||||
*/
|
||||
default void handleInstallation(ModDetail modDetail, String versionUrl, String mcVersion) {
|
||||
default void handleInstallation(ModDetail modDetail, int selectedVersion) {
|
||||
PojavApplication.sExecutorService.execute(() -> {
|
||||
installMod(modDetail, versionUrl, mcVersion);
|
||||
installMod(modDetail, selectedVersion);
|
||||
});
|
||||
}
|
||||
|
||||
@ -44,8 +43,7 @@ public interface ModpackApi {
|
||||
* May require the download of additional files.
|
||||
* May requires launching the installation of a modloader
|
||||
* @param modDetail The mod detail data
|
||||
* @param versionUrl The version that has been selected by the user
|
||||
* @param mcVersion The selected mc version
|
||||
* @param selectedVersion The selected version
|
||||
*/
|
||||
void installMod(ModDetail modDetail, String versionUrl, String mcVersion);
|
||||
void installMod(ModDetail modDetail, int selectedVersion);
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ import net.kdt.pojavlaunch.modloaders.modpacks.models.ModDetail;
|
||||
import net.kdt.pojavlaunch.modloaders.modpacks.models.ModItem;
|
||||
import net.kdt.pojavlaunch.modloaders.modpacks.models.ModrinthIndex;
|
||||
import net.kdt.pojavlaunch.modloaders.modpacks.models.Constants;
|
||||
import net.kdt.pojavlaunch.modloaders.modpacks.models.SearchFilters;
|
||||
import net.kdt.pojavlaunch.utils.DownloadUtils;
|
||||
import net.kdt.pojavlaunch.utils.FileUtils;
|
||||
import net.kdt.pojavlaunch.value.launcherprofiles.LauncherProfiles;
|
||||
@ -27,14 +28,18 @@ public class ModrinthApi implements ModpackApi {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ModItem[] searchMod(boolean searchModpack, String minecraftVersion, String name) {
|
||||
public ModItem[] searchMod(SearchFilters searchFilters) {
|
||||
HashMap<String, Object> params = new HashMap<>();
|
||||
|
||||
params.put("facets", String.format("[[\"project_type:%s\"],[\"versions:%s\"]]",
|
||||
searchModpack ? "modpack" : "mod",
|
||||
minecraftVersion
|
||||
));
|
||||
params.put("query", name);
|
||||
// Build the facets filters
|
||||
StringBuilder facetString = new StringBuilder();
|
||||
facetString.append("[");
|
||||
facetString.append(String.format("[\"project_type:%s\"]", searchFilters.isModpack ? "modpack" : "mod"));
|
||||
if(searchFilters.mcVersion != null && !searchFilters.mcVersion.isEmpty())
|
||||
facetString.append(String.format(",[\"versions:%s\"]", searchFilters.mcVersion));
|
||||
facetString.append("]");
|
||||
params.put("facets", facetString.toString());
|
||||
params.put("query", searchFilters.name);
|
||||
|
||||
JsonObject response = mApiHandler.get("search", params, JsonObject.class);
|
||||
JsonArray responseHits = response.getAsJsonArray("hits");
|
||||
@ -56,26 +61,31 @@ public class ModrinthApi implements ModpackApi {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ModDetail getModDetails(ModItem item, String targetMcVersion) {
|
||||
HashMap<String, Object> queryParams = new HashMap<>();
|
||||
queryParams.put("game_versions", String.format("[\"%s\"]", targetMcVersion));
|
||||
JsonArray response = mApiHandler.get(String.format("project/%s/version", item.id), queryParams, JsonArray.class);
|
||||
public ModDetail getModDetails(ModItem item) {
|
||||
|
||||
JsonArray response = mApiHandler.get(String.format("project/%s/version", item.id), JsonArray.class);
|
||||
System.out.println(response.toString());
|
||||
String[] names = new String[response.size()];
|
||||
String[] mcNames = new String[response.size()];
|
||||
String[] urls = new String[response.size()];
|
||||
|
||||
for (int i=0; i<response.size(); ++i) {
|
||||
JsonObject version = response.get(i).getAsJsonObject();
|
||||
names[i] = version.get("name").getAsString();
|
||||
mcNames[i] = version.get("game_versions").getAsJsonArray().get(0).getAsString();
|
||||
urls[i] = version.get("files").getAsJsonArray().get(0).getAsJsonObject().get("url").getAsString();
|
||||
|
||||
if (!names[i].contains(mcNames[i]))
|
||||
names[i] += " - " + mcNames[i];
|
||||
}
|
||||
|
||||
return new ModDetail(item, names, urls);
|
||||
return new ModDetail(item, names, mcNames, urls);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void installMod(ModDetail modDetail, String versionUrl, String mcVersion) {
|
||||
public void installMod(ModDetail modDetail, int selectedVersion){
|
||||
//TODO considering only modpacks for now
|
||||
String versionUrl = modDetail.versionUrls[selectedVersion];
|
||||
String modpackName = modDetail.title.toLowerCase(Locale.ROOT).trim().replace(" ", "_" );
|
||||
|
||||
// Build a new minecraft instance, folder first
|
||||
@ -133,7 +143,7 @@ public class ModrinthApi implements ModpackApi {
|
||||
profile.gameDir = "./custom_instances/" + modpackName;
|
||||
profile.name = modpackName;
|
||||
//FIXME add the proper version !
|
||||
profile.lastVersionId = mcVersion;
|
||||
profile.lastVersionId = "1.7.10";
|
||||
|
||||
LauncherProfiles.mainProfileJson.profiles.put(modpackName, profile);
|
||||
LauncherProfiles.update();
|
||||
|
@ -6,10 +6,12 @@ import java.util.Arrays;
|
||||
public class ModDetail extends ModItem {
|
||||
/* A cheap way to map from the front facing name to the underlying id */
|
||||
public String[] versionNames;
|
||||
public String [] mcVersionNames;
|
||||
public String[] versionUrls;
|
||||
public ModDetail(ModItem item, String[] versionNames, String[] versionUrls) {
|
||||
public ModDetail(ModItem item, String[] versionNames, String[] mcVersionNames, String[] versionUrls) {
|
||||
super(item.apiSource, item.isModpack, item.id, item.title, item.description, item.imageUrl);
|
||||
this.versionNames = versionNames;
|
||||
this.mcVersionNames = mcVersionNames;
|
||||
this.versionUrls = versionUrls;
|
||||
}
|
||||
|
||||
@ -17,6 +19,7 @@ public class ModDetail extends ModItem {
|
||||
public String toString() {
|
||||
return "ModDetail{" +
|
||||
"versionNames=" + Arrays.toString(versionNames) +
|
||||
", mcVersionNames=" + Arrays.toString(mcVersionNames) +
|
||||
", versionIds=" + Arrays.toString(versionUrls) +
|
||||
", id='" + id + '\'' +
|
||||
", title='" + title + '\'' +
|
||||
|
@ -0,0 +1,13 @@
|
||||
package net.kdt.pojavlaunch.modloaders.modpacks.models;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* Search filters, passed to APIs
|
||||
*/
|
||||
public class SearchFilters {
|
||||
public boolean isModpack;
|
||||
public String name;
|
||||
@Nullable public String mcVersion;
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user