Skip to content

Commit

Permalink
Merge pull request #5 from gdonisi/v0.1.4
Browse files Browse the repository at this point in the history
Temporary workaround for app crashing
  • Loading branch information
certified84 committed Feb 25, 2022
2 parents 812b04a + c3e7f86 commit 5f0e17d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 44 deletions.
59 changes: 24 additions & 35 deletions app/src/main/java/com/certified/audionote/ui/EditNoteFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,6 @@ class EditNoteFragment : Fragment(), View.OnClickListener, DatePickerDialog.OnDa
}

tvTitle.text = getString(R.string.edit_note)
etNoteTitle.apply {
// inputType = InputType.TYPE_NULL
keyListener = null
setOnClickListener { showToast(requireContext().getString(R.string.cant_edit_note_title)) }
}
btnRecord.setImageDrawable(
ResourcesCompat.getDrawable(
resources,
Expand Down Expand Up @@ -257,30 +252,26 @@ class EditNoteFragment : Fragment(), View.OnClickListener, DatePickerDialog.OnDa
}

private fun onClickWhenIdIsZero(p0: View?) {
val context = requireContext()
binding.apply {
when (p0) {
btnRecord -> {
if (!isRecording) {
if (hasPermission(requireContext(), Manifest.permission.RECORD_AUDIO))
if (etNoteTitle.text.toString().isNotBlank())
btnRecord.setImageDrawable(
ResourcesCompat.getDrawable(
resources,
R.drawable.ic_mic_recording,
null
)
).run {
isRecording = true
startRecording()
}
else {
showToast(requireContext().getString(R.string.title_required))
etNoteTitle.requestFocus()
if (hasPermission(context, Manifest.permission.RECORD_AUDIO))
btnRecord.setImageDrawable(
ResourcesCompat.getDrawable(
resources,
R.drawable.ic_mic_recording,
null
)
).run {
isRecording = true
startRecording()
}
else
requestPermission(
requireActivity(),
requireContext().getString(R.string.permission_required),
context.getString(R.string.permission_required),
MainActivity.RECORD_AUDIO_PERMISSION_CODE,
Manifest.permission.RECORD_AUDIO
)
Expand All @@ -299,17 +290,21 @@ class EditNoteFragment : Fragment(), View.OnClickListener, DatePickerDialog.OnDa
fabSaveNote -> {
if (etNoteTitle.text.toString().isNotBlank()) {
stopRecording()
if (_note.audioLength <= 0) {
showToast(context.getString(R.string.record_note_before_saving))
return
}
val note = _note.copy(
title = etNoteTitle.text.toString().trim(),
description = etNoteDescription.text.toString().trim()
)
viewModel.insertNote(note)
showToast(requireContext().getString(R.string.note_saved))
showToast(context.getString(R.string.note_saved))
if (pickedDateTime?.timeInMillis != null && pickedDateTime!!.timeInMillis <= currentDateTime.timeInMillis)
startAlarm(requireContext(), pickedDateTime!!.timeInMillis, note)
startAlarm(context, pickedDateTime!!.timeInMillis, note)
navController.navigate(R.id.action_editNoteFragment_to_homeFragment)
} else {
showToast(requireContext().getString(R.string.title_required))
showToast(context.getString(R.string.title_required))
etNoteTitle.requestFocus()
}
}
Expand Down Expand Up @@ -374,7 +369,7 @@ class EditNoteFragment : Fragment(), View.OnClickListener, DatePickerDialog.OnDa
}

private fun launchDeleteNoteDialog(context: Context) {
val materialDialog = MaterialAlertDialogBuilder(requireContext())
val materialDialog = MaterialAlertDialogBuilder(context)
materialDialog.apply {
setTitle(context.getString(R.string.delete_note))
setMessage("${context.getString(R.string.confirm_deletion)} ${_note.title}?")
Expand All @@ -390,7 +385,7 @@ class EditNoteFragment : Fragment(), View.OnClickListener, DatePickerDialog.OnDa

private fun startRecording() {
val filePath = filePath(requireActivity())
val fileName = "${binding.etNoteTitle.text.toString().trim()}.3gp"
val fileName = "${System.currentTimeMillis()}.3gp"
_note.filePath = "$filePath/$fileName"
showToast(requireContext().getString(R.string.started_recording))

Expand All @@ -410,7 +405,6 @@ class EditNoteFragment : Fragment(), View.OnClickListener, DatePickerDialog.OnDa
prepare()
start()
stopWatch!!.start()
disableNoteTitleEdit()
} catch (e: IOException) {
showToast(requireContext().getString(R.string.error_occurred))
}
Expand All @@ -429,9 +423,11 @@ class EditNoteFragment : Fragment(), View.OnClickListener, DatePickerDialog.OnDa
reset()
}
stopWatch = null
if (_note.audioLength <= 0)
return
val file = File(_note.filePath)
val fileByte = (file.readBytes().size.toDouble() / 1048576.00)
val fileSize = roundOffDecimal(fileByte).toString()
val fileSize = roundOffDecimal(fileByte)
_note.size = fileSize
showToast(requireContext().getString(R.string.stopped_recording))
}
Expand Down Expand Up @@ -470,13 +466,6 @@ class EditNoteFragment : Fragment(), View.OnClickListener, DatePickerDialog.OnDa
}
}

private fun disableNoteTitleEdit() {
binding.etNoteTitle.apply {
keyListener = null
setOnClickListener { showToast(requireContext().getString(R.string.cant_edit_note_title)) }
}
}

private fun pausePlayingRecording() {
mediaPlayer?.pause()
timer?.stop()
Expand Down
8 changes: 2 additions & 6 deletions app/src/main/java/com/certified/audionote/utils/Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ import com.certified.audionote.R
import com.certified.audionote.model.Note
import com.certified.audionote.ui.AlertReceiver
import com.vmadalin.easypermissions.EasyPermissions
import java.math.RoundingMode
import java.text.DecimalFormat
import java.text.SimpleDateFormat
import java.util.*
import java.util.concurrent.TimeUnit
Expand Down Expand Up @@ -116,8 +114,6 @@ fun cancelAlarm(context: Context, noteId: Int) {
Log.d("TAG", "cancelAlarm: Alarm canceled")
}

fun roundOffDecimal(number: Double): Double {
val df = DecimalFormat("#.##")
df.roundingMode = RoundingMode.CEILING
return df.format(number).toDouble()
fun roundOffDecimal(number: Double): String {
return "%.2f".format(number)
}
4 changes: 2 additions & 2 deletions app/src/main/res/values-it/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<string name="back_button">Pulsante Indietro</string>
<string name="delete_button">Pulsante Elimina</string>
<string name="share_button">Pulsante Condividi</string>
<string name="click_to_add_a_reminder_to_this_note">Clicca per aggiungere un promemoria a questa nota</string>
<string name="click_to_add_a_reminder_to_this_note">Aggiungi un promemoria a questa nota</string>
<string name="record_button">Registra</string>
<string name="reminder_button">Pulsante Promemoria</string>
<string name="welcome_back">Bentornato</string>
Expand Down Expand Up @@ -66,7 +66,6 @@
<string name="theme_light">Chiaro</string>
<string name="theme_dark">Scuro</string>
<string name="now_playing">In riproduzione</string>
<string name="cant_edit_note_title">Non puoi modificare il titolo della nota</string>
<string name="title_required">Il titolo della nota è obbligatorio</string>
<string name="permission_required">Questa autorizzazione è richiesta per abilitare le registrazioni audio</string>
<string name="note_saved">Nota salvata</string>
Expand All @@ -83,4 +82,5 @@
<string name="today">Oggi</string>
<string name="upcoming">Imminenti</string>
<string name="completed">Completate</string>
<string name="record_note_before_saving">Registra la tua nota audio prima di salvare</string>
</resources>
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
<string name="theme_light">Light</string>
<string name="theme_dark">Dark</string>
<string name="now_playing">Now playing</string>
<string name="cant_edit_note_title">You can\'t edit the note title</string>
<string name="title_required">The note title is required</string>
<string name="permission_required">This permission is required to enable audio recording</string>
<string name="note_saved">Note saved</string>
Expand All @@ -88,4 +87,5 @@
<string name="today">Today</string>
<string name="upcoming">Upcoming</string>
<string name="completed">Completed</string>
<string name="record_note_before_saving">Please record your audio note before saving</string>
</resources>

0 comments on commit 5f0e17d

Please sign in to comment.