Commit e0358034 authored by Varun Patil's avatar Varun Patil

Add Explore functionality

parent 8fafdcba
...@@ -30,6 +30,7 @@ import app.insti.data.Body; ...@@ -30,6 +30,7 @@ import app.insti.data.Body;
import app.insti.data.User; import app.insti.data.User;
import app.insti.fragment.BodyFragment; import app.insti.fragment.BodyFragment;
import app.insti.fragment.CalendarFragment; import app.insti.fragment.CalendarFragment;
import app.insti.fragment.ExploreFragment;
import app.insti.fragment.FeedFragment; import app.insti.fragment.FeedFragment;
import app.insti.fragment.MapFragment; import app.insti.fragment.MapFragment;
import app.insti.fragment.MessMenuFragment; import app.insti.fragment.MessMenuFragment;
...@@ -216,10 +217,16 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On ...@@ -216,10 +217,16 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
Toast.makeText(this, Constants.LOGIN_MESSAGE, Toast.LENGTH_LONG).show(); Toast.makeText(this, Constants.LOGIN_MESSAGE, Toast.LENGTH_LONG).show();
} }
break; break;
case R.id.nav_explore:
updateFragment(ExploreFragment.newInstance());
break;
case R.id.nav_news: case R.id.nav_news:
NewsFragment newsFragment = new NewsFragment(); NewsFragment newsFragment = new NewsFragment();
updateFragment(newsFragment); updateFragment(newsFragment);
break; break;
case R.id.nav_placement_blog: case R.id.nav_placement_blog:
if (session.isLoggedIn()) { if (session.isLoggedIn()) {
PlacementBlogFragment placementBlogFragment = new PlacementBlogFragment(); PlacementBlogFragment placementBlogFragment = new PlacementBlogFragment();
......
...@@ -71,7 +71,9 @@ public class BodyAdapter extends RecyclerView.Adapter<BodyAdapter.ViewHolder> { ...@@ -71,7 +71,9 @@ public class BodyAdapter extends RecyclerView.Adapter<BodyAdapter.ViewHolder> {
description = (TextView) itemView.findViewById(R.id.body_card_description); description = (TextView) itemView.findViewById(R.id.body_card_description);
image = (ImageView) itemView.findViewById(R.id.body_card_avatar); image = (ImageView) itemView.findViewById(R.id.body_card_avatar);
} }
}
public void setBodyList(List<Body> bodyList) {
this.bodyList = bodyList;
} }
} }
...@@ -96,4 +96,8 @@ public class FeedAdapter extends RecyclerView.Adapter<FeedAdapter.ViewHolder> { ...@@ -96,4 +96,8 @@ public class FeedAdapter extends RecyclerView.Adapter<FeedAdapter.ViewHolder> {
eventVenue = (TextView) itemView.findViewById(R.id.event_venue); eventVenue = (TextView) itemView.findViewById(R.id.event_venue);
} }
} }
public void setPosts(List<Event> posts) {
this.posts = posts;
}
} }
...@@ -74,4 +74,8 @@ public class UserAdapter extends RecyclerView.Adapter<UserAdapter.ViewHolder> { ...@@ -74,4 +74,8 @@ public class UserAdapter extends RecyclerView.Adapter<UserAdapter.ViewHolder> {
image = (ImageView) itemView.findViewById(R.id.role_card_avatar); image = (ImageView) itemView.findViewById(R.id.role_card_avatar);
} }
} }
public void setUserList(List<User> userList) {
this.userList = userList;
}
} }
...@@ -4,6 +4,7 @@ import java.util.List; ...@@ -4,6 +4,7 @@ import java.util.List;
import app.insti.api.model.EventCreateRequest; import app.insti.api.model.EventCreateRequest;
import app.insti.api.model.EventCreateResponse; import app.insti.api.model.EventCreateResponse;
import app.insti.api.model.ExploreResponse;
import app.insti.api.model.ImageUploadRequest; import app.insti.api.model.ImageUploadRequest;
import app.insti.api.model.ImageUploadResponse; import app.insti.api.model.ImageUploadResponse;
import app.insti.api.model.LoginResponse; import app.insti.api.model.LoginResponse;
...@@ -51,6 +52,9 @@ public interface RetrofitInterface { ...@@ -51,6 +52,9 @@ public interface RetrofitInterface {
@GET("bodies/{uuid}") @GET("bodies/{uuid}")
Call<app.insti.data.Body> getBody(@Header("Cookie") String sessionId, @Path("uuid") String uuid); Call<app.insti.data.Body> getBody(@Header("Cookie") String sessionId, @Path("uuid") String uuid);
@GET("bodies")
Call<List<app.insti.data.Body>> getAllBodies(@Header("Cookie") String sessionId);
@GET("bodies/{bodyID}/follow") @GET("bodies/{bodyID}/follow")
Call<Void> updateBodyFollowing(@Header("Cookie") String sessionID, @Path("bodyID") String eventID, @Query("action") int action); Call<Void> updateBodyFollowing(@Header("Cookie") String sessionID, @Path("bodyID") String eventID, @Query("action") int action);
...@@ -81,6 +85,6 @@ public interface RetrofitInterface { ...@@ -81,6 +85,6 @@ public interface RetrofitInterface {
@GET("logout") @GET("logout")
Call<Void> logout(@Header("Cookie") String sessionID); Call<Void> logout(@Header("Cookie") String sessionID);
// @POST("getNotifications/") @GET("search")
// Call<NotificationsResponse> getNotifications(@Body NotificationsRequest notificationsRequest); Call<ExploreResponse> search(@Header("Cookie") String sessionID, @Query("query") String query);
} }
package app.insti.api.model;
import com.google.gson.annotations.SerializedName;
import java.util.List;
import app.insti.data.Body;
import app.insti.data.Event;
import app.insti.data.User;
public class ExploreResponse {
@SerializedName("bodies")
private List<Body> bodies;
@SerializedName("events")
private List<Event> events;
@SerializedName("users")
private List<User> users;
public ExploreResponse(List<Body> bodies, List<Event> events, List<User> users) {
this.bodies = bodies;
this.events = events;
this.users = users;
}
public List<Body> getBodies() {
return bodies;
}
public void setBodies(List<Body> bodies) {
this.bodies = bodies;
}
public List<Event> getEvents() {
return events;
}
public void setEvents(List<Event> events) {
this.events = events;
}
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
}
package app.insti.fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;
import app.insti.Constants;
import app.insti.ItemClickListener;
import app.insti.MainActivity;
import app.insti.R;
import app.insti.adapter.BodyAdapter;
import app.insti.adapter.FeedAdapter;
import app.insti.adapter.UserAdapter;
import app.insti.api.RetrofitInterface;
import app.insti.api.ServiceGenerator;
import app.insti.api.model.ExploreResponse;
import app.insti.data.Body;
import app.insti.data.Event;
import app.insti.data.User;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
/**
* A simple {@link Fragment} subclass.
* Use the {@link ExploreFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class ExploreFragment extends Fragment {
private String sessionId;
private List<Body> allBodies = new ArrayList<>();
private List<Body> bodies = new ArrayList<>();
private List<Event> events = new ArrayList<>();
private List<User> users = new ArrayList<>();
private BodyAdapter bodyAdapter;
private FeedAdapter eventsAdapter;
private UserAdapter userAdapter;
public ExploreFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment.
* @return A new instance of fragment ExploreFragment.
*/
// TODO: Rename and change types and number of parameters
public static ExploreFragment newInstance() {
ExploreFragment fragment = new ExploreFragment();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onStart() {
super.onStart();
// Initialize
sessionId = ((MainActivity) getActivity()).getSessionIDHeader();
initRecyclerViews();
// Get all bodies
if (allBodies.size() == 0) {
RetrofitInterface retrofitInterface = ServiceGenerator.createService(RetrofitInterface.class);
retrofitInterface.getAllBodies(sessionId).enqueue(new Callback<List<Body>>() {
@Override
public void onResponse(Call<List<Body>> call, Response<List<Body>> response) {
allBodies = response.body();
bodies = allBodies;
updateAdapters(bodies, new ArrayList<Event>(), new ArrayList<User>());
}
@Override
public void onFailure(Call<List<Body>> call, Throwable t) {}
});
}
// Search on text change in search
final EditText searchEditText = getView().findViewById(R.id.explore_search);
searchEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void afterTextChanged(Editable s) {
if (searchEditText.getText().length() >= 3) {
doSearch(searchEditText.getText().toString());
} else if (searchEditText.getText().length() == 0) {
bodies = allBodies;
updateAdapters(bodies, new ArrayList<Event>(), new ArrayList<User>());
}
}
});
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_explore, container, false);
}
public void doSearch(String query) {
if (getActivity() == null || getView() == null) return;
RetrofitInterface retrofitInterface = ServiceGenerator.createService(RetrofitInterface.class);
retrofitInterface.search(sessionId, query).enqueue(new Callback<ExploreResponse>() {
@Override
public void onResponse(Call<ExploreResponse> call, Response<ExploreResponse> response) {
// Get data
bodies = response.body().getBodies();
events = response.body().getEvents();
users = response.body().getUsers();
updateAdapters(bodies, events, users);
}
@Override
public void onFailure(Call<ExploreResponse> call, Throwable t) {
// Request failed
}
});
}
private void updateAdapters(List<Body> bodies, List<Event> events, List<User> users) {
if (getActivity() == null || getView() == null) return;
// Make spinner gone
getView().findViewById(R.id.loadingPanel).setVisibility(View.GONE);
// Set adapters data
bodyAdapter.setBodyList(bodies);
eventsAdapter.setPosts(events);
userAdapter.setUserList(users);
// Notify all adapters
bodyAdapter.notifyDataSetChanged();
eventsAdapter.notifyDataSetChanged();
userAdapter.notifyDataSetChanged();
}
public void initRecyclerViews() {
if (getActivity() == null || getView() == null) return;
// Bodies
RecyclerView bodiesRecyclerView = getView().findViewById(R.id.explore_body_recycler_view);
bodyAdapter = new BodyAdapter(bodies, new ItemClickListener() {
@Override
public void onItemClick(View v, int position) {
Bundle bundle = new Bundle();
bundle.putString(Constants.BODY_JSON, new Gson().toJson(bodies.get(position)));
updateFragment(new BodyFragment(), bundle);
}
});
bodiesRecyclerView.setAdapter(bodyAdapter);
bodiesRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
// Events
RecyclerView eventsRecyclerView = getView().findViewById(R.id.explore_event_recycler_view);
eventsAdapter = new FeedAdapter(events, new ItemClickListener() {
@Override
public void onItemClick(View v, int position) {
Event event = events.get(position);
Bundle bundle = new Bundle();
bundle.putString(Constants.EVENT_JSON, new Gson().toJson(event));
updateFragment(new EventFragment(), bundle);
}
});
eventsRecyclerView.setAdapter(eventsAdapter);
eventsRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
// Users
RecyclerView usersRecyclerView = getView().findViewById(R.id.explore_user_recycler_view);
userAdapter = new UserAdapter(users, new ItemClickListener() {
@Override
public void onItemClick(View v, int position) {
User user = users.get(position);
Bundle bundle = new Bundle();
bundle.putString(Constants.USER_ID, user.getUserID());
updateFragment(new ProfileFragment(), bundle);
}
});
usersRecyclerView.setAdapter(userAdapter);
usersRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
}
public void updateFragment(Fragment fragment, Bundle bundle) {
fragment.setArguments(bundle);
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left, R.anim.slide_in_right, R.anim.slide_out_right);
ft.replace(R.id.framelayout_for_fragment, fragment, fragment.getTag());
ft.addToBackStack(fragment.getTag());
ft.commit();
}
}
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M15.5,14h-0.79l-0.28,-0.27C15.41,12.59 16,11.11 16,9.5 16,5.91 13.09,3 9.5,3S3,5.91 3,9.5 5.91,16 9.5,16c1.61,0 3.09,-0.59 4.23,-1.57l0.27,0.28v0.79l5,4.99L20.49,19l-4.99,-5zM9.5,14C7.01,14 5,11.99 5,9.5S7.01,5 9.5,5 14,7.01 14,9.5 11.99,14 9.5,14z"/>
</vector>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment.ExploreFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/explore_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Search"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="3dp" />
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/explore_body_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false" />
<android.support.v7.widget.RecyclerView
android:id="@+id/explore_event_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false" />
<android.support.v7.widget.RecyclerView
android:id="@+id/explore_user_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
<RelativeLayout
android:id="@+id/loadingPanel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true" />
</RelativeLayout>
</FrameLayout>
\ No newline at end of file
...@@ -19,6 +19,11 @@ ...@@ -19,6 +19,11 @@
android:icon="@drawable/ic_event" android:icon="@drawable/ic_event"
android:title="My Events" /> android:title="My Events" />
<item
android:id="@+id/nav_explore"
android:icon="@drawable/ic_search_black_24dp"
android:title="Explore" />
<item <item
android:id="@+id/nav_mess_menu" android:id="@+id/nav_mess_menu"
android:icon="@drawable/ic_restaurant_black_48dp" android:icon="@drawable/ic_restaurant_black_48dp"
......
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