Skip to content

Commit

Permalink
using sendreply api to rewrite the commenting module
Browse files Browse the repository at this point in the history
  • Loading branch information
kidozh committed Apr 19, 2020
1 parent a4f1594 commit 19c5e22
Show file tree
Hide file tree
Showing 12 changed files with 462 additions and 398 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ public void onChanged(String s) {
public void onChanged(String s) {
// reload chip if possible
configureThreadTypeChipGroup(s);
returned_res_json = s;
}
});
forumThreadViewModel.draftNumberLiveData.observe(this, new Observer<Integer>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -573,40 +573,6 @@ public boolean isScrollAtEnd(){
}
});

mRecyclerview.addOnItemTouchListener(new RecyclerItemClickListener(this,mRecyclerview,new RecyclerItemClickListener.OnItemClickListener(){

@Override
public void onItemClick(View view, int position) {


}

@Override
public void onItemLongClick(View view, int position) {
threadCommentInfo threadCommentInfo = adapter.getThreadInfoList().get(position);
selectedThreadComment = threadCommentInfo;
mThreadReplyBadge.setText(threadCommentInfo.author);
mThreadReplyBadge.setVisibility(View.VISIBLE);
mCommentEditText.setHint(String.format("@%s",threadCommentInfo.author));
String decodeString = threadCommentInfo.message;

Spanned sp = Html.fromHtml(decodeString);

mThreadReplyContent.setText(sp, TextView.BufferType.SPANNABLE);
mThreadReplyContent.setVisibility(View.VISIBLE);
mThreadReplyBadge.setOnCloseIconClickListener(new View.OnClickListener(){

@Override
public void onClick(View v) {
mThreadReplyBadge.setVisibility(View.GONE);
mThreadReplyContent.setVisibility(View.GONE);
mCommentEditText.setHint(R.string.bbs_thread_say_something);
selectedThreadComment = null;
}
});
}
}));

}

