Skip to content

Commit ee1481f

Browse files
committed
Simplify thread item layout and split thread item logic from convoluted AwfulCursorAdapter. Use networkimageview to replace AQ library for thread tags.
1 parent 1e8301d commit ee1481f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+5938
-95
lines changed

Diff for: .gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,6 @@ bin
3939

4040
*.idea/
4141
*.iml
42-
*.gradle
42+
*.gradle
43+
tests
44+
out

Diff for: application/res/layout/nu_thread_item.xml

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="fill_parent"
4+
android:layout_height="fill_parent"
5+
android:paddingTop="4dp"
6+
android:paddingBottom="6dp">
7+
<TextView
8+
android:id="@+id/title"
9+
android:layout_width="wrap_content"
10+
android:layout_height="wrap_content"
11+
android:minHeight="30dp"
12+
android:layout_alignParentLeft="true"
13+
android:layout_toLeftOf="@+id/unread_count"
14+
android:textColor="@color/default_post_font"
15+
android:layout_marginLeft="8dp"
16+
android:layout_marginBottom="2dp"
17+
android:textSize="18dip" />
18+
19+
<com.android.volley.toolbox.NetworkImageView
20+
android:id="@+id/thread_tag"
21+
android:layout_width="60dp"
22+
android:layout_height="15dp"
23+
android:layout_below="@id/title"
24+
android:layout_marginLeft="8dp" />
25+
26+
<TextView
27+
android:id="@+id/threadinfo"
28+
android:layout_width="wrap_content"
29+
android:layout_height="wrap_content"
30+
android:layout_below="@id/title"
31+
android:layout_toRightOf="@+id/thread_tag"
32+
android:layout_toLeftOf="@+id/unread_count"
33+
android:layout_alignTop="@+id/thread_tag"
34+
android:includeFontPadding="false"
35+
android:layout_marginLeft="4dp"
36+
android:textColor="@color/secondary_post_font"
37+
android:textSize="12dip" />
38+
39+
<TextView
40+
android:id="@+id/unread_count"
41+
android:layout_width="wrap_content"
42+
android:layout_height="wrap_content"
43+
android:layout_alignParentRight="true"
44+
android:layout_centerVertical="true"
45+
android:layout_marginLeft="12dip"
46+
android:layout_marginRight="4dip"
47+
android:minWidth="30dp"
48+
android:gravity="center_horizontal"
49+
android:text="151"
50+
android:background="@drawable/unread_counter"
51+
android:textColor="#ffffff"
52+
android:textSize="14dip"
53+
android:textStyle="bold" />
54+
55+
</RelativeLayout>

Diff for: application/src/com/ferg/awfulapp/AwfulApplication.java

+25-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
import java.util.HashMap;
66
import java.util.Set;
77

8+
import com.android.volley.Request;
9+
import com.android.volley.RequestQueue;
10+
import com.android.volley.toolbox.ImageLoader;
11+
import com.android.volley.toolbox.Volley;
12+
import com.ferg.awfulapp.util.LRUImageCache;
813
import org.acra.ACRA;
914
import org.acra.annotation.ReportsCrashes;
1015

@@ -29,6 +34,10 @@ public class AwfulApplication extends Application implements AwfulUpdateCallback
2934
private AwfulPreferences mPref;
3035
private HashMap<String, Typeface> fonts = new HashMap<String, Typeface>();
3136

37+
private RequestQueue networkQueue;
38+
private ImageLoader imageLoader;
39+
private LRUImageCache imageCache;
40+
3241
private Typeface currentFont;
3342
@Override
3443
public void onCreate() {
@@ -49,6 +58,10 @@ public void onCreate() {
4958
e.printStackTrace();
5059
}
5160
}
61+
62+
networkQueue = Volley.newRequestQueue(this);
63+
imageCache = new LRUImageCache();
64+
imageLoader = new ImageLoader(networkQueue, imageCache);
5265
}
5366

5467
public void setFontFromPreference(TextView textView, int flags){
@@ -154,6 +167,16 @@ public File getCacheDir() {
154167
Log.e(TAG,"getCacheDir(): "+super.getCacheDir());
155168
return super.getCacheDir();
156169
}
157-
158-
170+
171+
public void queueRequest(Request request){
172+
networkQueue.add(request);
173+
}
174+
175+
public void cancelRequests(Object tag){
176+
networkQueue.cancelAll(tag);
177+
}
178+
179+
public ImageLoader getImageLoader(){
180+
return imageLoader;
181+
}
159182
}

