Skip to content

Commit

Permalink
1. Add some module
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Aug 21, 2016
1 parent 125174f commit 62f9a95
Show file tree
Hide file tree
Showing 15 changed files with 334 additions and 31 deletions.
115 changes: 115 additions & 0 deletions app/src/main/java/ninhn/app/girlxinh/adapter/PhotoReviewAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package ninhn.app.girlxinh.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

import ninhn.app.girlxinh.R;
import ninhn.app.girlxinh.holder.AdmobViewHolder;
import ninhn.app.girlxinh.holder.LoadingViewHolder;
import ninhn.app.girlxinh.holder.PhotoHomeHolder;
import ninhn.app.girlxinh.listener.OnItemClickListener;
import ninhn.app.girlxinh.listener.OnLoadMoreListener;
import ninhn.app.girlxinh.model.PhotoModel;
import ninhn.app.girlxinh.model.PhotoReviewModel;

/**
* Created by ninhn on 4/8/2016.
*/
public class PhotoReviewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final int VIEW_TYPE_ITEM = 0;
private final int VIEW_TYPE_LOADING = 1;
private final int VIEW_TYPE_ADMOB = 2;

private Context context;
private List<PhotoReviewModel> photoModelList;

private OnLoadMoreListener mOnLoadMoreListener;
private OnItemClickListener listener;

private boolean isLoading;
private int visibleThreshold = 5;
private int lastVisibleItem, totalItemCount;

public PhotoReviewAdapter(Context context, RecyclerView recyclerView, List<PhotoReviewModel> photoModelList, OnItemClickListener listener) {
this.context = context;
this.photoModelList = photoModelList;
this.listener = listener;
final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);

totalItemCount = linearLayoutManager.getItemCount();
lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();

if (!isLoading && totalItemCount <= (lastVisibleItem + visibleThreshold)) {
if (mOnLoadMoreListener != null) {
mOnLoadMoreListener.onLoadMore();
}
isLoading = true;
}
}
});
}

public void setLoaded() {
isLoading = false;
}

@Override
public int getItemCount() {
return photoModelList == null ? 0 : photoModelList.size();
}

@Override
public int getItemViewType(int position) {
if (photoModelList.get(position) != null) {
if (!context.getString(R.string.banner_ad_unit_id).equals(photoModelList.get(position).getId())) {
return VIEW_TYPE_ITEM;
} else {
return VIEW_TYPE_ADMOB;
}
} else {
return VIEW_TYPE_LOADING;
}
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
if (viewType == VIEW_TYPE_ITEM) {
view = LayoutInflater.from(this.context).inflate(R.layout.photo_item_view, parent, false);
return new PhotoHomeHolder(view);
} else if (viewType == VIEW_TYPE_LOADING) {
view = LayoutInflater.from(this.context).inflate(R.layout.loading_item, parent, false);
return new LoadingViewHolder(view);
} else if (viewType == VIEW_TYPE_ADMOB) {
view = LayoutInflater.from(this.context).inflate(R.layout.admob_item, parent, false);
return new AdmobViewHolder(view);
}
return null;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof PhotoHomeHolder) {
((PhotoHomeHolder) holder).bind(this.context, photoModelList.get(position), this.listener);
} else if (holder instanceof LoadingViewHolder) {
LoadingViewHolder loadingViewHolder = (LoadingViewHolder) holder;
loadingViewHolder.progressBar.setIndeterminate(true);
} else if (holder instanceof AdmobViewHolder) {
//Dont handle
}
}

