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..ffca567 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", "Following", "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..b04b3e9 --- /dev/null +++ b/app/src/main/java/com/github/ghcli/adapter/ListMyFollowingAdapter.java @@ -0,0 +1,165 @@ +package com.github.ghcli.adapter; + +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.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) { + 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) { + + 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..6b3bdca --- /dev/null +++ b/app/src/main/java/com/github/ghcli/fragments/MyFollowingFragment.java @@ -0,0 +1,137 @@ +package com.github.ghcli.fragments; + +import android.content.Context; +import android.net.Uri; +import android.os.Bundle; +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 com.github.ghcli.R; +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; + +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() { + + } + + 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) { + 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; + } + + + 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..a2058e8 --- /dev/null +++ b/app/src/main/java/com/github/ghcli/interfaces/RecyclerViewOnClickListener.java @@ -0,0 +1,9 @@ +package com.github.ghcli.interfaces; + +import android.view.View; + +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 c2dc91d..9374665 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 @@ -33,6 +33,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..e4af743 --- /dev/null +++ b/app/src/main/res/layout/item_following_card.xml @@ -0,0 +1,127 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ 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