Skip to content

Commit

Permalink
短消息详情改为compose实现
Browse files Browse the repository at this point in the history
  • Loading branch information
Justwen committed Feb 7, 2025
1 parent 58540aa commit a51b448
Show file tree
Hide file tree
Showing 12 changed files with 433 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class ARouterConstants {
const val ACTIVITY_MESSAGE_LIST_NAME =
"com.justwen.androidnga.module.message.compose.MessageListActivity"
const val ACTIVITY_MESSAGE_POST = "/activity/post"
const val ACTIVITY_MESSAGE_DETAIL = "/activity/detail"
const val ACTIVITY_MESSAGE_DETAIL = "/message/detail"

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.justwen.androidnga.core.data;

import android.util.Pair;

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

import gov.anzong.androidnga.common.base.JavaBean;

public class MessageArticlePageInfo implements JavaBean {
Expand All @@ -16,6 +21,16 @@ public class MessageArticlePageInfo implements JavaBean {
private String signature;
private String formated_html_data;

private List<Pair<String, Boolean>> contentSections = new ArrayList<>();

public List<Pair<String, Boolean>> getContentSections() {
return contentSections;
}

public void setContentSections(List<Pair<String, Boolean>> contentSections) {
this.contentSections = contentSections;
}

public int getLou() {
return lou;
}
Expand All @@ -25,7 +40,7 @@ public void setLou(int lou) {
}

public String getTime() {
return time;
return time == null ? "22 13" : time;
}

public void setTime(String time) {
Expand All @@ -41,15 +56,15 @@ public void setFrom(String from) {
}

public String getContent() {
return content;
return content == null ? "content" : content;
}

public void setContent(String content) {
this.content = content;
}

public String getSubject() {
return subject;
return subject == null ? "subject" : subject;
}

public void setSubject(String subject) {
Expand Down
8 changes: 6 additions & 2 deletions lib_module_message/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
<activity
android:name="com.justwen.androidnga.module.message.compose.MessageListActivity"
android:exported="false"
android:label="@string/label_activity_message_list"
android:theme="@style/AppThemeDayNightGreen" />
android:label="@string/label_activity_message_list" />

<activity
android:name="com.justwen.androidnga.module.message.compose.detail.MessageDetailActivity"
android:exported="false"
android:label="@string/label_activity_message_detail" />

</application>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.justwen.androidnga.module.message;

import android.util.Pair;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.justwen.androidnga.core.data.MessageArticlePageInfo;
Expand Down Expand Up @@ -106,21 +108,164 @@ public MessageListInfo getMessageListInfo(String js) {
return ret;
}

public MessageDetailInfo getMessageDetailInfo(String js, int page) {

if (js == null) {
mErrorMsg = ContextUtils.getString(R.string.network_error);
return null;
}
js = js.replaceAll("window.script_muti_get_var_store=", "");
if (js.indexOf("/*error fill content") > 0)
js = js.substring(0, js.indexOf("/*error fill content"));
js = js.replaceAll("\"content\":\\+(\\d+),", "\"content\":\"+$1\",");
js = js.replaceAll("\"subject\":\\+(\\d+),", "\"subject\":\"+$1\",");
js = js.replaceAll("/\\*\\$js\\$\\*/", "");
js = js.replaceAll("\\[img\\]./mon_", "[img]http://img6.nga.178.com/attachments/mon_");

JSONObject o = null;
try {
o = (JSONObject) JSON.parseObject(js).get("data");
} catch (Exception e) {
NLog.e(TAG, "can not parse :\n" + js);
}
if (o == null) {

try {
o = (JSONObject) JSON.parseObject(js).get("error");
} catch (Exception e) {
NLog.e(TAG, "can not parse :\n" + js);
}
if (o == null) {
mErrorMsg = "请重新登录";
} else {
mErrorMsg = o.getString("0");
if (StringUtils.isEmpty(mErrorMsg))
mErrorMsg = "请重新登录";
}
return null;
}
MessageDetailInfo ret = parseJsonThreadPage(js, page);
return ret;
}

public String getErrorMsg() {
return mErrorMsg;
}

/**
* 解析页面内容
*
* @param js
* @param page
* @return
*/
public MessageDetailInfo parseJsonThreadPage(String js, int page) {
js = js.replaceAll("\"content\":\\+(\\d+),", "\"content\":\"+$1\",");
js = js.replaceAll("\"subject\":\\+(\\d+),", "\"subject\":\"+$1\",");

js = js.replaceAll("\"content\":(0\\d+),", "\"content\":\"$1\",");
js = js.replaceAll("\"subject\":(0\\d+),", "\"subject\":\"$1\",");
js = js.replaceAll("\"author\":(0\\d+),", "\"author\":\"$1\",");
final String start = "\"__P\":{\"aid\":";
final String end = "\"this_visit_rows\":";
if (js.indexOf(start) != -1 && js.indexOf(end) != -1) {
NLog.w(TAG, "here comes an invalid response");
String validJs = js.substring(0, js.indexOf(start));
validJs += js.substring(js.indexOf(end));
js = validJs;

}
JSONObject o = null;
try {
o = (JSONObject) JSON.parseObject(js).get("data");
} catch (Exception e) {
NLog.e(TAG, "can not parse :\n" + js);
}
if (o == null)
return null;

MessageDetailInfo data = new MessageDetailInfo();

JSONObject o1;
o1 = (JSONObject) o.get("0");
if (o1 == null)
return null;

JSONObject userInfoMap = (JSONObject) o1.get("userInfo");

List<MessageArticlePageInfo> messageEntryList = convertJSobjToList(o1, userInfoMap, page);
if (messageEntryList == null)
return null;
data.setMessageEntryList(messageEntryList);
data.set__currentPage(o1.getIntValue("currentPage"));
data.set__nextPage(o1.getIntValue("nextPage"));
String alluser = o1.getString("allUsers"), allusertmp = "";
alluser = alluser.replaceAll(" ", " ");
String alluserarray[] = alluser.split(" ");
for (int i = 1; i < alluserarray.length; i += 2) {
allusertmp += alluserarray[i] + ",";
}
if (allusertmp.length() > 0)
allusertmp = allusertmp.substring(0, allusertmp.length() - 1);
data.set_Alluser(allusertmp);
if (data.getMessageEntryList().get(0) != null) {
String title = data.getMessageEntryList().get(0).getSubject();
if (!StringUtils.isEmpty(title)) {
data.set_Title(title);
} else {
data.set_Title("");
}
}
return data;

}

private List<MessageArticlePageInfo> convertJSobjToList(JSONObject rowMap, JSONObject userInfoMap, int page) {
List<MessageArticlePageInfo> __R = new ArrayList<MessageArticlePageInfo>();
if (rowMap == null)
return null;
rowMap = (JSONObject) rowMap.get("allmsgs");
JSONObject rowObj = (JSONObject) rowMap.get("0");
for (int i = 1; rowObj != null; i++) {
MessageArticlePageInfo row = new MessageArticlePageInfo();

row.setContent(rowObj.getString("content"));
row.setLou(20 * (page - 1) + i);
row.setSubject(rowObj.getString("subject"));
int time = rowObj.getIntValue("time");
if (time > 0) {
row.setTime(StringUtils.timeStamp2Date1(String.valueOf(time)));
} else {
row.setTime("");
}
row.setFrom(rowObj.getString("from"));
fillUserInfo(row, userInfoMap);
formatContent(row);
__R.add(row);
rowObj = (JSONObject) rowMap.get(String.valueOf(i));
}
return __R;
}

private static void formatContent(MessageArticlePageInfo row) {

String content = row.getContent();
content = StringUtils.replaceAll(content, "\\[quote\\](.+?)\\[/quote\\]", "\n$1\n").replaceAll("<br/>", "\n");
row.setContent(content);

public static int showImageQuality() {
return 0;
// if (isInWifi()) {
// return 0;
// } else {
// return PhoneConfiguration.getInstance().imageQuality;
// }
List<Pair<String, Boolean>> contentSections = row.getContentSections();
for (String section : row.getContent().split("\\[url]")) {
if (section.contains("[/url]")) {
String[] urlSection = section.split("\\[/url]");
contentSections.add(new Pair<>(urlSection[0], Boolean.TRUE));
if (urlSection.length > 1) {
contentSections.add(new Pair<>(urlSection[1], Boolean.FALSE));
}
} else {
contentSections.add(new Pair<>(section, Boolean.FALSE));
}
}
row.setContentSections(contentSections);
}

private void fillUserInfo(MessageArticlePageInfo row, JSONObject userInfoMap) {
Expand All @@ -136,13 +281,4 @@ private void fillUserInfo(MessageArticlePageInfo row, JSONObject userInfoMap) {
row.setSignature(userInfo.getString("signature"));
}

private static String buildHeader(MessageArticlePageInfo row, String fgColorStr) {
if (row == null || StringUtils.isEmpty(row.getSubject()))
return "";
StringBuilder sb = new StringBuilder();
sb.append("<h4 style='color:").append(fgColorStr).append("' >")
.append(row.getSubject()).append("</h4>");
return sb.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class MessagePagingSource : PagingSource<Int, MessageThreadPageInfo>() {
val factory = MessageConvertFactory()
var nextKey: Int? = null
val result = factory.getMessageListInfo(jsonString)?.let {
nextKey = if (it.__nextPage > 0) it.__nextPage else null
nextKey = if (it.__nextPage > 0) page + 1 else null
it.messageEntryList ?: emptyList()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.justwen.androidnga.core.data.MessageThreadPageInfo
import kotlinx.coroutines.flow.Flow

class MessageViewModel : ViewModel() {

fun getMessageListData(): Flow<PagingData<com.justwen.androidnga.core.data.MessageThreadPageInfo>> {
fun getMessageListData(): Flow<PagingData<MessageThreadPageInfo>> {
return MessageRepository.getMessageListData().cachedIn(viewModelScope)
}
}
Loading

0 comments on commit a51b448

Please sign in to comment.