Skip to content

Commit

Permalink
refactor: Remove ok button from app selector dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
Xtr126 committed Dec 30, 2023
1 parent 071d401 commit d61fbd0
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 69 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/xtr/keymapper/keymap/KeymapConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 10 additions & 7 deletions app/src/main/java/xtr/keymapper/profiles/ProfileSelector.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
36 changes: 6 additions & 30 deletions app/src/main/java/xtr/keymapper/profiles/ProfilesApps.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -58,14 +54,8 @@ public void onDestroyView() {
public class AppsGridAdapter extends RecyclerView.Adapter<AppsGridAdapter.RecyclerViewHolder> {

private final ArrayList<RecyclerData> 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);
Expand All @@ -77,16 +67,13 @@ public AppsGridAdapter(Context context) {
ri.activityInfo.packageName,
ri.loadLabel(pm),
ri.activityInfo.loadIcon(pm)));

binding.currentProfile.setText(packageName);
}

@NonNull
@Override
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);
}

Expand All @@ -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
Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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()));
Expand Down
24 changes: 1 addition & 23 deletions app/src/main/res/layout/fragment_profiles_apps.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,8 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:gravity="center"
android:orientation="horizontal">

<TextView
android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/package_name"
android:visibility="visible" />

<TextView
android:id="@+id/current_profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:visibility="visible" />
</LinearLayout>


<androidx.recyclerview.widget.RecyclerView
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/apps_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand Down

0 comments on commit d61fbd0

Please sign in to comment.