|
|
@ -21,7 +21,7 @@ import android.content.Context; |
|
|
|
import android.view.LayoutInflater; |
|
|
|
import android.view.LayoutInflater; |
|
|
|
import android.view.View; |
|
|
|
import android.view.View; |
|
|
|
import android.view.ViewGroup; |
|
|
|
import android.view.ViewGroup; |
|
|
|
import android.widget.ArrayAdapter; |
|
|
|
import android.widget.BaseAdapter; |
|
|
|
import android.widget.FrameLayout; |
|
|
|
import android.widget.FrameLayout; |
|
|
|
import android.widget.LinearLayout; |
|
|
|
import android.widget.LinearLayout; |
|
|
|
import android.widget.TextView; |
|
|
|
import android.widget.TextView; |
|
|
@ -32,120 +32,139 @@ import org.floens.chan.core.ChanPreferences; |
|
|
|
import org.floens.chan.core.model.Pin; |
|
|
|
import org.floens.chan.core.model.Pin; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
|
|
public class PinnedAdapter extends ArrayAdapter<Pin> { |
|
|
|
public class PinnedAdapter extends BaseAdapter { |
|
|
|
private final HashMap<Pin, Integer> idMap; |
|
|
|
private final static int VIEW_TYPE_ITEM = 0; |
|
|
|
private int idCounter; |
|
|
|
private final static int VIEW_TYPE_HEADER = 1; |
|
|
|
|
|
|
|
|
|
|
|
public PinnedAdapter(Context context, int resId) { |
|
|
|
private Context context; |
|
|
|
super(context, resId, new ArrayList<Pin>()); |
|
|
|
private List<Pin> pins = new ArrayList<>(); |
|
|
|
|
|
|
|
|
|
|
|
idMap = new HashMap<>(); |
|
|
|
public PinnedAdapter(Context context) { |
|
|
|
|
|
|
|
this.context = context; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public View getView(int position, View convertView, ViewGroup parent) { |
|
|
|
public int getCount() { |
|
|
|
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
|
|
|
return pins.size() + 1; |
|
|
|
|
|
|
|
} |
|
|
|
LinearLayout view; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final Pin item = getItem(position); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (item.type == Pin.Type.HEADER) { |
|
|
|
|
|
|
|
view = (LinearLayout) inflater.inflate(R.layout.pin_item_header, null); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
((TextView) view.findViewById(R.id.drawer_item_header)).setText(R.string.drawer_pinned); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
view = (LinearLayout) inflater.inflate(R.layout.pin_item, null); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
((TextView) view.findViewById(R.id.drawer_item_text)).setText(item.loadable.title); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
FrameLayout frameLayout = (FrameLayout) view.findViewById(R.id.drawer_item_count_container); |
|
|
|
@Override |
|
|
|
if (ChanPreferences.getWatchEnabled()) { |
|
|
|
public int getViewTypeCount() { |
|
|
|
frameLayout.setVisibility(View.VISIBLE); |
|
|
|
return 2; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
TextView itemCount = (TextView) view.findViewById(R.id.drawer_item_count); |
|
|
|
@Override |
|
|
|
|
|
|
|
public int getItemViewType(final int position) { |
|
|
|
|
|
|
|
return position == 0 ? VIEW_TYPE_HEADER : VIEW_TYPE_ITEM; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (item.isError) { |
|
|
|
@Override |
|
|
|
itemCount.setText("Err"); |
|
|
|
public Pin getItem(final int position) { |
|
|
|
|
|
|
|
switch (getItemViewType(position)) { |
|
|
|
|
|
|
|
case VIEW_TYPE_ITEM: |
|
|
|
|
|
|
|
int itemPosition = position - 1; |
|
|
|
|
|
|
|
if (itemPosition >= 0 && itemPosition < pins.size()) { |
|
|
|
|
|
|
|
return pins.get(itemPosition); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
int count = item.getNewPostsCount(); |
|
|
|
return null; |
|
|
|
String total = Integer.toString(count); |
|
|
|
|
|
|
|
if (count > 999) { |
|
|
|
|
|
|
|
total = "1k+"; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
itemCount.setText(total); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
case VIEW_TYPE_HEADER: |
|
|
|
|
|
|
|
return null; |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
return null; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
itemCount.setOnClickListener(new View.OnClickListener() { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public long getItemId(int position) { |
|
|
|
public void onClick(View v) { |
|
|
|
switch (getItemViewType(position)) { |
|
|
|
item.toggleWatch(); |
|
|
|
case VIEW_TYPE_ITEM: |
|
|
|
} |
|
|
|
int itemPosition = position - 1; |
|
|
|
}); |
|
|
|
if (itemPosition >= 0 && itemPosition < pins.size()) { |
|
|
|
|
|
|
|
return pins.get(itemPosition).id; |
|
|
|
if (!item.watching) { |
|
|
|
|
|
|
|
frameLayout.setBackgroundResource(R.drawable.pin_icon_gray); |
|
|
|
|
|
|
|
} else if (item.getNewQuoteCount() > 0) { |
|
|
|
|
|
|
|
frameLayout.setBackgroundResource(R.drawable.pin_icon_red); |
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
frameLayout.setBackgroundResource(R.drawable.pin_icon_blue); |
|
|
|
return -1; |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
case VIEW_TYPE_HEADER: |
|
|
|
frameLayout.setVisibility(View.GONE); |
|
|
|
return -1; |
|
|
|
} |
|
|
|
default: |
|
|
|
|
|
|
|
return -1; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return view; |
|
|
|
@Override |
|
|
|
|
|
|
|
public boolean hasStableIds() { |
|
|
|
|
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void reload() { |
|
|
|
@Override |
|
|
|
clear(); |
|
|
|
public View getView(int position, View convertView, ViewGroup parent) { |
|
|
|
|
|
|
|
switch (getItemViewType(position)) { |
|
|
|
|
|
|
|
case VIEW_TYPE_ITEM: { |
|
|
|
|
|
|
|
final Pin item = getItem(position); |
|
|
|
|
|
|
|
|
|
|
|
Pin header = new Pin(); |
|
|
|
if (convertView == null) { |
|
|
|
header.type = Pin.Type.HEADER; |
|
|
|
convertView = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.pin_item, null); |
|
|
|
add(header); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
addAll(ChanApplication.getPinnedManager().getPins()); |
|
|
|
((TextView) convertView.findViewById(R.id.drawer_item_text)).setText(item.loadable.title); |
|
|
|
|
|
|
|
|
|
|
|
notifyDataSetChanged(); |
|
|
|
FrameLayout frameLayout = (FrameLayout) convertView.findViewById(R.id.drawer_item_count_container); |
|
|
|
} |
|
|
|
if (ChanPreferences.getWatchEnabled()) { |
|
|
|
|
|
|
|
frameLayout.setVisibility(View.VISIBLE); |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
TextView itemCount = (TextView) convertView.findViewById(R.id.drawer_item_count); |
|
|
|
public void remove(Pin item) { |
|
|
|
|
|
|
|
super.remove(item); |
|
|
|
|
|
|
|
idMap.remove(item); |
|
|
|
|
|
|
|
notifyDataSetChanged(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
if (item.isError) { |
|
|
|
public void add(Pin item) { |
|
|
|
itemCount.setText("Err"); |
|
|
|
idMap.put(item, ++idCounter); |
|
|
|
} else { |
|
|
|
super.add(item); |
|
|
|
int count = item.getNewPostsCount(); |
|
|
|
notifyDataSetChanged(); |
|
|
|
String total = Integer.toString(count); |
|
|
|
} |
|
|
|
if (count > 999) { |
|
|
|
|
|
|
|
total = "1k+"; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
itemCount.setText(total); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
itemCount.setOnClickListener(new View.OnClickListener() { |
|
|
|
public boolean hasStableIds() { |
|
|
|
@Override |
|
|
|
return true; |
|
|
|
public void onClick(View v) { |
|
|
|
} |
|
|
|
item.toggleWatch(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!item.watching) { |
|
|
|
|
|
|
|
frameLayout.setBackgroundResource(R.drawable.pin_icon_gray); |
|
|
|
|
|
|
|
} else if (item.getNewQuoteCount() > 0) { |
|
|
|
|
|
|
|
frameLayout.setBackgroundResource(R.drawable.pin_icon_red); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
frameLayout.setBackgroundResource(R.drawable.pin_icon_blue); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
frameLayout.setVisibility(View.GONE); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
return convertView; |
|
|
|
public long getItemId(int position) { |
|
|
|
} |
|
|
|
if (position < 0 || position >= getCount()) |
|
|
|
case VIEW_TYPE_HEADER: { |
|
|
|
return -1; |
|
|
|
if (convertView == null) { |
|
|
|
|
|
|
|
convertView = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.pin_item_header, null); |
|
|
|
Pin item = getItem(position); |
|
|
|
((TextView) convertView.findViewById(R.id.drawer_item_header)).setText(R.string.drawer_pinned); |
|
|
|
if (item == null) { |
|
|
|
} |
|
|
|
return -1; |
|
|
|
|
|
|
|
} else { |
|
|
|
return convertView; |
|
|
|
Integer i = idMap.get(item); |
|
|
|
|
|
|
|
if (i == null) { |
|
|
|
|
|
|
|
return -1; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
return i; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void reload() { |
|
|
|
|
|
|
|
pins.clear(); |
|
|
|
|
|
|
|
pins.addAll(ChanApplication.getPinnedManager().getPins()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
notifyDataSetChanged(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|