| | |
| | | package com.sinata.xqmuse.ui.discovery |
| | | |
| | | import android.Manifest |
| | | import android.util.Log |
| | | import androidx.recyclerview.widget.GridLayoutManager |
| | | import cn.sinata.xldutils.fragment.BaseFragment |
| | | import cn.sinata.xldutils.utils.SPUtils |
| | | import com.amap.api.location.AMapLocation |
| | | import com.amap.api.location.AMapLocationClient |
| | | import com.amap.api.location.AMapLocationClientOption |
| | | import com.amap.api.location.AMapLocationListener |
| | | import com.sinata.xqmuse.JkApplication |
| | | import com.sinata.xqmuse.R |
| | | import com.sinata.xqmuse.dialog.TipDialog |
| | |
| | | import org.greenrobot.eventbus.EventBus |
| | | import org.greenrobot.eventbus.Subscribe |
| | | import org.jetbrains.anko.support.v4.startActivity |
| | | import org.jetbrains.anko.support.v4.toast |
| | | |
| | | |
| | | class DiscoveryFragment : BaseFragment() { |
| | |
| | | private var page = 1 |
| | | var search: String? = null //搜索 |
| | | |
| | | |
| | | override fun onFirstVisibleToUser() { |
| | | EventBus.getDefault().register(this) |
| | | rv_list.layoutManager = GridLayoutManager(requireContext(),2) |
| | | rv_list.layoutManager = GridLayoutManager(requireContext(), 2) |
| | | rv_list.adapter = adapter |
| | | |
| | | adapter.setOnItemClickListener { view, position -> |
| | |
| | | |
| | | private fun checkLocation() { |
| | | val rxPermissions = RxPermissions(requireActivity()) |
| | | if (rxPermissions.isGranted(Manifest.permission.ACCESS_FINE_LOCATION)||SPUtils.instance().getBoolean(Const.User.LOCATION_REFUSED)){ |
| | | if (rxPermissions.isGranted(Manifest.permission.ACCESS_FINE_LOCATION)){ |
| | | requestLocation() |
| | | }else if (SPUtils.instance().getBoolean(Const.User.LOCATION_REFUSED)){ |
| | | refreshLayout.autoRefresh() |
| | | }else{ |
| | | TipDialog.show(childFragmentManager,"为了展示您附近的疗愈馆,我们需要获取该设备的位置权限",object :TipDialog.OnClickCallback{ |
| | | override fun onOk() { |
| | | val subscribe = |
| | | rxPermissions.request(Manifest.permission.ACCESS_FINE_LOCATION).subscribe { |
| | | if (it){ |
| | | //todo 高德定位 |
| | | refreshLayout.autoRefresh() |
| | | }else{ |
| | | SPUtils.instance().put(Const.User.LOCATION_REFUSED,true).apply() |
| | | refreshLayout.autoRefresh() |
| | | } |
| | | } |
| | | compositeDisposable.add(subscribe) |
| | | } |
| | | TipDialog.show( |
| | | childFragmentManager, |
| | | "为了展示您附近的疗愈馆,我们需要获取该设备的位置权限", |
| | | object : TipDialog.OnClickCallback { |
| | | override fun onOk() { |
| | | val subscribe = |
| | | rxPermissions.request(Manifest.permission.ACCESS_FINE_LOCATION) |
| | | .subscribe { |
| | | if (it) { |
| | | requestLocation() |
| | | } else { |
| | | SPUtils.instance().put(Const.User.LOCATION_REFUSED, true) |
| | | .apply() |
| | | refreshLayout.autoRefresh() |
| | | } |
| | | } |
| | | compositeDisposable.add(subscribe) |
| | | } |
| | | |
| | | override fun onCancel() { |
| | | SPUtils.instance().put(Const.User.LOCATION_REFUSED,true).apply() |
| | | refreshLayout.autoRefresh() |
| | | } |
| | | },"去授权","取消") |
| | | override fun onCancel() { |
| | | SPUtils.instance().put(Const.User.LOCATION_REFUSED, true).apply() |
| | | refreshLayout.autoRefresh() |
| | | } |
| | | }, |
| | | "去授权", |
| | | "取消" |
| | | ) |
| | | } |
| | | } |
| | | |
| | | private fun requestLocation(){ |
| | | toast("定位中...") |
| | | var locationClient:AMapLocationClient? |
| | | try { |
| | | locationClient = AMapLocationClient(requireActivity()) |
| | | } catch (e: Exception) { |
| | | locationClient = null |
| | | } |
| | | locationClient?.setLocationListener { |
| | | JkApplication.lat = it.latitude |
| | | JkApplication.lon = it.longitude |
| | | refreshLayout.autoRefresh() |
| | | } |
| | | val option = AMapLocationClientOption() |
| | | option.locationPurpose = AMapLocationClientOption.AMapLocationPurpose.SignIn |
| | | option.isOnceLocation = true |
| | | locationClient?.setLocationOption(option) |
| | | //设置场景模式后最好调用一次stop,再调用start以保证场景模式生效 |
| | | locationClient?.stopLocation() |
| | | locationClient?.startLocation() |
| | | } |
| | | |
| | | |
| | | |
| | | private fun getData(){ |
| | | HttpManager.getMeditationPage(page,search,JkApplication.lat, JkApplication.lon).requestByF(this,success = { _, data-> |
| | | if (page == 1) |
| | | list.clear() |
| | | list.addAll(data?.list?: arrayListOf()) |
| | | adapter.notifyDataSetChanged() |
| | | if (list.isEmpty()) |
| | | refreshLayout.finishRefreshWithNoMoreData() |
| | | else if (data?.list?.isNullOrEmpty() == true) |
| | | refreshLayout.finishLoadMoreWithNoMoreData() |
| | | else if (page == 1) |
| | | refreshLayout.finishRefresh() |
| | | else |
| | | refreshLayout.finishLoadMore() |
| | | }){_,_-> |
| | | HttpManager.getMeditationPage(page, search, JkApplication.lat, JkApplication.lon).requestByF( |
| | | this, |
| | | success = { _, data -> |
| | | if (page == 1) |
| | | list.clear() |
| | | list.addAll(data?.list ?: arrayListOf()) |
| | | adapter.notifyDataSetChanged() |
| | | if (list.isEmpty()) |
| | | refreshLayout.finishRefreshWithNoMoreData() |
| | | else if (data?.list?.isNullOrEmpty() == true) |
| | | refreshLayout.finishLoadMoreWithNoMoreData() |
| | | else if (page == 1) |
| | | refreshLayout.finishRefresh() |
| | | else |
| | | refreshLayout.finishLoadMore() |
| | | }){ _, _-> |
| | | if (page == 1) |
| | | refreshLayout.finishRefresh(false) |
| | | else |
| | |
| | | } |
| | | |
| | | @Subscribe |
| | | fun refresh(e:EmptyEvent){ |
| | | fun refresh(e: EmptyEvent){ |
| | | if (e.code == Const.EventCode.REFRESH_DISCOVERY) |
| | | refreshLayout.autoRefresh() |
| | | } |