Browse Source

Fix index and button confusion

The SoundButton itself (which is a FrameLayout) is added to the GridLayout, so we have to remove the SoundButton and not its TextView. Further more the index of a SoundButton must be determined dynamically.
pull/1/head
Malte Schmitz 7 years ago
parent
commit
c49da072e6
2 changed files with 15 additions and 13 deletions
  1. +13
    -11
      app/src/main/java/de/mlte/soundboard/MainActivity.kt
  2. +2
    -2
      app/src/main/java/de/mlte/soundboard/SoundButton.kt

+ 13
- 11
app/src/main/java/de/mlte/soundboard/MainActivity.kt View File

@@ -15,7 +15,6 @@ import android.widget.GridLayout
import java.io.BufferedInputStream import java.io.BufferedInputStream
import java.io.BufferedOutputStream import java.io.BufferedOutputStream



class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
private val buttons = ArrayList<SoundButton>() private val buttons = ArrayList<SoundButton>()
private var player: MediaPlayer? = null private var player: MediaPlayer? = null
@@ -71,13 +70,12 @@ class MainActivity : AppCompatActivity() {
} }


private fun addButton(soundButton: SoundButton) { private fun addButton(soundButton: SoundButton) {
val index = buttons.size
buttons.add(soundButton) buttons.add(soundButton)


val parent = findViewById<GridLayout>(R.id.grid_layout) val parent = findViewById<GridLayout>(R.id.grid_layout)
parent.addView(soundButton) parent.addView(soundButton)


soundButton.btn.setOnClickListener {
soundButton.textView.setOnClickListener {
if (playing) { if (playing) {
player?.let { mp -> player?.let { mp ->
if (mp.isPlaying) { if (mp.isPlaying) {
@@ -111,10 +109,12 @@ class MainActivity : AppCompatActivity() {
} }
} }


soundButton.btn.setOnLongClickListener {
soundButton.textView.setOnLongClickListener {
val intent = Intent(baseContext, EditActivity::class.java) val intent = Intent(baseContext, EditActivity::class.java)
val parent = findViewById<GridLayout>(R.id.grid_layout)
val index = parent.indexOfChild(soundButton)
intent.putExtra("index", index) intent.putExtra("index", index)
intent.putExtra("caption", soundButton.btn.text)
intent.putExtra("caption", soundButton.textView.text)
startActivityForResult(intent, 1234) startActivityForResult(intent, 1234)


true true
@@ -131,7 +131,7 @@ class MainActivity : AppCompatActivity() {
val soundButton = SoundButton(this) val soundButton = SoundButton(this)
addButton(soundButton) addButton(soundButton)
val caption = preferences.getString("caption" + index, "") val caption = preferences.getString("caption" + index, "")
soundButton.btn.text = caption
soundButton.textView.text = caption
val soundId = preferences.getLong("soundId" + index, 0) val soundId = preferences.getLong("soundId" + index, 0)
soundButton.soundId = soundId soundButton.soundId = soundId
} }
@@ -143,17 +143,19 @@ class MainActivity : AppCompatActivity() {
if (requestCode == 1234 && resultCode == Activity.RESULT_OK && data != null) { if (requestCode == 1234 && resultCode == Activity.RESULT_OK && data != null) {
val index = data.getIntExtra("index", -1) val index = data.getIntExtra("index", -1)
if (index > -1 && index < buttons.size) { if (index > -1 && index < buttons.size) {
val btn = buttons[index].btn
val button = buttons[index]
val textView = buttons[index].textView
if (data.getBooleanExtra("delete", false)) { if (data.getBooleanExtra("delete", false)) {
deleteFile("audio" + buttons[index].soundId) deleteFile("audio" + buttons[index].soundId)
buttons.removeAt(index) buttons.removeAt(index)
val parent = findViewById<GridLayout>(R.id.grid_layout) val parent = findViewById<GridLayout>(R.id.grid_layout)
parent.removeView(btn)
parent.removeView(button)
saveNumButtons()
organizeButtons() organizeButtons()
} else { } else {
val caption = data.getStringExtra("caption") val caption = data.getStringExtra("caption")
if (caption != null) { if (caption != null) {
btn.setText(caption)
textView.setText(caption)
} }
val uri = data.getParcelableExtra<Uri>("uri") val uri = data.getParcelableExtra<Uri>("uri")
savePreferences(caption, uri, index) savePreferences(caption, uri, index)
@@ -164,10 +166,10 @@ class MainActivity : AppCompatActivity() {


private fun savePreferences(caption: String, uri: Uri?, index: Int) { private fun savePreferences(caption: String, uri: Uri?, index: Int) {
if (index > -1 && index < buttons.size) { if (index > -1 && index < buttons.size) {
val btn = buttons[index].btn
val textView = buttons[index].textView
val soundId = buttons[index].soundId val soundId = buttons[index].soundId
val editor = getPreferences(Context.MODE_PRIVATE).edit() val editor = getPreferences(Context.MODE_PRIVATE).edit()
editor.putString("caption" + index, btn.text.toString())
editor.putString("caption" + index, textView.text.toString())
editor.commit() editor.commit()


uri?.let { uri -> uri?.let { uri ->


+ 2
- 2
app/src/main/java/de/mlte/soundboard/SoundButton.kt View File

@@ -11,7 +11,7 @@ import android.widget.TextView


class SoundButton : FrameLayout { class SoundButton : FrameLayout {
val progressBar: ProgressBar val progressBar: ProgressBar
val btn: TextView
val textView: TextView
val objectAnimator: ObjectAnimator val objectAnimator: ObjectAnimator
var soundId: Long = 0 var soundId: Long = 0


@@ -19,7 +19,7 @@ class SoundButton : FrameLayout {
View.inflate(context, R.layout.layout_button, this) View.inflate(context, R.layout.layout_button, this)


progressBar = findViewById<ProgressBar>(R.id.progress_bar) progressBar = findViewById<ProgressBar>(R.id.progress_bar)
btn = findViewById<TextView>(R.id.text_view_button)
textView = findViewById<TextView>(R.id.text_view_button)


objectAnimator = ObjectAnimator.ofInt(progressBar, "progress", progressBar.getProgress(), 1000) objectAnimator = ObjectAnimator.ofInt(progressBar, "progress", progressBar.getProgress(), 1000)
objectAnimator.interpolator = LinearInterpolator() objectAnimator.interpolator = LinearInterpolator()


Loading…
Cancel
Save