From 4c757707ede70869e38726aa96e719c79cb655d1 Mon Sep 17 00:00:00 2001 From: antonioPedroNeto Date: Tue, 13 Feb 2018 15:11:58 -0300 Subject: [PATCH 1/5] Added feature view my following --- .../com/github/ghcli/activities/HomePage.java | 6 +- .../ghcli/adapter/ListMyFollowingAdapter.java | 168 ++++++++++++++++++ .../ghcli/fragments/MyFollowingFragment.java | 165 +++++++++++++++++ .../RecyclerViewOnClickListener.java | 13 ++ .../ghcli/service/clients/IGitHubUser.java | 6 + .../res/layout/fragment_my_followings.xml | 19 ++ .../main/res/layout/item_following_card.xml | 130 ++++++++++++++ app/src/main/res/values/colors.xml | 1 + 8 files changed, 507 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/com/github/ghcli/adapter/ListMyFollowingAdapter.java create mode 100644 app/src/main/java/com/github/ghcli/fragments/MyFollowingFragment.java create mode 100644 app/src/main/java/com/github/ghcli/interfaces/RecyclerViewOnClickListener.java create mode 100644 app/src/main/res/layout/fragment_my_followings.xml create mode 100644 app/src/main/res/layout/item_following_card.xml diff --git a/app/src/main/java/com/github/ghcli/activities/HomePage.java b/app/src/main/java/com/github/ghcli/activities/HomePage.java index 25a2ef4..54f0fc3 100644 --- a/app/src/main/java/com/github/ghcli/activities/HomePage.java +++ b/app/src/main/java/com/github/ghcli/activities/HomePage.java @@ -23,6 +23,7 @@ import com.github.ghcli.R; import com.github.ghcli.fragments.FollowersFragment; import com.github.ghcli.fragments.IssuesFragment; +import com.github.ghcli.fragments.MyFollowingFragment; import com.github.ghcli.fragments.ProfileFragment; import com.github.ghcli.fragments.ReposFragment; import com.github.ghcli.models.GitHubOrganization; @@ -44,7 +45,7 @@ public class HomePage extends AppCompatActivity implements private static final String KEY_USER_ORGANIZATIONS = "organizations"; private DrawerLayout drawerLayout; - private String[] actions = {"Profile", "Repositories", "Followers", "Issues", "Sign out"}; + private String[] actions = {"Profile", "Repositories", "Followers", "Issues", "My Followings", "Sign out"}; private ListView leftDrawer; private ActionBarDrawerToggle drawerToggle; @@ -147,6 +148,9 @@ private void selectFragment(int item) { frag = IssuesFragment.newInstance("teste1","teste2"); break; case 4: + frag = MyFollowingFragment.newInstance("teste1", "teste2"); + break; + case 5: logout(); break; } diff --git a/app/src/main/java/com/github/ghcli/adapter/ListMyFollowingAdapter.java b/app/src/main/java/com/github/ghcli/adapter/ListMyFollowingAdapter.java new file mode 100644 index 0000000..215b0ed --- /dev/null +++ b/app/src/main/java/com/github/ghcli/adapter/ListMyFollowingAdapter.java @@ -0,0 +1,168 @@ +package com.github.ghcli.adapter; + +import android.content.Context; +import android.support.annotation.NonNull; +import android.support.v7.widget.RecyclerView; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.TextView; +import android.widget.ToggleButton; + +import com.github.ghcli.R; +import com.github.ghcli.models.GitHubUser; +import com.github.ghcli.service.clients.IGitHubUser; +import com.github.ghcli.util.Authentication; +import com.github.ghcli.util.Message; +import com.squareup.picasso.Picasso; + +import java.util.List; + +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; + +public class ListMyFollowingAdapter extends RecyclerView.Adapter { + + private List mList; + private LayoutInflater mLayoutInflater; + private Context context; + private IGitHubUser iGitHubUser; + + + public ListMyFollowingAdapter(Context c, List l, IGitHubUser iGitHubUser){ + mList = l; + mLayoutInflater = (LayoutInflater) c.getSystemService(c.LAYOUT_INFLATER_SERVICE); + context = c; + this.iGitHubUser = iGitHubUser; + } + + + @Override + public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + Log.i("LOG", "onCreateViewHolder"); + View v = mLayoutInflater.inflate(R.layout.item_following_card, parent, false); + + MyViewHolder myViewHolder = new MyViewHolder(v); + + + return myViewHolder; + } + + @Override + public void onBindViewHolder(final MyViewHolder holder, int position) { + Log.i("LOG", "onBindViewHolder"); + + final GitHubUser item = mList.get(position); + + if(item.getName() != null){ + holder.rvTxtNomeUser.setText(String.valueOf(item.getName())); + }else{ + holder.rvTxtNomeUser.setText(""); + } + + holder.rvTxtDescricao.setText(item.getBio()); + + holder.rvTxtLocalizacao.setText(item.getLocation()); + + holder.rvTxtLoginUser.setText(item.getLogin()); + + holder.switchFollow.setChecked(true); + + holder.switchFollow.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if(!holder.switchFollow.isChecked()){ + Message.showToast("UNFOLLOW", context); + Call call = iGitHubUser.unfollow( + Authentication.getToken(context), + item.getLogin()); + + call.enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, + @NonNull Response response) { + if (response.isSuccessful()) { + holder.switchFollow.setChecked(false); + } + } + + @Override + public void onFailure(Call call, Throwable t) { + + } + }); + }else{ + Message.showToast("FOLLOW", context); + + Call call = iGitHubUser.follow( + Authentication.getToken(context), + item.getLogin()); + + call.enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, + @NonNull Response response) { + if (response.isSuccessful()) { + holder.switchFollow.setChecked(true); + } + } + + @Override + public void onFailure(Call call, Throwable t) { + + } + }); + } + } + }); + + Picasso.with(context).load(item.getAvatarUrl()).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).into(holder.rvImgUser); + + + + } + + + @Override + public int getItemCount() { + return mList.size(); + } + + public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{ + + + public TextView rvTxtNomeUser; + public TextView rvTxtLocalizacao; + public TextView rvTxtLoginUser; + public TextView rvTxtDescricao; + public ImageView rvImgUser; + public ToggleButton switchFollow; + + + + + public MyViewHolder(View itemView) { + super(itemView); + + rvImgUser = (ImageView) itemView.findViewById(R.id.rvImgUser); + + rvTxtNomeUser = (TextView) itemView.findViewById(R.id.rvTxtNomeUser); + rvTxtLocalizacao = (TextView) itemView.findViewById(R.id.rvTxtLocalizacao); + rvTxtLoginUser = (TextView) itemView.findViewById(R.id.rvTxtLoginUser); + rvTxtDescricao = (TextView) itemView.findViewById(R.id.rvTxtDescricao); + switchFollow = (ToggleButton) itemView.findViewById(R.id.switchFollow); + + + itemView.setOnClickListener(this); + + } + + @Override + public void onClick(View v) { + + } + } +} diff --git a/app/src/main/java/com/github/ghcli/fragments/MyFollowingFragment.java b/app/src/main/java/com/github/ghcli/fragments/MyFollowingFragment.java new file mode 100644 index 0000000..504838b --- /dev/null +++ b/app/src/main/java/com/github/ghcli/fragments/MyFollowingFragment.java @@ -0,0 +1,165 @@ +package com.github.ghcli.fragments; + +import android.content.Context; +import android.net.Uri; +import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.v4.app.Fragment; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ProgressBar; + +import com.github.ghcli.R; +import com.github.ghcli.adapter.ListFollowersAdapter; +import com.github.ghcli.adapter.ListMyFollowingAdapter; +import com.github.ghcli.models.GitHubUser; +import com.github.ghcli.service.ServiceGenerator; +import com.github.ghcli.service.clients.IGitHubUser; +import com.github.ghcli.util.Authentication; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import butterknife.BindView; +import butterknife.ButterKnife; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; + +/** + * Created by pedro on 13/02/18. + */ + +public class MyFollowingFragment extends Fragment { + private static final String ARG_PARAM1 = "param1"; + private static final String ARG_PARAM2 = "param2"; + + @BindView(R.id.rvListMyFollowings) + RecyclerView recyclerView; + + private String mParam1; + private String mParam2; + + private FollowersFragment.OnFragmentInteractionListener mListener; + private IGitHubUser iGitHubUser; + private Context context; + + + + public MyFollowingFragment() { + + } + + /** + * Use this factory method to create a new instance of + * this fragment using the provided parameters. + * + * @param param1 Parameter 1. + * @param param2 Parameter 2. + * @return A new instance of fragment FollowersFragment. + */ + public static MyFollowingFragment newInstance(String param1, String param2) { + MyFollowingFragment fragment = new MyFollowingFragment(); + Bundle args = new Bundle(); + args.putString(ARG_PARAM1, param1); + args.putString(ARG_PARAM2, param2); + fragment.setArguments(args); + fragment.setiGitHubUser(ServiceGenerator.createService(IGitHubUser.class)); + return fragment; + } + + private void getFollowings() { + Call> callFollowers = iGitHubUser.getFollowing(Authentication.getToken(context)); + List gitHubUsers = new ArrayList<>(); + List myFollowers = new ArrayList<>(); + try { + gitHubUsers = callFollowers.execute().body(); + + for(GitHubUser gitHubUser : gitHubUsers){ + + Call callMyFollowing = iGitHubUser.getOneUser(Authentication.getToken(context), gitHubUser.getLogin()); + myFollowers.add(callMyFollowing.execute().body()); + + } + recyclerView.setAdapter(new ListMyFollowingAdapter(context, myFollowers,iGitHubUser)); + RecyclerView.LayoutManager layout = new LinearLayoutManager( + context, + LinearLayoutManager.VERTICAL, + false); + recyclerView.setLayoutManager(layout); + + + } catch (IOException e) { + e.printStackTrace(); + } + + + } + + + + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + if (getArguments() != null) { + mParam1 = getArguments().getString(ARG_PARAM1); + mParam2 = getArguments().getString(ARG_PARAM2); + } + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + // Inflate the layout for this fragment + View view = inflater.inflate(R.layout.fragment_my_followings, container, false); + ButterKnife.bind(this, view); + this.context = getActivity().getApplicationContext(); + getFollowings(); + return view; + } + + @Override + public void onAttach(Context context) { + super.onAttach(context); + if (context instanceof FollowersFragment.OnFragmentInteractionListener) { + mListener = (FollowersFragment.OnFragmentInteractionListener) context; + } else { + throw new RuntimeException(context.toString() + + " must implement OnFragmentInteractionListener"); + } + } + + @Override + public void onDetach() { + super.onDetach(); + mListener = null; + } + + public IGitHubUser getiGitHubUser() { + return iGitHubUser; + } + + public void setiGitHubUser(IGitHubUser iGitHubUser) { + this.iGitHubUser = iGitHubUser; + } + + +/** + * 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. + *

+ * See the Android Training lesson Communicating with Other Fragments for more information. + */ +public interface OnFragmentInteractionListener { + void onFragmentInteraction(Uri uri); +} +} \ No newline at end of file diff --git a/app/src/main/java/com/github/ghcli/interfaces/RecyclerViewOnClickListener.java b/app/src/main/java/com/github/ghcli/interfaces/RecyclerViewOnClickListener.java new file mode 100644 index 0000000..4787272 --- /dev/null +++ b/app/src/main/java/com/github/ghcli/interfaces/RecyclerViewOnClickListener.java @@ -0,0 +1,13 @@ +package com.github.ghcli.interfaces; + +import android.view.View; + +/** + * Created by pedro on 26/12/2016. + */ + +public interface RecyclerViewOnClickListener { + + public void onClickListener(View view, int position); + +} diff --git a/app/src/main/java/com/github/ghcli/service/clients/IGitHubUser.java b/app/src/main/java/com/github/ghcli/service/clients/IGitHubUser.java index b6aa588..6de1e78 100644 --- a/app/src/main/java/com/github/ghcli/service/clients/IGitHubUser.java +++ b/app/src/main/java/com/github/ghcli/service/clients/IGitHubUser.java @@ -29,6 +29,12 @@ public interface IGitHubUser { @GET("user/followers") Call> getFollowers(@Header("Authorization") String credentials); + @GET("user/following") + Call> getFollowing(@Header("Authorization") String credentials); + + @GET("users/{user}") + Call getOneUser(@Header("Authorization") String credentials, @Path("user") String user); + @GET("user/following/{user}") Call isFollowing(@Header("Authorization") String credentials, @Path("user") String user); diff --git a/app/src/main/res/layout/fragment_my_followings.xml b/app/src/main/res/layout/fragment_my_followings.xml new file mode 100644 index 0000000..2c0e312 --- /dev/null +++ b/app/src/main/res/layout/fragment_my_followings.xml @@ -0,0 +1,19 @@ + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_following_card.xml b/app/src/main/res/layout/item_following_card.xml new file mode 100644 index 0000000..1d09469 --- /dev/null +++ b/app/src/main/res/layout/item_following_card.xml @@ -0,0 +1,130 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 6e57015..d1dc88c 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -3,4 +3,5 @@ #1d2226 #2c3135 #000000 + #000000 From ce7532af6ea828626be5365fd6fbab147dc8bd61 Mon Sep 17 00:00:00 2001 From: antonioPedroNeto Date: Tue, 13 Feb 2018 15:12:29 -0300 Subject: [PATCH 2/5] Added feature view my following --- app/src/main/res/layout/item_following_card.xml | 3 --- 1 file changed, 3 deletions(-) diff --git a/app/src/main/res/layout/item_following_card.xml b/app/src/main/res/layout/item_following_card.xml index 1d09469..2d8123b 100644 --- a/app/src/main/res/layout/item_following_card.xml +++ b/app/src/main/res/layout/item_following_card.xml @@ -85,9 +85,6 @@ android:layout_height="wrap_content" android:orientation="horizontal"> - Date: Tue, 13 Feb 2018 16:23:09 -0300 Subject: [PATCH 3/5] adjustments in comments and logs --- .../com/github/ghcli/activities/HomePage.java | 2 +- .../ghcli/adapter/ListMyFollowingAdapter.java | 3 -- .../ghcli/fragments/MyFollowingFragment.java | 34 ++----------------- 3 files changed, 4 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/com/github/ghcli/activities/HomePage.java b/app/src/main/java/com/github/ghcli/activities/HomePage.java index 54f0fc3..ffca567 100644 --- a/app/src/main/java/com/github/ghcli/activities/HomePage.java +++ b/app/src/main/java/com/github/ghcli/activities/HomePage.java @@ -45,7 +45,7 @@ public class HomePage extends AppCompatActivity implements private static final String KEY_USER_ORGANIZATIONS = "organizations"; private DrawerLayout drawerLayout; - private String[] actions = {"Profile", "Repositories", "Followers", "Issues", "My Followings", "Sign out"}; + private String[] actions = {"Profile", "Repositories", "Followers", "Issues", "Following", "Sign out"}; private ListView leftDrawer; private ActionBarDrawerToggle drawerToggle; diff --git a/app/src/main/java/com/github/ghcli/adapter/ListMyFollowingAdapter.java b/app/src/main/java/com/github/ghcli/adapter/ListMyFollowingAdapter.java index 215b0ed..b04b3e9 100644 --- a/app/src/main/java/com/github/ghcli/adapter/ListMyFollowingAdapter.java +++ b/app/src/main/java/com/github/ghcli/adapter/ListMyFollowingAdapter.java @@ -3,7 +3,6 @@ import android.content.Context; import android.support.annotation.NonNull; import android.support.v7.widget.RecyclerView; -import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -42,7 +41,6 @@ public ListMyFollowingAdapter(Context c, List l, IGitHubUser iGitHub @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { - Log.i("LOG", "onCreateViewHolder"); View v = mLayoutInflater.inflate(R.layout.item_following_card, parent, false); MyViewHolder myViewHolder = new MyViewHolder(v); @@ -53,7 +51,6 @@ public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { @Override public void onBindViewHolder(final MyViewHolder holder, int position) { - Log.i("LOG", "onBindViewHolder"); final GitHubUser item = mList.get(position); diff --git a/app/src/main/java/com/github/ghcli/fragments/MyFollowingFragment.java b/app/src/main/java/com/github/ghcli/fragments/MyFollowingFragment.java index 504838b..6b3bdca 100644 --- a/app/src/main/java/com/github/ghcli/fragments/MyFollowingFragment.java +++ b/app/src/main/java/com/github/ghcli/fragments/MyFollowingFragment.java @@ -3,17 +3,14 @@ import android.content.Context; import android.net.Uri; import android.os.Bundle; -import android.support.annotation.NonNull; import android.support.v4.app.Fragment; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.ProgressBar; import com.github.ghcli.R; -import com.github.ghcli.adapter.ListFollowersAdapter; import com.github.ghcli.adapter.ListMyFollowingAdapter; import com.github.ghcli.models.GitHubUser; import com.github.ghcli.service.ServiceGenerator; @@ -27,12 +24,6 @@ import butterknife.BindView; import butterknife.ButterKnife; import retrofit2.Call; -import retrofit2.Callback; -import retrofit2.Response; - -/** - * Created by pedro on 13/02/18. - */ public class MyFollowingFragment extends Fragment { private static final String ARG_PARAM1 = "param1"; @@ -54,14 +45,6 @@ public MyFollowingFragment() { } - /** - * Use this factory method to create a new instance of - * this fragment using the provided parameters. - * - * @param param1 Parameter 1. - * @param param2 Parameter 2. - * @return A new instance of fragment FollowersFragment. - */ public static MyFollowingFragment newInstance(String param1, String param2) { MyFollowingFragment fragment = new MyFollowingFragment(); Bundle args = new Bundle(); @@ -115,7 +98,6 @@ public void onCreate(Bundle savedInstanceState) { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_my_followings, container, false); ButterKnife.bind(this, view); this.context = getActivity().getApplicationContext(); @@ -149,17 +131,7 @@ public void setiGitHubUser(IGitHubUser iGitHubUser) { } -/** - * 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. - *

- * See the Android Training lesson Communicating with Other Fragments for more information. - */ -public interface OnFragmentInteractionListener { - void onFragmentInteraction(Uri uri); -} + public interface OnFragmentInteractionListener { + void onFragmentInteraction(Uri uri); + } } \ No newline at end of file From 510bba3aac23345818883ea47ae41d17e29403a9 Mon Sep 17 00:00:00 2001 From: antonioPedroNeto Date: Tue, 13 Feb 2018 16:33:48 -0300 Subject: [PATCH 4/5] adjustments in comments and logs --- .../github/ghcli/interfaces/RecyclerViewOnClickListener.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/app/src/main/java/com/github/ghcli/interfaces/RecyclerViewOnClickListener.java b/app/src/main/java/com/github/ghcli/interfaces/RecyclerViewOnClickListener.java index 4787272..a2058e8 100644 --- a/app/src/main/java/com/github/ghcli/interfaces/RecyclerViewOnClickListener.java +++ b/app/src/main/java/com/github/ghcli/interfaces/RecyclerViewOnClickListener.java @@ -2,10 +2,6 @@ import android.view.View; -/** - * Created by pedro on 26/12/2016. - */ - public interface RecyclerViewOnClickListener { public void onClickListener(View view, int position); From 02fef448c9682f203dd2bd5fac50b5c547ca5738 Mon Sep 17 00:00:00 2001 From: antonioPedroNeto Date: Tue, 13 Feb 2018 16:38:29 -0300 Subject: [PATCH 5/5] adjustments in comments and logs --- app/src/main/res/layout/item_following_card.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/res/layout/item_following_card.xml b/app/src/main/res/layout/item_following_card.xml index 2d8123b..e4af743 100644 --- a/app/src/main/res/layout/item_following_card.xml +++ b/app/src/main/res/layout/item_following_card.xml @@ -54,7 +54,7 @@ />