Diff for: application/src/com/ferg/awfulapp/AwfulFragment.java

+33-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
import com.actionbarsherlock.view.ActionMode;
4343
import com.actionbarsherlock.view.Menu;
4444
import com.actionbarsherlock.view.MenuItem;
45+
import com.android.volley.Request;
46+
import com.android.volley.toolbox.ImageLoader;
4547
import com.androidquery.AQuery;
4648
import com.ferg.awfulapp.constants.Constants;
4749
import com.ferg.awfulapp.preferences.AwfulPreferences;
@@ -324,5 +326,35 @@ public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
324326
@Override
325327
public void onDestroyActionMode(ActionMode mode) { }
326328

327-
329+
protected AwfulApplication getAwfulApplication(){
330+
AwfulActivity act = getAwfulActivity();
331+
if(act != null){
332+
return (AwfulApplication) act.getApplication();
333+
}
334+
return null;
335+
}
336+
337+
protected void queueRequest(Request request, boolean cancelOnDestroy){
338+
AwfulApplication app = getAwfulApplication();
339+
if(app != null && request != null){
340+
if(cancelOnDestroy){
341+
request.setTag(this);
342+
}
343+
app.queueRequest(request);
344+
}
345+
}
346+
347+
protected void cancelNetworkRequests(){
348+
AwfulApplication app = getAwfulApplication();
349+
if(app != null){
350+
app.cancelRequests(this);
351+
}
352+
}
353+
354+
public ImageLoader getImageLoader(){
355+
if(getAwfulApplication() != null){
356+
return getAwfulApplication().getImageLoader();
357+
}
358+
return null;
359+
}
328360
}

