-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
125174f
commit 62f9a95
Showing
15 changed files
with
334 additions
and
31 deletions.
There are no files selected for viewing
115 changes: 115 additions & 0 deletions
115
app/src/main/java/ninhn/app/girlxinh/adapter/PhotoReviewAdapter.java
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
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; | ||
} | ||
} |
5 changes: 4 additions & 1 deletion
5
app/src/main/java/ninhn/app/girlxinh/constant/AppConstant.java
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
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
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
16 changes: 16 additions & 0 deletions
16
app/src/main/java/ninhn/app/girlxinh/model/PhotoReviewModel.java
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
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; | ||
} | ||
} |
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
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
69 changes: 69 additions & 0 deletions
69
app/src/main/java/ninhn/app/girlxinh/service/PhotoPublishUserService.java
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
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; | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
app/src/main/java/ninhn/app/girlxinh/service/PhotoReviewUserService.java
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
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; | ||
} | ||
} |
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
Oops, something went wrong.