Skip to content

Commit

Permalink
Implemented sort feature
Browse files Browse the repository at this point in the history
Increased version in build.gradle
Added dynamic version text to tvVersion
  • Loading branch information
aario committed Mar 31, 2017
1 parent f43caa2 commit e8a84c4
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 7 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "info.aario.snotepad"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
versionCode 2
versionName "1.2.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/java/info/aario/snotepad/AboutFragment.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package info.aario.snotepad;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
Expand Down Expand Up @@ -38,6 +39,14 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
} else {
tvAbout.setText(Html.fromHtml(aboutHtml));
}

TextView tvVersion = (TextView) view.findViewById(R.id.tvVersion);
try {
tvVersion.setText(tvVersion.getText() + activity.getPackageManager().getPackageInfo(activity.getPackageName(), 0).versionName);
} catch (PackageManager.NameNotFoundException e) {
tvVersion.setVisibility(View.INVISIBLE);
}

FloatingActionButton fab = (FloatingActionButton) activity.findViewById(R.id.fab);
fab.setImageDrawable(ContextCompat.getDrawable(activity, android.R.drawable.ic_dialog_info));
fab.setOnClickListener(new View.OnClickListener() {
Expand Down
7 changes: 4 additions & 3 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.text.SimpleDateFormat;
import java.util.Date;

/**
Expand All @@ -17,6 +18,7 @@

public class Filer {
private MainActivity activity;
public SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

public Filer(MainActivity mainActivity) {
activity = mainActivity;
Expand Down Expand Up @@ -102,10 +104,9 @@ 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;
return dateFormat.format(new Date(fl.lastModified()));
return "";
}

}
24 changes: 23 additions & 1 deletion app/src/main/java/info/aario/snotepad/ListFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
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.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -30,6 +31,7 @@ public class ListFragment extends Fragment {
private MainActivity activity;
private String path;
private final String extension = ".txt";
private boolean sortByDate = false;
ListView lvFiles;
SearchView svSearch;
ArrayList<String> fileNameList = new ArrayList<String>();
Expand Down Expand Up @@ -72,6 +74,21 @@ private void populateFilesList() {
item.put("date", activity.filer.getModifiedTimestamp(path + "/" + fileName));
data.add(item);
}
Collections.sort(data, new Comparator<Map<String, String>>() {
@Override
public int compare(Map<String, String> o1, Map<String, String> o2) {
if (sortByDate)
try {
return activity.filer.dateFormat.parse(o2.get("date")).compareTo(
activity.filer.dateFormat.parse(o1.get("date"))
);
} catch (Exception e) {
return 0;
}

return o2.get("title").compareTo(o1.get("title"));
}
});
SimpleAdapter adapter = new SimpleAdapter(activity, data,
android.R.layout.simple_list_item_2,
new String[]{"title", "date"},
Expand All @@ -91,6 +108,11 @@ public void refresh() {
svSearch.setQuery("", true);
}

public void sort(boolean byDate) {
sortByDate = byDate;
populateFilesList();
}

public String proposeNewFilePath(String prefix) {
int i = 0;
String result;
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/java/info/aario/snotepad/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,18 @@ public boolean onOptionsItemSelected(MenuItem item) {
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_sort_by_name) {
listFragment.sort(false);
return true;
}

//noinspection SimplifiableIfStatement
if (id == R.id.action_sort_by_date) {
listFragment.sort(true);
return true;
}

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
changeFragment(new SettingsFragment(), true);
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/menu/menu_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@
android:orderInCategory="100"
android:title="@string/action_refresh"
app:showAsAction="never" />
<item
android:id="@+id/action_sort_by_name"
android:orderInCategory="100"
android:title="@string/action_sort_by_name"
app:showAsAction="never" />
<item
android:id="@+id/action_sort_by_date"
android:orderInCategory="100"
android:title="@string/action_sort_by_date"
app:showAsAction="never" />
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<resources>
<string name="app_name">SNotepad</string>
<string name="action_refresh">Refresh</string>
<string name="action_sort_by_name">Sort by Name</string>
<string name="action_sort_by_date">Sort by Date</string>
<string name="action_settings">Settings</string>
<string name="action_about">About</string>
<string name="etTitle_hint">Title is mandatory</string>
Expand All @@ -10,7 +12,7 @@
<string name="btRedo_text">Redo</string>
<string name="saving_path">Saving path:</string>
<string name="btChangePath_title">Select Folder</string>
<string name="tvVersion_text">Version: 1.0.0</string>
<string name="tvVersion_text">Version: </string>
<string name="tvAbout_text">Copyright (C) 2017 Aario Shahbany&lt;br/&gt;&lt;br/&gt;
SNotepad is simple yet professional notepad app for android. The S
stands for Suckless/Simple.&lt;br/&gt;
Expand Down

0 comments on commit e8a84c4

Please sign in to comment.