From 0bf8114dc59740e9c2f85b0f1191598fb183baff Mon Sep 17 00:00:00 2001 From: squallzhjch Date: Mon, 9 Jan 2023 14:54:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B2=A1=E6=9C=89=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E7=9A=84=E6=8F=90=E7=A4=BA=E9=A1=B5=E9=9D=A2=20?= =?UTF-8?q?=E4=BF=AE=E6=94=B9user=E6=95=B0=E6=8D=AE=E7=9A=84=E4=BD=BF?= =?UTF-8?q?=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../preferences/PreferencesRepository.kt | 1 + .../preferences/PreferencesRepositoryImp.kt | 3 + .../volvo/ui/fragments/home/HomeAdapter.kt | 1 + .../volvo/ui/fragments/home/HomeFragment.kt | 10 +- .../volvo/ui/fragments/home/HomeViewModel.kt | 22 +- .../volvo/ui/fragments/login/LoginFragment.kt | 1 - .../message/ObtainMessageFragment.kt | 207 ++++++++++++------ .../message/ObtainMessageViewModel.kt | 64 ++++-- app/src/main/res/layout/adapter_home.xml | 6 +- app/src/main/res/layout/fragment_login.xml | 7 +- .../res/layout/fragment_obtain_message.xml | 16 +- .../main/res/layout/home_adapter_noting.xml | 12 + 12 files changed, 241 insertions(+), 109 deletions(-) create mode 100644 app/src/main/res/layout/home_adapter_noting.xml diff --git a/app/src/main/java/com/navinfo/volvo/repository/preferences/PreferencesRepository.kt b/app/src/main/java/com/navinfo/volvo/repository/preferences/PreferencesRepository.kt index 202cfd8..d35e07e 100644 --- a/app/src/main/java/com/navinfo/volvo/repository/preferences/PreferencesRepository.kt +++ b/app/src/main/java/com/navinfo/volvo/repository/preferences/PreferencesRepository.kt @@ -1,6 +1,7 @@ package com.navinfo.volvo.repository.preferences import com.navinfo.volvo.model.proto.LoginUser +import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.flow.Flow /** diff --git a/app/src/main/java/com/navinfo/volvo/repository/preferences/PreferencesRepositoryImp.kt b/app/src/main/java/com/navinfo/volvo/repository/preferences/PreferencesRepositoryImp.kt index c7268a8..1bdd0d9 100644 --- a/app/src/main/java/com/navinfo/volvo/repository/preferences/PreferencesRepositoryImp.kt +++ b/app/src/main/java/com/navinfo/volvo/repository/preferences/PreferencesRepositoryImp.kt @@ -7,9 +7,12 @@ import androidx.datastore.preferences.core.edit import androidx.datastore.preferences.core.intPreferencesKey import androidx.datastore.preferences.core.stringPreferencesKey import androidx.datastore.preferences.preferencesDataStore +import androidx.lifecycle.viewModelScope import com.navinfo.volvo.model.proto.LoginUser +import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.map +import kotlinx.coroutines.launch import javax.inject.Inject diff --git a/app/src/main/java/com/navinfo/volvo/ui/fragments/home/HomeAdapter.kt b/app/src/main/java/com/navinfo/volvo/ui/fragments/home/HomeAdapter.kt index 844c329..dda8c4a 100644 --- a/app/src/main/java/com/navinfo/volvo/ui/fragments/home/HomeAdapter.kt +++ b/app/src/main/java/com/navinfo/volvo/ui/fragments/home/HomeAdapter.kt @@ -35,6 +35,7 @@ class HomeAdapter(fragment: Fragment) : holder.onBind(position) } + fun getItemData(position: Int): GreetingMessage { return getItem(position)!! } diff --git a/app/src/main/java/com/navinfo/volvo/ui/fragments/home/HomeFragment.kt b/app/src/main/java/com/navinfo/volvo/ui/fragments/home/HomeFragment.kt index 860dbb1..406475e 100644 --- a/app/src/main/java/com/navinfo/volvo/ui/fragments/home/HomeFragment.kt +++ b/app/src/main/java/com/navinfo/volvo/ui/fragments/home/HomeFragment.kt @@ -14,6 +14,8 @@ import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.LinearLayoutManager import com.navinfo.volvo.R import com.navinfo.volvo.databinding.FragmentHomeBinding +import com.navinfo.volvo.databinding.HomeAdapterNotingBinding +import com.navinfo.volvo.databinding.LoadStateViewBinding import com.navinfo.volvo.tools.DisplayUtil import com.navinfo.volvo.ui.fragments.BaseFragment import com.yanzhenjie.recyclerview.* @@ -33,12 +35,14 @@ class HomeFragment : BaseFragment(), OnItemClickListener, OnItemMenuClickListene private val viewModel by viewModels() private val messageAdapter by lazy { HomeAdapter(this) } - + private var headBinding: HomeAdapterNotingBinding? = null override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View { _binding = FragmentHomeBinding.inflate(inflater, container, false) val root: View = binding.root + + headBinding = HomeAdapterNotingBinding.inflate(inflater, container, false) initView() return root } @@ -95,6 +99,7 @@ class HomeFragment : BaseFragment(), OnItemClickListener, OnItemMenuClickListene binding.homeRecyclerview.setOnItemClickListener(this) //使用下拉加载 // binding.homeRecyclerview.useDefaultLoadMore() // 使用默认的加载更多的View。 + binding.homeRecyclerview.setLoadMoreListener { Log.e("jingo", "下拉加载开始") @@ -118,9 +123,11 @@ class HomeFragment : BaseFragment(), OnItemClickListener, OnItemMenuClickListene messageAdapter.addLoadStateListener { when (it.refresh) { is LoadState.NotLoading -> { + binding.homeRecyclerview.addHeaderView(headBinding!!.root) Log.d("jingo", "is NotLoading") } is LoadState.Loading -> { + binding.homeRecyclerview.removeHeaderView(headBinding!!.root) Log.d("jingo", "is Loading") } is LoadState.Error -> { @@ -164,6 +171,7 @@ class HomeFragment : BaseFragment(), OnItemClickListener, OnItemMenuClickListene override fun onDestroyView() { super.onDestroyView() _binding = null + headBinding = null } override fun onItemClick(view: View?, adapterPosition: Int) { diff --git a/app/src/main/java/com/navinfo/volvo/ui/fragments/home/HomeViewModel.kt b/app/src/main/java/com/navinfo/volvo/ui/fragments/home/HomeViewModel.kt index 4a70292..8b4a0fe 100644 --- a/app/src/main/java/com/navinfo/volvo/ui/fragments/home/HomeViewModel.kt +++ b/app/src/main/java/com/navinfo/volvo/ui/fragments/home/HomeViewModel.kt @@ -12,6 +12,7 @@ import com.navinfo.volvo.model.network.NetworkDeleteMessagePost import com.navinfo.volvo.model.network.NetworkMessageListPost import com.navinfo.volvo.repository.database.DatabaseRepository import com.navinfo.volvo.repository.network.NetworkRepository +import com.navinfo.volvo.repository.preferences.PreferencesRepository import com.navinfo.volvo.util.NetResult import com.navinfo.volvo.util.asLiveData import dagger.hilt.android.lifecycle.HiltViewModel @@ -25,25 +26,35 @@ class HomeViewModel @Inject constructor( private val netRepository: NetworkRepository, private val dataRepository: DatabaseRepository, private val messageDao: GreetingMessageDao, + private val shard: PreferencesRepository ) : ViewModel() { private val _isLoading = MutableLiveData() val isLoading = _isLoading.asLiveData() + // private val _messageList = MutableLiveData>() // val messageList = _messageList.asLiveData() - val messageList: Flow> get() = dataRepository.getMessageByPaging() + lateinit var userName: String + + init { + viewModelScope.launch { + shard.loginUser().collect { + userName = it!!.username + } + } + } fun getNetMessageList() { if (_isLoading.value == true) return _isLoading.postValue(true) viewModelScope.launch { - val messagePost = NetworkMessageListPost(who = "", toWho = "") + val messagePost = NetworkMessageListPost(who = userName, toWho = "") when (val result = netRepository.getMessageList(messagePost)) { is NetResult.Success -> { _isLoading.value = false @@ -70,13 +81,18 @@ class HomeViewModel @Inject constructor( viewModelScope.launch { val post = NetworkDeleteMessagePost(id) netRepository.deleteMessage(post) + messageDao.deleteById(id) when (val result = netRepository.deleteMessage(post)) { is NetResult.Success -> { _isLoading.value = false } is NetResult.Failure -> { _isLoading.value = false - Toast.makeText(application, "${result.code}:${result.msg}", Toast.LENGTH_SHORT) + Toast.makeText( + application, + "服务返回信息:${result.code}:${result.msg}", + Toast.LENGTH_SHORT + ) .show() } is NetResult.Error -> { diff --git a/app/src/main/java/com/navinfo/volvo/ui/fragments/login/LoginFragment.kt b/app/src/main/java/com/navinfo/volvo/ui/fragments/login/LoginFragment.kt index 030bea2..4844f7a 100644 --- a/app/src/main/java/com/navinfo/volvo/ui/fragments/login/LoginFragment.kt +++ b/app/src/main/java/com/navinfo/volvo/ui/fragments/login/LoginFragment.kt @@ -45,7 +45,6 @@ class LoginFragment : BaseFragment() { if (it != null){ viewBinding.loginUser = it } - cancel() } } viewBinding.loginFragmentRegisterButton.setOnClickListener { diff --git a/app/src/main/java/com/navinfo/volvo/ui/fragments/message/ObtainMessageFragment.kt b/app/src/main/java/com/navinfo/volvo/ui/fragments/message/ObtainMessageFragment.kt index dc94c35..aa33d44 100644 --- a/app/src/main/java/com/navinfo/volvo/ui/fragments/message/ObtainMessageFragment.kt +++ b/app/src/main/java/com/navinfo/volvo/ui/fragments/message/ObtainMessageFragment.kt @@ -15,14 +15,18 @@ import android.widget.ArrayAdapter import android.widget.Toast import androidx.core.widget.addTextChangedListener import androidx.fragment.app.Fragment +import androidx.fragment.app.viewModels import androidx.lifecycle.Observer +import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider +import androidx.lifecycle.lifecycleScope import androidx.navigation.Navigation import androidx.navigation.fragment.findNavController import com.easytools.tools.DateUtils import com.easytools.tools.DisplayUtils import com.easytools.tools.FileIOUtils import com.easytools.tools.ResourceUtils +import com.easytools.tools.ThreadPoolUtils.runOnUiThread import com.easytools.tools.ToastUtils import com.elvishew.xlog.XLog import com.google.android.material.dialog.MaterialAlertDialogBuilder @@ -42,8 +46,10 @@ import com.navinfo.volvo.util.PhotoLoader import com.navinfo.volvo.utils.EasyMediaFile import com.navinfo.volvo.utils.SystemConstant import com.nhaarman.supertooltips.ToolTip +import dagger.hilt.android.AndroidEntryPoint import indi.liyi.viewer.Utils import indi.liyi.viewer.ViewData +import kotlinx.coroutines.launch import top.zibin.luban.Luban import top.zibin.luban.OnCompressListener import java.io.File @@ -51,12 +57,10 @@ import java.io.FileInputStream import java.util.* -//@RuntimePermissions -class ObtainMessageFragment: Fragment() { +@AndroidEntryPoint +class ObtainMessageFragment : Fragment() { private var _binding: FragmentObtainMessageBinding? = null - private val obtainMessageViewModel by lazy { - ViewModelProvider(requireActivity()).get(ObtainMessageViewModel::class.java) - } + private val obtainMessageViewModel by viewModels() private val photoHelper by lazy { EasyMediaFile().setCrop(true) } @@ -79,17 +83,17 @@ class ObtainMessageFragment: Fragment() { _binding = FragmentObtainMessageBinding.inflate(inflater, container, false) val root: View = binding.root - obtainMessageViewModel.setCurrentMessage(GreetingMessage()) + obtainMessageViewModel.setCurrentMessage(GreetingMessage(who = obtainMessageViewModel.username)) - obtainMessageViewModel?.getMessageLiveData()?.observe( + obtainMessageViewModel.getMessageLiveData().observe( viewLifecycleOwner, Observer { // 初始化界面显示内容 - if(it.name?.isNotEmpty() == true) + if (it.name?.isNotEmpty() == true) binding.tvMessageTitle?.setText(it.name) if (it.sendDate?.isNotEmpty() == true) { // 获取当前发送时间,如果早于当前时间,则显示现在 val sendDate = DateUtils.str2Date(it.sendDate, dateSendFormat) - if (sendDate<=Date()) { + if (sendDate <= Date()) { binding.btnSendTime.text = "现在" } else { binding.btnSendTime.text = it.sendDate @@ -99,39 +103,47 @@ class ObtainMessageFragment: Fragment() { } var hasPhoto = false var hasAudio = false - if (it.imageUrl!=null&&it.imageUrl?.isNotEmpty() == true) { + if (it.imageUrl != null && it.imageUrl?.isNotEmpty() == true) { hasPhoto = true -// Glide.with(this@ObtainMessageFragment) -// .asBitmap().fitCenter() -// .load(it.imageUrl) -// .diskCacheStrategy(DiskCacheStrategy.ALL) -// .into(binding.imgMessageAttachment) + // Glide.with(this@ObtainMessageFragment) + // .asBitmap().fitCenter() + // .load(it.imageUrl) + // .diskCacheStrategy(DiskCacheStrategy.ALL) + // .into(binding.imgMessageAttachment) // 如果当前attachment文件是本地文件,开始尝试网络上传 val str = it.imageUrl?.replace("\\", "/") - binding.tvPhotoName.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG ) + binding.tvPhotoName.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG) if (!str!!.startsWith("http")) { - obtainMessageViewModel.uploadAttachment(File(it.imageUrl), AttachmentType.PIC) + obtainMessageViewModel.uploadAttachment( + File(it.imageUrl), + AttachmentType.PIC + ) binding.tvPhotoName.text = str.substringAfterLast("/", "picture.jpg") } else { if (str.contains("?")) { - binding.tvPhotoName.text = str.substring(str.lastIndexOf("/")+1, str.indexOf("?")) + binding.tvPhotoName.text = + str.substring(str.lastIndexOf("/") + 1, str.indexOf("?")) } else { binding.tvPhotoName.text = str.substringAfterLast("/") } } } - if (it.mediaUrl!=null&&it.mediaUrl?.isNotEmpty() == true) { + if (it.mediaUrl != null && it.mediaUrl?.isNotEmpty() == true) { hasAudio = true - binding.tvAudioName.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG ) + binding.tvAudioName.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG) // 如果当前attachment文件是本地文件,开始尝试网络上传 val str = it.mediaUrl?.replace("\\", "/") if (!str!!.startsWith("http")) { - obtainMessageViewModel.uploadAttachment(File(it.mediaUrl),AttachmentType.AUDIO) + obtainMessageViewModel.uploadAttachment( + File(it.mediaUrl), + AttachmentType.AUDIO + ) binding.tvAudioName.text = str.substringAfterLast("/", "audio.m4a") } else { if (str.contains("?")) { - binding.tvAudioName.text = str.substring(str.lastIndexOf("/")+1, str.indexOf("?")) + binding.tvAudioName.text = + str.substring(str.lastIndexOf("/") + 1, str.indexOf("?")) } else { binding.tvAudioName.text = str.substringAfterLast("/") } @@ -139,13 +151,16 @@ class ObtainMessageFragment: Fragment() { } binding.layerPhotoResult.visibility = if (hasPhoto) VISIBLE else GONE binding.layerGetPhoto.visibility = if (hasPhoto) GONE else VISIBLE -// binding.imgMessageAttachment.visibility = if (hasPhoto) VISIBLE else GONE + // binding.imgMessageAttachment.visibility = if (hasPhoto) VISIBLE else GONE binding.layerAudioResult.visibility = if (hasAudio) VISIBLE else GONE binding.layerGetAudio.visibility = if (hasAudio) GONE else VISIBLE -// binding.llAudioPlay.visibility = if (hasAudio) VISIBLE else GONE + // binding.llAudioPlay.visibility = if (hasAudio) VISIBLE else GONE } ) + + + lifecycle.addObserver(recorderLifecycleObserver) initView() return root @@ -158,7 +173,7 @@ class ObtainMessageFragment: Fragment() { obtainMessageViewModel.getMessageLiveData().value?.name = it.toString() }) - binding.edtSendFrom.addTextChangedListener (afterTextChanged = { + binding.edtSendFrom.addTextChangedListener(afterTextChanged = { obtainMessageViewModel.getMessageLiveData().value?.who = it.toString() }) @@ -171,9 +186,11 @@ class ObtainMessageFragment: Fragment() { } val sendToArray = mutableListOf("LYVXFEFEXNL754427") - binding.edtSendTo.adapter = ArrayAdapter(context!!, - android.R.layout.simple_dropdown_item_1line, android.R.id.text1, sendToArray) - binding.edtSendTo.onItemSelectedListener = object: OnItemSelectedListener { + binding.edtSendTo.adapter = ArrayAdapter( + context!!, + android.R.layout.simple_dropdown_item_1line, android.R.id.text1, sendToArray + ) + binding.edtSendTo.onItemSelectedListener = object : OnItemSelectedListener { override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) { obtainMessageViewModel.getMessageLiveData().value?.toWho = sendToArray[p2] } @@ -190,9 +207,19 @@ class ObtainMessageFragment: Fragment() { override fun onClickListener(selectTime: String) { val sendDate = DateUtils.str2Date(selectTime, dateShowFormat) if (sendDate <= Date()) { - obtainMessageViewModel.updateMessageSendTime(DateUtils.date2Str(Date(), dateSendFormat)) + obtainMessageViewModel.updateMessageSendTime( + DateUtils.date2Str( + Date(), + dateSendFormat + ) + ) } else { - obtainMessageViewModel.updateMessageSendTime(DateUtils.date2Str(sendDate, dateSendFormat)) + obtainMessageViewModel.updateMessageSendTime( + DateUtils.date2Str( + sendDate, + dateSendFormat + ) + ) } } @@ -210,7 +237,8 @@ class ObtainMessageFragment: Fragment() { override fun onGranted(permissions: MutableList, all: Boolean) { if (!all) { - Toast.makeText(activity, "获取部分权限成功,但部分权限未正常授予", Toast.LENGTH_SHORT).show() + Toast.makeText(activity, "获取部分权限成功,但部分权限未正常授予", Toast.LENGTH_SHORT) + .show() return } // 开始启动拍照界面 @@ -259,18 +287,20 @@ class ObtainMessageFragment: Fragment() { .request(object : OnPermissionCallback { override fun onGranted(permissions: MutableList, all: Boolean) { if (!all) { - Toast.makeText(activity, "获取部分权限成功,但部分权限未正常授予", Toast.LENGTH_SHORT).show() + Toast.makeText(activity, "获取部分权限成功,但部分权限未正常授予", Toast.LENGTH_SHORT) + .show() return } - when(motionEvent.action) { - MotionEvent.ACTION_DOWN-> { + when (motionEvent.action) { + MotionEvent.ACTION_DOWN -> { // 申请权限 recorderLifecycleObserver.initAndStartRecorder() ToastUtils.showToast("开始录音!") false } MotionEvent.ACTION_UP -> { - val recorderAudioPath = recorderLifecycleObserver.stopAndReleaseRecorder() + val recorderAudioPath = + recorderLifecycleObserver.stopAndReleaseRecorder() if (File(recorderAudioPath).exists()) { obtainMessageViewModel.updateMessageAudio(recorderAudioPath) } @@ -300,7 +330,7 @@ class ObtainMessageFragment: Fragment() { photoHelper.setCallback { if (it.exists()) { val fileName = it.name.lowercase() - if (fileName.endsWith(".jpg")||fileName.endsWith(".jpeg")||fileName.endsWith(".png")) { + if (fileName.endsWith(".jpg") || fileName.endsWith(".jpeg") || fileName.endsWith(".png")) { // 获取选中的图片,自动压缩图片质量 // 压缩图片文件 Luban.with(context) @@ -318,19 +348,31 @@ class ObtainMessageFragment: Fragment() { override fun onSuccess(file: File?) { XLog.d("压缩图片成功:${file?.absolutePath}") - // 删除源文件 - if (!it.absolutePath.equals(file?.absolutePath)) { - it?.delete() - } + // 删除源文件 + if (!it.absolutePath.equals(file?.absolutePath)) { + it?.delete() + } // 如果当前文件不在camera缓存文件夹下,则移动该文件 if (!file!!.parentFile.absolutePath.equals(SystemConstant.CameraFolder)) { - val copyResult = FileIOUtils.writeFileFromIS(File(SystemConstant.CameraFolder, fileName), FileInputStream(file)) - XLog.e("拷贝结果:"+copyResult) + val copyResult = FileIOUtils.writeFileFromIS( + File( + SystemConstant.CameraFolder, + fileName + ), FileInputStream(file) + ) + XLog.e("拷贝结果:" + copyResult) // 跳转回原Fragment,展示拍摄的照片 - ViewModelProvider(requireActivity()).get(ObtainMessageViewModel::class.java).updateMessagePic(File(SystemConstant.CameraFolder, fileName).absolutePath) + ViewModelProvider(requireActivity()).get(ObtainMessageViewModel::class.java) + .updateMessagePic( + File( + SystemConstant.CameraFolder, + fileName + ).absolutePath + ) } else { // 跳转回原Fragment,展示拍摄的照片 - ViewModelProvider(requireActivity()).get(ObtainMessageViewModel::class.java).updateMessagePic(file!!.absolutePath) + ViewModelProvider(requireActivity()).get(ObtainMessageViewModel::class.java) + .updateMessagePic(file!!.absolutePath) } } @@ -338,12 +380,23 @@ class ObtainMessageFragment: Fragment() { XLog.d("压缩图片失败:${e.message}") } }).launch() - } else if (fileName.endsWith(".mp3")||fileName.endsWith(".wav")||fileName.endsWith(".amr")||fileName.endsWith(".m4a")) { + } else if (fileName.endsWith(".mp3") || fileName.endsWith(".wav") || fileName.endsWith( + ".amr" + ) || fileName.endsWith(".m4a") + ) { ToastUtils.showToast(it.absolutePath) if (!it.parentFile.parentFile.absolutePath.equals(SystemConstant.SoundFolder)) { - val copyResult = FileIOUtils.writeFileFromIS(File(SystemConstant.SoundFolder, fileName), FileInputStream(it)) - XLog.e("拷贝结果:"+copyResult) - obtainMessageViewModel.updateMessageAudio(File(SystemConstant.SoundFolder, fileName).absolutePath) + val copyResult = FileIOUtils.writeFileFromIS( + File(SystemConstant.SoundFolder, fileName), + FileInputStream(it) + ) + XLog.e("拷贝结果:" + copyResult) + obtainMessageViewModel.updateMessageAudio( + File( + SystemConstant.SoundFolder, + fileName + ).absolutePath + ) } else { obtainMessageViewModel.updateMessageAudio(it.absolutePath) } @@ -356,12 +409,14 @@ class ObtainMessageFragment: Fragment() { } binding.tvAudioName.setOnClickListener { - binding.llAudioPlay.visibility = if (binding.llAudioPlay.visibility == VISIBLE) GONE else VISIBLE + binding.llAudioPlay.visibility = + if (binding.llAudioPlay.visibility == VISIBLE) GONE else VISIBLE // 判断当前播放的文件是否在缓存文件夹内,如果不在首先下载该文件 val fileUrl = obtainMessageViewModel.getMessageLiveData().value!!.mediaUrl!! - val localFile = obtainMessageViewModel.getLocalFileFromNetUrl(fileUrl, AttachmentType.AUDIO) + val localFile = + obtainMessageViewModel.getLocalFileFromNetUrl(fileUrl, AttachmentType.AUDIO) if (!localFile.exists()) { - obtainMessageViewModel.downLoadFile(fileUrl, localFile, object: DownloadCallback { + obtainMessageViewModel.downLoadFile(fileUrl, localFile, object : DownloadCallback { override fun progress(progress: Int) { } @@ -379,7 +434,7 @@ class ObtainMessageFragment: Fragment() { } binding.btnObtainMessageBack.setOnClickListener { - Navigation.findNavController(it).popBackStack() + findNavController().popBackStack() } binding.btnObtainMessageConfirm.setOnClickListener { @@ -400,7 +455,7 @@ class ObtainMessageFragment: Fragment() { toolTipRelativeLayout.showToolTipForView(toolTip, binding.tiLayoutTitle) checkResult = false } else { - if (messageData?.name!!.length>10) { + if (messageData?.name!!.length > 10) { val toolTipRelativeLayout = binding.ttTitle val toolTip = ToolTip() @@ -438,7 +493,7 @@ class ObtainMessageFragment: Fragment() { checkResult = false } - if (messageData?.who?.isEmpty()==true) { + if (messageData?.who?.isEmpty() == true) { val toolTipRelativeLayout = binding.ttSendFrom val toolTip = ToolTip() @@ -450,7 +505,7 @@ class ObtainMessageFragment: Fragment() { toolTipRelativeLayout.showToolTipForView(toolTip, binding.edtSendFrom) checkResult = false } - if (messageData?.toWho?.isEmpty()==true) { + if (messageData?.toWho?.isEmpty() == true) { val toolTipRelativeLayout = binding.ttSendTo val toolTip = ToolTip() @@ -471,22 +526,30 @@ class ObtainMessageFragment: Fragment() { localAttachmentList.add(imageAttachment) } if (messageData?.mediaUrl?.startsWith("http") == false) { - val audioAttachment = Attachment("", messageData.mediaUrl!!, AttachmentType.AUDIO) + val audioAttachment = + Attachment("", messageData.mediaUrl!!, AttachmentType.AUDIO) localAttachmentList.add(audioAttachment) } if (localAttachmentList.isNotEmpty()) { MaterialAlertDialogBuilder(context!!) .setTitle("提示") .setMessage("当前照片及音频内容需首先上传,是否尝试上传?") - .setPositiveButton("确定", DialogInterface.OnClickListener { dialogInterface, i -> - dialogInterface.dismiss() - for (attachment in localAttachmentList) { - obtainMessageViewModel.uploadAttachment(File(attachment.pathUrl), attachment.attachmentType) - } - }) - .setNegativeButton("取消", DialogInterface.OnClickListener { - dialogInterface, i -> dialogInterface.dismiss() - }) + .setPositiveButton( + "确定", + DialogInterface.OnClickListener { dialogInterface, i -> + dialogInterface.dismiss() + for (attachment in localAttachmentList) { + obtainMessageViewModel.uploadAttachment( + File(attachment.pathUrl), + attachment.attachmentType + ) + } + }) + .setNegativeButton( + "取消", + DialogInterface.OnClickListener { dialogInterface, i -> + dialogInterface.dismiss() + }) .show() return@setOnClickListener } @@ -495,14 +558,14 @@ class ObtainMessageFragment: Fragment() { val sendDate = DateUtils.str2Date(messageData?.sendDate, dateSendFormat) val cal = Calendar.getInstance() cal.time = Date() - cal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE)+1) + cal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE) + 1) if (cal.time.time < sendDate.time) { // 发送时间设置小于当前时间1分钟前,Toast提示用户并自动设置发送时间 messageData?.sendDate = DateUtils.date2Str(cal.time, dateSendFormat) ToastUtils.showToast("自动调整发送时间为1分钟后发送") } // 开始网络提交数据 - if (obtainMessageViewModel.getMessageLiveData().value?.id==0L) { // 如果网络id为空,则调用更新操作 + if (obtainMessageViewModel.getMessageLiveData().value?.id == 0L) { // 如果网络id为空,则调用更新操作 obtainMessageViewModel.insertCardByApp(confirmCallback) } else { obtainMessageViewModel.updateCardByApp(confirmCallback) @@ -514,7 +577,8 @@ class ObtainMessageFragment: Fragment() { val viewData = ViewData() viewData.imageSrc = obtainMessageViewModel.getMessageLiveData().value!!.imageUrl viewData.targetX = Utils.dp2px(context, 10F).toFloat() - viewData.targetWidth = DisplayUtils.getScreenWidthPixels(activity) - Utils.dp2px(context, 20F) + viewData.targetWidth = + DisplayUtils.getScreenWidthPixels(activity) - Utils.dp2px(context, 20F) viewData.targetHeight = Utils.dp2px(context, 200F) val viewDataList = listOf(viewData) binding.imageViewer.overlayStatusBar(true) // ImageViewer 是否会占据 StatusBar 的空间 @@ -524,11 +588,13 @@ class ObtainMessageFragment: Fragment() { .watch(0) // 开启浏览 } + binding.edtSendFrom.setText(obtainMessageViewModel.username) + } - val confirmCallback = object: ObtainMessageViewModel.MyConfirmCallback { + val confirmCallback = object : ObtainMessageViewModel.MyConfirmCallback { override fun onSucess() { - findNavController().navigate(R.id.navigation_home) + findNavController().popBackStack() } } @@ -569,6 +635,7 @@ class ObtainMessageFragment: Fragment() { }) .show() } + fun onRecorderDenied() { ToastUtils.showToast("当前操作需要您授权录音权限!") } diff --git a/app/src/main/java/com/navinfo/volvo/ui/fragments/message/ObtainMessageViewModel.kt b/app/src/main/java/com/navinfo/volvo/ui/fragments/message/ObtainMessageViewModel.kt index e862707..49a8e7d 100644 --- a/app/src/main/java/com/navinfo/volvo/ui/fragments/message/ObtainMessageViewModel.kt +++ b/app/src/main/java/com/navinfo/volvo/ui/fragments/message/ObtainMessageViewModel.kt @@ -1,36 +1,48 @@ package com.navinfo.volvo.ui.fragments.message -import android.security.ConfirmationCallback -import androidx.lifecycle.* +import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope import com.easytools.tools.FileIOUtils import com.easytools.tools.FileUtils import com.easytools.tools.ToastUtils import com.elvishew.xlog.XLog -import com.navinfo.volvo.database.entity.Attachment import com.navinfo.volvo.database.entity.AttachmentType import com.navinfo.volvo.database.entity.GreetingMessage import com.navinfo.volvo.http.DownloadCallback import com.navinfo.volvo.http.DownloadManager import com.navinfo.volvo.http.DownloadState import com.navinfo.volvo.http.NavinfoVolvoCall +import com.navinfo.volvo.model.proto.LoginUser +import com.navinfo.volvo.repository.preferences.PreferencesRepository import com.navinfo.volvo.utils.SystemConstant -import kotlinx.coroutines.flow.Flow +import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.launch -import okhttp3.MediaType import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.MultipartBody import okhttp3.RequestBody import java.io.File import java.io.FileInputStream -import java.util.* import javax.inject.Inject - -class ObtainMessageViewModel @Inject constructor(): ViewModel() { +@HiltViewModel +class ObtainMessageViewModel @Inject constructor( + private val pre: PreferencesRepository +) : ViewModel() { private val msgLiveData: MutableLiveData by lazy { MutableLiveData() } + lateinit var username: String + + init { + viewModelScope.launch { + pre.loginUser().collect { + username = it!!.username + } + } + } + fun setCurrentMessage(msg: GreetingMessage) { msgLiveData.postValue(msg) } @@ -131,7 +143,11 @@ class ObtainMessageViewModel @Inject constructor(): ViewModel() { try { val requestFile: RequestBody = RequestBody.create("multipart/form-data".toMediaTypeOrNull(), attachmentFile) - val body = MultipartBody.Part.createFormData("picture", attachmentFile.getName(), requestFile) + val body = MultipartBody.Part.createFormData( + "picture", + attachmentFile.getName(), + requestFile + ) val result = NavinfoVolvoCall.getApi().uploadAttachment(body) XLog.d(result.code) if (result.code == 200) { // 请求成功 @@ -144,17 +160,19 @@ class ObtainMessageViewModel @Inject constructor(): ViewModel() { if (destFile.exists()) { FileUtils.deleteFile(destFile) } - val copyResult = FileIOUtils.writeFileFromIS(destFile, FileInputStream(attachmentFile)) - XLog.e("拷贝结果:"+copyResult) + val copyResult = + FileIOUtils.writeFileFromIS(destFile, FileInputStream(attachmentFile)) + XLog.e("拷贝结果:" + copyResult) } else { val destFile = File(SystemConstant.SoundFolder, newFileName) if (destFile.exists()) { FileUtils.deleteFile(destFile) } - val copyResult = FileIOUtils.writeFileFromIS(destFile, FileInputStream(attachmentFile)) - XLog.e("拷贝结果:"+copyResult) + val copyResult = + FileIOUtils.writeFileFromIS(destFile, FileInputStream(attachmentFile)) + XLog.e("拷贝结果:" + copyResult) } - if (fileKey!=null) { + if (fileKey != null) { downloadAttachment(fileKey, attachmentType) } } else { @@ -178,7 +196,7 @@ class ObtainMessageViewModel @Inject constructor(): ViewModel() { if (result.code == 200) { // 请求成功 // 获取上传后的结果 val imageUrl = result.data - if (imageUrl!=null) { + if (imageUrl != null) { XLog.d("downloadAttachment-imageUrl:${imageUrl}") // 获取到图片的网络地址 if (attachmentType == AttachmentType.PIC) { @@ -197,7 +215,7 @@ class ObtainMessageViewModel @Inject constructor(): ViewModel() { } } - fun downLoadFile(url: String, destFile: File, downloadCallback: DownloadCallback){ + fun downLoadFile(url: String, destFile: File, downloadCallback: DownloadCallback) { viewModelScope.launch { DownloadManager.download( url, @@ -234,7 +252,8 @@ class ObtainMessageViewModel @Inject constructor(): ViewModel() { "toWho" to message?.toWho, "sendDate" to message?.sendDate ) - val result = NavinfoVolvoCall.getApi().insertCardByApp(insertData as Map) + val result = + NavinfoVolvoCall.getApi().insertCardByApp(insertData as Map) XLog.d("insertCardByApp:${result.code}") if (result.code == 200) { // 请求成功 // 获取上传后的结果 @@ -266,7 +285,8 @@ class ObtainMessageViewModel @Inject constructor(): ViewModel() { "toWho" to message?.toWho, "sendDate" to message?.sendDate ) - val result = NavinfoVolvoCall.getApi().updateCardByApp(updateData as Map) + val result = + NavinfoVolvoCall.getApi().updateCardByApp(updateData as Map) XLog.d("updateCardByApp:${result.code}") if (result.code == 200) { // 请求成功 // 数据更新成功 @@ -286,14 +306,14 @@ class ObtainMessageViewModel @Inject constructor(): ViewModel() { /** * 根据网络地址获取本地的缓存文件路径 * */ - fun getLocalFileFromNetUrl(url: String, attachmentType: AttachmentType):File { + fun getLocalFileFromNetUrl(url: String, attachmentType: AttachmentType): File { if (url.startsWith("http")) { - val folder = when(attachmentType) { - AttachmentType.PIC-> SystemConstant.CameraFolder + val folder = when (attachmentType) { + AttachmentType.PIC -> SystemConstant.CameraFolder else -> SystemConstant.SoundFolder } var name = if (url.contains("?")) { - url.substring(url.lastIndexOf("/")+1, url.indexOf("?")) + url.substring(url.lastIndexOf("/") + 1, url.indexOf("?")) } else { url.substringAfterLast("/") } diff --git a/app/src/main/res/layout/adapter_home.xml b/app/src/main/res/layout/adapter_home.xml index 7a24c31..a1a666e 100644 --- a/app/src/main/res/layout/adapter_home.xml +++ b/app/src/main/res/layout/adapter_home.xml @@ -17,12 +17,14 @@ android:layout_height="wrap_content"> - diff --git a/app/src/main/res/layout/fragment_login.xml b/app/src/main/res/layout/fragment_login.xml index 581dde0..f468b7a 100644 --- a/app/src/main/res/layout/fragment_login.xml +++ b/app/src/main/res/layout/fragment_login.xml @@ -14,10 +14,11 @@ android:layout_height="match_parent" tools:context="com.navinfo.volvo.ui.fragments.login.LoginFragment"> - + tools:context="com.navinfo.volvo.ui.fragments.message.ObtainMessageFragment"> + + - \ No newline at end of file diff --git a/app/src/main/res/layout/home_adapter_noting.xml b/app/src/main/res/layout/home_adapter_noting.xml new file mode 100644 index 0000000..56ba3de --- /dev/null +++ b/app/src/main/res/layout/home_adapter_noting.xml @@ -0,0 +1,12 @@ + + + + + \ No newline at end of file