Skip to content

Commit

Permalink
Merge branch 'userPreferencesUI' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
nikosdel committed Jan 7, 2018
2 parents 0b5e2e9 + 21a77e0 commit 6d296d1
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

Expand Down Expand Up @@ -34,10 +36,14 @@ public RecyclerPreference(List<DiscountPreferencesResponse> discountPreferencesR
this.auth=auth;
}

public List<DiscountPreferencesResponse> getDiscountPreferencesResponses() {
return discountPreferencesResponses;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item_preference_list,parent,false);
return new MyViewHolder(view);
return new MyViewHolder(view,mContext);
}

@Override
Expand All @@ -47,7 +53,7 @@ public void onBindViewHolder(final MyViewHolder holder, int position) {
holder.price.setText(Float.toString(discountPreferencesResponses.get(position).getPrice()));
holder.categoryTitle.setText(discountPreferencesResponses.get(position).getCategoryTitle());
holder.id.setText(Integer.toString(discountPreferencesResponses.get(position).getId()));
holder.delete.setOnClickListener(new View.OnClickListener() {
/* holder.delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int idpref;
Expand All @@ -57,50 +63,59 @@ public void onClick(View view) {
notifyDataSetChanged();
}
});
}); */

}
public void removeDiscount(int position){
discountPreferencesResponses.remove(position);
}

public void restoreDiscount(DiscountPreferencesResponse discount,int position){
discountPreferencesResponses.add(position,discount);
}
public int getDiscountId(int position){
return discountPreferencesResponses.get(position).getId();
}



@Override
public int getItemCount() {
return discountPreferencesResponses.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
Button delete;
public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
// Button delete;
TextView id;
TextView price;
Context context;
LinearLayout foregroundViewPreference;
RelativeLayout backgroundViewPreference;
TextView tags;
TextView categoryTitle;

public MyViewHolder(View itemView) {
public MyViewHolder(View itemView,Context context) {
super(itemView);
delete=itemView.findViewById(R.id.openStoreButton);
//delete=itemView.findViewById(R.id.openStoreButton);
id=itemView.findViewById(R.id.idPref);
price=itemView.findViewById(R.id.priceText);
tags=itemView.findViewById(R.id.tagsTextView);
this.context = context;
categoryTitle=itemView.findViewById(R.id.categoryTextView);
foregroundViewPreference = itemView.findViewById(R.id.foreground_view_userpref);
backgroundViewPreference = itemView.findViewById(R.id.background_view_userpref);

itemView.setOnClickListener(this);

}

}
}

public void deletePref(final int id, String auth)
{ iuserService= ApiUtils.getUserService();
Call<Void> delete=iuserService.deleteDiscountPreference(id,auth);
delete.enqueue(new Callback<Void>() {
@Override
public void onResponse(Call<Void> call, Response<Void> response) {
Toast.makeText(mContext,"You Preference with "+id+" remove Succesfully",Toast.LENGTH_SHORT).show();
@Override
public void onClick(View view) {

}
}
}

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

}
});
}



Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package eu.jnksoftware.discountfinderandroid.ui.customer.userPreferences;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -21,12 +27,13 @@
import eu.jnksoftware.discountfinderandroid.models.discountPreferences.DiscountPreferencesResponse;
import eu.jnksoftware.discountfinderandroid.models.token.User;
import eu.jnksoftware.discountfinderandroid.services.IuserService;
import eu.jnksoftware.discountfinderandroid.ui.customer.adapters.RecyclerItemTouchHelper;
import eu.jnksoftware.discountfinderandroid.ui.customer.recyclers.RecyclerPreference;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class UserPreferenceList extends Fragment {
public class UserPreferenceList extends Fragment implements RecyclerItemTouchHelper.RecyclerItemTouchHelperListener{
private User user;
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
Expand All @@ -35,6 +42,7 @@ public class UserPreferenceList extends Fragment {
private List<DiscountPreferencesResponse> discountPreferencesResponses ;
private String auth;
private Button addPreference;
private ConstraintLayout layout;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Expand All @@ -44,17 +52,22 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

//layout=view.findViewById(R.id.userpreferencesConst);
user = ManageSharePrefs.readUser(null);
iuserService= ApiUtils.getUserService();
recyclerView= view.findViewById(R.id.userPreferenceRecycler);
layoutManager=new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
recyclerView.setHasFixedSize(true);
addPreference = view.findViewById(R.id.userPreferencesBtn);
addPreference.setOnClickListener(addPreferenceClick);

fetchUserPreferences("Bearer "+user.getAccessToken());

ItemTouchHelper.SimpleCallback itemTouchHelper= new RecyclerItemTouchHelper(0,ItemTouchHelper.LEFT,this);
new ItemTouchHelper(itemTouchHelper).attachToRecyclerView(recyclerView);
}

@Override
Expand Down Expand Up @@ -105,4 +118,49 @@ public void onFailure(Call<List<DiscountPreferencesResponse>> call, Throwable t)
}


@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction, int position) {
if (viewHolder instanceof RecyclerPreference.MyViewHolder){
String catTitle = discountPreferencesResponses.get(viewHolder.getAdapterPosition()).getCategoryTitle();
final DiscountPreferencesResponse deletedDiscount=discountPreferencesResponses.get(viewHolder.getAdapterPosition());
final int deletedIndex = viewHolder.getAdapterPosition();

int pos = viewHolder.getAdapterPosition();
deletePref(adapter.getDiscountId(pos));
adapter.removeDiscount(pos);
adapter.notifyItemRemoved(pos);

adapter.notifyDataSetChanged();
Snackbar snackbar = Snackbar
.make(layout, catTitle + " removed from your shop!", Snackbar.LENGTH_LONG);
snackbar.setAction("UNDO", new View.OnClickListener() {
@Override
public void onClick(View view) {
// undo is selected, restore the deleted item
adapter.restoreDiscount(deletedDiscount, deletedIndex);
adapter.notifyItemInserted(deletedIndex);
}
});
snackbar.setActionTextColor(Color.YELLOW);
snackbar.show();
}
}

