Skip to content
This repository has been archived by the owner on Jan 29, 2023. It is now read-only.

Show number of items #83

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ public class FileManagerActivityTest {
private final File testChildDirectory = new File(testDirectory, "testChildDir");
private final File testCopyDestination = new File(testDirectory, "testCopyDestination");
private final File testChildFile = new File(testDirectory, "testChildFile");
private final File testDirectoryWithZeroItems = new File(testDirectory, "testDirWithZeroItems");
private final File testDirectoryWithOneItem = new File(testDirectory, "testDirWithOneItem");
private final File testDirectoryWithTwoItems = new File(testDirectory, "testDirWithMultipleItems");

private final File countTestFile1 = new File(testDirectoryWithOneItem, "countTestFile1");
private final File countTestFile2 = new File(testDirectoryWithTwoItems, "countTestFile2");
private final File countTestFile3 = new File(testDirectoryWithTwoItems, "countTestFile3");

@SuppressWarnings("ResultOfMethodCallIgnored")
@Before
Expand All @@ -46,6 +53,14 @@ public void setUp() throws Exception {
testChildDirectory.mkdir();
testCopyDestination.mkdir();
testChildFile.createNewFile();

testDirectoryWithZeroItems.mkdir();
testDirectoryWithOneItem.mkdir();
testDirectoryWithTwoItems.mkdir();

countTestFile1.createNewFile();
countTestFile2.createNewFile();
countTestFile3.createNewFile();
}

@SuppressWarnings("ResultOfMethodCallIgnored")
Expand All @@ -67,6 +82,14 @@ public void showsLaunchedDirectory() throws Exception {
user.sees().fileInList(testChildFile);
}

@Test
public void showsCorrectCounts() throws Exception {
user.launches().viewWithFileScheme(testDirectory);
user.sees().fileInListWithSizeInfo(testDirectoryWithZeroItems, "No items");
user.sees().fileInListWithSizeInfo(testDirectoryWithOneItem, "1 item");
user.sees().fileInListWithSizeInfo(testDirectoryWithTwoItems, "2 items");
}

@Test
public void navigatesFwdAndBwd() throws Exception {
user.launches().dir();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static android.support.test.espresso.matcher.ViewMatchers.withParent;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static com.veniosg.dir.test.matcher.FileHolderHasName.hasName;
import static com.veniosg.dir.test.matcher.FileHolderHasSizeInfo.hasSizeInfo;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
Expand All @@ -39,12 +40,23 @@ public void fileInList(String filename) {
)).check(matches(isDisplayed()));
}

public void fileInListWithSizeInfo(File file, String sizeInfo) {
onData(allOf(
is(instanceOf(FileHolder.class)),
hasName(file.getName()),
hasSizeInfo(sizeInfo))
).inAdapterView(allOf(
withId(android.R.id.list),
isDescendantOfA(withId(R.id.zoomview))
)).check(matches(isDisplayed()));
}

public void fileInPath(File file) {
String pathFragmentName = file.getName();
onView(allOf(
withParent(withClassName(equalTo(PathItemView.class.getName()))),
withText(pathFragmentName))
).check(matches(isDisplayed()));
withText(pathFragmentName)
)).check(matches(isDisplayed()));
}

public void activityTitle(String title) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.veniosg.dir.test.matcher;

import android.support.test.InstrumentationRegistry;
import com.veniosg.dir.mvvm.model.FileHolder;
import org.hamcrest.Factory;
import org.hamcrest.FeatureMatcher;
import org.hamcrest.Matcher;

import static android.support.test.InstrumentationRegistry.getTargetContext;
import static org.hamcrest.core.IsEqual.equalTo;

