更新时间:2024-10-22 06:54:39下载pdf
本文介绍如何实现设备分享业务。更多信息,参考 设备分享业务能力。
在设备/群组的详情页,新增设备分享入口。
获取分享管理类。
val mShareManager : IDeviceShareManager = DeviceBusinessDataManager.getInstance().getDeviceShareManager()
val manager : ThingDeviceShareManager = ThingDeviceShareManager()
manager?.isSupportDeviceShare(resId, resType, object : IThingResultCallback<Boolean> {
override fun onSuccess(result: Boolean?) {
}
override fun onError(errorCode: String?, errorMessage: String?) {
}
})
resId?.let {
mShareManager?.querySharedCount(it, resType, object : Business.ResultListener<Long> {
override fun onFailure(p0: BusinessResponse?, p1: Long?, p2: String?) {
}
override fun onSuccess(p0: BusinessResponse?, p1: Long?, p2: String?) {
p1?.let {
var maxCount = 9999L
if (p1 != -1L) {
maxCount = p1
}
if (maxCount >= count) {
afterAction.invoke()
} else {
showToast("No sharing times")
}
}
}
})
}
若返回 -1
,则按照不限次数处理。
分享设备有两种方式:
manager?.shareToUser(
resId,
resType,
homeId,
userName,
object : IThingResultCallback<SharedUserInfoBean> {
override fun onSuccess(result: SharedUserInfoBean?) {
showToast("share success")
}
override fun onError(errorCode: String?, errorMessage: String?) {
showToast(errorMessage)
}
})
mShareManager?.createShareLink(
resId.toString(),
resType,
homeId,
5,
count,
object : Business.ResultListener<ShareDeviceLinkResultBean> {
override fun onFailure(
p0: BusinessResponse?,
p1: ShareDeviceLinkResultBean?,
p2: String?
) {
showToast(p0?.errorMsg)
}
override fun onSuccess(
p0: BusinessResponse?,
p1: ShareDeviceLinkResultBean?,
p2: String?
) {
p1?.let { copyToClipboard(it.code) }
}
})
首先,检测短链是否有效。
manager?.parseShortLinkAvailability(link, object : IThingResultCallback<Boolean> {
override fun onSuccess(result: Boolean?) {
if (result == true) {
getSharerInfo(link)
} else {
showToast("The link has expired")
}
}
override fun onError(errorCode: String?, errorMessage: String?) {
}
})
接着,获取短链的内容,包括分享的设备、分享人等信息。此时可以弹窗展示这些信息,并且提供接受分享的按钮。
mShareManager?.parseSharedDeviceLinkResult(code,
object : Business.ResultListener<ShareShortLinkResult> {
override fun onFailure(
p0: BusinessResponse?,
p1: ShareShortLinkResult?,
p2: String?
) {
}
override fun onSuccess(
p0: BusinessResponse?,
p1: ShareShortLinkResult?,
p2: String?
) {
p1?.let {
binding.tvUsername.text = p1.resName
binding.ivUserIcon.setImageURI(Uri.parse(p1.resIcon))
binding.llSharerInfo.visibility = View.VISIBLE
}
}
})
manager?.acceptShare(code, object : IResultCallback {
override fun onError(code: String?, error: String?) {
showToast(error)
}
override fun onSuccess() {
showToast("accept share success")
}
})
此处指当前设备或群组分享过的用户,可以使用列表进行展示。支持分页获取,但通常数量不会太多,也可以一次性获取所有用户。
manager?.getReceivers(
resId,
resType,
1,
100,
object : IThingResultCallback<List<ShareMember>> {
override fun onSuccess(result: List<ShareMember>?) {
}
override fun onError(errorCode: String?, errorMessage: String?) {
}
})
manager?.removeReceiver(memberId, resId, resType, object : IResultCallback {
override fun onError(code: String?, error: String?) {
showToast(error)
}
override fun onSuccess() {
showToast("delete success")
}
})
分享时间有两种模式:
0
。private fun showChangeTimeDialog(memberId: Long) {
ChangeTimeDialog.Builder(this)
.setButtonListener(object : ChangeTimeDialog.OnClickListener {
override fun onForeverButtonClick() {
changeTime(memberId,0, 0)
}
override fun onSometimeButtonClick(endTime: Long) {
changeTime(memberId,1, endTime)
}
})
.build()
.show()
}
private fun changeTime(memberId: Long,mode: Int, endTime: Long) {
mShareManager!!.updateSharedDeadline(memberId,
resId!!, resType, mode, endTime, object : Business.ResultListener<Boolean> {
override fun onFailure(p0: BusinessResponse?, p1: Boolean?, p2: String?) {
showToast(p0?.errorMsg)
}
override fun onSuccess(p0: BusinessResponse?, p1: Boolean?, p2: String?) {
showToast("change time success")
}
})
}
这指的是整合账户维度的,所有设备或群组分享过的用户,同样可以使用列表进行展示。
manager?.getRelationMembers(object : IThingResultCallback<List<ShareMember>> {
override fun onSuccess(result: List<ShareMember>?) {
var receivedUserList2: MutableList<ReceiverItemBean> = ArrayList()
result?.indices?.forEach { i ->
val bean = ReceiverItemBean(
result[i].iconUrl,
result[i].userName,
result[i].memberId,
result[i].uid
)
receivedUserList2.add(i, bean)
}
allUserAdapter.setData(receivedUserList2, null)
}
override fun onError(errorCode: String?, errorMessage: String?) {
L.i(TAG, errorMessage)
}
})
mShareManager?.deleteSharedContact(userId, object : Business.ResultListener<Boolean> {
override fun onFailure(p0: BusinessResponse?, p1: Boolean?, p2: String?) {
showToast(p0?.errorMsg)
}
override fun onSuccess(p0: BusinessResponse?, p1: Boolean?, p2: String?) {
showToast("delete success")
}
})
分享者是指将设备或群组分享给“我”的用户。
manager?.getShareReceivedUserList(object : IThingResultCallback<List<SharedUserInfoBean>> {
override fun onSuccess(result: List<SharedUserInfoBean>?) {
var receivedUserList3: MutableList<ReceiverItemBean> = ArrayList()
result?.indices?.forEach { i ->
val bean = ReceiverItemBean(
result[i].iconUrl,
result[i].userName,
result[i].memeberId,
null
)
receivedUserList3.add(i, bean)
}
sharerListAdapter.setData(receivedUserList3, null)
}
override fun onError(errorCode: String?, errorMessage: String?) {
}
})
manager?.removeReceivedUserShare(memberId, object : IResultCallback {
override fun onError(code: String?, error: String?) {
showToast(error)
}
override fun onSuccess() {
showToast("remove success")
}
})
可以创建一个分享者详情页,进入该页面时获取分享者的信息。
manager?.getSharerInfoDetail(memberId, object : IThingResultCallback<ShareReceivedUserDetailBean> {
override fun onSuccess(result: ShareReceivedUserDetailBean?) {
result?.let {
binding.etNickname.setText(result.remarkName)
val beans: MutableList<ItemBean> = ArrayList(8)
for (deviceBean in result.devices) {
beans.add(getItemBeanFromDevs(deviceBean))
}
adapter.setData(beans)
}
}
override fun onError(errorCode: String?, errorMessage: String?) {
showToast(errorMessage)
}
})
manager?.getSharerName(resId,resType,object :IThingResultCallback<String>{
override fun onSuccess(result: String?) {
}
override fun onError(errorCode: String?, errorMessage: String?) {
}
})
private fun changeNickname(){
val newName = binding.etNickname.text.toString()
manager?.renameReceivedShareNickname(memberId,newName,object :IResultCallback{
override fun onError(code: String?, error: String?) {
showToast(error)
}
override fun onSuccess() {
showToast("change success")
}
})
}
删除已接受分享的设备/群组。
manager?.removeReceivedShare(resId,resType,object :IResultCallback{
override fun onError(code: String?, error: String?) {
}
override fun onSuccess() {
}
})
该内容对您有帮助吗?
是意见反馈该内容对您有帮助吗?
是意见反馈