TestPlugin.kt 5.6 KB
Newer Older
徐健 committed
1 2
package com.yidianling.tests.home.plugin

3
import android.app.Activity
徐健 committed
4
import android.net.Uri
5
import com.channel.ydl_flutter_base.base.BaseFlutterFragment
徐健 committed
6 7 8 9 10 11
import com.ydl.webview.H5Params
import com.ydl.webview.NewH5Activity
import com.ydl.ydl_router.manager.YDLRouterManager
import com.ydl.ydl_router.manager.YDLRouterParams
import com.ydl.ydlcommon.base.config.HttpConfig
import com.ydl.ydlcommon.router.IYDLRouterConstant
严久程 committed
12 13
import com.ydl.ydlcommon.utils.actionutil.ActionCountUtils
import com.yidianling.tests.home.config.TestBIConstants
徐健 committed
14 15 16 17 18 19
import com.yidianling.tests.list.view.TestCategoryListActivity
import com.yidianling.tests.router.TestsIn
import com.yidianling.tests.search.TestSearchActivity
import io.flutter.app.FlutterActivity
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
20
import io.flutter.view.FlutterView
徐健 committed
21 22 23 24 25

/**
 * Created by xj on 2019/11/19.
 */
class TestPlugin : MethodChannel.MethodCallHandler {
26

27 28
    private var mActivity: Activity? = null
    private constructor(activity: Activity) {
徐健 committed
29 30 31 32 33 34 35 36 37 38 39 40
        mActivity = activity
    }
    companion object {
        const val CHANNEL: String = "lib/test_module/channel"
        const val GO_BACK = "goBack" // 左上角返回按钮
        const val GO_SEARCH = "goSearch" // 查找测评
        const val GO_MY_TESTS = "goMyTests" // 我的测评
        const val BANNER_CLICK = "bannerClick" // Banner点击
        const val CATEGORY_CLICK = "categoryClick" // 分类点击
        const val ADVERT_CLICK = "advertClick" // 实时测评点击
        const val TEST_LIST_ITEM_CLICK = "testListItemClick" // 测评列表点击
        const val LOOK_ALL_TEST = "lookAllTest" // 查看全部测评
徐健 committed
41
        const val IS_HIDE_LEFT_BACK = "hideLeftBackLL" // 是否隐藏标题左侧布局
严久程 committed
42

43 44
        var isHideLeftBack = false // 是否隐藏标题左侧布局

徐健 committed
45

46
        fun register(activity: FlutterActivity) {
47
            isHideLeftBack = false
徐健 committed
48 49
            MethodChannel(activity.flutterView, CHANNEL).setMethodCallHandler(TestPlugin(activity))
        }
50 51

        fun register(fragment: BaseFlutterFragment, flutterView: FlutterView) {
52
            isHideLeftBack = true
YKai committed
53
            MethodChannel(flutterView, CHANNEL).setMethodCallHandler(TestPlugin(fragment.activity!!))
54
        }
徐健 committed
55 56 57 58 59
    }

    override fun onMethodCall(methodCall: MethodCall, result: MethodChannel.Result) {
        when (methodCall.method) {
            GO_BACK -> {
严久程 committed
60
//                ActionCountUtils.count(TestBIConstants.YDL_TEST_MAIN_RETURN_CLICK)
徐健 committed
61 62 63
                mActivity?.finish()
            }
            GO_SEARCH -> {
严久程 committed
64
//                ActionCountUtils.count(TestBIConstants.YDL_TEST_MAIN_SEARCH_CLICK)
徐健 committed
65 66 67 68 69
                mActivity?.let {
                    TestSearchActivity.start(mActivity!!)
                }
            }
            GO_MY_TESTS -> {
70
                if (!TestsIn.loginByOneKeyLogin(mActivity!!,true)) {
徐健 committed
71 72 73 74 75 76 77 78 79
                    //如果未登录 走登录逻辑
                    return
                }
                val testParam = H5Params(HttpConfig.MH5_URL + "ceshi/my-test", "测试记录")
                mActivity?.let {
                    NewH5Activity.start(mActivity, testParam)
                }
            }
            BANNER_CLICK -> {
严久程 committed
80

徐健 committed
81 82 83 84
                var linkUrl: String? = methodCall.argument<String>("linkUrl")
                linkUrl?.let {
                    link(linkUrl)
                }
严久程 committed
85
//                ActionCountUtils.count(TestBIConstants.YDL_TEST_MAIN_BANNER_CLICK,linkUrl?:"")
严久程 committed
86

徐健 committed
87 88 89 90 91
            }
            CATEGORY_CLICK -> {
                mActivity?.let {
                    var tabName: String = methodCall.argument<String>("tabName")?:""
                    TestCategoryListActivity.start(mActivity!!, tabName)
严久程 committed
92

严久程 committed
93
//                    ActionCountUtils.count(TestBIConstants.YDL_TEST_MAIN_ICON_CLICK,tabName)
徐健 committed
94
                }
严久程 committed
95

徐健 committed
96 97
            }
            ADVERT_CLICK -> {
严久程 committed
98
//                ActionCountUtils.count(TestBIConstants.YDL_TEST_MAIN_SCROLL_BAR_CLICK)
严久程 committed
99

徐健 committed
100 101 102 103 104 105
                var linkUrl: String? = methodCall.argument<String>("linkUrl")
                linkUrl?.let {
                    link(linkUrl)
                }
            }
            TEST_LIST_ITEM_CLICK -> {
106
                var linkUrl: String? = methodCall.arguments.toString()
严久程 committed
107
//                ActionCountUtils.count(TestBIConstants.YDL_TEST_MAIN_ALL_TEST_LIST_CLICK,linkUrl?:"")
严久程 committed
108

徐健 committed
109 110 111 112 113
                linkUrl?.let {
                    link(linkUrl)
                }
            }
            LOOK_ALL_TEST -> {
严久程 committed
114
//                ActionCountUtils.count(TestBIConstants.YDL_TEST_MAIN_VIEW_ALL_CLICK)
严久程 committed
115

徐健 committed
116 117 118 119
                mActivity?.let {
                    TestCategoryListActivity.start(mActivity!!)
                }
            }
徐健 committed
120
            IS_HIDE_LEFT_BACK -> {
121
                result.success(isHideLeftBack)
徐健 committed
122
            }
徐健 committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
        }
    }

    private fun link(linkUrl: String?) {
        if (null == linkUrl || linkUrl.isEmpty()) {
            return
        }
        if (linkUrl.startsWith("app")) {
            val uri = Uri.parse(linkUrl)
            if ("ceshi" == uri.host) {
                val id = uri.getQueryParameter("id")
//                YDLRouterManager.router(IYDLRouterConstant.ROUTER_TEST_DETAIL, YDLRouterParams().putExtra(IYDLRouterConstant.EXTRA_ID, id))
                TestsIn.getTestsImpl().testDetailH5(id)
            }
        } else if (linkUrl.startsWith("http")) {
            YDLRouterManager.router(
                    IYDLRouterConstant.ROUTER_H5_H5,
                    YDLRouterParams().putExtra(IYDLRouterConstant.EXTRA_URL, linkUrl), "")
        } else {
            YDLRouterManager.router(linkUrl)
        }
    }
145 146 147 148

    interface TestPluginDelegate{
        fun getHideBack():Boolean
    }
徐健 committed
149
}