public void setOnLoadMoreListener(OnLoadMoreListener mOnLoadMoreListener) {
this.mOnLoadMoreListener = mOnLoadMoreListener;
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package ninhn.app.girlxinh.constant;

import java.util.ArrayList;

/**
* Created by NinHN on 5/22/16.
*/
public class AppConstant {

public static final String BLANK = "";

public static final String _JPG = ".jpg";

public static final ArrayList ARRAY_EMPTY = new ArrayList<>();


public static final int FLAG_PAGE_MORE = 2;
public static final int FLAG_PAGE_ONE = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ public class UrlConstant {

public static final String PHOTO_USER_UPLOAD = SERVER + "/user-photo-upload";

public static final String PHOTO_PUBLISH_USER = "/photo-publish-user";
public static final String PHOTO_REVIEW_USER = "/photo-review-user";

public static final String CONDITION_START = "?";
public static final String CONDITION_AND = "&";
public static final String CONDITION_PHOTO_ID = "photoId=";
public static final String CONDITION_USER_ID = "userId=";
public static final String CONDITION_PAGE = "page=";

public static final String PHOTO_INFO = "photo-info";
public static final String PHOTO_FILE = "photo-file";
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/java/ninhn/app/girlxinh/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class Model {
private String id;
private Date lastUpTime;
private Date createTime;
private boolean deleted = false;

public String getId() {
return id;
Expand All @@ -35,4 +36,12 @@ public Date getCreateTime() {
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}

public boolean isDeleted() {
return deleted;
}

public void setDeleted(boolean deleted) {
this.deleted = deleted;
}
}
16 changes: 16 additions & 0 deletions app/src/main/java/ninhn/app/girlxinh/model/PhotoReviewModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ninhn.app.girlxinh.model;

/**
* Created by NinHN on 8/21/16.
*/
public class PhotoReviewModel extends PhotoModel {
private String message;

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
/**
* Created by NinHN on 5/23/16.
*/
public class PhotoLoveService extends AsyncTask<String, Void, Boolean> {
public class PhotoLoveUserService extends AsyncTask<String, Void, Boolean> {

public static final String LOVE_UP = "1";
public static final String LOVE_DOWN = "0";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
/**
* Created by NinHN on 5/23/16.
*/
public class PhotoGetService extends AsyncTask<Integer, Void, List<PhotoModel>> {
public class PhotoPublishGlobalService extends AsyncTask<Integer, Void, List<PhotoModel>> {

private List<TaskListener> myListeners = new ArrayList<TaskListener>();

Expand All @@ -32,7 +32,7 @@ public void addListener(TaskListener tl) {

private int type;

public PhotoGetService(int type) {
public PhotoPublishGlobalService(int type) {
this.type = type;
}

Expand All @@ -43,7 +43,7 @@ protected void onPostExecute(List<PhotoModel> photoModels) {
if (photoModels != null) {
tl.onResultAvailable(AppConstant.FLAG_PHOTO_LOAD, this.type, photoModels);
} else {
tl.onResultAvailable(AppConstant.FLAG_PHOTO_LOAD, this.type, new ArrayList<>());
tl.onResultAvailable(AppConstant.FLAG_PHOTO_LOAD, this.type, AppConstant.ARRAY_EMPTY);
}
}
}
Expand All @@ -64,6 +64,6 @@ private List<PhotoModel> callService(int page) {
} catch (Exception e) {
Log.d(getClass().toString(), e.getMessage(), e);
}
return new ArrayList<>();
return AppConstant.ARRAY_EMPTY;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package ninhn.app.girlxinh.service;

import android.os.AsyncTask;
import android.util.Log;

import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import ninhn.app.girlxinh.constant.AppConstant;
import ninhn.app.girlxinh.constant.UrlConstant;
import ninhn.app.girlxinh.helper.AppValue;
import ninhn.app.girlxinh.listener.TaskListener;
import ninhn.app.girlxinh.model.PhotoModel;

/**
* Created by NinHN on 08/21/16.
*/
public class PhotoPublishUserService extends AsyncTask<Integer, Void, List<PhotoModel>> {

private List<TaskListener> myListeners = new ArrayList<TaskListener>();

public void addListener(TaskListener tl) {
myListeners.add(tl);
}

private int type;

public PhotoPublishUserService(int type) {
this.type = type;
}

@Override
protected void onPostExecute(List<PhotoModel> photoModels) {
super.onPostExecute(photoModels);
for (TaskListener tl : myListeners) {
if (photoModels != null) {
tl.onResultAvailable(AppConstant.FLAG_PHOTO_LOAD, this.type, photoModels);
} else {
tl.onResultAvailable(AppConstant.FLAG_PHOTO_LOAD, this.type, AppConstant.ARRAY_EMPTY);
}
}
}

@Override
protected List<PhotoModel> doInBackground(Integer... params) {
return callService(AppValue.getInstance().getUserModel().getId(), params[0]);
}

private List<PhotoModel> callService(String userId, int page) {
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
try {
ResponseEntity<PhotoModel[]> responseEntity = restTemplate.getForEntity(UrlConstant.PHOTO_PUBLISH_USER +
UrlConstant.CONDITION_START + UrlConstant.CONDITION_USER_ID + userId +
UrlConstant.CONDITION_AND + UrlConstant.CONDITION_PAGE + page, PhotoModel[].class);
PhotoModel[] photoArray = responseEntity.getBody();
List<PhotoModel> photolist = Arrays.asList(photoArray);
return photolist;
} catch (Exception e) {
Log.d(getClass().toString(), e.getMessage(), e);
}
return AppConstant.ARRAY_EMPTY;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package ninhn.app.girlxinh.service;

import android.os.AsyncTask;
import android.util.Log;

import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import ninhn.app.girlxinh.constant.AppConstant;
import ninhn.app.girlxinh.constant.UrlConstant;
import ninhn.app.girlxinh.helper.AppValue;
import ninhn.app.girlxinh.listener.TaskListener;
import ninhn.app.girlxinh.model.PhotoModel;

/**
* Created by NinHN on 08/21/16.
*/
public class PhotoReviewUserService extends AsyncTask<Integer, Void, List<PhotoModel>> {

private List<TaskListener> myListeners = new ArrayList<TaskListener>();

public void addListener(TaskListener tl) {
myListeners.add(tl);
}

private int type;

public PhotoReviewUserService(int type) {
this.type = type;
}

@Override
protected void onPostExecute(List<PhotoModel> photoModels) {
super.onPostExecute(photoModels);
for (TaskListener tl : myListeners) {
if (photoModels != null) {
tl.onResultAvailable(AppConstant.FLAG_PHOTO_LOAD, this.type, photoModels);
} else {
tl.onResultAvailable(AppConstant.FLAG_PHOTO_LOAD, this.type, AppConstant.ARRAY_EMPTY);
}
}
}

@Override
protected List<PhotoModel> doInBackground(Integer... params) {
return callService(AppValue.getInstance().getUserModel().getId(), params[0]);
}

private List<PhotoModel> callService(String userId, int page) {
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
try {
ResponseEntity<PhotoModel[]> responseEntity = restTemplate.getForEntity(UrlConstant.PHOTO_REVIEW_USER +
UrlConstant.CONDITION_START + UrlConstant.CONDITION_USER_ID + userId +
UrlConstant.CONDITION_AND + UrlConstant.CONDITION_PAGE + page, PhotoModel[].class);
PhotoModel[] photoArray = responseEntity.getBody();
List<PhotoModel> photolist = Arrays.asList(photoArray);
return photolist;
} catch (Exception e) {
Log.d(getClass().toString(), e.getMessage(), e);
}
return AppConstant.ARRAY_EMPTY;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ private List<PhotoModel> callService(String userId) {
List<PhotoModel> photolist = Arrays.asList(photoArray);
return photolist;
}
return new ArrayList<>();
return AppConstant.ARRAY_EMPTY;
} catch (Exception e) {
Log.d(getClass().toString(), e.getMessage(), e);
}
return new ArrayList<>();
return AppConstant.ARRAY_EMPTY;
}
}
Loading

0 comments on commit 62f9a95

Please sign in to comment.