Commit 911a4a58 authored by Varun Patil's avatar Varun Patil

Add follow button functionality

parent ab38e246
......@@ -37,6 +37,9 @@ public interface RetrofitInterface {
@GET("bodies/{uuid}")
Call<in.ac.iitb.gymkhana.iitbapp.data.Body> getBody(@Header("Cookie") String sessionId, @Path("uuid") String uuid);
@GET("bodies/{bodyID}/follow")
Call<Void> updateBodyFollowing(@Header("Cookie") String sessionID, @Path("bodyID") String eventID, @Query("action") int action);
@POST("upload")
Call<ImageUploadResponse> uploadImage(@Header("Cookie") String sessionID, @Body ImageUploadRequest imageUploadRequest);
......
......@@ -49,8 +49,11 @@ public class Body {
@ColumnInfo(name = "blog_url")
@SerializedName("blog_url")
String bodyBlogURL;
@ColumnInfo(name = "user_follows")
@SerializedName("user_follows")
Boolean bodyUserFollows;
public Body(String bodyID, String bodyStrID, String bodyName, String bodyShortDescription, String bodyDescription, String bodyImageURL, List<Body> bodyChildren, List<Body> bodyParents, List<Event> bodyEvents, int bodyFollowersCount, String bodyWebsiteURL, String bodyBlogURL) {
public Body(String bodyID, String bodyStrID, String bodyName, String bodyShortDescription, String bodyDescription, String bodyImageURL, List<Body> bodyChildren, List<Body> bodyParents, List<Event> bodyEvents, int bodyFollowersCount, String bodyWebsiteURL, String bodyBlogURL, Boolean bodyUserFollows) {
this.bodyID = bodyID;
this.bodyStrID = bodyStrID;
this.bodyName = bodyName;
......@@ -63,6 +66,7 @@ public class Body {
this.bodyFollowersCount = bodyFollowersCount;
this.bodyWebsiteURL = bodyWebsiteURL;
this.bodyBlogURL = bodyBlogURL;
this.bodyUserFollows = bodyUserFollows;
}
public String getBodyID() {
......@@ -161,4 +165,11 @@ public class Body {
this.bodyBlogURL = bodyBlogURL;
}
public Boolean getBodyUserFollows() {
return bodyUserFollows;
}
public void setBodyUserFollows(Boolean bodyUserFollows) {
this.bodyUserFollows = bodyUserFollows;
}
}
\ No newline at end of file
......@@ -4,6 +4,7 @@ import android.arch.persistence.room.Dao;
import android.arch.persistence.room.Delete;
import android.arch.persistence.room.Insert;
import android.arch.persistence.room.Query;
import android.arch.persistence.room.Update;
import java.util.List;
......@@ -69,6 +70,9 @@ public interface DbDao {
@Insert
void insertBody(Body body);
@Update
void updateBody(Body body);
@Insert
void insertVenues(List<Venue> venues);
......
......@@ -3,6 +3,7 @@ package in.ac.iitb.gymkhana.iitbapp.fragment;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
......@@ -127,11 +128,39 @@ public class BodyFragment extends Fragment {
ImageView eventPicture = (ImageView) getActivity().findViewById(R.id.body_picture);
ImageButton webBodyButton = getActivity().findViewById(R.id.web_body_button);
ImageButton shareBodyButton = getActivity().findViewById(R.id.share_body_button);
final Button followButton = getActivity().findViewById(R.id.follow_button);
/* Set body information */
bodyName.setText(body.getBodyName());
Markwon.setMarkdown(bodyDescription, body.getBodyDescription());
Picasso.with(getContext()).load(body.getBodyImageURL()).into(eventPicture);
/* Check if user is already following
* Initialize follow button */
followButton.setBackgroundColor(getResources().getColor(body.getBodyUserFollows() ? R.color.colorAccent : R.color.colorWhite));
followButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
RetrofitInterface retrofitInterface = ServiceGenerator.createService(RetrofitInterface.class);
retrofitInterface.updateBodyFollowing(((MainActivity) getActivity()).getSessionIDHeader(), body.getBodyID(), body.getBodyUserFollows() ? 0:1).enqueue(new Callback<Void>() {
@Override
public void onResponse(Call<Void> call, Response<Void> response) {
if (response.isSuccessful()) {
body.setBodyUserFollows(!body.getBodyUserFollows());
new updateDbBody().execute(body);
followButton.setBackgroundColor(getResources().getColor(body.getBodyUserFollows() ? R.color.colorAccent : R.color.colorWhite));
}
}
@Override
public void onFailure(Call<Void> call, Throwable t) {
Toast.makeText(getContext(), "Network Error", Toast.LENGTH_LONG).show();
}
});
}
});
/* Initialize web button */
if (body.getBodyWebsiteURL() != null && !body.getBodyWebsiteURL().isEmpty())
{
......@@ -180,6 +209,14 @@ public class BodyFragment extends Fragment {
eventRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
}
private class updateDbBody extends AsyncTask<Body, Void, Integer> {
@Override
protected Integer doInBackground(Body... body) {
appDatabase.dbDao().updateBody(body[0]);
return 1;
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment