-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from apedroneto/following
Following
- Loading branch information
Showing
8 changed files
with
469 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 165 additions & 0 deletions
165
app/src/main/java/com/github/ghcli/adapter/ListMyFollowingAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
137
app/src/main/java/com/github/ghcli/fragments/MyFollowingFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
app/src/main/java/com/github/ghcli/interfaces/RecyclerViewOnClickListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.