Skip to content

Commit

Permalink
Merge pull request #19 from apedroneto/following
Browse files Browse the repository at this point in the history
Following
  • Loading branch information
gabrielsvinha authored Feb 13, 2018
2 parents 5586559 + 02fef44 commit 74b9636
Show file tree
Hide file tree
Showing 8 changed files with 469 additions and 1 deletion.
6 changes: 5 additions & 1 deletion app/src/main/java/com/github/ghcli/activities/HomePage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down
165 changes: 165 additions & 0 deletions app/src/main/java/com/github/ghcli/adapter/ListMyFollowingAdapter.java
Original file line number Diff line number Diff line change
@@ -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<ListMyFollowingAdapter.MyViewHolder> {

private List<GitHubUser> mList;
private LayoutInflater mLayoutInflater;
private Context context;
private IGitHubUser iGitHubUser;


public ListMyFollowingAdapter(Context c, List<GitHubUser> 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<Void> call = iGitHubUser.unfollow(
Authentication.getToken(context),
item.getLogin());

call.enqueue(new Callback<Void>() {
@Override
public void onResponse(@NonNull Call<Void> call,
@NonNull Response<Void> response) {
if (response.isSuccessful()) {
holder.switchFollow.setChecked(false);
}
}

@Override
public void onFailure(Call<Void> call, Throwable t) {

}
});
}else{
Message.showToast("FOLLOW", context);

Call<Void> call = iGitHubUser.follow(
Authentication.getToken(context),
item.getLogin());

call.enqueue(new Callback<Void>() {
@Override
public void onResponse(@NonNull Call<Void> call,
@NonNull Response<Void> response) {
if (response.isSuccessful()) {
holder.switchFollow.setChecked(true);
}
}

@Override
public void onFailure(Call<Void> 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) {

}
}
}
137 changes: 137 additions & 0 deletions app/src/main/java/com/github/ghcli/fragments/MyFollowingFragment.java
Original file line number Diff line number Diff line change
@@ -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<List<GitHubUser>> callFollowers = iGitHubUser.getFollowing(Authentication.getToken(context));
List<GitHubUser> gitHubUsers = new ArrayList<>();
List<GitHubUser> myFollowers = new ArrayList<>();
try {
gitHubUsers = callFollowers.execute().body();

for(GitHubUser gitHubUser : gitHubUsers){

Call<GitHubUser> 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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.github.ghcli.interfaces;

import android.view.View;

public interface RecyclerViewOnClickListener {

public void onClickListener(View view, int position);

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ public interface IGitHubUser {
@GET("user/followers")
Call<List<GitHubUser>> getFollowers(@Header("Authorization") String credentials);

@GET("user/following")
Call<List<GitHubUser>> getFollowing(@Header("Authorization") String credentials);

@GET("users/{user}")
Call<GitHubUser> getOneUser(@Header("Authorization") String credentials, @Path("user") String user);

@GET("user/following/{user}")
Call<Void> isFollowing(@Header("Authorization") String credentials, @Path("user") String user);

Expand Down
19 changes: 19 additions & 0 deletions app/src/main/res/layout/fragment_my_followings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="3dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v7.widget.RecyclerView
android:id="@+id/rvListMyFollowings"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:scrollbars="vertical"/>


</LinearLayout>
Loading

0 comments on commit 74b9636

Please sign in to comment.