Polyv Help Center

Help Center

7_10-核心common-文档

Updated: 2023-04-17 15:27:57

1 Feature Overview

The document module is a feature used in the three-screen broadcasting client. It includes controlling the display content of the document WebView and managing channel documents.

2 MVP Pattern

The document module uses the MVP pattern, with interfaces defined as the IPLVDocumentContract class.

2.1 View

The View corresponding to the document MVP pattern is PLVAbsDocumentView, which is an abstract View with empty implementations. When registering an MVP-View, you can inherit this View and only override the callback methods you need to listen to.

2.2 Presenter

The Presenter corresponding to the document MVP pattern is PLVDocumentPresenter, which provides many functional methods for document operations. Before using this Presenter, you must call the init() method to initialize it.

interface Presenter {
    /**
     * 初始化方法,必须调用一次
     *
     * @param lifecycleOwner 生命周期
     * @param liveRoomDataManager 直播间数据管理器
     * @param documentWebProcessor 封装的WebView处理器
     */
    void init(LifecycleOwner lifecycleOwner,
              IPLVLiveRoomDataManager liveRoomDataManager,
              PLVSDocumentWebProcessor documentWebProcessor);

}
2.2.1 Calls to the View Layer

Generally, calls from the Presenter to the View are callback-oriented. Most callbacks are implemented by listening to LiveData changes in the Model layer and then invoking the View layer. The example code below shows how PPT list data is retrieved via a network request and then passed back to the View layer.

/**
 * 监听Model层所有PPT文档列表更新
 * 向view层回调
 *
 * @param lifecycleOwner
 */
private void observePptInfo(LifecycleOwner lifecycleOwner) {
    plvDocumentRepository.getPptInfoLiveData().observe(lifecycleOwner, new Observer<PLVStatefulData<PLVSPPTInfo>>() {
        @Override
        public void onChanged(@Nullable PLVStatefulData<PLVSPPTInfo> plvspptInfo) {
            if (plvspptInfo == null || !plvspptInfo.isSuccess()) {
                return;
            }
            for (WeakReference<IPLVDocumentContract.View> viewWeakReference : viewWeakReferenceList) {
                IPLVDocumentContract.View view = viewWeakReference.get();
                if (view != null) {
                    view.onPptCoverList(plvspptInfo.getData());
                }
            }
        }
    });
}

When the Model layer plvDocumentRepository performs a network request to obtain PPT list data, it updates the data in pptInfoLiveData. Here, the LiveData data update is subscribed to. When the data changes, the onChanged method is triggered, calling onPptCoverList() on each registered View to notify them of the PPT list data update.

2.2.2 Calls to the Model Layer

PLVDocumentPresenter depends on two Model layer objects: PLVDocumentRepository, which is responsible for interacting with the WebView and managing documents, and PLVPptUploadLocalRepository, which handles local caching of upload progress when uploading documents. Here, the PPT WebView is also treated as a remote object, and interaction with the WebView is considered equivalent to a network request, so the Model layer handles WebView interactions.

The example code below shows how to switch the PPT displayed in the WebView.

@Override
public void changePpt(int autoId) {
    if (!checkInitialized()) {
        return;
    }
    plvDocumentRepository.sendWebMessage(PLVSDocumentWebProcessor.CHANGEPPT, "{\"autoId\":" + autoId + "}");

Here, autoId is the identifier of the PPT returned by the backend, which is included in the PPT list. First, you need to check whether the Presenter has been initialized using checkInitialized(), and then directly call the Model layer method to send the data.

2.3 Model

2.3.1 PLVDocumentRepository

PLVDocumentRepository is responsible for sending data to the WebView, sending requests to the server, and receiving corresponding response data. PLVDocumentRepository does not hold a reference to the Presenter; instead, data is returned to the Presenter by the Presenter actively subscribing to LiveData. The following example code shows how to obtain the PPT list.

/**
 * 请求更新PPT文档列表数据
 * 回调 {@link #getPptInfoLiveData()}
 */
public void requestPptCoverList() {
    PLVDocumentDataManager.getDocumentList(new PLVrResponseCallback<PLVSPPTInfo>() {
        @Override
        public void onSuccess(PLVSPPTInfo plvspptInfo) {
            cachePptInfo = plvspptInfo;
            plvsPptInfoLiveData.postValue(PLVStatefulData.success(cachePptInfo));
        }
        
        @Override
        public void onFinish() {
        }
    });
}

PLVDocumentDataManager is a document data class provided internally by the SDK. It encapsulates the HTTP request for obtaining the PPT list. Simply calling the getDocumentList() method allows you to retrieve the PPT list data in the onSuccess callback. After obtaining the PPT list, the data in plvsPptInfoLiveData is updated. If the Presenter has subscribed to this LiveData, it can automatically receive the new data in the subscription callback.

2.3.2 PLVPptUploadLocalRepository

PLVPptUploadLocalRepository is responsible for locally caching the upload progress when uploading PPT documents. When the user logs back into the live room next time, it checks whether there are any incomplete uploads from the previous session. This class uses SharedPreferences for local data storage.

联系客服,在线咨询