From e8a84c48888355968e66fa4c319bf1bd4dd90f0a Mon Sep 17 00:00:00 2001 From: Aario Shahbany Date: Fri, 31 Mar 2017 22:21:19 +0200 Subject: [PATCH] Implemented sort feature Increased version in build.gradle Added dynamic version text to tvVersion --- app/build.gradle | 4 ++-- .../info/aario/snotepad/AboutFragment.java | 9 +++++++ .../main/java/info/aario/snotepad/Filer.java | 7 +++--- .../info/aario/snotepad/ListFragment.java | 24 ++++++++++++++++++- .../info/aario/snotepad/MainActivity.java | 12 ++++++++++ app/src/main/res/menu/menu_main.xml | 10 ++++++++ app/src/main/res/values/strings.xml | 4 +++- 7 files changed, 63 insertions(+), 7 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index a8445ee..1dba554 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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 { diff --git a/app/src/main/java/info/aario/snotepad/AboutFragment.java b/app/src/main/java/info/aario/snotepad/AboutFragment.java index 0fa2cbb..2204e7c 100644 --- a/app/src/main/java/info/aario/snotepad/AboutFragment.java +++ b/app/src/main/java/info/aario/snotepad/AboutFragment.java @@ -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; @@ -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() { diff --git a/app/src/main/java/info/aario/snotepad/Filer.java b/app/src/main/java/info/aario/snotepad/Filer.java index b2b767a..235a4fd 100644 --- a/app/src/main/java/info/aario/snotepad/Filer.java +++ b/app/src/main/java/info/aario/snotepad/Filer.java @@ -9,6 +9,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.text.SimpleDateFormat; import java.util.Date; /** @@ -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; @@ -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 ""; } } diff --git a/app/src/main/java/info/aario/snotepad/ListFragment.java b/app/src/main/java/info/aario/snotepad/ListFragment.java index b051e0d..0c52ba3 100644 --- a/app/src/main/java/info/aario/snotepad/ListFragment.java +++ b/app/src/main/java/info/aario/snotepad/ListFragment.java @@ -10,7 +10,6 @@ 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; @@ -18,6 +17,8 @@ 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; @@ -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 fileNameList = new ArrayList(); @@ -72,6 +74,21 @@ private void populateFilesList() { item.put("date", activity.filer.getModifiedTimestamp(path + "/" + fileName)); data.add(item); } + Collections.sort(data, new Comparator>() { + @Override + public int compare(Map o1, Map 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"}, @@ -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; diff --git a/app/src/main/java/info/aario/snotepad/MainActivity.java b/app/src/main/java/info/aario/snotepad/MainActivity.java index 7f1dc2d..e061b26 100644 --- a/app/src/main/java/info/aario/snotepad/MainActivity.java +++ b/app/src/main/java/info/aario/snotepad/MainActivity.java @@ -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); diff --git a/app/src/main/res/menu/menu_main.xml b/app/src/main/res/menu/menu_main.xml index 2477b11..3bedf9c 100644 --- a/app/src/main/res/menu/menu_main.xml +++ b/app/src/main/res/menu/menu_main.xml @@ -7,6 +7,16 @@ android:orderInCategory="100" android:title="@string/action_refresh" app:showAsAction="never" /> + + SNotepad Refresh + Sort by Name + Sort by Date Settings About Title is mandatory @@ -10,7 +12,7 @@ Redo Saving path: Select Folder - Version: 1.0.0 + Version: Copyright (C) 2017 Aario Shahbany<br/><br/> SNotepad is simple yet professional notepad app for android. The S stands for Suckless/Simple.<br/>