Fix[profiles]: properly reload profiles on resume

This commit is contained in:
Mathias Boulay 2023-08-27 17:36:09 +02:00
parent b8e3b2a6f9
commit b23787e5a4
3 changed files with 30 additions and 7 deletions

View File

@ -77,6 +77,11 @@ public class mcVersionSpinner extends ExtendedTextView {
mProfileAdapter.setViewProfile(this, (String) mProfileAdapter.getItem(position), false); mProfileAdapter.setViewProfile(this, (String) mProfileAdapter.getItem(position), false);
} }
/** Reload profiles from the file, forcing the spinner to consider the new data */
public void reloadProfiles(){
mProfileAdapter.reloadProfiles();
}
/** Initialize various behaviors */ /** Initialize various behaviors */
private void init(){ private void init(){
// Setup various attributes // Setup various attributes

View File

@ -13,6 +13,8 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import com.kdt.mcgui.mcVersionSpinner;
import net.kdt.pojavlaunch.CustomControlsActivity; import net.kdt.pojavlaunch.CustomControlsActivity;
import net.kdt.pojavlaunch.R; import net.kdt.pojavlaunch.R;
import net.kdt.pojavlaunch.Tools; import net.kdt.pojavlaunch.Tools;
@ -24,6 +26,8 @@ import net.kdt.pojavlaunch.value.launcherprofiles.LauncherProfiles;
public class MainMenuFragment extends Fragment { public class MainMenuFragment extends Fragment {
public static final String TAG = "MainMenuFragment"; public static final String TAG = "MainMenuFragment";
private mcVersionSpinner mVersionSpinner;
public MainMenuFragment(){ public MainMenuFragment(){
super(R.layout.fragment_launcher); super(R.layout.fragment_launcher);
} }
@ -37,6 +41,7 @@ public class MainMenuFragment extends Fragment {
ImageButton mEditProfileButton = view.findViewById(R.id.edit_profile_button); ImageButton mEditProfileButton = view.findViewById(R.id.edit_profile_button);
Button mPlayButton = view.findViewById(R.id.play_button); Button mPlayButton = view.findViewById(R.id.play_button);
mVersionSpinner = view.findViewById(R.id.mc_version_spinner);
mNewsButton.setOnClickListener(v -> Tools.openURL(requireActivity(), Tools.URL_HOME)); mNewsButton.setOnClickListener(v -> Tools.openURL(requireActivity(), Tools.URL_HOME));
mCustomControlButton.setOnClickListener(v -> startActivity(new Intent(requireContext(), CustomControlsActivity.class))); mCustomControlButton.setOnClickListener(v -> startActivity(new Intent(requireContext(), CustomControlsActivity.class)));
@ -60,7 +65,7 @@ public class MainMenuFragment extends Fragment {
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
LauncherProfiles.load(); mVersionSpinner.reloadProfiles();
} }
private void runInstallerWithConfirmation(boolean isCustomArgs) { private void runInstallerWithConfirmation(boolean isCustomArgs) {

View File

@ -31,15 +31,11 @@ public class ProfileAdapter extends BaseAdapter {
private Map<String, MinecraftProfile> mProfiles; private Map<String, MinecraftProfile> mProfiles;
private final MinecraftProfile dummy = new MinecraftProfile(); private final MinecraftProfile dummy = new MinecraftProfile();
private List<String> mProfileList; private List<String> mProfileList;
private final ProfileAdapterExtra[] mExtraEntires; private ProfileAdapterExtra[] mExtraEntires;
public ProfileAdapter(Context context, ProfileAdapterExtra[] extraEntries) { public ProfileAdapter(Context context, ProfileAdapterExtra[] extraEntries) {
ProfileIconCache.initDefault(context); ProfileIconCache.initDefault(context);
LauncherProfiles.load(); reloadProfiles(extraEntries);
mProfiles = new HashMap<>(LauncherProfiles.mainProfileJson.profiles);
if(extraEntries == null) mExtraEntires = new ProfileAdapterExtra[0];
else mExtraEntires = extraEntries;
mProfileList = new ArrayList<>(Arrays.asList(mProfiles.keySet().toArray(new String[0])));
} }
/* /*
* Gets how much profiles are loaded in the adapter right now * Gets how much profiles are loaded in the adapter right now
@ -67,6 +63,8 @@ public class ProfileAdapter extends BaseAdapter {
return null; return null;
} }
public int resolveProfileIndex(String name) { public int resolveProfileIndex(String name) {
return mProfileList.indexOf(name); return mProfileList.indexOf(name);
} }
@ -134,4 +132,19 @@ public class ProfileAdapter extends BaseAdapter {
extendedTextView.setText(extra.name); extendedTextView.setText(extra.name);
extendedTextView.setBackgroundColor(Color.TRANSPARENT); extendedTextView.setBackgroundColor(Color.TRANSPARENT);
} }
/** Reload profiles from the file */
public void reloadProfiles(){
LauncherProfiles.load();
mProfiles = new HashMap<>(LauncherProfiles.mainProfileJson.profiles);
mProfileList = new ArrayList<>(Arrays.asList(mProfiles.keySet().toArray(new String[0])));
notifyDataSetChanged();
}
/** Reload profiles from the file, with additional extra entries */
public void reloadProfiles(ProfileAdapterExtra[] extraEntries) {
if(extraEntries == null) mExtraEntires = new ProfileAdapterExtra[0];
else mExtraEntires = extraEntries;
this.reloadProfiles();
}
} }