feat: V0.1.53 location timeline and durable device conversations
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
package cn.ucvl.zhaodisplay;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.view.*;
|
||||
import android.widget.*;
|
||||
import org.json.*;
|
||||
import java.util.*;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/** A bounded page of a permanent conversation; older pages are fetched on demand. */
|
||||
final class VoiceChat extends LinearLayout {
|
||||
final PhoneStyle s;final LinearLayout messages;final TextView notice,heading;final Button older,latest;final ScrollView scroll;
|
||||
boolean night=false,savedMore=false,savedPast=false;JSONArray saved=new JSONArray();
|
||||
String signature="";Consumer<String> replay=id->{};
|
||||
VoiceChat(Activity app){
|
||||
super(app);s=new PhoneStyle(app);setOrientation(VERTICAL);s.card(this);
|
||||
LinearLayout head=s.line();heading=s.bold("聊天记录",18);head.addView(heading,new LayoutParams(0,-2,1));older=s.button("更早",false);latest=s.button("最近",false);head.addView(older);head.addView(latest);addView(head);
|
||||
notice=s.label("选择沟通对象后查看记录",12,PhoneStyle.MUTED);addView(notice);
|
||||
scroll=new ScrollView(app);scroll.setFillViewport(true);messages=s.column();scroll.addView(messages);addView(scroll,new LayoutParams(-1,s.dp(218)));
|
||||
scroll.setOnTouchListener((v,e)->{if(e.getActionMasked()==MotionEvent.ACTION_DOWN)v.getParent().requestDisallowInterceptTouchEvent(true);if(e.getActionMasked()==MotionEvent.ACTION_UP||e.getActionMasked()==MotionEvent.ACTION_CANCEL)v.getParent().requestDisallowInterceptTouchEvent(false);return false;});
|
||||
older.setEnabled(false);
|
||||
}
|
||||
void clear(String text){signature="";saved=new JSONArray();messages.removeAllViews();notice.setText(text);older.setEnabled(false);}
|
||||
void theme(boolean value){if(night==value)return;night=value;setBackground(s.shape(night?0xff21372d:PhoneStyle.CARD,20));heading.setTextColor(night?0xffc8d5cd:PhoneStyle.INK);notice.setTextColor(night?0xff96aaa0:PhoneStyle.MUTED);for(Button b:new Button[]{older,latest}){b.setTextColor(night?0xffc8d5cd:PhoneStyle.GREEN);b.setBackground(s.shape(night?0xff2d4439:PhoneStyle.PALE,14));}if(saved.length()>0){signature="";show(saved,savedMore,savedPast,replay);}}
|
||||
void show(JSONArray items,boolean hasMore,boolean past,Consumer<String> play){
|
||||
replay=play;saved=items;savedMore=hasMore;savedPast=past;older.setEnabled(hasMore);notice.setText(past?"较早的记录 · 可继续向前翻阅":"长期保留 · 点击语音回听");
|
||||
String next=items.toString();if(next.equals(signature))return;boolean follow=signature.isEmpty()||scroll.getScrollY()+scroll.getHeight()>=messages.getHeight()-s.dp(32);int oldScroll=scroll.getScrollY();signature=next;messages.removeAllViews();
|
||||
if(items.length()==0){TextView empty=s.label("还没有聊天记录\n按住下方按钮,给家人说句话吧",15,PhoneStyle.MUTED);empty.setGravity(Gravity.CENTER);empty.setPadding(0,s.dp(42),0,s.dp(30));messages.addView(empty);return;}
|
||||
for(int i=0;i<items.length();i++){
|
||||
JSONObject item=items.optJSONObject(i);if(item==null)continue;boolean mine=item.optBoolean("mine");
|
||||
int ink=night?0xffc8d5cd:PhoneStyle.INK,muted=night?0xff96aaa0:PhoneStyle.MUTED;
|
||||
LinearLayout row=s.column();row.setPadding(s.dp(12),s.dp(10),s.dp(12),s.dp(10));row.setBackground(s.shape(night?(mine?0xff2e4d40:0xff2a3d33):mine?PhoneStyle.PALE:0xfff1f0e9,14));LayoutParams lp=new LayoutParams(-1,-2);lp.topMargin=s.dp(9);lp.leftMargin=s.dp(mine?22:0);lp.rightMargin=s.dp(mine?0:22);messages.addView(row,lp);
|
||||
String name=(mine?"我 · ":"")+item.optString("senderName","设备");TextView author=s.bold(name,14);author.setTextColor(ink);row.addView(author);
|
||||
row.addView(s.label(new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.CHINA).format(new Date((long)(item.optDouble("at")*1000))),11,muted));
|
||||
Button voice=s.button("▶ 语音 · "+String.format(Locale.CHINA,"%.1f",item.optDouble("seconds"))+" 秒",mine);if(night){voice.setTextColor(ink);voice.setBackground(s.shape(0xff355549,14));}voice.setContentDescription("播放 "+name+" 的语音");voice.setOnClickListener(v->replay.accept(item.optString("id")));row.addView(voice,new LayoutParams(-1,-2));
|
||||
String delivery=item.optString("delivery"),archive=item.optString("archive");
|
||||
String state=delivery.equals("played")?"已播放":delivery.equals("received")?"已送达":delivery.equals("queued")?"等待播放":"未自动播放 · 可以回听";
|
||||
row.addView(s.label(state+" · "+(archive.equals("cloud")?"已存网盘":archive.equals("retry")?"网盘归档重试中":"网盘归档中"),11,muted));
|
||||
}
|
||||
if(!past&&follow)scroll.post(()->scroll.fullScroll(FOCUS_DOWN));else scroll.post(()->scroll.scrollTo(0,past?0:oldScroll));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user