Cleanup
This commit is contained in:
@@ -8,6 +8,7 @@ import com.jagrosh.discordipc.entities.Packet;
|
||||
import com.jagrosh.discordipc.entities.RichPresence;
|
||||
import com.jagrosh.discordipc.entities.User;
|
||||
import com.jagrosh.discordipc.exceptions.NoDiscordClientException;
|
||||
import de.winniepat.parrotmod.ParrotLogger;
|
||||
import de.winniepat.parrotmod.config.ConfigManager;
|
||||
import net.minecraft.client.player.LocalPlayer;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
@@ -49,15 +50,11 @@ public class DiscordRPCManager {
|
||||
public static void init() {
|
||||
ConfigManager.registerListener(cfg -> {
|
||||
if (cfg.enableDiscordRPC) {
|
||||
if (!connected && client == null) {
|
||||
if (client == null) {
|
||||
startClient();
|
||||
}
|
||||
} else {
|
||||
if (client != null) {
|
||||
shutdown();
|
||||
client = null;
|
||||
connected = false;
|
||||
}
|
||||
shutdown();
|
||||
}
|
||||
});
|
||||
|
||||
@@ -189,6 +186,17 @@ public class DiscordRPCManager {
|
||||
}
|
||||
|
||||
public static void shutdown() {
|
||||
if (client != null) client.close();
|
||||
if (client != null) {
|
||||
try {
|
||||
if (connected) {
|
||||
client.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
ParrotLogger.logError(DiscordRPCManager.class, "Error while closing Discord RPC client", e);
|
||||
} finally {
|
||||
client = null;
|
||||
connected = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,7 +35,6 @@ public abstract class BaseTabFragment extends Fragment {
|
||||
tv.setText(title.toUpperCase());
|
||||
tv.setTextSize(11);
|
||||
tv.setTextColor(COLOR_ACCENT);
|
||||
// Letter spacing might not be available in all ModernUI versions, but let's try if it exists or just skip
|
||||
var p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
|
||||
p.topMargin = dp(24);
|
||||
p.bottomMargin = dp(8);
|
||||
|
||||
@@ -7,9 +7,13 @@ import icyllis.modernui.view.ViewGroup;
|
||||
import icyllis.modernui.widget.LinearLayout;
|
||||
import icyllis.modernui.widget.Switch;
|
||||
import icyllis.modernui.widget.ScrollView;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class DiscordTabFragment extends BaseTabFragment {
|
||||
|
||||
private final List<View> rpcDetailViews = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, DataSet savedInstanceData) {
|
||||
ScrollView scrollView = new ScrollView(getContext());
|
||||
@@ -26,37 +30,61 @@ public class DiscordTabFragment extends BaseTabFragment {
|
||||
ConfigManager.getInstance().enableDiscordRPC, (v, checked) -> {
|
||||
ConfigManager.getInstance().enableDiscordRPC = checked;
|
||||
ConfigManager.save();
|
||||
updateRPCDetailsEnabled(checked);
|
||||
});
|
||||
|
||||
addSectionHeader(layout, "RPC Details");
|
||||
|
||||
addSwitchRow(layout, "Show Biome", "Display current biome in status",
|
||||
rpcDetailViews.add(addSwitchRow(layout, "Show Biome", "Display current biome in status",
|
||||
ConfigManager.getInstance().showBiomeInRPC, (v, checked) -> {
|
||||
ConfigManager.getInstance().showBiomeInRPC = checked;
|
||||
ConfigManager.save();
|
||||
});
|
||||
}));
|
||||
|
||||
addSwitchRow(layout, "Show Held Item", "Display what you are holding",
|
||||
rpcDetailViews.add(addSwitchRow(layout, "Show Held Item", "Display what you are holding",
|
||||
ConfigManager.getInstance().showHeldItemInRPC, (v, checked) -> {
|
||||
ConfigManager.getInstance().showHeldItemInRPC = checked;
|
||||
ConfigManager.save();
|
||||
});
|
||||
}));
|
||||
|
||||
addSwitchRow(layout, "Show Health", "Display your current HP",
|
||||
rpcDetailViews.add(addSwitchRow(layout, "Show Health", "Display your current HP",
|
||||
ConfigManager.getInstance().showHealthInRPC, (v, checked) -> {
|
||||
ConfigManager.getInstance().showHealthInRPC = checked;
|
||||
ConfigManager.save();
|
||||
});
|
||||
}));
|
||||
|
||||
updateRPCDetailsEnabled(ConfigManager.getInstance().enableDiscordRPC);
|
||||
|
||||
return scrollView;
|
||||
}
|
||||
|
||||
private void addSwitchRow(LinearLayout parent, String title, String desc, boolean checked, Switch.OnCheckedChangeListener listener) {
|
||||
private void updateRPCDetailsEnabled(boolean enabled) {
|
||||
for (View v : rpcDetailViews) {
|
||||
v.setEnabled(enabled);
|
||||
v.setAlpha(enabled ? 1.0f : 0.5f);
|
||||
if (v instanceof ViewGroup) {
|
||||
setChildrenEnabled((ViewGroup) v, enabled);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void setChildrenEnabled(ViewGroup vg, boolean enabled) {
|
||||
for (int i = 0; i < vg.getChildCount(); i++) {
|
||||
View child = vg.getChildAt(i);
|
||||
child.setEnabled(enabled);
|
||||
if (child instanceof ViewGroup) {
|
||||
setChildrenEnabled((ViewGroup) child, enabled);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private View addSwitchRow(LinearLayout parent, String title, String desc, boolean checked, Switch.OnCheckedChangeListener listener) {
|
||||
LinearLayout card = createSettingCard(title, desc);
|
||||
Switch s = new Switch(getContext());
|
||||
s.setChecked(checked);
|
||||
s.setOnCheckedChangeListener(listener);
|
||||
card.addView(s);
|
||||
parent.addView(card);
|
||||
return card;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,11 +19,9 @@ public class SettingsFragment extends Fragment {
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, DataSet savedInstanceState) {
|
||||
// Main container with shadow/margin if needed, but here it's centering
|
||||
FrameLayout root = new FrameLayout(getContext());
|
||||
root.setLayoutParams(new FrameLayout.LayoutParams(dp(720), dp(480), Gravity.CENTER));
|
||||
|
||||
// Background
|
||||
View bg = new View(getContext());
|
||||
bg.setBackground(makeRoundedBg(COLOR_BACKGROUND, 16));
|
||||
root.addView(bg);
|
||||
@@ -32,19 +30,17 @@ public class SettingsFragment extends Fragment {
|
||||
layout.setOrientation(LinearLayout.HORIZONTAL);
|
||||
root.addView(layout);
|
||||
|
||||
// Sidebar Container
|
||||
FrameLayout sidebarContainer = new FrameLayout(getContext());
|
||||
var sidebarParams = new LinearLayout.LayoutParams(dp(170), ViewGroup.LayoutParams.MATCH_PARENT);
|
||||
layout.addView(sidebarContainer, sidebarParams);
|
||||
|
||||
View sidebarBg = new View(getContext());
|
||||
sidebarBg.setBackground(makeRoundedBg(COLOR_SIDEBAR, 16));
|
||||
// We only want right side to be sharp, but for simplicity we just overlap or use a large radius
|
||||
sidebarContainer.addView(sidebarBg);
|
||||
|
||||
LinearLayout sidebarContent = new LinearLayout(getContext());
|
||||
sidebarContent.setOrientation(LinearLayout.VERTICAL);
|
||||
sidebarContent.setPadding(0, dp(24), 0, dp(16)); // Removed horizontal padding to allow edge indicator
|
||||
sidebarContent.setPadding(0, dp(24), 0, dp(16));
|
||||
sidebarContainer.addView(sidebarContent, new FrameLayout.LayoutParams(
|
||||
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
|
||||
|
||||
@@ -53,22 +49,19 @@ public class SettingsFragment extends Fragment {
|
||||
title.setTextSize(22);
|
||||
title.setGravity(Gravity.CENTER);
|
||||
title.setTextColor(COLOR_ACCENT);
|
||||
title.setPadding(dp(16), 0, dp(16), dp(32)); // Added horizontal padding back to title
|
||||
title.setPadding(dp(16), 0, dp(16), dp(32));
|
||||
sidebarContent.addView(title);
|
||||
|
||||
// Tabs container
|
||||
FrameLayout tabsContainer = new FrameLayout(getContext());
|
||||
sidebarContent.addView(tabsContainer, new LinearLayout.LayoutParams(
|
||||
ViewGroup.LayoutParams.MATCH_PARENT, 0, 1f));
|
||||
|
||||
LinearLayout tabsLayout = new LinearLayout(getContext());
|
||||
tabsLayout.setOrientation(LinearLayout.VERTICAL);
|
||||
// Center tabs vertically in available space
|
||||
tabsContainer.addView(tabsLayout, new FrameLayout.LayoutParams(
|
||||
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER));
|
||||
|
||||
|
||||
// Content Area
|
||||
FrameLayout contentArea = new FrameLayout(getContext());
|
||||
contentArea.setId(R.id.content);
|
||||
var contentParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1f);
|
||||
@@ -112,7 +105,6 @@ public class SettingsFragment extends Fragment {
|
||||
tabButtons[index] = tab;
|
||||
}
|
||||
|
||||
// Footer
|
||||
TextView footer = new TextView(getContext());
|
||||
String version = FabricLoader.getInstance().getModContainer("parrotmod")
|
||||
.map(m -> m.getMetadata().getVersion().getFriendlyString()).orElse("?.?.?");
|
||||
@@ -123,7 +115,6 @@ public class SettingsFragment extends Fragment {
|
||||
footer.setPadding(dp(16), dp(12), dp(16), dp(12));
|
||||
sidebarContent.addView(footer);
|
||||
|
||||
// Entrance Animation
|
||||
root.setAlpha(0);
|
||||
root.setScaleX(0.96f);
|
||||
root.setScaleY(0.96f);
|
||||
@@ -131,7 +122,6 @@ public class SettingsFragment extends Fragment {
|
||||
ObjectAnimator.ofFloat(root, View.SCALE_X, 0.96f, 1).setDuration(450).start();
|
||||
ObjectAnimator.ofFloat(root, View.SCALE_Y, 0.96f, 1).setDuration(450).start();
|
||||
|
||||
// Initial tab
|
||||
root.post(() -> {
|
||||
tabButtons[0].callOnClick();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user