Skip to content

Commit

Permalink
#502 - Updated mytrips to fix duplicates (#516)
Browse files Browse the repository at this point in the history
  • Loading branch information
Balram02 authored and Swati4star committed Oct 19, 2018
1 parent f54d44d commit 1321d12
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.airbnb.lottie.LottieAnimationView;

Expand Down Expand Up @@ -52,6 +53,8 @@ public class MyTripsFragment extends Fragment implements SwipeRefreshLayout.OnRe
SwipeRefreshLayout swipeRefreshLayout;
@BindView(R.id.rv)
RecyclerView mRecyclerView;
@BindView(R.id.my_trips_main_layout)
RelativeLayout my_trips_main_layout;
private String mToken;
private Handler mHandler;
private Activity mActivity;
Expand All @@ -61,10 +64,10 @@ public class MyTripsFragment extends Fragment implements SwipeRefreshLayout.OnRe

private List<Trip> mTripList = new ArrayList<>();


public MyTripsFragment() {
// Required empty public constructor
}

public static MyTripsFragment newInstance() {
return new MyTripsFragment();
}
Expand Down Expand Up @@ -100,6 +103,8 @@ private LinearLayoutManager getLayoutManager() {

private void mytrip() {

swipeRefreshLayout.setRefreshing(true);

String uri = API_LINK_V2 + "get-all-trips";

Log.v("EXECUTING", uri);
Expand All @@ -124,7 +129,7 @@ public void onFailure(Call call, IOException e) {
public void onResponse(Call call, final Response response) {

mHandler.post(() -> {
swipeRefreshLayout.setRefreshing(false);
mTripList.clear();
if (response.isSuccessful() && response.body() != null) {
JSONArray arr;
try {
Expand All @@ -146,27 +151,28 @@ public void onResponse(Call call, final Response response) {
String image = arr.getJSONObject(i).getJSONObject("city").getString("image");
mTripList.add(new Trip(id, name, image, start, end, tname));
animationView.setVisibility(View.GONE);
my_trips_main_layout.setVisibility(View.VISIBLE);
}
mMyTripsAdapter.notifyItemRangeInserted(0, arr.length());
mMyTripsAdapter.notifyDataSetChanged();

} catch (JSONException | IOException | NullPointerException e) {
e.printStackTrace();
Log.e("ERROR", "Message : " + e.getMessage());
networkError();
}

} else {
networkError();
}
swipeRefreshLayout.setRefreshing(false);
});
}
});
}

@OnClick(R.id.add_trip)
void addTrip() {
Intent intent = new Intent(getContext() , AddNewTripActivity.class);
startActivityForResult(intent , ADDNEWTRIP_ACTIVITY);
Intent intent = new Intent(getContext(), AddNewTripActivity.class);
startActivityForResult(intent, ADDNEWTRIP_ACTIVITY);

}

Expand All @@ -175,6 +181,9 @@ void addTrip() {
*/
private void networkError() {
animationView.setAnimation(R.raw.network_lost);
animationView.setVisibility(View.VISIBLE);
swipeRefreshLayout.setRefreshing(false);
my_trips_main_layout.setVisibility(View.GONE);
animationView.playAnimation();
}

Expand All @@ -185,6 +194,7 @@ private void noResults() {
TravelmateSnackbars.createSnackBar(mTripsView.findViewById(R.id.my_trips_frag), R.string.no_trips,
Snackbar.LENGTH_LONG).show();
animationView.setAnimation(R.raw.empty_list);
animationView.setVisibility(View.VISIBLE);
animationView.playAnimation();
}

Expand All @@ -198,20 +208,18 @@ public void onAttach(Context activity) {
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == ADDNEWTRIP_ACTIVITY && resultCode == RESULT_OK) {
if (mMyTripsAdapter != null) {
mMyTripsAdapter.notifyDataSetChanged();
}
mytrip();
}
}

@Override
public void onResume() {
mytrip();
super.onResume();
}

@Override
public void onRefresh() {
mytrip();
swipeRefreshLayout.setRefreshing(true);
}
}
78 changes: 43 additions & 35 deletions Android/app/src/main/res/layout/fragment_my_trips.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,51 @@
android:layout_height="match_parent"
android:layout_margin="5dp">

<FrameLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".mytrips.MyTripsFragment">

<com.airbnb.lottie.LottieAnimationView
android:id="@+id/animation_view"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center"
app:lottie_autoPlay="true"
app:lottie_fileName="loading.json"
app:lottie_imageAssetsFolder="images"
app:lottie_loop="true" />

<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh"
android:layout_height="wrap_content">

<FrameLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="match_parent"
tools:context=".mytrips.MyTripsFragment">

<com.airbnb.lottie.LottieAnimationView
android:id="@+id/animation_view"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center"
app:lottie_autoPlay="true"
app:lottie_fileName="loading.json"
app:lottie_imageAssetsFolder="images"
app:lottie_loop="true" />

<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
<RelativeLayout
android:id="@+id/my_trips_main_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:clipToPadding="false"
tools:listitem="@layout/trip_listitem"/>
</android.support.v4.widget.SwipeRefreshLayout>

<android.support.design.widget.FloatingActionButton
android:id="@+id/add_trip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="16dp"
android:src="@drawable/ic_add_circle_white_24dp" />

</FrameLayout>
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:padding="10dp"
tools:listitem="@layout/trip_listitem" />

<android.support.design.widget.FloatingActionButton
android:id="@+id/add_trip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_margin="16dp"
android:src="@drawable/ic_add_circle_white_24dp" />

</RelativeLayout>

</FrameLayout>
</android.support.v4.widget.SwipeRefreshLayout>
</android.support.design.widget.CoordinatorLayout>

0 comments on commit 1321d12

Please sign in to comment.