Kaydet (Commit) 686651c3 authored tarafından brainbreaker's avatar brainbreaker Kaydeden (comit) Aleksandar Stefanović

Combine List Item & Grid Item Adapters of File Explorer View

 This commit will combine the code of ListItemAdapter &
  GridItemAdapter in LibreOfficeUIActivity to one
  ExplorerItemAdapter which will handle both the view
  types.

Change-Id: I45c1f5124afee82ff0b78f13609acd37be87fde1
Reviewed-on: https://gerrit.libreoffice.org/34680Tested-by: 's avatarJenkins <ci@libreoffice.org>
Reviewed-by: 's avatarAleksandar Stefanović <theonewithideas@gmail.com>
Tested-by: 's avatarAleksandar Stefanović <theonewithideas@gmail.com>
üst 6bf42c1c
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
android:orientation="vertical" > android:orientation="vertical" >
<ImageView <ImageView
android:id="@+id/grid_item_image" android:id="@+id/file_item_icon"
tools:src="@drawable/ic_folder_black_24dp" tools:src="@drawable/ic_folder_black_24dp"
tools:tint="@color/text_color_secondary" tools:tint="@color/text_color_secondary"
android:layout_width="100dp" android:layout_width="100dp"
...@@ -25,7 +25,7 @@ ...@@ -25,7 +25,7 @@
</ImageView> </ImageView>
<TextView <TextView
android:id="@+id/grid_item_label" android:id="@+id/file_item_name"
tools:text="file or dirname" tools:text="file or dirname"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
android:layout_marginEnd="@dimen/list_item_margin" android:layout_marginEnd="@dimen/list_item_margin"
android:layout_marginRight="@dimen/list_item_margin"> android:layout_marginRight="@dimen/list_item_margin">
<ImageView <ImageView
android:id="@+id/file_list_item_icon" android:id="@+id/file_item_icon"
tools:src="@drawable/ic_folder_black_24dp" tools:src="@drawable/ic_folder_black_24dp"
tools:tint="@color/text_color_secondary" tools:tint="@color/text_color_secondary"
android:layout_height="match_parent" android:layout_height="match_parent"
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
android:layout_height="48dp" android:layout_height="48dp"
android:orientation="horizontal"> android:orientation="horizontal">
<TextView <TextView
android:id="@+id/file_list_item_name" android:id="@+id/file_item_name"
tools:text="file or dirname" tools:text="file or dirname"
style="@style/ListItemText" style="@style/ListItemText"
android:layout_height="match_parent" android:layout_height="match_parent"
...@@ -38,14 +38,14 @@ ...@@ -38,14 +38,14 @@
android:ellipsize="end" android:ellipsize="end"
android:maxLines="1"/> android:maxLines="1"/>
<TextView <TextView
android:id="@+id/file_list_item_size" android:id="@+id/file_item_size"
tools:text="filesize" tools:text="filesize"
style="@style/ListItemText" style="@style/ListItemText"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_weight="1" /> android:layout_weight="1" />
<TextView <TextView
android:id="@+id/file_list_item_date" android:id="@+id/file_item_date"
tools:text="date/time" tools:text="date/time"
style="@style/ListItemText" style="@style/ListItemText"
android:layout_height="match_parent" android:layout_height="match_parent"
......
...@@ -29,7 +29,6 @@ ...@@ -29,7 +29,6 @@
<item >0</item> <item >0</item>
<item >1</item> <item >1</item>
</string-array> </string-array>
<string-array name="FilterTypeNames"> <string-array name="FilterTypeNames">
<item>Everything</item> <item>Everything</item>
<item>Documents</item> <item>Documents</item>
......
...@@ -268,13 +268,8 @@ public class LibreOfficeUIActivity extends AppCompatActivity implements Settings ...@@ -268,13 +268,8 @@ public class LibreOfficeUIActivity extends AppCompatActivity implements Settings
FileUtilities.sortFiles(filePaths, sortMode); FileUtilities.sortFiles(filePaths, sortMode);
// refresh view // refresh view
if (viewMode == GRID_VIEW) { fileRecyclerView.setLayoutManager(isViewModeList() ? new LinearLayoutManager(this) : new GridLayoutManager(this, 3));
fileRecyclerView.setLayoutManager(new GridLayoutManager(this, 3)); fileRecyclerView.setAdapter(new ExplorerItemAdapter(this, filePaths));
fileRecyclerView.setAdapter(new GridItemAdapter(this, filePaths));
} else {
fileRecyclerView.setLayoutManager(new LinearLayoutManager(this));
fileRecyclerView.setAdapter(new ListItemAdapter(this, filePaths));
}
// close drawer if it was open // close drawer if it was open
drawerLayout.closeDrawer(navigationDrawer); drawerLayout.closeDrawer(navigationDrawer);
} }
...@@ -328,6 +323,10 @@ public class LibreOfficeUIActivity extends AppCompatActivity implements Settings ...@@ -328,6 +323,10 @@ public class LibreOfficeUIActivity extends AppCompatActivity implements Settings
} }
} }
private boolean isViewModeList(){
return viewMode == LIST_VIEW;
}
private void switchToDocumentProvider(IDocumentProvider provider) { private void switchToDocumentProvider(IDocumentProvider provider) {
new AsyncTask<IDocumentProvider, Void, Void>() { new AsyncTask<IDocumentProvider, Void, Void>() {
...@@ -875,15 +874,14 @@ public class LibreOfficeUIActivity extends AppCompatActivity implements Settings ...@@ -875,15 +874,14 @@ public class LibreOfficeUIActivity extends AppCompatActivity implements Settings
} }
} }
class ExplorerItemAdapter extends RecyclerView.Adapter<ExplorerItemAdapter.ViewHolder> {
class ListItemAdapter extends RecyclerView.Adapter<ListItemAdapter.ViewHolder> {
private Activity mActivity; private Activity mActivity;
private List<IFile> filePaths; private List<IFile> filePaths;
private final long KB = 1024; private final long KB = 1024;
private final long MB = 1048576; private final long MB = 1048576;
ListItemAdapter(Activity activity, List<IFile> filePaths) { ExplorerItemAdapter(Activity activity, List<IFile> filePaths) {
this.mActivity = activity; this.mActivity = activity;
this.filePaths = filePaths; this.filePaths = filePaths;
} }
...@@ -891,7 +889,7 @@ public class LibreOfficeUIActivity extends AppCompatActivity implements Settings ...@@ -891,7 +889,7 @@ public class LibreOfficeUIActivity extends AppCompatActivity implements Settings
@Override @Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View item = LayoutInflater.from(parent.getContext()) View item = LayoutInflater.from(parent.getContext())
.inflate(R.layout.file_list_item, parent, false); .inflate(isViewModeList() ? R.layout.file_list_item : R.layout.file_explorer_grid_item, parent, false);
return new ViewHolder(item); return new ViewHolder(item);
} }
...@@ -917,24 +915,6 @@ public class LibreOfficeUIActivity extends AppCompatActivity implements Settings ...@@ -917,24 +915,6 @@ public class LibreOfficeUIActivity extends AppCompatActivity implements Settings
}); });
holder.filenameView.setText(file.getName()); holder.filenameView.setText(file.getName());
if (!file.isDirectory()) {
String size;
long length = filePaths.get(position).getSize();
if (length < KB){
size = Long.toString(length) + "B";
} else if (length < MB){
size = Long.toString(length/KB) + "KB";
} else {
size = Long.toString(length/MB) + "MB";
}
holder.fileSizeView.setText(size);
}
SimpleDateFormat df = new SimpleDateFormat("dd MMM yyyy hh:ss");
Date date = file.getLastModified();
//TODO format date
holder.fileDateView.setText(df.format(date));
switch (FileUtilities.getType(file.getName())) { switch (FileUtilities.getType(file.getName())) {
case FileUtilities.DOC: case FileUtilities.DOC:
holder.iconView.setImageResource(R.drawable.writer); holder.iconView.setImageResource(R.drawable.writer);
...@@ -954,96 +934,28 @@ public class LibreOfficeUIActivity extends AppCompatActivity implements Settings ...@@ -954,96 +934,28 @@ public class LibreOfficeUIActivity extends AppCompatActivity implements Settings
//Eventually have thumbnails of each sub file on a black circle //Eventually have thumbnails of each sub file on a black circle
//For now just a folder icon //For now just a folder icon
holder.iconView.setImageResource(R.drawable.ic_folder_black_24dp); holder.iconView.setImageResource(R.drawable.ic_folder_black_24dp);
holder.iconView.setColorFilter(ContextCompat.getColor(mActivity,R.color.text_color_secondary)); holder.iconView.setColorFilter(ContextCompat.getColor(mActivity, R.color.text_color_secondary));
}
} }
@Override // Date and Size field only exist when we are displaying items in a list.
public int getItemCount() { if(isViewModeList()) {
return filePaths.size(); if (!file.isDirectory()) {
} String size;
long length = filePaths.get(position).getSize();
class ViewHolder extends RecyclerView.ViewHolder { if (length < KB) {
size = Long.toString(length) + "B";
View itemView; } else if (length < MB) {
TextView filenameView, fileSizeView, fileDateView; size = Long.toString(length / KB) + "KB";
ImageView iconView; } else {
size = Long.toString(length / MB) + "MB";
ViewHolder(View itemView) {
super(itemView);
this.itemView = itemView;
filenameView = (TextView) itemView.findViewById(R.id.file_list_item_name);
fileSizeView = (TextView) itemView.findViewById(R.id.file_list_item_size);
fileDateView = (TextView) itemView.findViewById(R.id.file_list_item_date);
iconView = (ImageView) itemView.findViewById(R.id.file_list_item_icon);
}
}
}
class GridItemAdapter extends RecyclerView.Adapter<GridItemAdapter.ViewHolder> {
private Activity mActivity;
private List<IFile> filePaths;
GridItemAdapter(Activity mActivity, List<IFile> filePaths) {
this.mActivity = mActivity;
this.filePaths = filePaths;
}
@Override
public GridItemAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View item = LayoutInflater.from(parent.getContext())
.inflate(R.layout.file_explorer_grid_item, parent, false);
return new ViewHolder(item);
}
@Override
public void onBindViewHolder(final GridItemAdapter.ViewHolder holder, final int position) {
final IFile file = filePaths.get(position);
holder.itemView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
open(position);
}
});
holder.itemView.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
//to be picked out by floating context menu (workaround-ish)
currentlySelectedFile = position;
//must return false so the click is not consumed
return false;
} }
}); holder.fileSizeView.setText(size);
String filename = file.getName();
holder.filenameView.setText(filename);
switch (FileUtilities.getType(filename)) {
case FileUtilities.DOC:
holder.iconView.setImageResource(R.drawable.writer);
break;
case FileUtilities.CALC:
holder.iconView.setImageResource(R.drawable.calc);
break;
case FileUtilities.DRAWING:
holder.iconView.setImageResource(R.drawable.draw);
break;
case FileUtilities.IMPRESS:
holder.iconView.setImageResource(R.drawable.impress);
break;
} }
SimpleDateFormat df = new SimpleDateFormat("dd MMM yyyy hh:ss");
if (file.isDirectory()) { Date date = file.getLastModified();
//Eventually have thumbnails of each sub file on a black circle //TODO format date
//For now just a folder icon holder.fileDateView.setText(df.format(date));
holder.iconView.setImageResource(R.drawable.ic_folder_black_24dp);
holder.iconView.setColorFilter(ContextCompat.getColor(mActivity,R.color.text_color_secondary));
} }
} }
@Override @Override
...@@ -1054,14 +966,19 @@ public class LibreOfficeUIActivity extends AppCompatActivity implements Settings ...@@ -1054,14 +966,19 @@ public class LibreOfficeUIActivity extends AppCompatActivity implements Settings
class ViewHolder extends RecyclerView.ViewHolder { class ViewHolder extends RecyclerView.ViewHolder {
View itemView; View itemView;
TextView filenameView; TextView filenameView, fileSizeView, fileDateView;
ImageView iconView; ImageView iconView;
ViewHolder(View itemView) { ViewHolder(View itemView) {
super(itemView); super(itemView);
this.itemView = itemView; this.itemView = itemView;
this.filenameView = (TextView) itemView.findViewById(R.id.grid_item_label); filenameView = (TextView) itemView.findViewById(R.id.file_item_name);
this.iconView = (ImageView) itemView.findViewById(R.id.grid_item_image); iconView = (ImageView) itemView.findViewById(R.id.file_item_icon);
// Check if view mode is List, only then initialise Size and Date field
if (isViewModeList()) {
fileSizeView = (TextView) itemView.findViewById(R.id.file_item_size);
fileDateView = (TextView) itemView.findViewById(R.id.file_item_date);
}
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment