use the showKeyboard(view) function for showing the keyboard on dialogs

This commit is contained in:
tibbi 2018-05-17 20:44:26 +02:00
parent 1b6221bc80
commit 7c5e20b36e
3 changed files with 70 additions and 72 deletions

View file

@ -5,11 +5,11 @@ import android.content.Context
import android.net.Uri
import android.provider.MediaStore
import android.support.v7.app.AlertDialog
import android.view.WindowManager
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.musicplayer.R
import com.simplemobiletools.musicplayer.models.Song
import kotlinx.android.synthetic.main.dialog_rename_song.*
import kotlinx.android.synthetic.main.dialog_rename_song.view.*
import java.io.File
@ -28,48 +28,48 @@ class EditDialog(val activity: BaseSimpleActivity, val song: Song, val callback:
.setPositiveButton(R.string.ok, null)
.setNegativeButton(R.string.cancel, null)
.create().apply {
window!!.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
activity.setupDialogStuff(view, this, R.string.rename_song) {
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
val newTitle = view.title.value
val newArtist = view.artist.value
val newFilename = view.file_name.value
val newFileExtension = view.extension.value
activity.setupDialogStuff(view, this, R.string.rename_song) {
showKeyboard(title)
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
val newTitle = view.title.value
val newArtist = view.artist.value
val newFilename = view.file_name.value
val newFileExtension = view.extension.value
if (newTitle.isEmpty() || newArtist.isEmpty() || newFilename.isEmpty() || newFileExtension.isEmpty()) {
activity.toast(R.string.rename_song_empty)
return@setOnClickListener
}
val uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
song.artist = newArtist
song.title = newTitle
if (updateContentResolver(context, uri, song.id, newTitle, newArtist)) {
context.contentResolver.notifyChange(uri, null)
val file = File(song.path)
val newFile = File(file.parent, "$newFilename.$newFileExtension")
if (file == newFile) {
callback(song)
dismiss()
return@setOnClickListener
}
if (file.renameTo(newFile)) {
context.scanFiles(arrayListOf(file, newFile)) {
song.path = newFile.absolutePath
callback(song)
if (newTitle.isEmpty() || newArtist.isEmpty() || newFilename.isEmpty() || newFileExtension.isEmpty()) {
activity.toast(R.string.rename_song_empty)
return@setOnClickListener
}
dismiss()
return@setOnClickListener
}
activity.toast(R.string.rename_song_error)
val uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
song.artist = newArtist
song.title = newTitle
if (updateContentResolver(context, uri, song.id, newTitle, newArtist)) {
context.contentResolver.notifyChange(uri, null)
val file = File(song.path)
val newFile = File(file.parent, "$newFilename.$newFileExtension")
if (file == newFile) {
callback(song)
dismiss()
return@setOnClickListener
}
if (file.renameTo(newFile)) {
context.scanFiles(arrayListOf(file, newFile)) {
song.path = newFile.absolutePath
callback(song)
}
dismiss()
return@setOnClickListener
}
activity.toast(R.string.rename_song_error)
}
}
}
}
}
}
}
private fun updateContentResolver(context: Context, uri: Uri, songID: Long, newSongTitle: String, newSongArtist: String): Boolean {

View file

@ -2,8 +2,8 @@ package com.simplemobiletools.musicplayer.dialogs
import android.app.Activity
import android.support.v7.app.AlertDialog
import android.view.WindowManager
import com.simplemobiletools.commons.extensions.setupDialogStuff
import com.simplemobiletools.commons.extensions.showKeyboard
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.extensions.value
import com.simplemobiletools.musicplayer.R
@ -26,40 +26,40 @@ class NewPlaylistDialog(val activity: Activity, var playlist: Playlist? = null,
.setPositiveButton(R.string.ok, null)
.setNegativeButton(R.string.cancel, null)
.create().apply {
window!!.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
val dialogTitle = if (isNewPlaylist) R.string.create_playlist else R.string.rename_playlist
activity.setupDialogStuff(view, this, dialogTitle) {
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
val title = view.new_playlist_title.value
val playlistIdWithTitle = activity.dbHelper.getPlaylistIdWithTitle(title)
var isPlaylistTitleTaken = isNewPlaylist && playlistIdWithTitle != -1
if (!isPlaylistTitleTaken)
isPlaylistTitleTaken = !isNewPlaylist && playlist!!.id != playlistIdWithTitle && playlistIdWithTitle != -1
val dialogTitle = if (isNewPlaylist) R.string.create_playlist else R.string.rename_playlist
activity.setupDialogStuff(view, this, dialogTitle) {
showKeyboard(view.new_playlist_title)
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
val title = view.new_playlist_title.value
val playlistIdWithTitle = activity.dbHelper.getPlaylistIdWithTitle(title)
var isPlaylistTitleTaken = isNewPlaylist && playlistIdWithTitle != -1
if (!isPlaylistTitleTaken)
isPlaylistTitleTaken = !isNewPlaylist && playlist!!.id != playlistIdWithTitle && playlistIdWithTitle != -1
if (title.isEmpty()) {
activity.toast(R.string.empty_name)
return@setOnClickListener
} else if (isPlaylistTitleTaken) {
activity.toast(R.string.playlist_name_exists)
return@setOnClickListener
}
if (title.isEmpty()) {
activity.toast(R.string.empty_name)
return@setOnClickListener
} else if (isPlaylistTitleTaken) {
activity.toast(R.string.playlist_name_exists)
return@setOnClickListener
}
playlist!!.title = title
playlist!!.title = title
val eventTypeId = if (isNewPlaylist) {
activity.dbHelper.insertPlaylist(playlist!!)
} else {
activity.dbHelper.updatePlaylist(playlist!!)
}
val eventTypeId = if (isNewPlaylist) {
activity.dbHelper.insertPlaylist(playlist!!)
} else {
activity.dbHelper.updatePlaylist(playlist!!)
}
if (eventTypeId != -1) {
dismiss()
callback(eventTypeId)
} else {
activity.toast(R.string.unknown_error_occurred)
if (eventTypeId != -1) {
dismiss()
callback(eventTypeId)
} else {
activity.toast(R.string.unknown_error_occurred)
}
}
}
}
}
}
}
}

View file

@ -2,7 +2,6 @@ package com.simplemobiletools.musicplayer.dialogs
import android.app.Activity
import android.support.v7.app.AlertDialog
import android.view.WindowManager
import com.simplemobiletools.commons.extensions.setupDialogStuff
import com.simplemobiletools.musicplayer.R
import com.simplemobiletools.musicplayer.models.Playlist
@ -18,9 +17,8 @@ class RemovePlaylistDialog(val activity: Activity, val playlist: Playlist? = nul
.setPositiveButton(R.string.ok, { dialog, which -> callback(view.remove_playlist_checkbox.isChecked) })
.setNegativeButton(R.string.cancel, null)
.create().apply {
window!!.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
activity.setupDialogStuff(view, this, R.string.remove_playlist)
}
activity.setupDialogStuff(view, this, R.string.remove_playlist)
}
}
private fun getDescriptionText(): String {