mirror of https://github.com/ThmmyNoLife/mTHMMY
Apostolos Fanakis
8 years ago
11 changed files with 450 additions and 39 deletions
@ -0,0 +1,109 @@ |
|||
package gr.thmmy.mthmmy.activities.main.unread; |
|||
|
|||
import android.content.Context; |
|||
import android.support.annotation.NonNull; |
|||
import android.support.v7.widget.RecyclerView; |
|||
import android.view.LayoutInflater; |
|||
import android.view.View; |
|||
import android.view.ViewGroup; |
|||
import android.widget.TextView; |
|||
|
|||
import java.util.List; |
|||
|
|||
import gr.thmmy.mthmmy.R; |
|||
import gr.thmmy.mthmmy.base.BaseFragment; |
|||
import gr.thmmy.mthmmy.model.TopicSummary; |
|||
|
|||
class UnreadAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { |
|||
private final Context context; |
|||
private final List<TopicSummary> unreadList; |
|||
private final UnreadFragment.UnreadFragmentInteractionListener mListener; |
|||
|
|||
private final int VIEW_TYPE_ITEM = 0; |
|||
private final int VIEW_TYPE_NADA = 1; |
|||
|
|||
UnreadAdapter(Context context, @NonNull List<TopicSummary> topicSummaryList, BaseFragment.FragmentInteractionListener listener) { |
|||
this.context = context; |
|||
this.unreadList = topicSummaryList; |
|||
mListener = (UnreadFragment.UnreadFragmentInteractionListener) listener; |
|||
} |
|||
|
|||
@Override |
|||
public int getItemViewType(int position) { |
|||
return unreadList.get(position).getTopicUrl() == null ? VIEW_TYPE_NADA : VIEW_TYPE_ITEM; |
|||
} |
|||
|
|||
@Override |
|||
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { |
|||
if (viewType == VIEW_TYPE_ITEM) { |
|||
View view = LayoutInflater.from(parent.getContext()) |
|||
.inflate(R.layout.fragment_unread_row, parent, false); |
|||
return new ViewHolder(view); |
|||
} else if (viewType == VIEW_TYPE_NADA) { |
|||
View view = LayoutInflater.from(parent.getContext()) |
|||
.inflate(R.layout.fragment_unread_empty_row, parent, false); |
|||
return new EmptyViewHolder(view); |
|||
} |
|||
return null; |
|||
} |
|||
|
|||
@Override |
|||
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { |
|||
if (holder instanceof UnreadAdapter.EmptyViewHolder) { |
|||
final UnreadAdapter.EmptyViewHolder emptyViewHolder = (UnreadAdapter.EmptyViewHolder) holder; |
|||
emptyViewHolder.text.setText(unreadList.get(position).getDateTimeModified()); |
|||
} else if (holder instanceof UnreadAdapter.ViewHolder) { |
|||
final UnreadAdapter.ViewHolder viewHolder = (UnreadAdapter.ViewHolder) holder; |
|||
|
|||
viewHolder.mTitleView.setText(unreadList.get(position).getSubject()); |
|||
viewHolder.mDateTimeView.setText(unreadList.get(position).getDateTimeModified()); |
|||
viewHolder.mUserView.setText(context.getString(R.string.byUser, unreadList.get(position).getLastUser())); |
|||
|
|||
viewHolder.topic = unreadList.get(position); |
|||
|
|||
viewHolder.mView.setOnClickListener(new View.OnClickListener() { |
|||
@Override |
|||
public void onClick(View v) { |
|||
|
|||
if (null != mListener) { |
|||
// Notify the active callbacks interface (the activity, if the
|
|||
// fragment is attached to one) that an item has been selected.
|
|||
mListener.onUnreadFragmentInteraction(viewHolder.topic); //?
|
|||
|
|||
} |
|||
|
|||
} |
|||
}); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public int getItemCount() { |
|||
return unreadList.size(); |
|||
} |
|||
|
|||
private static class ViewHolder extends RecyclerView.ViewHolder { |
|||
final View mView; |
|||
final TextView mTitleView; |
|||
final TextView mUserView; |
|||
final TextView mDateTimeView; |
|||
public TopicSummary topic; |
|||
|
|||
ViewHolder(View view) { |
|||
super(view); |
|||
mView = view; |
|||
mTitleView = (TextView) view.findViewById(R.id.title); |
|||
mUserView = (TextView) view.findViewById(R.id.lastUser); |
|||
mDateTimeView = (TextView) view.findViewById(R.id.dateTime); |
|||
} |
|||
} |
|||
|
|||
private static class EmptyViewHolder extends RecyclerView.ViewHolder { |
|||
final TextView text; |
|||
|
|||
EmptyViewHolder(View view) { |
|||
super(view); |
|||
text = (TextView) view.findViewById(R.id.text); |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,184 @@ |
|||
package gr.thmmy.mthmmy.activities.main.unread; |
|||
|
|||
import android.os.AsyncTask; |
|||
import android.os.Bundle; |
|||
import android.support.v4.widget.SwipeRefreshLayout; |
|||
import android.support.v7.widget.DividerItemDecoration; |
|||
import android.support.v7.widget.LinearLayoutManager; |
|||
import android.view.LayoutInflater; |
|||
import android.view.View; |
|||
import android.view.ViewGroup; |
|||
import android.widget.ProgressBar; |
|||
import android.widget.RelativeLayout; |
|||
|
|||
import org.jsoup.nodes.Document; |
|||
import org.jsoup.nodes.Element; |
|||
import org.jsoup.select.Elements; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
|
|||
import gr.thmmy.mthmmy.R; |
|||
import gr.thmmy.mthmmy.base.BaseFragment; |
|||
import gr.thmmy.mthmmy.model.TopicSummary; |
|||
import gr.thmmy.mthmmy.session.SessionManager; |
|||
import gr.thmmy.mthmmy.utils.CustomRecyclerView; |
|||
import gr.thmmy.mthmmy.utils.ParseTask; |
|||
import gr.thmmy.mthmmy.utils.exceptions.ParseException; |
|||
import me.zhanghai.android.materialprogressbar.MaterialProgressBar; |
|||
import timber.log.Timber; |
|||
|
|||
/** |
|||
* A {@link BaseFragment} subclass. |
|||
* Activities that contain this fragment must implement the |
|||
* {@link UnreadFragment.UnreadFragmentInteractionListener} interface |
|||
* to handle interaction events. |
|||
* Use the {@link UnreadFragment#newInstance} factory method to |
|||
* create an instance of this fragment. |
|||
*/ |
|||
|
|||
public class UnreadFragment extends BaseFragment { |
|||
private static final String TAG = "UnreadFragment"; |
|||
// Fragment initialization parameters, e.g. ARG_SECTION_NUMBER
|
|||
|
|||
private MaterialProgressBar progressBar; |
|||
private SwipeRefreshLayout swipeRefreshLayout; |
|||
private UnreadAdapter unreadAdapter; |
|||
|
|||
private List<TopicSummary> topicSummaries; |
|||
|
|||
private UnreadTask unreadTask; |
|||
|
|||
// Required empty public constructor
|
|||
public UnreadFragment() { |
|||
} |
|||
|
|||
/** |
|||
* Use ONLY this factory method to create a new instance of |
|||
* this fragment using the provided parameters. |
|||
* |
|||
* @return A new instance of fragment Unread. |
|||
*/ |
|||
public static UnreadFragment newInstance(int sectionNumber) { |
|||
UnreadFragment fragment = new UnreadFragment(); |
|||
Bundle args = new Bundle(); |
|||
args.putString(ARG_TAG, TAG); |
|||
args.putInt(ARG_SECTION_NUMBER, sectionNumber); |
|||
fragment.setArguments(args); |
|||
return fragment; |
|||
} |
|||
|
|||
@Override |
|||
public void onCreate(Bundle savedInstanceState) { |
|||
super.onCreate(savedInstanceState); |
|||
topicSummaries = new ArrayList<>(); |
|||
} |
|||
|
|||
@Override |
|||
public void onActivityCreated(Bundle savedInstanceState) { |
|||
super.onActivityCreated(savedInstanceState); |
|||
if (topicSummaries.isEmpty()) { |
|||
unreadTask = new UnreadTask(); |
|||
unreadTask.execute(SessionManager.unreadUrl.toString()); |
|||
|
|||
} |
|||
Timber.d("onActivityCreated"); |
|||
} |
|||
|
|||
|
|||
@Override |
|||
public View onCreateView(LayoutInflater inflater, ViewGroup container, |
|||
Bundle savedInstanceState) { |
|||
// Inflate the layout for this fragment
|
|||
final View rootView = inflater.inflate(R.layout.fragment_unread, container, false); |
|||
|
|||
// Set the adapter
|
|||
if (rootView instanceof RelativeLayout) { |
|||
progressBar = (MaterialProgressBar) rootView.findViewById(R.id.progressBar); |
|||
unreadAdapter = new UnreadAdapter(getActivity(), topicSummaries, fragmentInteractionListener); |
|||
|
|||
CustomRecyclerView recyclerView = (CustomRecyclerView) rootView.findViewById(R.id.list); |
|||
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(rootView.findViewById(R.id.list).getContext()); |
|||
recyclerView.setLayoutManager(linearLayoutManager); |
|||
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), |
|||
linearLayoutManager.getOrientation()); |
|||
recyclerView.addItemDecoration(dividerItemDecoration); |
|||
recyclerView.setAdapter(unreadAdapter); |
|||
|
|||
swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swiperefresh); |
|||
swipeRefreshLayout.setOnRefreshListener( |
|||
new SwipeRefreshLayout.OnRefreshListener() { |
|||
@Override |
|||
public void onRefresh() { |
|||
if (unreadTask != null && unreadTask.getStatus() != AsyncTask.Status.RUNNING) { |
|||
unreadTask = new UnreadTask(); |
|||
unreadTask.execute(SessionManager.unreadUrl.toString()); |
|||
} |
|||
} |
|||
|
|||
} |
|||
); |
|||
} |
|||
|
|||
return rootView; |
|||
} |
|||
|
|||
@Override |
|||
public void onDestroy() { |
|||
super.onDestroy(); |
|||
if (unreadTask != null && unreadTask.getStatus() != AsyncTask.Status.RUNNING) |
|||
unreadTask.cancel(true); |
|||
} |
|||
|
|||
|
|||
public interface UnreadFragmentInteractionListener extends FragmentInteractionListener { |
|||
void onUnreadFragmentInteraction(TopicSummary topicSummary); |
|||
} |
|||
|
|||
//---------------------------------------ASYNC TASK-----------------------------------
|
|||
private class UnreadTask extends ParseTask { |
|||
protected void onPreExecute() { |
|||
progressBar.setVisibility(ProgressBar.VISIBLE); |
|||
} |
|||
|
|||
@Override |
|||
public void parse(Document document) throws ParseException { |
|||
Elements unread = document.select("table.bordercolor[cellspacing=1] tr:not(.titlebg)"); |
|||
if (!unread.isEmpty()) { |
|||
topicSummaries.clear(); |
|||
for (Element row : unread) { |
|||
Elements information = row.select("td"); |
|||
String link = information.get(2).select("a").first().attr("href"); |
|||
String title = information.get(2).select("a").first().text(); |
|||
|
|||
Element lastUserAndDate = information.get(6); |
|||
String lastUser = lastUserAndDate.select("a").text(); |
|||
String dateTime = lastUserAndDate.select("span").html(); |
|||
dateTime = dateTime.substring(3, dateTime.indexOf("<b")); |
|||
dateTime = dateTime.replace("</b>", ""); |
|||
|
|||
topicSummaries.add(new TopicSummary(link, title, lastUser, dateTime)); |
|||
} |
|||
} else { |
|||
String message = document.select("table.bordercolor[cellspacing=1]").first().text(); |
|||
if (message.contains("No messages")){ //It's english
|
|||
message = "No unread posts!"; |
|||
}else{ //It's greek
|
|||
message = "Δεν υπάρχουν μη διαβασμένα μυνήματα!"; |
|||
} |
|||
topicSummaries.add(new TopicSummary(null, null, null, message)); |
|||
} |
|||
} |
|||
|
|||
|
|||
@Override |
|||
protected void postParsing(ParseTask.ResultCode result) { |
|||
if (result == ResultCode.SUCCESS) |
|||
unreadAdapter.notifyDataSetChanged(); |
|||
|
|||
progressBar.setVisibility(ProgressBar.INVISIBLE); |
|||
swipeRefreshLayout.setRefreshing(false); |
|||
} |
|||
|
|||
} |
|||
} |
@ -0,0 +1,38 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<RelativeLayout |
|||
xmlns:android="http://schemas.android.com/apk/res/android" |
|||
xmlns:app="http://schemas.android.com/apk/res-auto" |
|||
xmlns:tools="http://schemas.android.com/tools" |
|||
android:layout_width="match_parent" |
|||
android:layout_height="match_parent"> |
|||
|
|||
<android.support.v4.widget.SwipeRefreshLayout |
|||
android:id="@+id/swiperefresh" |
|||
android:layout_width="match_parent" |
|||
android:layout_height="match_parent"> |
|||
|
|||
<gr.thmmy.mthmmy.utils.CustomRecyclerView |
|||
android:id="@+id/list" |
|||
android:name="gr.thmmy.mthmmy.sections.unread.UnreadFragment" |
|||
android:layout_width="match_parent" |
|||
android:layout_height="match_parent" |
|||
android:background="@color/background" |
|||
android:paddingBottom="4dp" |
|||
android:paddingTop="4dp" |
|||
app:layoutManager="LinearLayoutManager" |
|||
tools:context=".activities.main.unread.UnreadFragment" |
|||
tools:listitem="@layout/fragment_unread_row"/> |
|||
</android.support.v4.widget.SwipeRefreshLayout> |
|||
|
|||
<me.zhanghai.android.materialprogressbar.MaterialProgressBar |
|||
android:id="@+id/progressBar" |
|||
style="@style/Widget.MaterialProgressBar.ProgressBar.Horizontal.NoPadding" |
|||
android:layout_width="match_parent" |
|||
android:layout_height="@dimen/progress_bar_height" |
|||
android:layout_alignParentTop="true" |
|||
android:indeterminate="true" |
|||
android:visibility="invisible" |
|||
app:mpb_indeterminateTint="@color/accent" |
|||
app:mpb_progressStyle="horizontal"/> |
|||
|
|||
</RelativeLayout> |
@ -0,0 +1,19 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
|||
android:layout_width="match_parent" |
|||
android:layout_height="match_parent" |
|||
android:orientation="vertical" |
|||
android:paddingBottom="6dp" |
|||
android:paddingLeft="10dp" |
|||
android:paddingRight="10dp" |
|||
android:paddingTop="20dp"> |
|||
|
|||
<TextView |
|||
android:id="@+id/text" |
|||
android:layout_width="match_parent" |
|||
android:layout_height="match_parent" |
|||
android:textAlignment="center" |
|||
android:textColor="@color/accent" |
|||
android:textSize="@dimen/big_text" /> |
|||
|
|||
</LinearLayout> |
@ -0,0 +1,54 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<LinearLayout |
|||
xmlns:android="http://schemas.android.com/apk/res/android" |
|||
android:layout_width="match_parent" |
|||
android:layout_height="wrap_content" |
|||
android:orientation="vertical"> |
|||
|
|||
<RelativeLayout |
|||
android:layout_width="match_parent" |
|||
android:layout_height="wrap_content" |
|||
android:background="@color/primary_light" |
|||
android:foreground="?android:attr/selectableItemBackground" |
|||
android:paddingBottom="6dp" |
|||
android:paddingLeft="10dp" |
|||
android:paddingRight="10dp" |
|||
android:paddingTop="6dp"> |
|||
|
|||
<TextView |
|||
android:id="@+id/title" |
|||
android:layout_width="match_parent" |
|||
android:layout_height="wrap_content" |
|||
android:layout_alignParentStart="true" |
|||
android:layout_alignParentTop="true" |
|||
android:textAppearance="?attr/textAppearanceListItem" |
|||
android:textColor="@color/primary_text"/> |
|||
|
|||
<Space |
|||
android:id="@+id/spacer" |
|||
android:layout_width="match_parent" |
|||
android:layout_height="20dp" |
|||
android:layout_alignParentBottom="@+id/title" |
|||
android:layout_below="@+id/title" |
|||
android:layout_toEndOf="@+id/dateTime"/> |
|||
|
|||
<TextView |
|||
android:id="@+id/dateTime" |
|||
android:layout_width="wrap_content" |
|||
android:layout_height="wrap_content" |
|||
android:layout_alignParentStart="true" |
|||
android:layout_below="@+id/spacer" |
|||
android:textColor="@color/secondary_text"/> |
|||
|
|||
<TextView |
|||
android:id="@+id/lastUser" |
|||
android:layout_width="wrap_content" |
|||
android:layout_height="wrap_content" |
|||
android:layout_alignBaseline="@+id/dateTime" |
|||
android:layout_alignBottom="@+id/dateTime" |
|||
android:layout_alignParentEnd="true" |
|||
android:textColor="@color/secondary_text" |
|||
android:textStyle="italic"/> |
|||
</RelativeLayout> |
|||
|
|||
</LinearLayout> |
Loading…
Reference in new issue