Skip to content

Commit

Permalink
Move completions adapter into its own file
Browse files Browse the repository at this point in the history
And rename it accordingly for clarity.
  • Loading branch information
markusfisch committed Jun 2, 2024
1 parent b351181 commit b5fd44a
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -344,74 +341,6 @@ public void onGlobalLayout() {
: View.GONE);
}

private static final DiffUtil.ItemCallback<String> DIFF_CALLBACK =
new DiffUtil.ItemCallback<String>() {
@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<String, Adapter.ViewHolder> {
@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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, CompletionsAdapter.ViewHolder> {
public interface OnInsertListener {
void onInsert(CharSequence sequence);
}

private static final DiffUtil.ItemCallback<String> DIFF_CALLBACK =
new DiffUtil.ItemCallback<String>() {
@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);
}
}
}

0 comments on commit b5fd44a

Please sign in to comment.