Last active
August 29, 2015 14:25
-
-
Save romtsn/b10af6b03aae51dbc75c to your computer and use it in GitHub Desktop.
Code example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.stylezappmen.android.fragments; | |
import android.app.Activity; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.support.v4.app.Fragment; | |
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.AdapterView; | |
import android.widget.ImageView; | |
import android.widget.RelativeLayout; | |
import android.widget.TextView; | |
import com.costum.android.widget.LoadMoreListView; | |
import com.stylezappmen.android.R; | |
import com.stylezappmen.android.activities.SingleProductActivity; | |
import com.stylezappmen.android.adapters.list.ProductsListAdapter; | |
import com.stylezappmen.android.enums.DialogType; | |
import com.stylezappmen.android.interfaces.DialogCallbacks; | |
import com.stylezappmen.android.interfaces.IRequestableDefaultImpl; | |
import com.stylezappmen.android.interfaces.ProductInteractionDefaultImpl; | |
import com.stylezappmen.android.interfaces.ProgressCallbacks; | |
import com.stylezappmen.android.models.Product; | |
import com.stylezappmen.android.network.API; | |
import com.stylezappmen.android.utils.ConstantUtils; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
/** | |
* A simple {@link Fragment} subclass. | |
*/ | |
public class FavoritesFragment extends Fragment { | |
private static final String MAX_PAGE_COUNT = "max_page_count"; | |
protected LoadMoreListView favoritesList; | |
protected RelativeLayout blankContainer; | |
protected TextView blankText; | |
protected ImageView blankImage; | |
protected ProductsListAdapter adapter; | |
protected DialogCallbacks dialogCallbacks; | |
protected ProgressCallbacks progressCallbacks; | |
int page = 1; | |
int maxPageCount; | |
public FavoritesFragment() { | |
// Required empty public constructor | |
} | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
adapter = new ProductsListAdapter(getActivity()); | |
adapter.setFavorite(true); | |
setHasOptionsMenu(true); | |
if (savedInstanceState != null) | |
maxPageCount = savedInstanceState.getInt(MAX_PAGE_COUNT); | |
} | |
@Override | |
public void onSaveInstanceState(Bundle outState) { | |
outState.putInt(MAX_PAGE_COUNT, maxPageCount); | |
super.onSaveInstanceState(outState); | |
} | |
@Override | |
public void onAttach(Activity activity) { | |
super.onAttach(activity); | |
dialogCallbacks = (DialogCallbacks) activity; | |
progressCallbacks = (ProgressCallbacks) activity; | |
} | |
@Override | |
public void onDetach() { | |
super.onDetach(); | |
progressCallbacks.hideLoader(); | |
dialogCallbacks = null; | |
progressCallbacks = null; | |
} | |
@Override | |
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { | |
super.onCreateOptionsMenu(menu, inflater); | |
inflater.inflate(R.menu.menu_favorites, menu); | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
switch (item.getItemId()) { | |
case R.id.action_email: | |
dialogCallbacks.showDialog(DialogType.NOTIFIED, ""); | |
break; | |
case R.id.action_dislike_all: | |
progressCallbacks.showLoader(); | |
API.dislikeAll(new IRequestableDefaultImpl(getActivity(), this) { | |
@Override | |
public void completed(Object... params) { | |
if (isAdded()) | |
getActivity().runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
progressCallbacks.hideLoader(); | |
adapter.swap(new ArrayList<>()); | |
renderProducts(); | |
} | |
}); | |
} | |
}); | |
break; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
View view = inflater.inflate(R.layout.fragment_favorites, container, false); | |
blankContainer = (RelativeLayout) view.findViewById(R.id.blank_container); | |
favoritesList = (LoadMoreListView) view.findViewById(R.id.products_list); | |
blankText = (TextView) view.findViewById(R.id.blank_text); | |
blankImage = (ImageView) view.findViewById(R.id.blank_image); | |
favoritesList.setAdapter(adapter); | |
favoritesList.setOnItemClickListener(new AdapterView.OnItemClickListener() { | |
@Override | |
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { | |
Product product = (Product) adapter.getItem(position); | |
startActivity(new Intent(getActivity(), SingleProductActivity.class).putExtra(SingleProductActivity.PRODUCT_EXTRA, product)); | |
} | |
}); | |
adapter.setOnProductInteractionListener(new ProductInteractionDefaultImpl() { | |
@Override | |
public void onRemove(final int position) { | |
Product product = (Product) adapter.getItem(position); | |
progressCallbacks.showLoader(); | |
API.dislike(product.id, new IRequestableDefaultImpl(getActivity(), FavoritesFragment.this) { | |
@Override | |
public void completed(Object... params) { | |
getActivity().runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
progressCallbacks.hideLoader(); | |
adapter.remove(position); | |
renderProducts(); | |
} | |
}); | |
} | |
}); | |
} | |
@Override | |
public void onShare(int position) { | |
Product product = (Product) adapter.getItem(position); | |
dialogCallbacks.showDialog(DialogType.SHARE, product.shareText); | |
} | |
}); | |
// ArrayList<Product> products = new ArrayList<>(); | |
// for (int i = 0; i < 10; i++) { | |
// Product product = new Product(UUID.randomUUID().toString(), R.drawable.ic_jacket, i + 188.90, i + 220.50, "Suede Bomber Blue Jacket for Men 2015", "ARMANI"); | |
// products.add(product); | |
// } | |
// adapter.swap(products); | |
loadFavorites(false); | |
favoritesList.setOnLoadMoreListener(new LoadMoreListView.OnLoadMoreListener() { | |
@Override | |
public void onLoadMore() { | |
if (page < maxPageCount) { | |
page++; | |
loadFavorites(true); | |
} | |
} | |
}); | |
return view; | |
} | |
private void loadFavorites(boolean isLoadMore) { | |
if (isAdded()) { | |
if (!isLoadMore) | |
progressCallbacks.showLoader(); | |
API.products("/my", page, new IRequestableDefaultImpl(getActivity(), this) { | |
@Override | |
public void completed(final Object... params) { | |
if (isAdded()) | |
getActivity().runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
progressCallbacks.hideLoader(); | |
ArrayList<Product> products = new ArrayList<>(Arrays.asList((Product[]) params[0])); | |
adapter.addAllProducts(products); | |
favoritesList.onLoadMoreComplete(); | |
renderProducts(); | |
maxPageCount = (Integer) params[2]; | |
favoritesList.setShouldLoadMore(page < maxPageCount); | |
} | |
}); | |
} | |
@Override | |
public void failed(Object... params) { | |
super.failed(params); | |
if (isAdded()) | |
getActivity().runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
favoritesList.onLoadMoreComplete(); | |
} | |
}); | |
} | |
}); | |
} | |
} | |
private void renderProducts() { | |
if (adapter.getCount() > 0) { | |
favoritesList.setVisibility(View.VISIBLE); | |
blankContainer.setVisibility(View.GONE); | |
setMenuVisibility(true); | |
} else { | |
setMenuVisibility(false); | |
favoritesList.setVisibility(View.GONE); | |
blankContainer.setVisibility(View.VISIBLE); | |
blankText.setText(getString(R.string.blank_favorites)); | |
blankImage.setImageResource(ConstantUtils.IS_MALE ? R.drawable.ic_not_liked_men : R.drawable.ic_not_liked); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.stylezappmen.android.adapters.list; | |
import android.app.Activity; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ImageView; | |
import android.widget.TextView; | |
import com.squareup.picasso.Picasso; | |
import com.stylezappmen.android.R; | |
import com.stylezappmen.android.interfaces.OnProductInteractionListener; | |
import com.stylezappmen.android.models.Product; | |
import com.stylezappmen.android.utils.ConstantUtils; | |
import java.util.Collection; | |
/** | |
* Created by qq on 27.03.2015. | |
*/ | |
public class ProductsListAdapter extends BaseListAdapter { | |
boolean isFavorite; | |
OnProductInteractionListener onProductInteractionListener; | |
public void setFavorite(boolean isFavorite) { | |
this.isFavorite = isFavorite; | |
} | |
public void setOnProductInteractionListener(OnProductInteractionListener onProductInteractionListener) { | |
this.onProductInteractionListener = onProductInteractionListener; | |
} | |
public ProductsListAdapter(Activity context) { | |
super(context); | |
} | |
public void addAllProducts(Collection<?> products) { | |
items.addAll(products); | |
notifyDataSetChanged(); | |
} | |
@Override | |
public View getView(final int position, View convertView, ViewGroup parent) { | |
if (convertView == null) { | |
LayoutInflater inflater = context.get().getLayoutInflater(); | |
convertView = inflater.inflate(R.layout.product_list_item, parent, false); | |
} | |
Product product = (Product) getItem(position); | |
ImageView productImage = (ImageView) convertView.findViewById(R.id.product_image); | |
TextView productName = (TextView) convertView.findViewById(R.id.product_name); | |
TextView productBrand = (TextView) convertView.findViewById(R.id.product_brand); | |
TextView productCost = (TextView) convertView.findViewById(R.id.product_cost); | |
ImageView remove = (ImageView) convertView.findViewById(R.id.remove_product); | |
ImageView share = (ImageView) convertView.findViewById(R.id.share_product); | |
ImageView like = (ImageView) convertView.findViewById(R.id.like_product); | |
if (isFavorite) { | |
like.setVisibility(View.GONE); | |
remove.setVisibility(View.VISIBLE); | |
remove.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
if (onProductInteractionListener != null) | |
onProductInteractionListener.onRemove(position); | |
} | |
}); | |
} else { | |
remove.setVisibility(View.GONE); | |
like.setVisibility(View.VISIBLE); | |
if (product.status.equals("liked")) | |
like.setImageResource(ConstantUtils.IS_MALE ? R.drawable.ic_like_men : R.drawable.ic_like); | |
else | |
like.setImageResource(ConstantUtils.IS_MALE ? R.drawable.ic_not_liked_men : R.drawable.ic_not_liked); | |
like.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
if (onProductInteractionListener != null) | |
onProductInteractionListener.onLike(position); | |
} | |
}); | |
} | |
share.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
if (onProductInteractionListener != null) | |
onProductInteractionListener.onShare(position); | |
} | |
}); | |
productName.setText(product.name); | |
productBrand.setText(product.brand); | |
productCost.setText("$" + product.cost); | |
Picasso.with(context.get()) | |
.load(product.image) | |
// .resizeDimen(R.dimen.image_size, R.dimen.image_size) | |
// .centerCrop() | |
.into(productImage); | |
return convertView; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment