diff --git a/app/build.gradle b/app/build.gradle index 85cf281e..5dd89bee 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -33,7 +33,7 @@ dependencies { compile 'com.android.support:support-v4:25.3.1' compile 'com.android.support:cardview-v7:25.3.1' compile 'com.android.support:recyclerview-v7:25.3.1' - compile 'com.google.firebase:firebase-crash:11.0.0' + compile 'com.google.firebase:firebase-crash:10.2.6' compile 'com.squareup.okhttp3:okhttp:3.8.0' compile 'com.squareup.picasso:picasso:2.5.2' compile 'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.1.0' diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java index 45b1140d..8f4a9663 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java @@ -12,11 +12,9 @@ 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; @@ -27,14 +25,13 @@ 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 okhttp3.HttpUrl; import okhttp3.Request; -import okhttp3.Response; import timber.log.Timber; + /** * A {@link BaseFragment} subclass. * Activities that contain this fragment must implement the @@ -84,7 +81,7 @@ public class RecentFragment extends BaseFragment { if (topicSummaries.isEmpty()) { recentTask =new RecentTask(); - recentTask.execute(); + recentTask.execute(SessionManager.indexUrl.toString()); } Timber.d("onActivityCreated"); @@ -117,7 +114,7 @@ public class RecentFragment extends BaseFragment { public void onRefresh() { if (recentTask != null && recentTask.getStatus() != AsyncTask.Status.RUNNING) { recentTask = new RecentTask(); - recentTask.execute(); + recentTask.execute(SessionManager.indexUrl.toString()); } } @@ -141,51 +138,21 @@ public class RecentFragment extends BaseFragment { } //---------------------------------------ASYNC TASK----------------------------------- - - private class RecentTask extends AsyncTask { - private final HttpUrl thmmyUrl = SessionManager.indexUrl; - private Document document; - + private class RecentTask extends ParseTask { protected void onPreExecute() { - progressBar.setVisibility(ProgressBar.VISIBLE); } - protected Integer doInBackground(Void... voids) { - Request request = new Request.Builder() - .url(thmmyUrl) + @Override + protected Request prepareRequest(String... strings) { + return new Request.Builder() + .url(strings[0]) .build(); - try { - Response response = client.newCall(request).execute(); - document = Jsoup.parse(response.body().string()); - parse(document); - return 0; - } catch (ParseException e) { - Timber.e(e, "ParseException"); - return 1; - } catch (IOException e) { - Timber.i(e, "Network Error"); - return 2; - } catch (Exception e) { - Timber.e(e, "Exception"); - return 3; - } - } - - protected void onPostExecute(Integer result) { - - if (result == 0) - recentAdapter.notifyDataSetChanged(); - else if (result == 2) - Toast.makeText(getActivity(), "Network error", Toast.LENGTH_SHORT).show(); //Fixme, sometimes activity isn't ready - - progressBar.setVisibility(ProgressBar.INVISIBLE); - swipeRefreshLayout.setRefreshing(false); - } - - private void parse(Document document) throws ParseException { + @Override + public void parse(Document document) throws ParseException + { Elements recent = document.select("#block8 :first-child div"); if (!recent.isEmpty()) { topicSummaries.clear(); @@ -217,6 +184,20 @@ public class RecentFragment extends BaseFragment { } throw new ParseException("Parsing failed"); } + + + @Override + protected void onPostExecute(ParseTask.ResultCode result) { + + if (result == ResultCode.SUCCESS) + recentAdapter.notifyDataSetChanged(); + else if (result == ResultCode.NETWORK_ERROR) + Toast.makeText(getActivity(), "Network error", Toast.LENGTH_SHORT).show(); //Fixme, sometimes activity isn't ready + + progressBar.setVisibility(ProgressBar.INVISIBLE); + swipeRefreshLayout.setRefreshing(false); + } + } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicParser.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicParser.java index c3e7271c..81b1e47f 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicParser.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicParser.java @@ -1,7 +1,6 @@ package gr.thmmy.mthmmy.activities.topic; import android.graphics.Color; -import android.util.Log; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/CrashReportingTree.java b/app/src/main/java/gr/thmmy/mthmmy/utils/CrashReportingTree.java index c5851d30..a3407254 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/CrashReportingTree.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/CrashReportingTree.java @@ -14,16 +14,16 @@ public class CrashReportingTree extends DebugTree { return; } - String level; + char level; if (priority == Log.INFO) - level = "I"; + level = 'I'; else if (priority == Log.WARN) - level = "W"; + level = 'W'; else if(priority == Log.ERROR) - level = "E"; + level = 'E'; else - level = "A"; + level = 'A'; FirebaseCrash.log(level + "/" + tag + ": " + message); diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/ParseTask.java b/app/src/main/java/gr/thmmy/mthmmy/utils/ParseTask.java new file mode 100644 index 00000000..cd85ce20 --- /dev/null +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/ParseTask.java @@ -0,0 +1,48 @@ +package gr.thmmy.mthmmy.utils; + +import android.os.AsyncTask; + +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; + +import java.io.IOException; + +import gr.thmmy.mthmmy.base.BaseApplication; +import gr.thmmy.mthmmy.utils.exceptions.ParseException; + +import okhttp3.Request; +import okhttp3.Response; +import timber.log.Timber; + +public abstract class ParseTask extends AsyncTask { + protected enum ResultCode { + SUCCESS, PARSING_ERROR, NETWORK_ERROR, OTHER_ERROR + } + + @Override + protected ResultCode doInBackground(String... params) { + Request request = prepareRequest(params); + try { + Response response = BaseApplication.getInstance().getClient().newCall(request).execute(); + Document document = Jsoup.parse(response.body().string()); + parse(document); + return ResultCode.SUCCESS; + } catch (ParseException e) { + Timber.tag(this.getClass().getSimpleName()); + Timber.e(e, "Parsing Error"); + return ResultCode.PARSING_ERROR; + } catch (IOException e) { + Timber.tag(this.getClass().getSimpleName()); + Timber.i(e, "Network Error"); + return ResultCode.NETWORK_ERROR; + } catch (Exception e) { + Timber.tag(this.getClass().getSimpleName()); + Timber.e(e, "Other Error"); + return ResultCode.OTHER_ERROR; + } + } + + protected abstract Request prepareRequest(String... params); + protected abstract void parse (Document document) throws ParseException; +} +