Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package com.feresr.restapp.fragments;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.feresr.restapp.AppHelper;
import com.feresr.restapp.R;
import com.feresr.restapp.RoundedImageView;
import com.feresr.restapp.activities.IntroActivity;
import com.feresr.restapp.adapters.OrdersAdapter;
import com.feresr.restapp.events.OrderEvent;
import com.feresr.restapp.models.Order;
import com.feresr.restapp.models.Waiter;
import com.squareup.otto.Subscribe;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
/**
* Created by feresr on 4/2/15.
*/
public class OrderListFragment extends ListFragment<Order> implements OrdersAdapter.OnOrderSelectedListener {
private OrderInteractionListener listener;
@Override
protected RecyclerView.Adapter getAdapter() {
OrdersAdapter adapter = new OrdersAdapter(items, getActivity());
adapter.setOnItemClickListener(this);
return adapter;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
listener = (OrderInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.getClass().getSimpleName() + " must implement OrderInteractionListener: " + e.toString());
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
getActivity().setTitle("RestApp");
if (AppHelper.getInstance().waiter != null) {
Waiter waiter = AppHelper.getInstance().waiter;
String firstName = waiter.getFirstname();
String secondName = waiter.getLastname();
String username = waiter.getUsername();
TextView waiterName = ((TextView) view.findViewById(R.id.waiter_name));
Typeface font = Typeface.createFromAsset(getActivity().getAssets(),
"fonts/LeagueGothicRegular.otf");
waiterName.setTypeface(font);
waiterName.setText(firstName + " " + secondName);
((TextView) view.findViewById(R.id.waiter_username)).setText("#" + username);
CollapsingToolbarLayout collapsingToolbar =
(CollapsingToolbarLayout) view.findViewById(R.id.collapsing_toolbar);
collapsingToolbar.setTitle(getActivity().getString(R.string.header_orders));
//collapsingToolbar.setExpandedTitleColor(getActivity().getResources().getColor(R.color.dark_primary));
collapsingToolbar.setExpandedTitleTextAppearance(R.style.myorders_header);
if (waiter.getImageurl() != null && !waiter.getImageurl().isEmpty()) {
Picasso.with(getActivity()).load(waiter.getImageurl()).into(((RoundedImageView) view.findViewById(R.id.waiter_picture)));
}
}
return view;
}
@Override
public void setEmptyViewText(TextView emptyView) {
emptyView.setText(getActivity().getString(R.string.no_orders));
}
@Override
protected View setRecyclerViewView(LayoutInflater inflater, ViewGroup container) {
return inflater.inflate(R.layout.fragment_order_list, container, false);
}
@Override
protected ArrayList<Order> getItems() {
return AppHelper.getInstance().ordersController.getItems();
}
@Override
public void onOrderSelected(View v, Order order) {
listener.onOrderSelected(order);
}
@Subscribe
public void onOrderEvent(OrderEvent event) {
Order order = event.getOrder();
if (order == null) {
return;
}
switch (event.getReason()) {
case "added":
if (!items.contains(order)) {
items.add(order);
mAdapter.notifyItemInserted(items.indexOf(order));
}
break;
case "removed":
if (items.contains(order)) {
int index = items.indexOf(order);
items.remove(order);
mAdapter.notifyItemRemoved(index);
listener.onOrderRemoved(order);
}
break;
case "modified":
if (items.contains(order)) {
mAdapter.notifyItemChanged(items.indexOf(order));
return;
}
break;
}
notifyItemsChanged();
}
public interface OrderInteractionListener {
void onOrderSelected(Order order);
void onOrderRemoved(Order order);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.order_list_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.select_waiter_menu:
new ConfirmationDialogFragment().show(getFragmentManager(), "SUP");
break;
}
return super.onOptionsItemSelected(item);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment