mirror of https://github.com/M66B/FairEmail.git
parent
faaa1b432d
commit
b6ee20e56f
@ -0,0 +1,184 @@
|
|||||||
|
package eu.faircode.email;
|
||||||
|
|
||||||
|
/*
|
||||||
|
This file is part of FairEmail.
|
||||||
|
|
||||||
|
FairEmail is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
FairEmail is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
Copyright 2018-2019 by Marcel Bokhorst (M66B)
|
||||||
|
*/
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.view.LayoutInflater;
|
||||||
|
import android.view.View;
|
||||||
|
import android.view.ViewGroup;
|
||||||
|
import android.widget.ImageView;
|
||||||
|
import android.widget.TextView;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.lifecycle.LifecycleOwner;
|
||||||
|
import androidx.recyclerview.widget.DiffUtil;
|
||||||
|
import androidx.recyclerview.widget.ListUpdateCallback;
|
||||||
|
import androidx.recyclerview.widget.RecyclerView;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class AdapterFolderSelect extends RecyclerView.Adapter<AdapterFolderSelect.ViewHolder> {
|
||||||
|
private Context context;
|
||||||
|
private LifecycleOwner owner;
|
||||||
|
private IFolderSelectedListener listener;
|
||||||
|
private LayoutInflater inflater;
|
||||||
|
|
||||||
|
private List<EntityFolder> items = new ArrayList<>();
|
||||||
|
|
||||||
|
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
|
||||||
|
private View view;
|
||||||
|
private ImageView ivType;
|
||||||
|
private TextView tvName;
|
||||||
|
|
||||||
|
ViewHolder(View itemView) {
|
||||||
|
super(itemView);
|
||||||
|
|
||||||
|
view = itemView.findViewById(R.id.clItem);
|
||||||
|
ivType = itemView.findViewById(R.id.ivType);
|
||||||
|
tvName = itemView.findViewById(R.id.tvName);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void wire() {
|
||||||
|
view.setOnClickListener(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void unwire() {
|
||||||
|
view.setOnClickListener(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void bindTo(EntityFolder folder) {
|
||||||
|
ivType.setImageResource(EntityFolder.getIcon(folder.type));
|
||||||
|
tvName.setText(folder.getDisplayName(context));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onClick(View v) {
|
||||||
|
int pos = getAdapterPosition();
|
||||||
|
if (pos == RecyclerView.NO_POSITION)
|
||||||
|
return;
|
||||||
|
|
||||||
|
EntityFolder folder = items.get(pos);
|
||||||
|
if (folder != null)
|
||||||
|
listener.onFolderSelected(folder);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
AdapterFolderSelect(Context context, LifecycleOwner owner, IFolderSelectedListener listener) {
|
||||||
|
this.context = context;
|
||||||
|
this.owner = owner;
|
||||||
|
this.listener = listener;
|
||||||
|
this.inflater = LayoutInflater.from(context);
|
||||||
|
setHasStableIds(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void set(@NonNull List<EntityFolder> folders) {
|
||||||
|
Log.i("Set folders=" + folders.size());
|
||||||
|
|
||||||
|
DiffUtil.DiffResult diff = DiffUtil.calculateDiff(new DiffCallback(items, folders), false);
|
||||||
|
|
||||||
|
items = folders;
|
||||||
|
|
||||||
|
diff.dispatchUpdatesTo(new ListUpdateCallback() {
|
||||||
|
@Override
|
||||||
|
public void onInserted(int position, int count) {
|
||||||
|
Log.i("Inserted @" + position + " #" + count);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onRemoved(int position, int count) {
|
||||||
|
Log.i("Removed @" + position + " #" + count);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onMoved(int fromPosition, int toPosition) {
|
||||||
|
Log.i("Moved " + fromPosition + ">" + toPosition);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onChanged(int position, int count, Object payload) {
|
||||||
|
Log.i("Changed @" + position + " #" + count);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
diff.dispatchUpdatesTo(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
private class DiffCallback extends DiffUtil.Callback {
|
||||||
|
private List<EntityFolder> prev = new ArrayList<>();
|
||||||
|
private List<EntityFolder> next = new ArrayList<>();
|
||||||
|
|
||||||
|
DiffCallback(List<EntityFolder> prev, List<EntityFolder> next) {
|
||||||
|
this.prev.addAll(prev);
|
||||||
|
this.next.addAll(next);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getOldListSize() {
|
||||||
|
return prev.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getNewListSize() {
|
||||||
|
return next.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
|
||||||
|
EntityFolder m1 = prev.get(oldItemPosition);
|
||||||
|
EntityFolder m2 = next.get(newItemPosition);
|
||||||
|
return m1.id.equals(m2.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
|
||||||
|
EntityFolder m1 = prev.get(oldItemPosition);
|
||||||
|
EntityFolder m2 = next.get(newItemPosition);
|
||||||
|
return m1.id.equals(m2.id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long getItemId(int position) {
|
||||||
|
return items.get(position).id;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getItemCount() {
|
||||||
|
return items.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@NonNull
|
||||||
|
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||||
|
return new ViewHolder(inflater.inflate(R.layout.item_folder_select, parent, false));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
|
||||||
|
holder.unwire();
|
||||||
|
EntityFolder folder = items.get(position);
|
||||||
|
holder.bindTo(folder);
|
||||||
|
holder.wire();
|
||||||
|
}
|
||||||
|
|
||||||
|
interface IFolderSelectedListener {
|
||||||
|
void onFolderSelected(EntityFolder folder);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent">
|
||||||
|
|
||||||
|
<androidx.recyclerview.widget.RecyclerView
|
||||||
|
android:id="@+id/rvFolder"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:padding="12dp"
|
||||||
|
android:scrollbarStyle="outsideOverlay"
|
||||||
|
android:scrollbars="vertical"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintTop_toTopOf="parent" />
|
||||||
|
|
||||||
|
<eu.faircode.email.ContentLoadingProgressBar
|
||||||
|
android:id="@+id/pbWait"
|
||||||
|
style="@style/Base.Widget.AppCompat.ProgressBar"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:indeterminate="true"
|
||||||
|
android:padding="24dp"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintTop_toTopOf="parent" />
|
||||||
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
@ -0,0 +1,39 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content">
|
||||||
|
|
||||||
|
<androidx.constraintlayout.widget.ConstraintLayout
|
||||||
|
android:id="@+id/clItem"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:background="?attr/activatableItemBackground"
|
||||||
|
android:padding="6dp">
|
||||||
|
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/ivType"
|
||||||
|
android:layout_width="24dp"
|
||||||
|
android:layout_height="24dp"
|
||||||
|
android:contentDescription="@string/title_legend_folder_tye"
|
||||||
|
android:src="@drawable/baseline_inbox_24"
|
||||||
|
app:layout_constraintBottom_toBottomOf="@+id/tvName"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintTop_toTopOf="@+id/tvName" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/tvName"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginStart="12dp"
|
||||||
|
android:ellipsize="middle"
|
||||||
|
android:gravity="center_vertical"
|
||||||
|
android:minHeight="24dp"
|
||||||
|
android:singleLine="true"
|
||||||
|
android:text="Name"
|
||||||
|
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintStart_toEndOf="@id/ivType"
|
||||||
|
app:layout_constraintTop_toTopOf="parent" />
|
||||||
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||||
|
</FrameLayout>
|
Loading…
Reference in new issue