From 2792c7c9d56fae02abfbd5a33b4448306b4dfd92 Mon Sep 17 00:00:00 2001 From: Ezerous Date: Tue, 29 Nov 2016 19:40:41 +0200 Subject: [PATCH] A base for Forum fragment --- .../thmmy/mthmmy/activities/MainActivity.java | 13 +- .../mthmmy/sections/forum/ForumAdapter.java | 87 ++++++ .../mthmmy/sections/forum/ForumFragment.java | 289 ++++++++++++++++++ 3 files changed, 384 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/gr/thmmy/mthmmy/sections/forum/ForumAdapter.java create mode 100644 app/src/main/java/gr/thmmy/mthmmy/sections/forum/ForumFragment.java diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/MainActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/MainActivity.java index 7f23e9c4..f791d871 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/MainActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/MainActivity.java @@ -16,12 +16,13 @@ import android.widget.Toast; import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.data.TopicSummary; +import gr.thmmy.mthmmy.sections.forum.ForumFragment; import gr.thmmy.mthmmy.sections.recent.RecentFragment; import static gr.thmmy.mthmmy.session.SessionManager.LOGGED_IN; import static gr.thmmy.mthmmy.session.SessionManager.LOGGED_OUT; -public class MainActivity extends BaseActivity implements RecentFragment.OnListFragmentInteractionListener { +public class MainActivity extends BaseActivity implements RecentFragment.OnListFragmentInteractionListener, ForumFragment.OnListFragmentInteractionListener { //----------------------------------------CLASS VARIABLES----------------------------------------- private static final String TAG = "MainActivity"; @@ -138,14 +139,16 @@ public class MainActivity extends BaseActivity implements RecentFragment.OnListF @Override public Fragment getItem(int position) { - // getItem is called to instantiate the fragment for the given page. - // Return a PlaceholderFragment (defined as a static inner class below). - return RecentFragment.newInstance(position + 1); + switch(position) { + case 0: return RecentFragment.newInstance(position +1); + case 1: return ForumFragment.newInstance(position +1); + default: return RecentFragment.newInstance(position +1); //temp (?) + } } @Override public int getCount() { - // Show 1 total pages. + // Show 2 total pages. return 2; } diff --git a/app/src/main/java/gr/thmmy/mthmmy/sections/forum/ForumAdapter.java b/app/src/main/java/gr/thmmy/mthmmy/sections/forum/ForumAdapter.java new file mode 100644 index 00000000..bfd72204 --- /dev/null +++ b/app/src/main/java/gr/thmmy/mthmmy/sections/forum/ForumAdapter.java @@ -0,0 +1,87 @@ +package gr.thmmy.mthmmy.sections.forum; + +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.data.TopicSummary; +import gr.thmmy.mthmmy.sections.recent.RecentFragment; + + +/** + * {@link RecyclerView.Adapter} that can display a {@link TopicSummary} and makes a call to the + * specified {@link RecentFragment.OnListFragmentInteractionListener}. + */ +public class ForumAdapter extends RecyclerView.Adapter { + private final List recentList; + private final ForumFragment.OnListFragmentInteractionListener mListener; + + public ForumAdapter(List topicSummaryList, ForumFragment.OnListFragmentInteractionListener listener) { + this.recentList = topicSummaryList; + mListener = listener; + } + + + @Override + public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + View view = LayoutInflater.from(parent.getContext()) + .inflate(R.layout.fragment_recent_row, parent, false); + return new ViewHolder(view); + } + + + @Override + public void onBindViewHolder(final ViewHolder holder, final int position) { + + holder.mTitleView.setText(recentList.get(position).getTitle()); + holder.mDateTimeView.setText(recentList.get(position).getDateTimeModified()); + holder.mUserView.setText("by " + recentList.get(position).getLastUser()); + + holder.topic = recentList.get(position); + + holder.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.onFragmentInteraction(holder.topic); //? + + } + + } + }); + } + + @Override + public int getItemCount() { + return recentList.size(); + } + + public class ViewHolder extends RecyclerView.ViewHolder { + public final View mView; + public final TextView mTitleView; + public final TextView mUserView; + public final TextView mDateTimeView; + public TopicSummary topic; + + public 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); + } + +// @Override +// public String toString() { +// return super.toString() + " '" + mContentView.getText() + "'"; +// } + } +} diff --git a/app/src/main/java/gr/thmmy/mthmmy/sections/forum/ForumFragment.java b/app/src/main/java/gr/thmmy/mthmmy/sections/forum/ForumFragment.java new file mode 100644 index 00000000..02e40d17 --- /dev/null +++ b/app/src/main/java/gr/thmmy/mthmmy/sections/forum/ForumFragment.java @@ -0,0 +1,289 @@ +package gr.thmmy.mthmmy.sections.forum; + +import android.content.Context; +import android.os.AsyncTask; +import android.os.Bundle; +import android.support.v4.app.Fragment; +import android.support.v4.widget.SwipeRefreshLayout; +import android.support.v7.widget.LinearLayoutManager; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ProgressBar; +import android.widget.RelativeLayout; +import android.widget.Toast; + +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.jsoup.select.Elements; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import gr.thmmy.mthmmy.R; +import gr.thmmy.mthmmy.activities.BaseActivity; +import gr.thmmy.mthmmy.data.TopicSummary; +import gr.thmmy.mthmmy.session.SessionManager; +import gr.thmmy.mthmmy.utils.CustomRecyclerView; +import okhttp3.HttpUrl; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; + +/** + * A simple {@link Fragment} subclass. + * Activities that contain this fragment must implement the + * {@link ForumFragment.OnListFragmentInteractionListener} interface + * to handle interaction events. + * Use the {@link ForumFragment#newInstance} factory method to + * create an instance of this fragment. + */ +public class ForumFragment extends Fragment +{ + private static final String TAG = "ForumFragment"; + // Fragment initialization parameters, e.g. ARG_SECTION_NUMBER + private static final String ARG_SECTION_NUMBER = "SectionNumber"; + private int sectionNumber; + + private ProgressBar progressBar; + private SwipeRefreshLayout swipeRefreshLayout; + private ForumAdapter forumAdapter; + + private List topicSummaries; + + private OnListFragmentInteractionListener mListener; + + private OkHttpClient client; + + // Required empty public constructor + public ForumFragment() { + } + + /** + * Use ONLY this factory method to create a new instance of + * this fragment using the provided parameters. + * + * @param sectionNumber + * @return A new instance of fragment Forum. + */ + public static ForumFragment newInstance(int sectionNumber) { + ForumFragment fragment = new ForumFragment(); + Bundle args = new Bundle(); + args.putInt(ARG_SECTION_NUMBER, sectionNumber); + fragment.setArguments(args); + return fragment; + } + + public int getSectionNumber() { + return sectionNumber; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + sectionNumber = getArguments().getInt(ARG_SECTION_NUMBER); + + client = BaseActivity.getClient(); + + topicSummaries = new ArrayList<>(); + + + if (sectionNumber == 1) //? + Log.d(TAG, "onCreate"); + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + if (sectionNumber == 1)//temp + { + if (topicSummaries.isEmpty()) + new RecentTask().execute(); + + + } + Log.d(TAG, "onActivityCreated"); + } + + @Override + public void onStart() { + super.onStart(); + if (sectionNumber == 1) + Log.d(TAG, "onStart"); + } + + @Override + public void onResume() { + super.onResume(); + if (sectionNumber == 1) + Log.d(TAG, "onResume"); + } + + @Override + public void onPause() { + super.onPause(); + if (sectionNumber == 1) + Log.d(TAG, "onPause"); + } + + @Override + public void onStop() { + super.onStop(); + if (sectionNumber == 1) + Log.d(TAG, "onStop"); + } + + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + // Inflate the layout for this fragment + final View rootView = inflater.inflate(R.layout.fragment_recent, container, false); + + // Set the adapter + if (rootView instanceof RelativeLayout) { + progressBar = (ProgressBar) rootView.findViewById(R.id.progressBar); + forumAdapter = new ForumAdapter(topicSummaries, mListener); + + CustomRecyclerView recyclerView = (CustomRecyclerView) rootView.findViewById(R.id.list); + recyclerView.setLayoutManager(new LinearLayoutManager(rootView.findViewById(R.id.list).getContext())); + recyclerView.setAdapter(forumAdapter); + + swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swiperefresh); + swipeRefreshLayout.setOnRefreshListener( + new SwipeRefreshLayout.OnRefreshListener() { + @Override + public void onRefresh() { + new RecentTask().execute(); + + } + + } + ); + + + } + + return rootView; + } + + @Override + public void onAttach(Context context) { + super.onAttach(context); + if (context instanceof OnListFragmentInteractionListener) { + mListener = (OnListFragmentInteractionListener) context; + + } else { + throw new RuntimeException(context.toString() + + " must implement OnFragmentInteractionListener"); + } + } + + @Override + public void onDetach() { + super.onDetach(); + mListener = null; + } + + /** + * This interface must be implemented by activities that contain this + * fragment to allow an interaction in this fragment to be communicated + * to the activity and potentially other fragments contained in that + * activity. + */ + public interface OnListFragmentInteractionListener { + // TODO: Update argument type and name + void onFragmentInteraction(TopicSummary topicSummary); + } + + //---------------------------------------ASYNC TASK----------------------------------- + + public class RecentTask extends AsyncTask { + private static final String TAG = "RecentTask"; + private final HttpUrl thmmyUrl = SessionManager.indexUrl; + + private Document document; + + + protected void onPreExecute() { + + progressBar.setVisibility(ProgressBar.VISIBLE); + } + + protected Integer doInBackground(Void... voids) { + + Request request = new Request.Builder() + .url(thmmyUrl) + .build(); + try { + Response response = client.newCall(request).execute(); + document = Jsoup.parse(response.body().string()); + parse(document); + return 0; + } catch (IOException e) { + Log.d("DEB", "ERROR", e); + return 1; + } catch (Exception e) { + Log.d("DEB", "ERROR", e); + return 2; + } + + } + + + protected void onPostExecute(Integer result) { + + if (result == 0) + forumAdapter.notifyDataSetChanged(); + else if (result == 1) + Toast.makeText(getActivity(), "Network error", Toast.LENGTH_SHORT).show(); + + progressBar.setVisibility(ProgressBar.INVISIBLE); + swipeRefreshLayout.setRefreshing(false); + } + + private void parse(Document document) { + Elements recent = document.select("#block8 :first-child div"); + if (recent.size() == 30) { + topicSummaries.clear(); + + for (int i = 0; i < recent.size(); i += 3) { + String link = recent.get(i).child(0).attr("href"); + String title = recent.get(i).child(0).attr("title"); + + String lastUser = recent.get(i + 1).text(); + Pattern pattern = Pattern.compile("\\b (.*)"); + Matcher matcher = pattern.matcher(lastUser); + if (matcher.find()) + lastUser = matcher.group(1); + else { + Log.e(TAG, "Parsing failed (lastUser)!"); + return; + } + + String dateTime = recent.get(i + 2).text(); + pattern = Pattern.compile("\\[(.*)\\]"); + matcher = pattern.matcher(dateTime); + if (matcher.find()) + dateTime = matcher.group(1); + else { + Log.e(TAG, "Parsing failed (dateTime)!"); + return; + } + + + topicSummaries.add(new TopicSummary(link, title, lastUser, dateTime)); + } + + return; + } + Log.e(TAG, "Parsing failed!"); + } + } + +}