public class FileHolderHasSizeInfo<T> extends FeatureMatcher<T, String> {
private FileHolderHasSizeInfo(Matcher<? super String> hasFileHolderMatcher) {
super(hasFileHolderMatcher, "with getSizeInfo()", "getSizeInfo()");
}

@Override
protected String featureValueOf(T actual) {
if (actual instanceof FileHolder) {
return ((FileHolder) actual).getSizeInfo(getTargetContext());
} else {
return null;
}
}

/**
* Creates a matcher that matches any examined object whose <code>toString</code> method
* returns a value equalTo the specified string.
* <p/>
* For example:
* <pre>assertThat(true, hasToString("TRUE"))</pre>
*
* @param expectedSizeInfo the expected toString result
*/
@Factory
public static <T> Matcher<T> hasSizeInfo(String expectedSizeInfo) {
return new FileHolderHasSizeInfo<>(equalTo(expectedSizeInfo));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,17 @@ View newView(Context context){

@Override
public View getView(int position, View convertView, ViewGroup parent) {
Context context = parent.getContext();
FileHolder item = mItems.get(position);
if(convertView == null)
convertView = newView(parent.getContext());
if (convertView == null) convertView = newView(context);
ViewHolder holder = (ViewHolder) convertView.getTag();

getInstance().cancelDisplayTask(holder.icon);
holder.icon.setImageDrawable(item.getBestIcon());
holder.primaryInfo.setText(item.getName());
holder.secondaryInfo.setText(item.getFormattedModificationDate(convertView.getContext()));
holder.secondaryInfo.setText(item.getFormattedModificationDate(context));
// Hide directories' size as it's irrelevant if we can't recursively find it.
holder.tertiaryInfo.setText(item.getFile().isDirectory()? "" : item.getFormattedSize(
convertView.getContext(), false));
holder.tertiaryInfo.setText(item.getSizeInfo(context));

ThumbnailHelper.requestIcon(item, holder.icon);

Expand All @@ -128,4 +127,4 @@ private boolean shouldLoadIcon(FileHolder item){
public interface OnItemToggleListener {
public void onItemToggle(int position);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void bind(String filePath, OnItemClickListener listener) {

primaryInfo.setText(item.getName());
secondaryInfo.setText(item.getFormattedModificationDate(context));
tertiaryInfo.setText(isDirectory ? "" : item.getFormattedSize(context, false));
tertiaryInfo.setText(item.getSizeInfo(context));
icon.setImageDrawable(item.getBestIcon());
requestIcon(item, icon);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,4 @@ protected String doInBackground(Void... params) {
return "" + options.outHeight + 'x' + options.outWidth;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.util.List;

public class DirectoryHolder {
public List<FileHolder> listDir;
public List<FileHolder> listDir;
public List<FileHolder> listFile;
public List<FileHolder> listSdCard;

Expand Down
21 changes: 18 additions & 3 deletions app/src/main/java/com/veniosg/dir/mvvm/model/FileHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import android.text.format.DateUtils;
import android.text.format.Formatter;

import com.veniosg.dir.R;
import com.veniosg.dir.android.FileManagerApplication;
import com.veniosg.dir.android.misc.MimeTypes;
import com.veniosg.dir.android.util.FileUtils;
Expand All @@ -32,9 +33,11 @@
import java.io.File;

public class FileHolder implements Parcelable, Comparable<FileHolder> {
private static final String EMPTY_STRING = "";

private File mFile;
private Drawable mIcon;
private Drawable mPreview;
private Drawable mPreview;
private String mMimeType = "";
private String mExtension;

Expand Down Expand Up @@ -133,13 +136,25 @@ public CharSequence getFormattedModificationDate(Context c){
DateUtils.MINUTE_IN_MILLIS, DateUtils.YEAR_IN_MILLIS * 10, 0);
}

public String getSizeInfo(@NonNull Context context) {
boolean isDirectory = mFile.isDirectory();
return isDirectory ? getFormattedNumberOfItems(context) : getFormattedSize(context, false);
}

/**
* @param recursive Whether to return size of the whole tree below this file (Directories only).
*/
public String getFormattedSize(Context c, boolean recursive){
public String getFormattedSize(@NonNull Context c, boolean recursive){
return Formatter.formatFileSize(c, getSizeInBytes(recursive));
}

private String getFormattedNumberOfItems(@NonNull Context context) {
int numOfDirectoryItems = mFile.listFiles().length;
return context
.getResources()
.getQuantityString(R.plurals.num_of_directory_items, numOfDirectoryItems, numOfDirectoryItems);
}

private long getSizeInBytes(boolean recursive){
if (recursive && mFile.isDirectory())
return FileUtils.folderSize(mFile);
Expand Down Expand Up @@ -185,4 +200,4 @@ private String parseExtension() {
}
return ext;
}
}
}
8 changes: 8 additions & 0 deletions app/src/main/res/values/plurals.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep this file but point to strings in strings.xml. If it's allowed, this should be marked as translatable=false.

e.g. <item quantity="zero">@string/no_items</item>

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

<plurals name="num_of_directory_items" translatable="false">
<item quantity="zero">@string/zero_items</item>
<item quantity="one">@string/one_item</item>
<item quantity="other">@string/more_items</item>
</plurals>
</resources>
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,8 @@
<string name="goto_path">Go to path</string>
<!-- Shown when the Marshmallow permissions have been denied. -->
<string name="permissions_denied">Dir needs your permission to access storage</string>
<!-- # of items in a Directory -->
<string name="zero_items">No items</string>
<string name="one_item">%1$s item</string>
<string name="more_items">%1$s items</string>
</resources>