Skip to content

Commit

Permalink
Merge pull request haiwen#645 from haiwen/sort_folder_file
Browse files Browse the repository at this point in the history
Sort folder file
  • Loading branch information
freeplant authored May 13, 2017
2 parents bc65f20 + be61255 commit 1363d85
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.seafile.seadroid2.ui.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
Expand Down Expand Up @@ -171,6 +172,7 @@ protected void onStart() {
}
}

@SuppressLint("LongLogTag")
@Override
protected void onDestroy() {
Log.d(DEBUG_TAG, "onDestroy is called");
Expand Down Expand Up @@ -445,12 +447,32 @@ private void refreshDir() {
}

private void updateAdapterWithDirents(List<SeafDirent> dirents) {
getDirentsAdapter().setDirents(dirents);
DirentsAdapter adapter=getDirentsAdapter();
if (dirents.size() > 0) {
adapter.clearDirents();
for (SeafDirent dirent : dirents) {
adapter.add(dirent);
}
int sort_type = SettingsManager.instance().getSortFilesTypePref();
int sort_order = SettingsManager.instance().getSortFilesOrderPref();
adapter.sortFiles(sort_type, sort_order);
adapter.notifyDataSetChanged();
}
showListOrEmptyText(dirents.size());
}

private void updateAdapterWithRepos(List<SeafRepo> repos) {
getReposAdapter().setRepos(repos);
SeafReposAdapter adapter=getReposAdapter();
if (repos.size() > 0) {
adapter.clearRepos();
for (SeafRepo item: repos) {
adapter.add(item);
}
int sort_type = SettingsManager.instance().getSortFilesTypePref();
int sort_order = SettingsManager.instance().getSortFilesOrderPref();
adapter.sortFiles(sort_type, sort_order);
adapter.notifyDataSetChanged();
}
showListOrEmptyText(repos.size());
}

Expand Down Expand Up @@ -622,6 +644,7 @@ protected Void doInBackground(Void... params) {
return null;
}

@SuppressLint("LongLogTag")
@Override
protected void onPostExecute(Void v) {
showLoading(false);
Expand Down Expand Up @@ -670,6 +693,7 @@ protected Void doInBackground(Void... params) {
return null;
}

@SuppressLint("LongLogTag")
@Override
protected void onPostExecute(Void v) {
if (mStep != STEP_CHOOSE_REPO) {
Expand Down Expand Up @@ -710,6 +734,7 @@ protected Void doInBackground(Void... params) {
return null;
}

@SuppressLint("LongLogTag")
@Override
protected void onPostExecute(Void v) {
if (mStep != STEP_CHOOSE_DIR) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.seafile.seadroid2.ui.adapter;

import java.util.List;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -15,6 +13,9 @@
import com.seafile.seadroid2.SeadroidApplication;
import com.seafile.seadroid2.data.SeafDirent;

import java.util.Collections;
import java.util.List;

public class DirentsAdapter extends BaseAdapter {

private List<SeafDirent> dirents;
Expand All @@ -23,6 +24,15 @@ public DirentsAdapter() {
dirents = Lists.newArrayList();
}

/** sort files type */
public static final int SORT_BY_NAME = 9;
/** sort files type */
public static final int SORT_BY_LAST_MODIFIED_TIME = 10;
/** sort files order */
public static final int SORT_ORDER_ASCENDING = 11;
/** sort files order */
public static final int SORT_ORDER_DESCENDING = 12;

@Override
public int getCount() {
return dirents.size();
Expand All @@ -37,6 +47,7 @@ public void add(SeafDirent entry) {
dirents.add(entry);
}


@Override
public SeafDirent getItem(int position) {
return dirents.get(position);
Expand All @@ -59,6 +70,43 @@ public void setDirents(List<SeafDirent> dirents) {
notifyDataSetChanged();
}

public void sortFiles(int type, int order) {
List<SeafDirent> folders = Lists.newArrayList();
List<SeafDirent> files = Lists.newArrayList();

for (SeafDirent item : dirents) {
if (item.isDir()) {
folders.add(item);
} else {
files.add(item);
}
}

dirents.clear();

// sort SeafDirents
if (type == SORT_BY_NAME) {
// sort by name, in ascending order
Collections.sort(folders, new SeafDirent.DirentNameComparator());
Collections.sort(files, new SeafDirent.DirentNameComparator());
if (order == SORT_ORDER_DESCENDING) {
Collections.reverse(folders);
Collections.reverse(files);
}
} else if (type == SORT_BY_LAST_MODIFIED_TIME) {
// sort by last modified time, in ascending order
Collections.sort(folders, new SeafDirent.DirentLastMTimeComparator());
Collections.sort(files, new SeafDirent.DirentLastMTimeComparator());
if (order == SORT_ORDER_DESCENDING) {
Collections.reverse(folders);
Collections.reverse(files);
}
}
// Adds the objects in the specified collection to this ArrayList
dirents.addAll(folders);
dirents.addAll(files);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
Expand Down Expand Up @@ -111,4 +159,4 @@ public Viewholder(TextView title, TextView subtitle, ImageView icon) {
this.subtitle = subtitle;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.seafile.seadroid2.ui.adapter;

import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.google.common.collect.Lists;
import com.seafile.seadroid2.R;
import com.seafile.seadroid2.data.SeafRepo;

import java.util.Collections;
import java.util.List;

/**
Expand All @@ -17,6 +18,16 @@ public SeafReposAdapter(boolean onlyShowWritableRepos, String encryptedRepoId) {
super(onlyShowWritableRepos, encryptedRepoId);
}

/** sort files type */
public static final int SORT_BY_NAME = 9;
/** sort files type */
public static final int SORT_BY_LAST_MODIFIED_TIME = 10;
/** sort files order */
public static final int SORT_ORDER_ASCENDING = 11;
/** sort files order */
public static final int SORT_ORDER_DESCENDING = 12;


@Override
public int getCount() {
return repos.size();
Expand All @@ -36,6 +47,40 @@ public SeafRepo getItem(int position) {
return repos.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

public void clearRepos() {
repos.clear();
}

public void sortFiles(int type, int order) {
List<SeafRepo> folders = Lists.newArrayList();

for (SeafRepo item : repos) {
folders.add(item);
}
repos.clear();

if (type == SORT_BY_NAME) {
// sort by name, in ascending order
Collections.sort(folders, new SeafRepo.RepoNameComparator());
if (order == SORT_ORDER_DESCENDING) {
Collections.reverse(folders);
}
} else if (type == SORT_BY_LAST_MODIFIED_TIME) {
// sort by last modified time, in ascending order
Collections.sort(folders, new SeafRepo.RepoLastMTimeComparator());
if (order == SORT_ORDER_DESCENDING) {
Collections.reverse(folders);
}
}
// Adds the objects in the specified collection to this ArrayList
repos.addAll(folders);
}

@Override
protected int getChildLayout() {
return R.layout.repo_list_item;
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Sat Apr 08 14:51:02 CST 2017
#Sat Apr 08 17:29:25 CST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
Expand Down

0 comments on commit 1363d85

Please sign in to comment.