TestPlugin.kt 4.92 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
import com.yidianling.tests.home.config.TestBIConstants
徐健 committed
13 14 15 16 17 18
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
19
import io.flutter.view.FlutterView
徐健 committed
20 21 22 23 24

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

26 27
    private var mActivity: Activity? = null
    private constructor(activity: Activity) {
徐健 committed
28 29 30 31 32 33 34 35 36 37 38 39
        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
40
        const val IS_HIDE_LEFT_BACK = "hideLeftBackLL" // 是否隐藏标题左侧布局
严久程 committed
41

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

徐健 committed
44

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

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

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

徐健 committed
78 79 80 81
                var linkUrl: String? = methodCall.argument<String>("linkUrl")
                linkUrl?.let {
                    link(linkUrl)
                }
严久程 committed
82

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

徐健 committed
89
                }
严久程 committed
90

徐健 committed
91 92
            }
            ADVERT_CLICK -> {
严久程 committed
93

徐健 committed
94 95 96 97 98 99
                var linkUrl: String? = methodCall.argument<String>("linkUrl")
                linkUrl?.let {
                    link(linkUrl)
                }
            }
            TEST_LIST_ITEM_CLICK -> {
100
                var linkUrl: String? = methodCall.arguments.toString()
严久程 committed
101

徐健 committed
102 103 104 105 106
                linkUrl?.let {
                    link(linkUrl)
                }
            }
            LOOK_ALL_TEST -> {
严久程 committed
107

徐健 committed
108 109 110 111
                mActivity?.let {
                    TestCategoryListActivity.start(mActivity!!)
                }
            }
徐健 committed
112
            IS_HIDE_LEFT_BACK -> {
113
                result.success(isHideLeftBack)
徐健 committed
114
            }
徐健 committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
        }
    }

    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)
        }
    }
137 138 139 140

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