CollectSexAndBirthActivity.kt 9.71 KB
Newer Older
徐健 committed
1 2 3 4 5
package com.yidianling.user.ui.collect

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Color
YKai committed
6
import androidx.core.content.ContextCompat
徐健 committed
7 8 9 10 11 12 13 14 15 16 17
import android.util.Log
import android.view.KeyEvent
import android.widget.NumberPicker
import android.widget.RelativeLayout
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.BaseActivity
import com.ydl.ydlcommon.data.http.RxUtils
import com.ydl.ydlcommon.router.IYDLRouterConstant
18
import com.ydl.ydlcommon.utils.StatusBarUtils
徐健 committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
import com.ydl.ydlcommon.utils.actionutil.ActionCountUtils
import com.ydl.ydlcommon.view.dialog.YDLCommonDialog
import com.yidianling.common.tools.RxImageTool
import com.yidianling.common.tools.ToastUtil
import com.yidianling.user.R
import com.yidianling.user.constants.UserBIConstants
import com.yidianling.user.http.UserHttpImpl
import com.yidianling.user.http.request.UserInfoCollectParam
import com.yidianling.user.http.request.UserInfoCollectUploadParam
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.user_activity_sex_birth_layout.*
import java.util.*


/**
 * 收集用户性别与出生年页面
 */

class CollectSexAndBirthActivity : BaseActivity() {

    private var selectedSex = -1 // 1 男 2 女
    private var selectedYear = 1995
42 43
    private var originYear = 1900
    private var nowYear: Int = 0
徐健 committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
    private var collectOutDialog: YDLCommonDialog? = null
    private var yearList: ArrayList<String> = ArrayList()

    override fun initDataAndEvent() {

        ActionCountUtils.count(UserBIConstants.COLLECT_USER_INFO_PAGE + "|" + UserBIConstants.COLLECT_USER_INFO_PAGE_VISIT)

        sex_and_birth_upload_btn.setOnClickListener {
            // 只需要选择了性别即可点击上传数据,因为年份默认就是有选择的
            if (selectedSex != -1) {
                uploadSexAndBirth()
            } else {
                ToastUtil.toastShort("请选择性别")
            }
        }

        male_rl.setOnClickListener {
            if (selectedSex != 1) {
                selectedSex = 1
                checkUpSelect()
64 65 66 67 68
                ActionCountUtils.count(
                    UserBIConstants.COLLECT_USER_INFO_PAGE + "|" + UserBIConstants.COLLECT_USER_INFO_SEX_MAN_CLICK,
                    "1"
                )
            } else {
徐健 committed
69 70
                selectedSex = -1
                checkUpSelect()
71 72 73 74
                ActionCountUtils.count(
                    UserBIConstants.COLLECT_USER_INFO_PAGE + "|" + UserBIConstants.COLLECT_USER_INFO_SEX_MAN_CLICK,
                    "2"
                )
徐健 committed
75 76 77 78 79 80
            }
        }
        female_rl.setOnClickListener {
            if (selectedSex != 2) {
                selectedSex = 2
                checkUpSelect()
81 82 83 84
                ActionCountUtils.count(
                    UserBIConstants.COLLECT_USER_INFO_PAGE + "|" + UserBIConstants.COLLECT_USER_INFO_SEX_FEMALE_CLICK,
                    "1"
                )
徐健 committed
85

86
            } else {
徐健 committed
87 88
                selectedSex = -1
                checkUpSelect()
89 90 91 92
                ActionCountUtils.count(
                    UserBIConstants.COLLECT_USER_INFO_PAGE + "|" + UserBIConstants.COLLECT_USER_INFO_SEX_FEMALE_CLICK,
                    "2"
                )
徐健 committed
93 94 95 96 97 98
            }
        }

        sex_and_birth_out.setOnClickListener {
            ActionCountUtils.count(UserBIConstants.COLLECT_USER_INFO_PAGE + "|" + UserBIConstants.COLLECT_USER_INFO_EXIT_CLICK)
            if (null == collectOutDialog) {
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
                collectOutDialog =
                    YDLCommonDialog(this, object : YDLCommonDialog.OnYDLCommonDialogListener {
                        override fun onCancel() {
                            // 这是左侧按钮 - 确定
                            collectOutDialog?.dismiss()
                            jumpToNewUserH5()
                        }

                        override fun onSure() {
                            // 这是右侧按钮 - 取消
                            collectOutDialog?.dismiss()
                        }

                    }).setNullTitleDesc(
                        "确定要退出吗?\n" +
                                "很多人在这里得到了帮助哦"
                    ).setCancelText("确定").setSureText("取消")
徐健 committed
116 117 118 119 120
            }
            collectOutDialog?.show()
        }
        //年份选择器
        val date = Calendar.getInstance()
121
        nowYear = date.get(Calendar.YEAR)
徐健 committed
122 123 124 125 126 127 128 129 130 131
        while (originYear <= nowYear) {
            yearList.add("$originYear")
            originYear++
        }

        user_collect_date_pick.setDividerColor("#CCCCCC")
        user_collect_date_pick.setNumberPickerDividerHeight(1)

        user_collect_date_pick.post {
            if (user_collect_date_pick.measuredHeight > RxImageTool.dp2px(175f)) {
132 133 134 135
                val param = RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT,
                    RxImageTool.dp2px(175f)
                )
徐健 committed
136 137
                user_collect_date_pick.layoutParams = param

138 139
                user_collect_date_pick?.value = selectedYear

徐健 committed
140 141 142 143 144 145
            }
        }

        user_collect_date_pick.displayedValues = yearList.toTypedArray()
        user_collect_date_pick.minValue = 1900
        user_collect_date_pick.maxValue = nowYear
146 147
        user_collect_date_pick.descendantFocusability =
            NumberPicker.FOCUS_BLOCK_DESCENDANTS // 关闭编辑功能
徐健 committed
148 149
        user_collect_date_pick.wrapSelectorWheel = false // 关闭循环

150 151
        user_collect_date_pick.value = selectedYear

徐健 committed
152 153 154 155 156 157 158
        //更新用户选择的年份
        user_collect_date_pick.setOnValueChangedListener { _, _, newVal -> selectedYear = newVal }
    }

    @SuppressLint("CheckResult")
    private fun uploadSexAndBirth() {

159 160 161 162
        ActionCountUtils.count(
            UserBIConstants.COLLECT_USER_INFO_PAGE + "|" + UserBIConstants.COLLECT_USER_INFO_NEXT_CLICK,
            "$selectedYear"
        )
徐健 committed
163 164
        val userInfoBean = UserInfoCollectUploadParam()
        userInfoBean.userInfoList = ArrayList()
165 166 167 168 169 170
        userInfoBean.userInfoList.add(
            UserInfoCollectParam(
                if (selectedSex == 1) "男" else "女",
                "gener"
            )
        )
徐健 committed
171
        userInfoBean.userInfoList.add(UserInfoCollectParam("$selectedYear", "age"))
徐健 committed
172 173 174 175 176
        UserHttpImpl.getInstance().userInfoCollectUpload(userInfoBean)
            .compose(RxUtils.resultJavaData())
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
177 178
                CollectFocusActivity.start(this, nowYear - selectedYear <= 16)
                finish()
徐健 committed
179 180 181 182 183 184 185 186 187 188 189 190 191
            }, {
                Log.i("上传数据异常", it.toString())
            })
    }

    @SuppressLint("CheckResult")
    private fun jumpToNewUserH5() {
        UserHttpImpl.getInstance().getNewUserJumpUrl()
            .compose(RxUtils.resultJavaData())
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                if (!isFinishing) {
192 193 194 195 196
                    val h5Params = H5Params(
                        "$it?barHeight=" + StatusBarUtils.getStatusBarHeight(
                            this
                        ).toString(), H5Params.BackLimit.NEW_USER_PAGE, true
                    )
徐健 committed
197
                    NewH5Activity.start(this, h5Params)
198
                    finish()
徐健 committed
199 200
                }
            }, {
201
                ToastUtil.toastShort(it.message)
徐健 committed
202 203 204
            })
    }

205
    @SuppressLint("ResourceAsColor")
徐健 committed
206 207 208 209 210 211
    private fun checkUpSelect() {
        if (selectedSex == 1) {
            male_rl.setBackgroundResource(R.drawable.user_collect_select_bg)
            female_rl.setBackgroundResource(R.drawable.user_collect_unselect_bg)
            male_img.setImageResource(R.drawable.user_male_selected)
            female_img.setImageResource(R.drawable.user_female_unselected)
212
            male_txt.setTextColor(ContextCompat.getColor(mContext, R.color.platform_main_theme))
213
            female_txt.setTextColor(Color.parseColor("#242424"))
徐健 committed
214 215 216

            sex_and_birth_upload_btn.setBackgroundResource(R.drawable.user_collect_bottom_btn_bg)
        } else if (selectedSex == 2) {
217
            male_rl.setBackgroundResource(R.drawable.user_collect_unselect_bg)
徐健 committed
218 219 220 221
            female_rl.setBackgroundResource(R.drawable.user_collect_select_bg)
            male_img.setImageResource(R.drawable.user_male_unselected)
            female_img.setImageResource(R.drawable.user_female_selected)
            male_txt.setTextColor(Color.parseColor("#242424"))
222
            female_txt.setTextColor(ContextCompat.getColor(mContext, R.color.platform_main_theme))
徐健 committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250

            sex_and_birth_upload_btn.setBackgroundResource(R.drawable.user_collect_bottom_btn_bg)
        } else {
            male_rl.setBackgroundResource(R.drawable.user_collect_unselect_bg)
            female_rl.setBackgroundResource(R.drawable.user_collect_unselect_bg)
            male_img.setImageResource(R.drawable.user_male_unselected)
            female_img.setImageResource(R.drawable.user_female_unselected)
            male_txt.setTextColor(Color.parseColor("#242424"))
            female_txt.setTextColor(Color.parseColor("#242424"))

            sex_and_birth_upload_btn.setBackgroundResource(R.drawable.user_collect_bottom_btn_un_bg)
        }
    }

    /**
     * 当前页面不能退出
     */
    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true
        }
        return super.onKeyDown(keyCode, event)
    }

    override fun layoutResId(): Int {
        return R.layout.user_activity_sex_birth_layout
    }
}