removed some comments, modified indentation, moved some strings to companion object

Signed-off-by: Giacomo Pacini <giacomopacini98@gmail.com>
This commit is contained in:
Giacomo Pacini 2024-03-12 21:13:16 +01:00
parent ca5a379883
commit 064c3809b0
No known key found for this signature in database
GPG Key ID: 2FBC97406B43D889

View File

@ -420,11 +420,6 @@ class ChatActivity :
val typingParticipants = HashMap<String, TypingParticipant>() val typingParticipants = HashMap<String, TypingParticipant>()
var callStarted = false var callStarted = false
private val CURRENT_AUDIO_MESSAGE_KEY = "CURRENT_AUDIO_MESSAGE"
private val CURRENT_AUDIO_POSITION_KEY = "CURRENT_AUDIO_POSITION"
private val CURRENT_AUDIO_WAS_PLAYING_KEY = "CURRENT_AUDIO_PLAYING"
private var RESUME_AUDIO_TAG = "RESUME_AUDIO_TAG"
private var voiceMessageToRestoreId = "" private var voiceMessageToRestoreId = ""
private var voiceMessageToRestoreAudioPosition = 0 private var voiceMessageToRestoreAudioPosition = 0
private var voiceMessageToRestoreWasPlaying = false private var voiceMessageToRestoreWasPlaying = false
@ -505,21 +500,20 @@ class ChatActivity :
var voiceMessageId = savedInstanceState.getString(CURRENT_AUDIO_MESSAGE_KEY, "") var voiceMessageId = savedInstanceState.getString(CURRENT_AUDIO_MESSAGE_KEY, "")
var voiceMessagePosition = savedInstanceState.getInt(CURRENT_AUDIO_POSITION_KEY, 0) var voiceMessagePosition = savedInstanceState.getInt(CURRENT_AUDIO_POSITION_KEY, 0)
var wasAudioPLaying = savedInstanceState.getBoolean(CURRENT_AUDIO_WAS_PLAYING_KEY, false) var wasAudioPLaying = savedInstanceState.getBoolean(CURRENT_AUDIO_WAS_PLAYING_KEY, false)
if( ! voiceMessageId.equals("")) { if (!voiceMessageId.equals("")) {
Log.d(RESUME_AUDIO_TAG, "restored voice messageID: " + voiceMessageId) Log.d(RESUME_AUDIO_TAG, "restored voice messageID: " + voiceMessageId)
Log.d(RESUME_AUDIO_TAG, "audio position: " + voiceMessagePosition) Log.d(RESUME_AUDIO_TAG, "audio position: " + voiceMessagePosition)
Log.d(RESUME_AUDIO_TAG, "audio was playing: " + wasAudioPLaying.toString()) Log.d(RESUME_AUDIO_TAG, "audio was playing: " + wasAudioPLaying.toString())
voiceMessageToRestoreId = voiceMessageId voiceMessageToRestoreId = voiceMessageId
voiceMessageToRestoreAudioPosition = voiceMessagePosition voiceMessageToRestoreAudioPosition = voiceMessagePosition
voiceMessageToRestoreWasPlaying = wasAudioPLaying voiceMessageToRestoreWasPlaying = wasAudioPLaying
}else{ } else {
Log.d(RESUME_AUDIO_TAG, "stored voice message id is empty, not resuming audio playing") Log.d(RESUME_AUDIO_TAG, "stored voice message id is empty, not resuming audio playing")
voiceMessageToRestoreId = "" voiceMessageToRestoreId = ""
voiceMessageToRestoreAudioPosition = 0 voiceMessageToRestoreAudioPosition = 0
voiceMessageToRestoreWasPlaying = false voiceMessageToRestoreWasPlaying = false
} }
} else {
}else{
voiceMessageToRestoreId = "" voiceMessageToRestoreId = ""
voiceMessageToRestoreAudioPosition = 0 voiceMessageToRestoreAudioPosition = 0
voiceMessageToRestoreWasPlaying = false voiceMessageToRestoreWasPlaying = false
@ -586,16 +580,15 @@ class ChatActivity :
} }
override fun onSaveInstanceState(outState: Bundle) { override fun onSaveInstanceState(outState: Bundle) {
if(currentlyPlayedVoiceMessage != null) { if (currentlyPlayedVoiceMessage != null) {
// stores audio message ID and audio position outState.putString(CURRENT_AUDIO_MESSAGE_KEY, currentlyPlayedVoiceMessage!!.getId())
// so that can be restored in resumeAudioPlaybackIfNeeded method
outState.putString( CURRENT_AUDIO_MESSAGE_KEY, currentlyPlayedVoiceMessage!!.getId())
outState.putInt(CURRENT_AUDIO_POSITION_KEY, currentlyPlayedVoiceMessage!!.voiceMessagePlayedSeconds) outState.putInt(CURRENT_AUDIO_POSITION_KEY, currentlyPlayedVoiceMessage!!.voiceMessagePlayedSeconds)
outState.putBoolean(CURRENT_AUDIO_WAS_PLAYING_KEY, currentlyPlayedVoiceMessage!!.isPlayingVoiceMessage) outState.putBoolean(CURRENT_AUDIO_WAS_PLAYING_KEY, currentlyPlayedVoiceMessage!!.isPlayingVoiceMessage)
Log.d(RESUME_AUDIO_TAG, "Stored current audio message ID: " + currentlyPlayedVoiceMessage!!.getId()) Log.d(RESUME_AUDIO_TAG, "Stored current audio message ID: " + currentlyPlayedVoiceMessage!!.getId())
Log.d(RESUME_AUDIO_TAG, "Audio Position: " + currentlyPlayedVoiceMessage!!.voiceMessagePlayedSeconds Log.d(
.toString() + " | isPLaying: " + currentlyPlayedVoiceMessage!!.isPlayingVoiceMessage) RESUME_AUDIO_TAG, "Audio Position: " + currentlyPlayedVoiceMessage!!.voiceMessagePlayedSeconds
// stores also audio currently playing status .toString() + " | isPLaying: " + currentlyPlayedVoiceMessage!!.isPlayingVoiceMessage
)
} }
super.onSaveInstanceState(outState) super.onSaveInstanceState(outState)
} }
@ -610,10 +603,6 @@ class ChatActivity :
if (mediaRecorderState == MediaRecorderState.RECORDING) { if (mediaRecorderState == MediaRecorderState.RECORDING) {
stopAudioRecording() stopAudioRecording()
} }
//if (currentlyPlayedVoiceMessage != null) {
// stopMediaPlayer(currentlyPlayedVoiceMessage!!)
//} this is done also in onDestroy,
// it is better to continue audio playback when the activity is not visible but still open
val text = binding.messageInputView.messageInput.text.toString() val text = binding.messageInputView.messageInput.text.toString()
val cursor = binding.messageInputView.messageInput.selectionStart val cursor = binding.messageInputView.messageInput.selectionStart
val previous = context.getSharedPreferences(localClassName, MODE_PRIVATE).getString(roomToken, "null") val previous = context.getSharedPreferences(localClassName, MODE_PRIVATE).getString(roomToken, "null")
@ -1399,7 +1388,7 @@ class ChatActivity :
} }
} }
private fun setUpWaveform(message: ChatMessage, thenPlay : Boolean = true) { private fun setUpWaveform(message: ChatMessage, thenPlay: Boolean = true) {
val filename = message.selectedIndividualHashMap!!["name"] val filename = message.selectedIndividualHashMap!!["name"]
val file = File(context.cacheDir, filename!!) val file = File(context.cacheDir, filename!!)
if (file.exists() && message.voiceMessageFloatArray == null) { if (file.exists() && message.voiceMessageFloatArray == null) {
@ -2368,7 +2357,7 @@ class ChatActivity :
} }
} }
private fun startPlayback(message: ChatMessage, doPlay : Boolean = true) { private fun startPlayback(message: ChatMessage, doPlay: Boolean = true) {
if (!active) { if (!active) {
// don't begin to play voice message if screen is not visible anymore. // don't begin to play voice message if screen is not visible anymore.
// this situation might happen if file is downloading but user already left the chatview. // this situation might happen if file is downloading but user already left the chatview.
@ -3811,7 +3800,6 @@ class ChatActivity :
scrollToRequestedMessageIfNeeded() scrollToRequestedMessageIfNeeded()
//FENOM: add here audio resume policy //FENOM: add here audio resume policy
resumeAudioPlaybackIfNeeded() resumeAudioPlaybackIfNeeded()
} }
private fun scrollToFirstUnreadMessage() { private fun scrollToFirstUnreadMessage() {
@ -3923,8 +3911,8 @@ class ChatActivity :
* and eventually resumes audio playback * and eventually resumes audio playback
* @author Giacomo Pacini * @author Giacomo Pacini
*/ */
private fun resumeAudioPlaybackIfNeeded(){ private fun resumeAudioPlaybackIfNeeded() {
if( ! voiceMessageToRestoreId.equals("")) { if (!voiceMessageToRestoreId.equals("")) {
Log.d(RESUME_AUDIO_TAG, "begin method to resume audio playback") Log.d(RESUME_AUDIO_TAG, "begin method to resume audio playback")
if (adapter != null) { if (adapter != null) {
@ -3932,7 +3920,7 @@ class ChatActivity :
val voiceMessagePosition = adapter!!.items!!.indexOfFirst { val voiceMessagePosition = adapter!!.items!!.indexOfFirst {
it.item is ChatMessage && (it.item as ChatMessage).id == voiceMessageToRestoreId it.item is ChatMessage && (it.item as ChatMessage).id == voiceMessageToRestoreId
} }
if(voiceMessagePosition >= 0) { if (voiceMessagePosition >= 0) {
val currentItem = adapter?.items?.get(voiceMessagePosition)?.item val currentItem = adapter?.items?.get(voiceMessagePosition)?.item
if (currentItem is ChatMessage && currentItem.id == voiceMessageToRestoreId) { if (currentItem is ChatMessage && currentItem.id == voiceMessageToRestoreId) {
currentlyPlayedVoiceMessage = currentItem currentlyPlayedVoiceMessage = currentItem
@ -3947,19 +3935,21 @@ class ChatActivity :
} else { } else {
Log.d(RESUME_AUDIO_TAG, "currentItem retrieved was not chatmessage or its id was not correct") Log.d(RESUME_AUDIO_TAG, "currentItem retrieved was not chatmessage or its id was not correct")
} }
}else{ } else {
Log.d(RESUME_AUDIO_TAG, "voiceMessagePosition is -1, adapter # of items: " + adapter!!.getItemCount()) Log.d(
RESUME_AUDIO_TAG,
"voiceMessagePosition is -1, adapter # of items: " + adapter!!.getItemCount()
)
} }
} else { } else {
Log.d(RESUME_AUDIO_TAG, "TalkMessagesListAdapater is null") Log.d(RESUME_AUDIO_TAG, "TalkMessagesListAdapater is null")
} }
}else{ } else {
Log.d(RESUME_AUDIO_TAG, "No voice message to restore") Log.d(RESUME_AUDIO_TAG, "No voice message to restore")
} }
voiceMessageToRestoreId = "" voiceMessageToRestoreId = ""
voiceMessageToRestoreAudioPosition = 0 voiceMessageToRestoreAudioPosition = 0
voiceMessageToRestoreWasPlaying = false voiceMessageToRestoreWasPlaying = false
} }
private fun scrollToRequestedMessageIfNeeded() { private fun scrollToRequestedMessageIfNeeded() {
@ -4975,5 +4965,9 @@ class ChatActivity :
private const val MILISEC_15: Long = 15 private const val MILISEC_15: Long = 15
private const val LINEBREAK = "\n" private const val LINEBREAK = "\n"
private const val CURSOR_KEY = "_cursor" private const val CURSOR_KEY = "_cursor"
private const val CURRENT_AUDIO_MESSAGE_KEY = "CURRENT_AUDIO_MESSAGE"
private const val CURRENT_AUDIO_POSITION_KEY = "CURRENT_AUDIO_POSITION"
private const val CURRENT_AUDIO_WAS_PLAYING_KEY = "CURRENT_AUDIO_PLAYING"
private const val RESUME_AUDIO_TAG = "RESUME_AUDIO_TAG"
} }
} }