Java tutorial
/* * Copyright (c) 2015 Matthieu Harl * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ package fr.shywim.antoinedaniel.ui.fragment; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v7.widget.Toolbar; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import com.crashlytics.android.Crashlytics; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.koushikdutta.async.future.FutureCallback; import com.koushikdutta.ion.Ion; import com.koushikdutta.ion.Response; import fr.shywim.antoinedaniel.BuildConfig; import fr.shywim.antoinedaniel.R; import fr.shywim.antoinedaniel.utils.NetUtils; import fr.shywim.antoinedaniel.utils.UiUtils; public class NewsFragment extends Fragment { private ViewGroup mPostsBadLayout; private ViewGroup mPostsAntoineLayout; private ViewGroup mConnectionLayout; private Context mContext; public static NewsFragment newInstance() { return new NewsFragment(); } public NewsFragment() { // Required empty public constructor } @Override public void onAttach(Activity activity) { super.onAttach(activity); mContext = activity; ((FragmentsCallbacks) activity).onFragmentAttached(this); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_news, container, false); UiUtils.setStatusBarBackground(root, getResources()); Toolbar toolbar = (Toolbar) root.findViewById(R.id.toolbar_actionbar); toolbar.setTitle(R.string.drawer_news); ((FragmentsCallbacks) mContext).setActionBarToolbar(toolbar); mPostsBadLayout = (ViewGroup) root.findViewById(R.id.post_bad); mPostsAntoineLayout = (ViewGroup) root.findViewById(R.id.posts_antoine); mConnectionLayout = (ViewGroup) root.findViewById(R.id.connection_warning); return root; } @Override public void onStart() { super.onStart(); if (NetUtils.isConnectedToInternet(mContext)) { if (NetUtils.sWarningLayoutOn) { NetUtils.sWarningLayoutOn = false; UiUtils.collapse(mConnectionLayout); } if (BuildConfig.BETA_VERSION) fetchBadNewsPost("http://antoine.shywim.fr/getbadtestpost"); fetchBadNewsPost("http://antoine.shywim.fr/getbadpost"); fetchAntoinePosts(); } else { NetUtils.sWarningLayoutOn = true; UiUtils.expand(mConnectionLayout); } } public void fetchBadNewsPost(final String url) { mPostsBadLayout.removeView(mPostsBadLayout.findViewById(R.id.post_error)); mPostsBadLayout.findViewById(R.id.progress).setVisibility(View.VISIBLE); Ion.with(mContext).load(url).asJsonObject().withResponse() .setCallback(new FutureCallback<Response<JsonObject>>() { @Override public void onCompleted(Exception e, Response<JsonObject> result) { mPostsBadLayout.findViewById(R.id.progress).setVisibility(View.GONE); if (e != null) { e.printStackTrace(); Crashlytics.logException(e); return; } JsonObject jo = result.getResult(); if (jo != null) { String message = jo.get("message") != null ? jo.get("message").getAsString() : ""; final String link = jo.get("link") != null ? jo.get("link").getAsString() : null; View postCard = LayoutInflater.from(mContext).inflate(R.layout.post_bad, mPostsBadLayout, false); ((TextView) postCard.findViewById(R.id.message)).setText(message); if (link != null) { postCard.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(link)); mContext.startActivity(intent); } }); } mPostsBadLayout.addView(postCard); } else { TextView tv = (TextView) mPostsBadLayout.findViewById(R.id.post_error); if (tv == null) { tv = (TextView) LayoutInflater.from(mContext).inflate(R.layout.simple_textview, mPostsBadLayout, false); } tv.setText( "Problme lors de la rception des donnes, " + "cliquez pour ressayer"); tv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { fetchBadNewsPost(url); } }); mPostsBadLayout.addView(tv); } } }); } public void fetchAntoinePosts() { mPostsAntoineLayout.removeView(mPostsAntoineLayout.findViewById(R.id.post_error)); mPostsAntoineLayout.findViewById(R.id.progress).setVisibility(View.VISIBLE); Ion.with(mContext).load("http://antoine.shywim.fr/getfacebookposts").asJsonArray().withResponse() .setCallback(new FutureCallback<Response<JsonArray>>() { @Override public void onCompleted(Exception e, Response<JsonArray> result) { mPostsAntoineLayout.findViewById(R.id.progress).setVisibility(View.GONE); int i = 0, j = 0; if (e != null) { e.printStackTrace(); Crashlytics.logException(e); return; } JsonArray ja = result.getResult(); if (ja != null) { while (i < 5 && j < ja.size()) { JsonObject jo = ja.get(j).getAsJsonObject(); if (jo.get("story") == null) { String name; i++; String message = jo.get("message") != null ? jo.get("message").getAsString() : ""; final String link = jo.get("link") != null ? jo.get("link").getAsString() : ""; if ("photo".equals(jo.get("type").getAsString())) { name = "Voir la photo sur Facebook"; } else { name = jo.get("name") != null ? jo.get("name").getAsString() : ""; } View postCard = LayoutInflater.from(mContext).inflate(R.layout.post_antoine, mPostsAntoineLayout, false); ((TextView) postCard.findViewById(R.id.message)).setText(message); ((TextView) postCard.findViewById(R.id.name)).setText(name); if (link != null && !link.isEmpty()) { postCard.findViewById(R.id.name) .setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(link)); mContext.startActivity(intent); } }); } ((TextView) postCard.findViewById(R.id.link)).setText(link); mPostsAntoineLayout.addView(postCard); } j++; } } else { TextView tv = (TextView) mPostsAntoineLayout.findViewById(R.id.post_error); if (tv == null) { tv = (TextView) LayoutInflater.from(mContext).inflate(R.layout.simple_textview, mPostsAntoineLayout, false); } tv.setText( "Problme lors de la rception des donnes, " + "cliquez pour ressayer"); tv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { fetchAntoinePosts(); } }); mPostsAntoineLayout.addView(tv); } } }); } }