public void deletePref( int id)
{ auth="Bearer "+user.getAccessToken();
iuserService= ApiUtils.getUserService();
Call<Void> delete=iuserService.deleteDiscountPreference(id,auth);
delete.enqueue(new Callback<Void>() {
@Override
public void onResponse(Call<Void> call, Response<Void> response) {


}

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

}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class UserPreferences extends AppCompatActivity {
private ArrayAdapter<String> spinContentAdapter;
private Spinner spinnerCat;
private String auth;
private EditText tags;

@SuppressLint("SetTextI18n")
@Override
Expand All @@ -53,6 +54,7 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_user_preferences);
iuserService= ApiUtils.getUserService();
user = ManageSharePrefs.readUser(null);
tags=findViewById(R.id.textTag);



Expand Down Expand Up @@ -101,7 +103,7 @@ public void onClick(View view) {
DiscountPreferencesRequest discountPreferencesRequest=new DiscountPreferencesRequest();
discountPreferencesRequest.setCategory(String.valueOf(categories.get((int) spinnerCat.getSelectedItemId()).getId()));
discountPreferencesRequest.setPrice(String.valueOf(seekBarProgress));
discountPreferencesRequest.setTags("Sample");
discountPreferencesRequest.setTags(tags.getText().toString());


doUserPreference(discountPreferencesRequest);
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/activity_user_preference_list.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/userpreferencesConst"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="eu.jnksoftware.discountfinderandroid.ui.customer.userPreferences.UserPreferenceList"
Expand Down
39 changes: 36 additions & 3 deletions app/src/main/res/layout/activity_user_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,17 @@
android:id="@+id/spinnerCategory"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="136dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="20dp"
android:layout_marginTop="88dp"
android:background="@android:drawable/btn_dropdown"
app:layout_constraintBottom_toTopOf="@+id/tagsText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvCategory" />
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="spread_inside" />

<TextView
android:id="@+id/tvPrice"
Expand Down Expand Up @@ -106,8 +109,38 @@
android:text="ΠΡΟΣΘΗΚΗ"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.607"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/seekBarPrice" />
app:layout_constraintTop_toBottomOf="@id/seekBarPrice"
app:layout_constraintVertical_bias="0.784" />

<TextView
android:id="@+id/tagsText"
android:layout_width="45dp"
android:layout_height="16dp"
android:layout_marginBottom="207dp"
android:text="Tags"
android:textAlignment="center"
android:textColor="@android:color/white"
android:textColorHint="@android:color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<EditText
android:id="@+id/textTag"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="25dp"
android:layout_marginStart="25dp"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toTopOf="@+id/btSavePreferences"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tagsText" />

</android.support.constraint.ConstraintLayout>

Expand Down
Loading

0 comments on commit 6d296d1

Please sign in to comment.