Commit f6cb9add authored by wanglei's avatar wanglei

...

parent de944271
......@@ -8,4 +8,6 @@ data class ViewerBean(
val nickName: String,
var status: Int,//0不可以看 1可以看
val device: String,
)
) {
var localNickName: String? = null
}
......@@ -19,7 +19,7 @@ abstract class BaseActivity<T : ViewBinding> : AppCompatActivity() {
lateinit var launcher: ActivityLauncher
var isRunning: Boolean = false//需要用时在onResume()最后一行复制
var isRunning: Boolean = false//需要用时在onResume()最后一行赋值
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
......
......@@ -20,13 +20,13 @@ object LocationLoginUtils {
field = value
AppPreferences.getInstance().put("invitationCodeSp", value, true)
}
var nickNameSp = ""
var myNickNameSp = ""
get() {
return AppPreferences.getInstance().getString("nickNameSp", field)
return AppPreferences.getInstance().getString("myNickNameSp", field)
}
set(value) {
field = value
AppPreferences.getInstance().put("nickNameSp", value, true)
AppPreferences.getInstance().put("myNickNameSp", value, true)
}
private val TAG = "LocationLoginUtils"
......@@ -86,7 +86,7 @@ object LocationLoginUtils {
invitationCodeSp = invitationCode
val nickName = data.getString("nickName")
nickNameSp = nickName
myNickNameSp = nickName
LogEx.logDebug(TAG, "invitationCode=$invitationCode nickName=$nickName")
}
......
package com.base.locationsharewhite.ui.main
import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Color
import androidx.activity.addCallback
import androidx.core.view.updatePadding
import androidx.lifecycle.lifecycleScope
import com.base.locationsharewhite.R
import com.base.locationsharewhite.bean.ViewerBean
import com.base.locationsharewhite.databinding.ActivityLocationShareBinding
import com.base.locationsharewhite.helper.BaseActivity
import com.base.locationsharewhite.location.LocationShareListUtils
......@@ -14,10 +17,16 @@ import com.base.locationsharewhite.location.LocationShareUtils.SHARE_STATE_MYSEL
import com.base.locationsharewhite.location.LocationShareUtils.SHARE_STATE_SHARED
import com.base.locationsharewhite.location.LocationShareUtils.SHARE_STATE_SUCCESS
import com.base.locationsharewhite.location.LocationStatusUtils
import com.base.locationsharewhite.ui.set.RenameActivity
import com.base.locationsharewhite.ui.views.DialogView.showViewerMoreDialog
import com.base.locationsharewhite.utils.BarUtils
import com.base.locationsharewhite.utils.LogEx
import com.base.locationsharewhite.utils.SpStringUtils
import com.base.locationsharewhite.utils.SpStringUtils.VIEWER_NICKNAME_KEY
import com.base.locationsharewhite.utils.SpStringUtils._DEVICE_NICKNAME_
import com.base.locationsharewhite.utils.ToastUtils.toast
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
/**
* 添加谁可以看我
......@@ -66,6 +75,12 @@ class LocationShareActivity : BaseActivity<ActivityLocationShareBinding>() {
toast(it.toString())
}
}
}, renameAction = {
startActivity(Intent(this, RenameActivity::class.java).apply {
putExtra("doWhat", RenameActivity.RENAME_FRIEND_NICK_NAME)
putExtra("device", viewerBean.device)
putExtra("nickName", viewerBean.localNickName ?: viewerBean.nickName)
})
})
}
binding.rv.adapter = adapter
......@@ -121,11 +136,40 @@ class LocationShareActivity : BaseActivity<ActivityLocationShareBinding>() {
private fun initShareData() {
LocationShareListUtils.getShareList(viewerListCallBack = {
val nickList = SpStringUtils.getSpStringList(VIEWER_NICKNAME_KEY)
it.forEach { viewer -> changeLocalNickName(viewer, nickList) }
runOnUiThread {
adapter.submitList(it)
}
})
}
override fun onResume() {
super.onResume()
refreshNickName()
}
@SuppressLint("NotifyDataSetChanged")
private fun refreshNickName() = lifecycleScope.launch(Dispatchers.IO) {
val nickList = SpStringUtils.getSpStringList(VIEWER_NICKNAME_KEY)
adapter.items.map { viewer ->
changeLocalNickName(viewer, nickList)
}
launch(Dispatchers.Main) {
adapter.notifyDataSetChanged()
}
}
private fun changeLocalNickName(viewer: ViewerBean, nickList: List<String>) {
val deviceNicknameSp = nickList.find { it.contains(viewer.device) }
deviceNicknameSp?.let { sp ->
runCatching {
val localNickName = sp.split(_DEVICE_NICKNAME_)[1]
viewer.localNickName = localNickName
}
}
}
}
\ No newline at end of file
......@@ -3,7 +3,6 @@ package com.base.locationsharewhite.ui.main
import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.CompoundButton
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.base.locationsharewhite.R
import com.base.locationsharewhite.bean.ViewerBean
......@@ -23,7 +22,8 @@ class ViewerAdapter(
item ?: return
val binding = ItemViewerBinding.bind(holder.itemView)
runCatching { binding.tvNameLetters.text = item.nickName.substring(0, 1) }
binding.tvNameCode.text = item.nickName
binding.tvNameCode.text = item.localNickName ?: item.nickName
binding.tvTime.text = "time"
binding.tvSwitch.isChecked = item.status == 1
binding.tvSwitch.setOnCheckedChangeListener { buttonView, isChecked ->
......
package com.base.locationsharewhite.ui.set
import android.content.Intent
import android.graphics.Color
import android.view.View
import androidx.core.view.updatePadding
import com.base.locationsharewhite.BuildConfig
import com.base.locationsharewhite.R
import com.base.locationsharewhite.databinding.ActivityRenameBinding
import com.base.locationsharewhite.helper.BaseActivity
import com.base.locationsharewhite.helper.MyApplication
import com.base.locationsharewhite.location.LocationLoginUtils
import com.base.locationsharewhite.location.LocationNameUtils
import com.base.locationsharewhite.utils.AppPreferences
import com.base.locationsharewhite.utils.BarUtils
import com.base.locationsharewhite.utils.ClipboardUtils.copyText
import com.base.locationsharewhite.utils.SpStringUtils
import com.base.locationsharewhite.utils.SpStringUtils.VIEWER_NICKNAME_KEY
import com.base.locationsharewhite.utils.SpStringUtils._DEVICE_NICKNAME_
import com.base.locationsharewhite.utils.ToastUtils.toast
class RenameActivity : BaseActivity<ActivityRenameBinding>() {
......@@ -23,38 +23,96 @@ class RenameActivity : BaseActivity<ActivityRenameBinding>() {
ActivityRenameBinding.inflate(layoutInflater)
}
private var doWhat = RENAME_USER_NAME
private var device: String = ""
private var nickName: String = ""
companion object {
const val RENAME_USER_NAME = "rename_user_name"
const val RENAME_FRIEND_NICK_NAME = "rename_friend_nick_name"
}
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
doWhat = intent?.extras?.getString("doWhat", RENAME_USER_NAME) ?: RENAME_USER_NAME
device = intent?.extras?.getString("device", "") ?: ""
nickName = intent?.extras?.getString("nickName", "") ?: ""
}
override fun initView() {
BarUtils.setStatusBarLightMode(this, true)
BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
binding.root.updatePadding(top = BarUtils.getStatusBarHeight())
doWhat = intent.extras?.getString("doWhat", RENAME_USER_NAME) ?: RENAME_USER_NAME
device = intent?.extras?.getString("device", "") ?: ""
nickName = intent?.extras?.getString("nickName", "") ?: ""
binding.editName.setText(LocationLoginUtils.nickNameSp)
if (BuildConfig.DEBUG) {
binding.tvDevice.visibility = View.VISIBLE
binding.tvDevice.text = AppPreferences.getInstance().getString("uuid", "")
binding.tvDevice.setOnClickListener {
copyText("device", binding.tvDevice.text.toString())
}
if (doWhat == RENAME_USER_NAME) {
binding.editName.setText(LocationLoginUtils.myNickNameSp)
} else {
binding.editName.setText(nickName)
binding.tvEnterTip.text = getString(R.string.enter_friend_nickname)
}
// if (BuildConfig.DEBUG) {
// binding.tvDevice.visibility = View.VISIBLE
// binding.tvDevice.text = AppPreferences.getInstance().getString("uuid", "")
// binding.tvDevice.setOnClickListener {
// copyText("device", binding.tvDevice.text.toString())
// }
// }
}
override fun initListener() {
super.initListener()
binding.tvRename.setOnClickListener {
when (doWhat) {
RENAME_USER_NAME -> {
renameUser()
}
RENAME_FRIEND_NICK_NAME -> {
renameFriendNickName()
}
}
}
}
private fun renameFriendNickName() {
val newName = binding.editName.text.toString()
if (newName.isEmpty()) {
toast(getString(R.string.name_is_empty))
return@setOnClickListener
return
}
val nickList = SpStringUtils.getSpStringList(VIEWER_NICKNAME_KEY)
val olderSp = nickList.find { it.contains(device) }
olderSp?.let { SpStringUtils.deleteSpString(VIEWER_NICKNAME_KEY, it) }
val sp = device + _DEVICE_NICKNAME_ + newName
SpStringUtils.addSpString(VIEWER_NICKNAME_KEY, sp)
toast("Rename success")
finish()
}
private fun renameUser() {
val newName = binding.editName.text.toString()
if (newName.isEmpty()) {
toast(getString(R.string.name_is_empty))
return
}
LocationNameUtils.renameNickName(newName) { flag ->
runCatching {
runOnUiThread {
if (flag) {
toast("Rename success")
} else {
toast("Rename failed")
}
}
finish()
}
}
}
......
......@@ -26,7 +26,7 @@ class SettingActivity : BaseActivity<ActivitySettingBinding>() {
BarUtils.setStatusBarColor(this, Color.WHITE)
binding.root.updatePadding(top = BarUtils.getStatusBarHeight())
binding.tvHelloName.text = getString(R.string.hello) + ",\n" + LocationLoginUtils.nickNameSp
binding.tvHelloName.text = getString(R.string.hello) + ",\n" + LocationLoginUtils.myNickNameSp
}
override fun initListener() {
......
......@@ -122,7 +122,8 @@ object DialogView {
dialog.window?.attributes = params
binding.tvRename.setOnClickListener {
dialog.dismiss()
renameAction?.invoke()
}
binding.tvDelete.setOnClickListener {
dialog.dismiss()
......
package com.base.locationsharewhite.utils
object SpStringUtils {
private val TAG = "SpStringUtils"
const val VIEWER_NICKNAME_KEY = "viewer_nickname_key"
const val _DEVICE_NICKNAME_ = "_device_nickname_"
fun getSpStringList(key: String): List<String> {
val sp = AppPreferences.getInstance().getString(key, "")
return if (sp.equals("")) {
listOf()
} else {
sp.split("|||")
}
}
fun addSpString(key: String, value: String) {
LogEx.logDebug(TAG, "key=$key value=$value")
val list = getSpStringList(key).toMutableList()
list.add(value)
val string = list.joinToString(separator = "|||")
AppPreferences.getInstance().put(key, string)
}
fun deleteSpString(key: String, value: String) {
val list = getSpStringList(key).toMutableList()
list.remove(value)
val string = list.joinToString(separator = "|||")
AppPreferences.getInstance().put(key, string)
}
}
\ No newline at end of file
......@@ -38,6 +38,7 @@
android:orientation="vertical">
<TextView
android:id="@+id/tv_enter_tip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
......
......@@ -59,4 +59,5 @@
<string name="my_code">My Code</string>
<string name="share">Share</string>
<string name="preparing_advertisement"><![CDATA[Preparing advertisement&#8230;]]></string>
<string name="enter_friend_nickname">Enter friend nickname</string>
</resources>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment