From b5fd44a3c5b2ba3e4c3686b3d8b2f66be7ef991f Mon Sep 17 00:00:00 2001 From: Markus Fisch Date: Sun, 2 Jun 2024 19:12:29 +0200 Subject: [PATCH] Move completions adapter into its own file And rename it accordingly for clarity. --- .../shadereditor/activity/MainActivity.java | 79 +--------------- .../adapter/CompletionsAdapter.java | 91 ++++++++++++++++++- 2 files changed, 94 insertions(+), 76 deletions(-) diff --git a/app/src/main/java/de/markusfisch/android/shadereditor/activity/MainActivity.java b/app/src/main/java/de/markusfisch/android/shadereditor/activity/MainActivity.java index b27906ae..044e646e 100644 --- a/app/src/main/java/de/markusfisch/android/shadereditor/activity/MainActivity.java +++ b/app/src/main/java/de/markusfisch/android/shadereditor/activity/MainActivity.java @@ -20,10 +20,7 @@ import android.os.Looper; import android.util.DisplayMetrics; import android.view.KeyEvent; -import android.view.LayoutInflater; -import android.view.MotionEvent; import android.view.View; -import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.view.ViewTreeObserver; import android.widget.AdapterView; @@ -47,10 +44,8 @@ import androidx.appcompat.widget.Toolbar; import androidx.core.content.ContextCompat; import androidx.core.view.ViewCompat; -import androidx.recyclerview.widget.DiffUtil; import androidx.recyclerview.widget.DividerItemDecoration; import androidx.recyclerview.widget.LinearLayoutManager; -import androidx.recyclerview.widget.ListAdapter; import androidx.recyclerview.widget.RecyclerView; import java.io.IOException; @@ -60,6 +55,7 @@ import java.util.concurrent.Executors; import de.markusfisch.android.shadereditor.R; +import de.markusfisch.android.shadereditor.adapter.CompletionsAdapter; import de.markusfisch.android.shadereditor.adapter.ShaderAdapter; import de.markusfisch.android.shadereditor.app.ShaderEditorApp; import de.markusfisch.android.shadereditor.database.Database; @@ -106,7 +102,7 @@ public void run() { private volatile int fps; private float[] qualityValues; private float quality = 1f; - private Adapter completionsAdapter; + private CompletionsAdapter completionsAdapter; @Override public void onConfigurationChanged(@NonNull Configuration newConfig) { @@ -304,7 +300,8 @@ private void initExtraKeys() { RecyclerView completions = extraKeys.findViewById(R.id.completions); completions.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false)); - this.completionsAdapter = new Adapter(this); + completionsAdapter = new CompletionsAdapter(this, + sequence -> editorFragment.insert(sequence)); completions.setAdapter(completionsAdapter); DividerItemDecoration divider = new DividerItemDecoration(completions.getContext(), DividerItemDecoration.HORIZONTAL); @@ -344,74 +341,6 @@ public void onGlobalLayout() { : View.GONE); } - private static final DiffUtil.ItemCallback DIFF_CALLBACK = - new DiffUtil.ItemCallback() { - @Override - public boolean areItemsTheSame(@NonNull String oldItem, @NonNull String newItem) { - // Update the condition according to your unique identifier - return oldItem.equals(newItem); - } - - @Override - public boolean areContentsTheSame(@NonNull String oldItem, - @NonNull String newItem) { - // Return true if the contents of the items have not changed - return oldItem.equals(newItem); - } - }; - - private class Adapter extends ListAdapter { - @NonNull - private final LayoutInflater inflater; - int position = 0; - - public Adapter(Context context) { - super(DIFF_CALLBACK); - this.inflater = LayoutInflater.from(context); - } - - @NonNull - @Override - public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { - View view = inflater.inflate(R.layout.extra_key_btn, parent, false); - return new ViewHolder(view); - } - - @Override - public void onBindViewHolder(@NonNull ViewHolder holder, int position) { - String item = getItem(position); // Use getItem provided by ListAdapter - holder.update(item); - } - - public void setPosition(int position) { - this.position = position; - } - - private class ViewHolder extends RecyclerView.ViewHolder { - private final Button btn; - - public ViewHolder(@NonNull View itemView) { - super(itemView); - btn = itemView.findViewById(R.id.btn); - btn.setOnClickListener((v) -> { - CharSequence text = btn.getText(); - editorFragment.insert(text.subSequence(position, text.length())); - }); - itemView.setOnTouchListener(new View.OnTouchListener() { - @SuppressLint("ClickableViewAccessibility") - @Override - public boolean onTouch(View v, MotionEvent event) { - return btn.onTouchEvent(event); // The FrameLayout always forwards - } - }); - } - - public void update(String item) { - btn.setText(item); - } - } - } - private void initToolbar() { toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); diff --git a/app/src/main/java/de/markusfisch/android/shadereditor/adapter/CompletionsAdapter.java b/app/src/main/java/de/markusfisch/android/shadereditor/adapter/CompletionsAdapter.java index 0390ef71..dca204d3 100644 --- a/app/src/main/java/de/markusfisch/android/shadereditor/adapter/CompletionsAdapter.java +++ b/app/src/main/java/de/markusfisch/android/shadereditor/adapter/CompletionsAdapter.java @@ -1,2 +1,91 @@ -package de.markusfisch.android.shadereditor.adapter;public class CompletionsAdapter { +package de.markusfisch.android.shadereditor.adapter; + +import android.annotation.SuppressLint; +import android.content.Context; +import android.view.LayoutInflater; +import android.view.MotionEvent; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; + +import androidx.annotation.NonNull; +import androidx.recyclerview.widget.DiffUtil; +import androidx.recyclerview.widget.ListAdapter; +import androidx.recyclerview.widget.RecyclerView; + +import de.markusfisch.android.shadereditor.R; + +public class CompletionsAdapter extends ListAdapter { + public interface OnInsertListener { + void onInsert(CharSequence sequence); + } + + private static final DiffUtil.ItemCallback DIFF_CALLBACK = + new DiffUtil.ItemCallback() { + @Override + public boolean areItemsTheSame(@NonNull String oldItem, @NonNull String newItem) { + // Update the condition according to your unique identifier + return oldItem.equals(newItem); + } + + @Override + public boolean areContentsTheSame(@NonNull String oldItem, + @NonNull String newItem) { + // Return true if the contents of the items have not changed + return oldItem.equals(newItem); + } + }; + + @NonNull + private final LayoutInflater inflater; + private final OnInsertListener onInsertListener; + + private int position = 0; + + public CompletionsAdapter(Context context, OnInsertListener onInsertListener) { + super(DIFF_CALLBACK); + this.inflater = LayoutInflater.from(context); + this.onInsertListener = onInsertListener; + } + + @NonNull + @Override + public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + View view = inflater.inflate(R.layout.extra_key_btn, parent, false); + return new ViewHolder(view); + } + + @Override + public void onBindViewHolder(@NonNull ViewHolder holder, int position) { + String item = getItem(position); + holder.update(item); + } + + public void setPosition(int position) { + this.position = position; + } + + public class ViewHolder extends RecyclerView.ViewHolder { + private final Button btn; + + public ViewHolder(@NonNull View itemView) { + super(itemView); + btn = itemView.findViewById(R.id.btn); + btn.setOnClickListener((v) -> { + CharSequence text = btn.getText(); + onInsertListener.onInsert(text.subSequence(position, text.length())); + }); + itemView.setOnTouchListener(new View.OnTouchListener() { + @SuppressLint("ClickableViewAccessibility") + @Override + public boolean onTouch(View v, MotionEvent event) { + return btn.onTouchEvent(event); // The FrameLayout always forwards + } + }); + } + + public void update(String item) { + btn.setText(item); + } + } }