Skip to content

Commit

Permalink
Added file modified timestamp to the list
Browse files Browse the repository at this point in the history
  • Loading branch information
aario committed Mar 27, 2017
1 parent 8b52820 commit f43caa2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
9 changes: 9 additions & 0 deletions app/src/main/java/info/aario/snotepad/Filer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Date;

/**
* Created by aario on 3/16/17.
Expand Down Expand Up @@ -99,4 +100,12 @@ public String getStringFromFile(String filePath) {
}
}

public String getModifiedTimestamp(String filePath) {
File fl = new File(filePath);
String ret = "";
if (fl.exists())
ret = new Date(fl.lastModified()).toString();
return ret;
}

}
25 changes: 19 additions & 6 deletions app/src/main/java/info/aario/snotepad/ListFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SimpleAdapter;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
Expand All @@ -30,7 +32,7 @@ public class ListFragment extends Fragment {
private final String extension = ".txt";
ListView lvFiles;
SearchView svSearch;
ArrayList<String> filePathList = new ArrayList<String>();
ArrayList<String> fileNameList = new ArrayList<String>();

Map<String, String> contentsCache = new HashMap<String, String>();

Expand Down Expand Up @@ -63,12 +65,23 @@ public boolean accept(File file) {
}

private void populateFilesList() {
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(activity, android.R.layout.simple_list_item_1, filePathList);
lvFiles.setAdapter(arrayAdapter);
List<Map<String, String>> data = new ArrayList<Map<String, String>>();
for (String fileName : fileNameList) {
Map<String, String> item = new HashMap<String, String>(2);
item.put("title", fileName);
item.put("date", activity.filer.getModifiedTimestamp(path + "/" + fileName));
data.add(item);
}
SimpleAdapter adapter = new SimpleAdapter(activity, data,
android.R.layout.simple_list_item_2,
new String[]{"title", "date"},
new int[]{android.R.id.text1,
android.R.id.text2});
lvFiles.setAdapter(adapter);
}

private void search(String text) {
filePathList = searchFiles(text);
fileNameList = searchFiles(text);
populateFilesList();
}

Expand Down Expand Up @@ -98,7 +111,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
lvFiles.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
activity.editFile(path + "/" + filePathList.get(arg2));
activity.editFile(path + "/" + fileNameList.get(arg2));
}
});
registerForContextMenu(lvFiles);
Expand Down Expand Up @@ -139,7 +152,7 @@ public boolean onContextItemSelected(final MenuItem item) {

//noinspection SimplifiableIfStatement
if (id == R.id.action_delete) {
String filename = filePathList.get(itemInfo.position);
String filename = fileNameList.get(itemInfo.position);
if (activity.filer.delete(path + "/" + filename)) {
search(svSearch.getQuery().toString());
activity.makeSnackBar("File " + filename + " successfully deleted.");
Expand Down

0 comments on commit f43caa2

Please sign in to comment.