Skip to content

Commit

Permalink
Use enums where they should be used.. DisplayAdapted.java now has cod…
Browse files Browse the repository at this point in the history
…e which is a little less horrible. Various other fixes too.
  • Loading branch information
JonasCz authored and JonasCz committed Nov 19, 2015
1 parent 790756a commit 8690359
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 57 deletions.
116 changes: 88 additions & 28 deletions app/src/main/java/jonas/tool/saveForOffline/DisplayAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,63 @@

public class DisplayAdapter extends BaseAdapter {

public static class Constants {
private Constants() {}
public final static int LIST_LAYOUT_DEFAULT = 1;
public final static int LIST_LAYOUT_GRID = 2;
public final static int LIST_LAYOUT_DETAILS_THUMBNAILS = 4;
public final static int LIST_LAYOUT_DETAILS_SMALL_TEXT_ONLY = 5;
public final static int LIST_LAYOUT_SMALL_ICON = 6;
public enum SortOrder {
NEWEST_FIRST, OLDEST_FIRST, ALPHABETICAL;

public static int toInt (SortOrder o) {
switch (o) {
case OLDEST_FIRST:
return 1;
case ALPHABETICAL:
return 2;
default:
return 0;
}
}

public static SortOrder fromInt (int i) {
switch (i) {
case 1:
return OLDEST_FIRST;
case 2:
return ALPHABETICAL;
default:
return NEWEST_FIRST;
}
}
}

public enum Layout {
DEFAULT, GRID, DETAILS_THUMBNAILS, SMALL_TEXT_ONLY, SMALL_ICON;

private static Layout currentLayout = DEFAULT;

public static void setCurrentLayout (Layout l) {
currentLayout = l;
}

public static Layout getCurrentLayout () {
return currentLayout;
}

public static Layout fromInt (int i) {
switch (i) {
case 2:
return GRID;
case 4:
return DETAILS_THUMBNAILS;
case 5:
return SMALL_TEXT_ONLY;
case 6:
return SMALL_ICON;
default:
return DEFAULT;
}
}

public static boolean hasDate (Layout l) {
return l == Layout.DETAILS_THUMBNAILS || l == Layout.SMALL_TEXT_ONLY;
}
}

private Context mContext;
Expand All @@ -36,7 +86,6 @@ private Constants() {}
private String searchQuery = "";
private String sqlStatement;

public int list_layout_type = 1;
private boolean darkMode;

public ArrayList<Integer> selectedViewsPositions = new ArrayList<Integer>();
Expand All @@ -45,13 +94,19 @@ private Constants() {}

public Cursor dbCursor;

public void refreshData(String searchQuery, int sortOrder, boolean dataSetChanged) {
//sortOrder 0 = date newest first, 1 = oldest first, 2 = alphabetical
if (sortOrder == 0) sqlStatement = "SELECT * FROM " + DbHelper.TABLE_NAME + " WHERE " + DbHelper.KEY_TITLE + " LIKE'%" + searchQuery + "%' ORDER BY " + DbHelper.KEY_ID + " DESC";
if (sortOrder == 1) sqlStatement = "SELECT * FROM " + DbHelper.TABLE_NAME + " WHERE " + DbHelper.KEY_TITLE + " LIKE'%" + searchQuery + "%' ORDER BY " + DbHelper.KEY_ID + " ASC";
else if (sortOrder == 2) sqlStatement = "SELECT * FROM " + DbHelper.TABLE_NAME + " WHERE " + DbHelper.KEY_TITLE + " LIKE'%" + searchQuery + "%' ORDER BY " + DbHelper.KEY_TITLE + " ASC";
else sqlStatement = "SELECT * FROM " + DbHelper.TABLE_NAME + " WHERE " + DbHelper.KEY_TITLE + " LIKE'%" + searchQuery + "%' ORDER BY " + DbHelper.KEY_ID + " DESC";

public void refreshData(String searchQuery, SortOrder order, boolean dataSetChanged) {
switch (order) {
case OLDEST_FIRST:
sqlStatement = "SELECT * FROM " + DbHelper.TABLE_NAME + " WHERE " + DbHelper.KEY_TITLE + " LIKE'%" + searchQuery + "%' ORDER BY " + DbHelper.KEY_ID + " ASC";
break;
case ALPHABETICAL:
sqlStatement = "SELECT * FROM " + DbHelper.TABLE_NAME + " WHERE " + DbHelper.KEY_TITLE + " LIKE'%" + searchQuery + "%' ORDER BY " + DbHelper.KEY_TITLE + " ASC";
break;
default: //newest first
sqlStatement = "SELECT * FROM " + DbHelper.TABLE_NAME + " WHERE " + DbHelper.KEY_TITLE + " LIKE'%" + searchQuery + "%' ORDER BY " + DbHelper.KEY_ID + " DESC";
break;
}

dbCursor = dataBase.rawQuery(sqlStatement, null);
dbCursor.moveToFirst();
if (dataSetChanged) notifyDataSetChanged();
Expand All @@ -67,10 +122,10 @@ public DisplayAdapter(Context c) {

SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(mContext);

list_layout_type = Integer.parseInt(sharedPref.getString("layout" , "1"));
Layout.setCurrentLayout(Layout.fromInt(Integer.parseInt(sharedPref.getString("layout" , "1"))));
darkMode = sharedPref.getBoolean("dark_mode", false);

refreshData(null, 1, false);
refreshData(null, SortOrder.NEWEST_FIRST, false);

placeHolder = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.icon_website_large);

Expand Down Expand Up @@ -122,16 +177,21 @@ public String getPropertiesByPosition(int position, String type) {

private View inflateView (View convertView, Holder mHolder) {
LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
switch (list_layout_type) {
case 2: convertView = layoutInflater.inflate(R.layout.listcell_grid, null); break;
case 4: convertView = layoutInflater.inflate(R.layout.listcell_list_details, null); break;
case 5: convertView = layoutInflater.inflate(R.layout.listcell_list_details_small, null); break;
case 6: convertView = layoutInflater.inflate(R.layout.listcell_list_details_small_icon_only, null); break;
default: convertView = layoutInflater.inflate(R.layout.listcell_default, null);
switch (Layout.getCurrentLayout()) {
case GRID:
convertView = layoutInflater.inflate(R.layout.listcell_grid, null); break;
case DETAILS_THUMBNAILS:
convertView = layoutInflater.inflate(R.layout.listcell_list_details, null); break;
case SMALL_TEXT_ONLY:
convertView = layoutInflater.inflate(R.layout.listcell_list_details_small, null); break;
case SMALL_ICON:
convertView = layoutInflater.inflate(R.layout.listcell_list_details_small_icon_only, null); break;
default:
convertView = layoutInflater.inflate(R.layout.listcell_default, null);
}
if (darkMode) {
convertView.setBackgroundColor(Color.BLACK);
if (list_layout_type == 4 || list_layout_type == 5) {
if (Layout.hasDate(Layout.getCurrentLayout())) {
mHolder.txt_date = (TextView) convertView.findViewById(R.id.txt_date);
mHolder.txt_date.setTextColor(Color.WHITE);
}
Expand All @@ -142,7 +202,7 @@ private View inflateView (View convertView, Holder mHolder) {
mHolder.txt_title = (TextView) convertView.findViewById(R.id.txt_title);
mHolder.txt_title.setTextColor(Color.WHITE);
} else {
if (list_layout_type == 4 || list_layout_type == 5) {
if (Layout.hasDate(Layout.getCurrentLayout())) {
mHolder.txt_date = (TextView) convertView.findViewById(R.id.txt_date);
}
mHolder.txt_id = (TextView) convertView.findViewById(R.id.txt_id);
Expand All @@ -151,7 +211,7 @@ private View inflateView (View convertView, Holder mHolder) {
mHolder.txt_title = (TextView) convertView.findViewById(R.id.txt_title);
}

if (list_layout_type != 5) {
if (Layout.getCurrentLayout() != Layout.SMALL_TEXT_ONLY) {
mHolder.listimage = (ImageView) convertView.findViewById(R.id.listimage);
}
convertView.setTag(mHolder);
Expand All @@ -160,7 +220,7 @@ private View inflateView (View convertView, Holder mHolder) {
}

private void setListImage (ImageView imageView) {
if (list_layout_type == Constants.LIST_LAYOUT_DETAILS_SMALL_TEXT_ONLY) return;
if (Layout.getCurrentLayout() == Layout.SMALL_TEXT_ONLY) return;
switch ((String) imageView.getTag()) {
case "show:icon":
File icon = new File(new File(dbCursor.getString(dbCursor.getColumnIndex(DbHelper.KEY_THUMBNAIL))).getParent(), "saveForOffline_icon.png");
Expand Down Expand Up @@ -199,7 +259,7 @@ public View getView(int position, View convertView, ViewGroup parent) {
}
}

if (list_layout_type == 4 || list_layout_type == 5) {
if (Layout.hasDate(Layout.getCurrentLayout())) {
try {
mHolder.txt_date.setText("Saved " + fuzzyFormatter.getFuzzy(dbCursor.getString(dbCursor.getColumnIndex(DbHelper.KEY_TIMESTAMP))));
} catch (ParseException e) {
Expand Down
12 changes: 5 additions & 7 deletions app/src/main/java/jonas/tool/saveForOffline/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public class MainActivity extends Activity implements SearchView.OnQueryTextList

private TextView noSavedPages;
private TextView helpText;

private int sortOrder = 0;
private DisplayAdapter.SortOrder sortOrder = DisplayAdapter.SortOrder.NEWEST_FIRST;


private GridView mainGrid;
Expand Down Expand Up @@ -150,9 +150,9 @@ public boolean onOptionsItemSelected(MenuItem item) {

case R.id.action_sort_by:
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setSingleChoiceItems(R.array.sort_by, sortOrder, new DialogInterface.OnClickListener() {
builder.setSingleChoiceItems(R.array.sort_by, DisplayAdapter.SortOrder.toInt(sortOrder), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
sortOrder = which;
sortOrder = DisplayAdapter.SortOrder.fromInt(which);
displayData(searchQuery);
dialogSortItemsBy.cancel();
}
Expand Down Expand Up @@ -241,11 +241,9 @@ class ModeCallback implements ListView.MultiChoiceModeListener {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {

MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_multi_choice, menu);
getMenuInflater().inflate(R.menu.main_activity_multi_choice, menu);
mode.setTitle("Select Items");


return true;
}

Expand Down
8 changes: 3 additions & 5 deletions app/src/main/java/jonas/tool/saveForOffline/PageSaver.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ public class PageSaver {
private List<String> framesToGrab = new ArrayList<String>();
//cssToGrab - list of all css files to download an parse
private List<String> cssToGrab = new ArrayList<String>();
//we do another pass for this one
private List<String> extraCssToGrab = new ArrayList<String>();


private String title = "";

private String indexFileName = "index.html";
Expand Down Expand Up @@ -224,7 +222,7 @@ public void run() {
try {
Response response = client.newCall(request).execute();
InputStream is = response.body().byteStream();

FileOutputStream fos = new FileOutputStream(outputFile);
final byte[] buffer = new byte[1024 * 16]; // read in batches of 16K
int length;
Expand All @@ -237,7 +235,7 @@ public void run() {
fos.close();
is.close();

} catch (MalformedURLException e) {
} catch (IllegalArgumentException e) {
eventCallback.onError(e.getMessage());
e.printStackTrace();
} catch (IOException e) {
Expand Down
34 changes: 17 additions & 17 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">Save For Offline</string>

<string name="action_add">Add New</string>
<string name="action_delete">Delete</string>
<string name="action_open_in_external">Open Link</string>
<string name="action_bar_subtitle_showing_all">Showing all saved pages</string>
<string name="one_search_result">One search result</string>
<string name="no_search_results">No search results</string>
<string name="num_search_results">search results</string>




</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">Save For Offline</string>

<string name="action_add">Add New</string>
<string name="action_delete">Delete</string>
<string name="action_open_in_external">Open Link</string>
<string name="action_bar_subtitle_showing_all">All saved pages</string>
<string name="one_search_result">One search result</string>
<string name="no_search_results">No search results</string>
<string name="num_search_results">search results</string>




</resources>

0 comments on commit 8690359

Please sign in to comment.