private void getThreadComment(){
Expand Down Expand Up @@ -784,7 +750,8 @@ public void onResponse(Call call, Response response) throws IOException {
if(response.body()!=null){
String s = response.body().string();
Log.d(TAG,"Recv comment info "+s);
if(s.contains("succeedhandle_fastpost")){
bbsParseUtils.returnMessage returnedMessage = bbsParseUtils.parseReturnMessage(s);
if(returnedMessage!=null && returnedMessage.value.equals("post_reply_succeed")){
// success!
mHandler.post(new Runnable() {
@Override
Expand All @@ -795,7 +762,7 @@ public void run() {
reloadThePage();
threadDetailViewModel.getThreadDetail(threadDetailViewModel.threadStatusMutableLiveData.getValue());
//getThreadComment();
Toasty.success(getApplicationContext(),getString(R.string.bbs_comment_successfully),Toast.LENGTH_LONG).show();
Toasty.success(getApplicationContext(),returnedMessage.string,Toast.LENGTH_LONG).show();
}
});
}
Expand All @@ -805,24 +772,12 @@ public void run() {
public void run() {
mCommentBtn.setText(R.string.bbs_thread_comment);
mCommentBtn.setEnabled(true);
Pattern pattern = Pattern.compile("<p>(.*?)</p>");
Matcher matcher = pattern.matcher(s);
if(matcher.find()){
String rawWithTagString = matcher.group();
String infoString = rawWithTagString.replace("<[^>]*>","");
if(infoString.length()>0){
Toasty.error(getApplicationContext(), infoString, Toast.LENGTH_LONG).show();
}
else {
Toasty.error(getApplicationContext(), getString(R.string.bbs_comment_failed), Toast.LENGTH_LONG).show();
}
if(returnedMessage == null){
Toasty.error(getApplicationContext(), getString(R.string.network_failed), Toast.LENGTH_LONG).show();
}
else {
Toasty.error(getApplicationContext(), getString(R.string.bbs_comment_failed), Toast.LENGTH_LONG).show();
Toasty.error(getApplicationContext(), returnedMessage.string, Toast.LENGTH_LONG).show();
}



}
});
}
Expand Down Expand Up @@ -887,7 +842,7 @@ private void postReplyToSomeoneInThread(String replyPid,String message,String no
//.add("subject", message)
.build();
Request request = new Request.Builder()
.url(bbsURLUtils.getReplyToSomeoneThreadUrl(fid,tid))
.url(bbsURLUtils.getReplyThreadUrl(fid,tid))
.post(formBody)
.build();

Expand All @@ -903,7 +858,7 @@ public void onFailure(Call call, IOException e) {
public void run() {
mCommentBtn.setText(R.string.bbs_thread_comment);
mCommentBtn.setEnabled(true);
Toasty.error(getApplicationContext(),getString(R.string.bbs_comment_failed),Toast.LENGTH_LONG).show();
Toasty.error(getApplicationContext(),getString(R.string.network_failed),Toast.LENGTH_LONG).show();
}
});

Expand All @@ -913,8 +868,10 @@ public void run() {
public void onResponse(Call call, Response response) throws IOException {
if (response.body() != null) {
String s = response.body().string();
bbsParseUtils.returnMessage returnedMessage = bbsParseUtils.parseReturnMessage(s);

Log.d(TAG, "Recv reply comment info " + s);
if (s.contains("succeedhandle_reply")) {
if(returnedMessage!=null && returnedMessage.value.equals("post_reply_succeed")) {
// success!
mHandler.post(new Runnable() {
@Override
Expand All @@ -925,7 +882,7 @@ public void run() {
reloadThePage();
threadDetailViewModel.getThreadDetail(threadDetailViewModel.threadStatusMutableLiveData.getValue());
//getThreadComment();
Toasty.success(getApplicationContext(), getString(R.string.bbs_comment_successfully), Toast.LENGTH_LONG).show();
Toasty.success(getApplicationContext(), returnedMessage.string, Toast.LENGTH_LONG).show();
}
});
} else {
Expand All @@ -934,21 +891,12 @@ public void run() {
public void run() {
mCommentBtn.setText(R.string.bbs_thread_comment);
mCommentBtn.setEnabled(true);
Pattern pattern = Pattern.compile("<p>(.*?)</p>");
Matcher matcher = pattern.matcher(s);
if (matcher.find()) {
String rawWithTagString = matcher.group();
String infoString = rawWithTagString.replace("<[^>]*>", "");
if (infoString.length() > 0) {
Toasty.error(getApplicationContext(), infoString, Toast.LENGTH_LONG).show();
} else {
Toasty.error(getApplicationContext(), getString(R.string.bbs_comment_failed), Toast.LENGTH_LONG).show();
}
} else {
Toasty.error(getApplicationContext(), getString(R.string.bbs_comment_failed), Toast.LENGTH_LONG).show();
if(returnedMessage == null){
Toasty.error(getApplicationContext(), getString(R.string.network_failed), Toast.LENGTH_LONG).show();
}
else {
Toasty.error(getApplicationContext(), returnedMessage.string, Toast.LENGTH_LONG).show();
}


}
});
}
Expand Down Expand Up @@ -1022,7 +970,6 @@ public boolean onOptionsItemSelected(MenuItem item) {
return true;
}
case R.id.bbs_forum_nav_dateline_sort:{

Context context = this;
threadStatus = threadDetailViewModel.threadStatusMutableLiveData.getValue();
Log.d(TAG,"You press sort btn "+threadStatus.datelineAscend);
Expand Down Expand Up @@ -1070,7 +1017,7 @@ public boolean onCreateOptionsMenu(Menu menu) {
// configureIntentData();

if(userBriefInfo == null){
getMenuInflater().inflate(R.menu.menu_bbs_user_status, menu);
getMenuInflater().inflate(R.menu.bbs_incognitive_thread_nav_menu, menu);

}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
Expand Down Expand Up @@ -57,6 +58,9 @@ public class HomeFragment extends Fragment {
TextView bbsPortalErrorText;
@BindView(R.id.bbs_portal_progressBar)
ProgressBar bbsPortalProgressbar;
@BindView(R.id.bbs_portal_refresh_page)
Button bbsPortalRefreshPageBtn;

bbsPortalCategoryAdapter adapter;
bbsInformation curBBS;
forumUserBriefInfo curUser;
Expand All @@ -75,7 +79,7 @@ public View onCreateView(@NonNull LayoutInflater inflater,
configurePortalRecyclerview();
bindLiveDataFromViewModel();
//getPortalCategoryInfo();

configureRefreshBtn();

return root;
}
Expand All @@ -99,7 +103,7 @@ private void getIntentInfo(){
}

private void bindLiveDataFromViewModel(){
bbsPortalProgressbar.setVisibility(View.VISIBLE);
//bbsPortalProgressbar.setVisibility(View.VISIBLE);
homeViewModel.getForumCategoryInfo().observe(getViewLifecycleOwner(), new Observer<List<forumCategorySection>>() {
@Override
public void onChanged(List<forumCategorySection> forumCategorySections) {
Expand All @@ -110,6 +114,7 @@ public void onChanged(List<forumCategorySection> forumCategorySections) {
});
homeViewModel.errorText.observe(getViewLifecycleOwner(), errorText -> {
if(errorText!=null){

if(errorText.equals("mobile_is_closed")){
bbsPortalErrorText.setText(R.string.bbs_mobile_is_closed);
}
Expand All @@ -119,9 +124,12 @@ else if(errorText.equals("user_banned")){
else {
bbsPortalErrorText.setText(errorText);
}
bbsPortalErrorText.setVisibility(View.VISIBLE);
bbsPortalRefreshPageBtn.setVisibility(View.VISIBLE);
}
else {
Toasty.error(getActivity(),getString(R.string.parse_failed), Toast.LENGTH_SHORT).show();
bbsPortalErrorText.setVisibility(View.GONE);
bbsPortalRefreshPageBtn.setVisibility(View.GONE);
}
});
homeViewModel.userBriefInfoMutableLiveData.observe(getViewLifecycleOwner(), new Observer<forumUserBriefInfo>() {
Expand Down Expand Up @@ -153,10 +161,18 @@ public void onClick(DialogInterface dialog, int which) {
}
}
});
}

private void showReloginDialog(){
homeViewModel.isLoading.observe(this, new Observer<Boolean>() {
@Override
public void onChanged(Boolean aBoolean) {
if(aBoolean){
bbsPortalProgressbar.setVisibility(View.VISIBLE);
}
else {
bbsPortalProgressbar.setVisibility(View.GONE);
}

}
});
}


Expand All @@ -166,4 +182,13 @@ private void configurePortalRecyclerview(){
adapter = new bbsPortalCategoryAdapter(getContext(),null,curBBS,userBriefInfo);
portalRecyclerView.setAdapter(adapter);
}

private void configureRefreshBtn(){
bbsPortalRefreshPageBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
homeViewModel.loadForumCategoryInfo();
}
});
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.kidozh.discuzhub.activities.ui.home;

import android.app.Application;
import android.content.Context;
import android.util.Log;

import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.kidozh.discuzhub.R;
import com.kidozh.discuzhub.entities.bbsInformation;
import com.kidozh.discuzhub.entities.forumCategorySection;
import com.kidozh.discuzhub.entities.forumUserBriefInfo;
Expand All @@ -31,6 +33,7 @@ public class HomeViewModel extends AndroidViewModel {
private MutableLiveData<List<forumCategorySection>> forumCategories;
public MutableLiveData<String> errorText, jsonString;
public MutableLiveData<forumUserBriefInfo> userBriefInfoMutableLiveData;
public MutableLiveData<Boolean> isLoading;

bbsInformation curBBS;
forumUserBriefInfo curUser;
Expand All @@ -42,13 +45,15 @@ public HomeViewModel(Application application) {
mText.postValue("This is home fragment");
errorText = new MutableLiveData<String>();
jsonString = new MutableLiveData<String>();
isLoading = new MutableLiveData<>(false);

}

public void setBBSInfo(bbsInformation curBBS, forumUserBriefInfo curUser){
this.curBBS = curBBS;
this.curUser = curUser;
userBriefInfoMutableLiveData = new MutableLiveData<>(curUser);

}

public LiveData<List<forumCategorySection>> getForumCategoryInfo(){
Expand All @@ -59,7 +64,7 @@ public LiveData<List<forumCategorySection>> getForumCategoryInfo(){
return forumCategories;
}

private void loadForumCategoryInfo(){
public void loadForumCategoryInfo(){
if( curBBS == null){
return;
}
Expand All @@ -69,22 +74,38 @@ private void loadForumCategoryInfo(){
.url(bbsURLUtils.getBBSForumInfoApi())
.build();
Log.d(TAG,"Send request to "+bbsURLUtils.getBBSForumInfoApi());
Context context = getApplication();
errorText.setValue(null);
forumCategories.setValue(null);
isLoading.setValue(true);
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
forumCategories.postValue(null);
isLoading.postValue(false);
}

@Override
public void onResponse(Call call, Response response) throws IOException {
isLoading.postValue(false);
if(response.isSuccessful() && response.body()!=null){
String s = response.body().string();
Log.d(TAG,"Recv Portal JSON "+s);
jsonString.postValue(s);
List<forumCategorySection> categorySectionFidList = bbsParseUtils.parseCategoryFids(s);
forumCategories.postValue(categorySectionFidList);
if(categorySectionFidList == null){
errorText.postValue(bbsParseUtils.parseErrorInformation(s));
String errorString = bbsParseUtils.parseErrorInformation(s);
if(errorString!=null){
errorText.postValue(errorString);
}
else {
errorText.postValue(context.getString(R.string.parse_failed));
}
// errorText.postValue(bbsParseUtils.parseErrorInformation(s));
}
else {
errorText.postValue(null);
}
// parse person info
forumUserBriefInfo severReturnedUser = bbsParseUtils.parseBreifUserInfo(s);
Expand All @@ -94,7 +115,8 @@ public void onResponse(Call call, Response response) throws IOException {
}
else {
String s = response.body().string();
errorText.postValue(bbsParseUtils.parseErrorInformation(s));
errorText.postValue(context.getString(R.string.parse_failed));
// errorText.postValue(bbsParseUtils.parseErrorInformation(s));
forumCategories.postValue(null);
}
}
Expand Down
Loading

0 comments on commit 19c5e22

Please sign in to comment.