From d61fbd02599ed1e558c093ad765897d168d75149 Mon Sep 17 00:00:00 2001 From: Xtr126 Date: Sat, 30 Dec 2023 20:25:01 +0530 Subject: [PATCH] refactor: Remove ok button from app selector dialog --- .../xtr/keymapper/keymap/KeymapConfig.java | 2 +- .../keymapper/profiles/ProfileSelector.java | 17 +++++---- .../xtr/keymapper/profiles/ProfilesApps.java | 36 ++++--------------- .../profiles/ProfilesViewAdapter.java | 17 ++++----- .../res/layout/fragment_profiles_apps.xml | 24 +------------ 5 files changed, 27 insertions(+), 69 deletions(-) diff --git a/app/src/main/java/xtr/keymapper/keymap/KeymapConfig.java b/app/src/main/java/xtr/keymapper/keymap/KeymapConfig.java index 8492c8af..a1812028 100644 --- a/app/src/main/java/xtr/keymapper/keymap/KeymapConfig.java +++ b/app/src/main/java/xtr/keymapper/keymap/KeymapConfig.java @@ -85,7 +85,7 @@ private void loadSharedPrefs() { ctrlMouseWheelZoom = sharedPref.getBoolean("ctrl_mouse_wheel_zoom", false); ctrlDragMouseGesture = sharedPref.getBoolean("ctrl_drag_mouse_gesture", true); mouseAimToggle = sharedPref.getBoolean("mouse_aim_shortcut_toggle", true); - disableAutoProfiling = sharedPref.getBoolean("disable_auto_profile", false); + disableAutoProfiling = sharedPref.getBoolean("disable_auto_profile", true); launchEditorShortcutKey = sharedPref.getInt("launch_editor_shortcut", -1); pauseResumeShortcutKey = sharedPref.getInt("pause_resume_shortcut", -1); diff --git a/app/src/main/java/xtr/keymapper/profiles/ProfileSelector.java b/app/src/main/java/xtr/keymapper/profiles/ProfileSelector.java index 471ba73e..f25fae59 100644 --- a/app/src/main/java/xtr/keymapper/profiles/ProfileSelector.java +++ b/app/src/main/java/xtr/keymapper/profiles/ProfileSelector.java @@ -110,18 +110,21 @@ public static void showAppSelectionDialog(Context context, OnAppSelectedListener ProfilesApps appsView = new ProfilesApps(context); MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(context); - builder.setPositiveButton(R.string.ok, (dialog, which) -> { - listener.onAppSelected(appsView.packageName); - appsView.onDestroyView(); - }) - .setView(appsView.view); - showDialog(builder); + builder.setView(appsView.view); + AlertDialog dialog = showDialog(builder); + + appsView.setListener(packageName -> { + listener.onAppSelected(packageName); + appsView.onDestroyView(); + dialog.dismiss(); + }); } - private static void showDialog(MaterialAlertDialogBuilder builder) { + private static AlertDialog showDialog(MaterialAlertDialogBuilder builder) { AlertDialog dialog = builder.create(); if (Settings.canDrawOverlays(dialog.getContext())) dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY); dialog.show(); + return dialog; } } \ No newline at end of file diff --git a/app/src/main/java/xtr/keymapper/profiles/ProfilesApps.java b/app/src/main/java/xtr/keymapper/profiles/ProfilesApps.java index 8017af96..c88c3792 100644 --- a/app/src/main/java/xtr/keymapper/profiles/ProfilesApps.java +++ b/app/src/main/java/xtr/keymapper/profiles/ProfilesApps.java @@ -4,7 +4,6 @@ import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; -import android.graphics.Color; import android.graphics.drawable.Drawable; import android.view.LayoutInflater; import android.view.View; @@ -18,26 +17,23 @@ import xtr.keymapper.databinding.AppViewBinding; import xtr.keymapper.databinding.FragmentProfilesAppsBinding; -import xtr.keymapper.keymap.KeymapProfiles; public class ProfilesApps { - public String packageName; public FragmentProfilesAppsBinding binding; public final View view; - public ProfilesApps(Context context, String profileName){ - this.packageName = new KeymapProfiles(context).getProfile(profileName).packageName; + private ProfileSelector.OnAppSelectedListener mListener; - view = createView(LayoutInflater.from(context)); - onViewCreated(view); - } public ProfilesApps(Context context){ - this.packageName = null; view = createView(LayoutInflater.from(context)); onViewCreated(view); } + public void setListener(ProfileSelector.OnAppSelectedListener mListener) { + this.mListener = mListener; + } + public View createView(@NonNull LayoutInflater inflater) { // Inflate the layout for this fragment binding = FragmentProfilesAppsBinding.inflate(inflater); @@ -58,14 +54,8 @@ public void onDestroyView() { public class AppsGridAdapter extends RecyclerView.Adapter { private final ArrayList appsDataArrayList = new ArrayList<>(); - private int defaultColor; - - private final int selectedColor; - - private View selectedView; public AppsGridAdapter(Context context) { - selectedColor = context.getColor(com.google.android.material.R.color.material_on_surface_stroke); PackageManager pm = context.getPackageManager(); Intent i = new Intent(Intent.ACTION_MAIN, null); i.addCategory(Intent.CATEGORY_LAUNCHER); @@ -77,8 +67,6 @@ public AppsGridAdapter(Context context) { ri.activityInfo.packageName, ri.loadLabel(pm), ri.activityInfo.loadIcon(pm))); - - binding.currentProfile.setText(packageName); } @NonNull @@ -86,7 +74,6 @@ public AppsGridAdapter(Context context) { public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { // Inflate Layout AppViewBinding itemBinding = AppViewBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false); - defaultColor = Color.TRANSPARENT; return new RecyclerViewHolder(itemBinding); } @@ -96,12 +83,6 @@ public void onBindViewHolder(@NonNull RecyclerViewHolder holder, int position) { RecyclerData recyclerData = appsDataArrayList.get(position); holder.binding.appName.setText(recyclerData.title); holder.binding.appIcon.setImageDrawable(recyclerData.icon); - - - if (recyclerData.packageName.equals(packageName ) && selectedView == null) { - selectedView = holder.binding.getRoot(); - selectedView.setBackgroundColor(selectedColor); - } } @Override @@ -124,12 +105,7 @@ public RecyclerViewHolder(@NonNull AppViewBinding binding) { @Override public void onClick (View view) { int i = getAdapterPosition(); - ProfilesApps.this.packageName = appsDataArrayList.get(i).packageName; - ProfilesApps.this.binding.currentProfile.setText(packageName); - - if (selectedView != null) selectedView.setBackgroundColor(defaultColor); - view.setBackgroundColor(selectedColor); - selectedView = view; + mListener.onAppSelected(appsDataArrayList.get(i).packageName); } } diff --git a/app/src/main/java/xtr/keymapper/profiles/ProfilesViewAdapter.java b/app/src/main/java/xtr/keymapper/profiles/ProfilesViewAdapter.java index b7099a49..81e2cbfd 100644 --- a/app/src/main/java/xtr/keymapper/profiles/ProfilesViewAdapter.java +++ b/app/src/main/java/xtr/keymapper/profiles/ProfilesViewAdapter.java @@ -8,6 +8,7 @@ import android.view.ViewGroup; import androidx.annotation.NonNull; +import androidx.appcompat.app.AlertDialog; import androidx.appcompat.content.res.AppCompatResources; import androidx.recyclerview.widget.RecyclerView; @@ -105,16 +106,16 @@ public void onBindViewHolder(ViewHolder viewHolder, final int position) { // Show dialog for user to select app for a profile from a grid of apps viewHolder.binding.appIconButton.setOnClickListener(view -> { - ProfilesApps appsView = new ProfilesApps(view.getContext(), profileName); + ProfilesApps appsView = new ProfilesApps(view.getContext()); MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(context); - builder.setPositiveButton(R.string.ok, (dialog, which) -> { - keymapProfiles.setProfilePackageName(recyclerData.name.toString(), appsView.packageName); - appsView.onDestroyView(); - }) - .setNegativeButton(R.string.cancel, (dialog, which) -> {}) - .setView(appsView.view) - .show(); + AlertDialog dialog = builder.setView(appsView.view).show(); + + appsView.setListener(packageName -> { + keymapProfiles.setProfilePackageName(recyclerData.name.toString(), packageName); + appsView.onDestroyView(); + dialog.dismiss(); + }); }); viewHolder.binding.switch1.setChecked(keymapProfiles.isProfileEnabled(recyclerData.name.toString())); diff --git a/app/src/main/res/layout/fragment_profiles_apps.xml b/app/src/main/res/layout/fragment_profiles_apps.xml index 10664776..94263179 100644 --- a/app/src/main/res/layout/fragment_profiles_apps.xml +++ b/app/src/main/res/layout/fragment_profiles_apps.xml @@ -10,30 +10,8 @@ xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"> - - - - - - - -