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