feat: add snapshot feature for devkit, this is for android

This commit is contained in:
pengfei.zhou
2021-07-09 17:16:30 +08:00
committed by osborn
parent 35b9d67de6
commit dbe94165b9
19 changed files with 500 additions and 7 deletions

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,212 @@
/*
* Copyright [2019] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pub.doric.devkit.ui;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import com.github.pengfeizhou.jscore.ArchiveException;
import com.github.pengfeizhou.jscore.JSArray;
import com.github.pengfeizhou.jscore.JSDecoder;
import com.github.pengfeizhou.jscore.JSObject;
import java.util.concurrent.Callable;
import pub.doric.DoricContext;
import pub.doric.async.AsyncResult;
import pub.doric.devkit.R;
import pub.doric.shader.RootNode;
import pub.doric.shader.ViewNode;
import pub.doric.utils.ThreadMode;
/**
* @Description: pub.doric.devkit.ui
* @Author: pengfei.zhou
* @CreateDate: 2021/7/9
*/
@SuppressLint("ViewConstructor")
public class DoricSnapshotView extends FrameLayout {
private int lastX;
private int lastY;
private final DoricContext doricContext;
private int snapNo = -1;
private int snapSize = 0;
private TextView snapIndex;
private ImageView spanPre;
private ImageView spanNext;
public DoricSnapshotView(@NonNull Context context, DoricContext doricContext) {
super(context);
this.doricContext = doricContext;
initView(context);
this.setAlpha(0.5f);
}
private void initView(Context context) {
LayoutInflater.from(context).inflate(R.layout.layout_doric_dev_view_snapshot, this);
snapIndex = findViewById(R.id.snap_idx);
spanPre = findViewById(R.id.snap_pre);
spanNext = findViewById(R.id.snap_next);
findViewById(R.id.snap_close).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
rollupSnapshot(snapSize);
((ViewGroup) (DoricSnapshotView.this.getParent())).removeView(DoricSnapshotView.this);
}
});
this.doricContext.callEntity("__renderSnapshotDepth__").setCallback(new AsyncResult.Callback<JSDecoder>() {
@Override
public void onResult(final JSDecoder result) {
snapIndex.post(new Runnable() {
@Override
public void run() {
try {
snapSize = result.decode().asNumber().toInt();
} catch (ArchiveException e) {
e.printStackTrace();
}
snapNo = snapSize;
rollupSnapshot(snapNo);
spanPre.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (snapNo <= 0) {
return;
}
snapNo--;
rollupSnapshot(snapNo);
}
});
spanNext.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (snapNo >= snapSize) {
return;
}
snapNo++;
rollupSnapshot(snapNo);
}
});
}
});
}
@Override
public void onError(Throwable t) {
}
@Override
public void onFinish() {
}
});
}
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
lastX = x;
lastY = y;
break;
case MotionEvent.ACTION_MOVE:
int offsetX = x - lastX;
int offsetY = y - lastY;
layout(getLeft() + offsetX, getTop() + offsetY, getRight() + offsetX, getBottom() + offsetY);
break;
default:
break;
}
return true;
}
private void rollupSnapshot(int index) {
spanPre.setVisibility(index <= 0 ? View.INVISIBLE : View.VISIBLE);
spanNext.setVisibility(index >= snapSize ? View.INVISIBLE : View.VISIBLE);
snapIndex.setText(String.valueOf(index));
doricContext.callEntity("__restoreRenderSnapshot__", index).setCallback(new AsyncResult.Callback<JSDecoder>() {
@Override
public void onResult(JSDecoder result) {
try {
final JSArray jsArray = result.decode().asArray();
doricContext.getDriver().asyncCall(new Callable<Object>() {
@Override
public Object call() {
if (doricContext.getContext() instanceof Activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1
&& ((Activity) doricContext.getContext()).isDestroyed()) {
return null;
}
}
doricContext.getRootNode().getView().removeAllViews();
doricContext.getRootNode().clearSubModel();
for (int i = 0; i < jsArray.size(); i++) {
JSObject jsObject = jsArray.get(i).asObject();
String viewId = jsObject.getProperty("id").asString().value();
RootNode rootNode = doricContext.getRootNode();
if (TextUtils.isEmpty(rootNode.getId()) && "Root".equals(jsObject.getProperty("type").asString().value())) {
rootNode.setId(viewId);
rootNode.blend(jsObject.getProperty("props").asObject());
} else {
ViewNode viewNode = doricContext.targetViewNode(viewId);
if (viewNode != null) {
viewNode.blend(jsObject.getProperty("props").asObject());
}
}
}
return null;
}
}, ThreadMode.UI);
} catch (ArchiveException e) {
e.printStackTrace();
}
}
@Override
public void onError(Throwable t) {
}
@Override
public void onFinish() {
}
});
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.9 KiB

View File

@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#ecf0f1"
android:gravity="center"
android:orientation="horizontal"
android:padding="15dp">
<ImageView
android:id="@+id/snap_pre"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/icon_doricdev_prev" />
<TextView
android:id="@+id/snap_idx"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center"
android:text="1"
android:textColor="#000"
android:textSize="30dp" />
<ImageView
android:id="@+id/snap_next"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/icon_doricdev_next" />
<ImageView
android:id="@+id/snap_close"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="20dp"
android:src="@drawable/icon_doricdev_close" />
</LinearLayout>
</merge>