Diff for: application/src/com/ferg/awfulapp/EmoteFragment.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public void onCreate(Bundle savedInstanceState) {
100100
public View onCreateView(LayoutInflater inflater, ViewGroup container,
101101
Bundle savedInstanceState) {
102102
View v = inflateView(R.layout.emote_view, container, inflater);
103-
adapter = new AwfulCursorAdapter(getAwfulActivity(), null);
103+
adapter = new AwfulCursorAdapter(getAwfulActivity(), null, null);
104104
aq.find(R.id.delete_button).clicked(this);
105105
filterText = aq.find(R.id.filter_text).textColor(ColorProvider.getTextColor(mPrefs)).getEditText();
106106
filterText.addTextChangedListener(new TextWatcher() {

Diff for: application/src/com/ferg/awfulapp/ForumDisplayFragment.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import com.ferg.awfulapp.provider.ColorProvider;
5757
import com.ferg.awfulapp.service.AwfulCursorAdapter;
5858
import com.ferg.awfulapp.service.AwfulSyncService;
59+
import com.ferg.awfulapp.service.ThreadCursorAdapter;
5960
import com.ferg.awfulapp.thread.AwfulForum;
6061
import com.ferg.awfulapp.thread.AwfulPagedItem;
6162
import com.ferg.awfulapp.thread.AwfulThread;
@@ -122,7 +123,7 @@ public ForumDisplayFragment() {
122123
TAG = "ForumDisplayFragment";
123124
}
124125

125-
private AwfulCursorAdapter mCursorAdapter;
126+
private ThreadCursorAdapter mCursorAdapter;
126127
private ForumContentsCallback mForumLoaderCallback = new ForumContentsCallback(mHandler);
127128
private ForumDataCallback mForumDataCallback = new ForumDataCallback(mHandler);
128129

@@ -211,7 +212,7 @@ public void onActivityCreated(Bundle aSavedState) {
211212
}
212213

213214

214-
mCursorAdapter = new AwfulCursorAdapter((AwfulActivity) getActivity(), null, getForumId(), getActivity() instanceof ThreadDisplayActivity, mMessenger);
215+
mCursorAdapter = new ThreadCursorAdapter((AwfulActivity) getActivity(), null, this);
215216
mPullRefreshListView.setAdapter(mCursorAdapter);
216217
mPullRefreshListView.setOnItemClickListener(onThreadSelected);
217218
mPullRefreshListView.setBackgroundColor(ColorProvider.getBackgroundColor(mPrefs));
@@ -628,9 +629,6 @@ public void openForum(int id, int page){
628629
lastRefresh = 0;
629630
loadFailed = false;
630631
if(getActivity() != null){
631-
if(mCursorAdapter != null){
632-
mCursorAdapter.setId(id);
633-
}
634632
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
635633
getActivity().invalidateOptionsMenu();
636634
}

Diff for: application/src/com/ferg/awfulapp/PrivateMessageListFragment.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public void onActivityCreated(Bundle aSavedState) {
9898

9999
mPMList.setOnItemClickListener(onPMSelected);
100100

101-
mCursorAdapter = new AwfulCursorAdapter((AwfulActivity) getActivity(), null);
101+
mCursorAdapter = new AwfulCursorAdapter((AwfulActivity) getActivity(), null, this);
102102
mPMList.setAdapter(mCursorAdapter);
103103
}
104104

Diff for: application/src/com/ferg/awfulapp/service/AwfulCursorAdapter.java

+8-9
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
import com.androidquery.AQuery;
3939
import com.ferg.awfulapp.AwfulActivity;
40+
import com.ferg.awfulapp.AwfulFragment;
4041
import com.ferg.awfulapp.R;
4142
import com.ferg.awfulapp.constants.Constants;
4243
import com.ferg.awfulapp.preferences.AwfulPreferences;
@@ -50,27 +51,25 @@ public class AwfulCursorAdapter extends CursorAdapter {
5051
private static final String TAG = "AwfulCursorAdapter";
5152
private AwfulPreferences mPrefs;
5253
private AwfulActivity mParent;
54+
private AwfulFragment mFragment;
5355
private LayoutInflater inf;
5456
private int mId;
5557
private int selectedId = -1;
5658
private AQuery aq;
5759
private Messenger msgCallback;
5860

59-
public AwfulCursorAdapter(AwfulActivity context, Cursor c) {
60-
this(context, c, 0, false, null);
61+
public AwfulCursorAdapter(AwfulActivity context, Cursor c, AwfulFragment fragment) {
62+
this(context, c, 0, false, null, fragment);
6163
}
62-
public AwfulCursorAdapter(AwfulActivity context, Cursor c, int id, boolean isSidebar, Messenger messageCallback) {
64+
public AwfulCursorAdapter(AwfulActivity context, Cursor c, int id, boolean isSidebar, Messenger messageCallback, AwfulFragment fragment) {
6365
super(context, c, 0);
6466
mPrefs = new AwfulPreferences(context);
6567
inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
6668
mParent = context;
6769
mId = id;
6870
msgCallback = messageCallback;
6971
aq = new AQuery(context);
70-
}
71-
72-
public AwfulCursorAdapter(AwfulActivity awfulActivity, Cursor c, Messenger buttonMessenger) {
73-
this(awfulActivity, c, 0, false, buttonMessenger);
72+
mFragment = fragment;
7473
}
7574

7675
public void setSelected(int id){
@@ -84,7 +83,7 @@ public void setId(int id){
8483
@Override
8584
public void bindView(View current, Context context, Cursor data) {
8685
if(data.getColumnIndex(AwfulThread.BOOKMARKED) >= 0){//unique to threads
87-
AwfulThread.getView(current, mPrefs, data, aq, mId == Constants.USERCP_ID, false);
86+
AwfulThread.getView(current, mPrefs, data, aq, mFragment);
8887
}else if(data.getColumnIndex(AwfulForum.PARENT_ID) >= 0){//unique to forums
8988
assert(false);
9089
}else if(data.getColumnIndex(AwfulPost.PREVIOUSLY_READ) >= 0){
@@ -102,7 +101,7 @@ public View newView(Context context, Cursor data, ViewGroup parent) {
102101
View row;
103102
if(data.getColumnIndex(AwfulThread.BOOKMARKED) >= 0){//unique to threads
104103
row = inf.inflate(R.layout.thread_item, parent, false);
105-
AwfulThread.getView(row, mPrefs, data, aq, mId == Constants.USERCP_ID, false);
104+
AwfulThread.getView(row, mPrefs, data, aq, mFragment);
106105
}else if(data.getColumnIndex(AwfulForum.PARENT_ID) >= 0){//unique to forums
107106
row = inf.inflate(R.layout.thread_item, parent, false);
108107
assert(false);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/********************************************************************************
2+
* Copyright (c) 2012, Matthew Shepard
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
* * Redistributions of source code must retain the above copyright
8+
* notice, this list of conditions and the following disclaimer.
9+
* * Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
* * Neither the name of the software nor the
13+
* names of its contributors may be used to endorse or promote products
14+
* derived from this software without specific prior written permission.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY SCOTT FERGUSON ''AS IS'' AND ANY
17+
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
* DISCLAIMED. IN NO EVENT SHALL SCOTT FERGUSON BE LIABLE FOR ANY
20+
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
*******************************************************************************/
27+
28+
package com.ferg.awfulapp.service;
29+
30+
import android.content.Context;
31+
import android.database.Cursor;
32+
import android.os.Messenger;
33+
import android.support.v4.widget.CursorAdapter;
34+
import android.view.LayoutInflater;
35+
import android.view.View;
36+
import android.view.ViewGroup;
37+
import com.androidquery.AQuery;
38+
import com.ferg.awfulapp.AwfulActivity;
39+
import com.ferg.awfulapp.AwfulFragment;
40+
import com.ferg.awfulapp.R;
41+
import com.ferg.awfulapp.constants.Constants;
42+
import com.ferg.awfulapp.preferences.AwfulPreferences;
43+
import com.ferg.awfulapp.thread.*;
44+
45+
public class ThreadCursorAdapter extends CursorAdapter {
46+
private static final String TAG = "ThreadCursorAdapter";
47+
private AwfulPreferences mPrefs;
48+
private AwfulActivity mParent;
49+
private AwfulFragment mFragment;
50+
private LayoutInflater inf;
51+
private AQuery aq;
52+
53+
public ThreadCursorAdapter(AwfulActivity context, Cursor c, AwfulFragment fragment) {
54+
super(context, c, 0);
55+
mPrefs = new AwfulPreferences(context);
56+
inf = LayoutInflater.from(context);
57+
mParent = context;
58+
aq = new AQuery(context);
59+
mFragment = fragment;
60+
}
61+
62+
@Override
63+
public void bindView(View current, Context context, Cursor data) {
64+
AwfulThread.getView(current, mPrefs, data, aq, mFragment);
65+
mParent.setPreferredFont(current);
66+
}
67+
68+
@Override
69+
public View newView(Context context, Cursor data, ViewGroup parent) {
70+
View row = inf.inflate(R.layout.nu_thread_item, parent, false);
71+
AwfulThread.getView(row, mPrefs, data, aq, mFragment);
72+
mParent.setPreferredFont(row);
73+
return row;
74+
}
75+
76+
public int getInt(long id, String column){
77+
Cursor tmpcursor = getRow(id);
78+
if(tmpcursor != null){
79+
int col = tmpcursor.getColumnIndex(column);
80+
return tmpcursor.getInt(col);
81+
}
82+
return 0;
83+
}
84+
85+
public String getString(long id, String column){
86+
Cursor tmpcursor = getRow(id);
87+
if(tmpcursor != null){
88+
int col = tmpcursor.getColumnIndex(column);
89+
return tmpcursor.getString(col);
90+
}
91+
return null;
92+
}
93+
/**
94+
* Returns a cursor pointing to the row containing the specified ID or null if not found.
95+
* DO NOT CLOSE THE CURSOR.
96+
* @param id
97+
* @return cursor with specified row or null. DO NOT CLOSE.
98+
*/
99+
public Cursor getRow(long id){
100+
Cursor tmpcursor = getCursor();
101+
if(tmpcursor != null && tmpcursor.moveToFirst()){
102+
do{
103+
if(tmpcursor.getLong(tmpcursor.getColumnIndex(AwfulThread.ID)) == id){//contentprovider id tables are required to be _id
104+
return tmpcursor;
105+
}
106+
}while(tmpcursor.moveToNext());
107+
}
108+
return null;
109+
}
110+
}

0 commit comments

Comments
 (0)