PlayerAndroid/app/src/main/java/code/name/monkey/retromusic/dialogs/SongShareDialog.kt

105 lines
3.4 KiB
Kotlin
Raw Normal View History

2019-03-03 09:20:15 +00:00
/*
2020-10-06 08:46:04 +00:00
* Copyright (c) 2020 Hemanth Savarla.
2019-03-03 09:20:15 +00:00
*
* Licensed under the GNU General Public License v3
*
2020-10-06 08:46:04 +00:00
* This is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
2019-03-03 09:20:15 +00:00
*
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
2020-10-06 08:46:04 +00:00
*
2019-03-03 09:20:15 +00:00
*/
2018-12-06 08:52:57 +00:00
package code.name.monkey.retromusic.dialogs
2019-05-15 20:55:57 +00:00
import android.app.Dialog
2018-12-06 08:52:57 +00:00
import android.content.Intent
import android.os.Bundle
2020-12-05 13:32:49 +00:00
import androidx.core.os.bundleOf
2019-05-15 20:55:57 +00:00
import androidx.fragment.app.DialogFragment
2020-05-23 22:26:45 +00:00
import code.name.monkey.retromusic.EXTRA_SONG
2018-12-06 08:52:57 +00:00
import code.name.monkey.retromusic.R
2020-02-02 12:44:16 +00:00
import code.name.monkey.retromusic.activities.ShareInstagramStory
2020-07-19 11:21:15 +00:00
import code.name.monkey.retromusic.extensions.colorButtons
2020-07-19 14:39:04 +00:00
import code.name.monkey.retromusic.extensions.materialDialog
2018-12-06 08:52:57 +00:00
import code.name.monkey.retromusic.model.Song
import code.name.monkey.retromusic.util.MusicUtil
2019-05-15 20:55:57 +00:00
class SongShareDialog : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
2020-05-23 22:26:45 +00:00
val song: Song? = requireArguments().getParcelable(EXTRA_SONG)
2020-05-23 21:47:23 +00:00
val listening: String =
2020-05-23 22:26:45 +00:00
String.format(
getString(R.string.currently_listening_to_x_by_x),
song?.title,
song?.artistName
)
2020-07-19 14:39:04 +00:00
return materialDialog(R.string.what_do_you_want_to_share)
2020-05-23 21:47:23 +00:00
.setItems(
arrayOf(
getString(R.string.the_audio_file),
"\u201C" + listening + "\u201D",
getString(R.string.social_stories)
)
) { _, which ->
withAction(which, song, listening)
}
.create()
2020-07-19 11:21:15 +00:00
.colorButtons()
2020-05-23 21:47:23 +00:00
}
2020-05-23 21:47:23 +00:00
private fun withAction(
which: Int,
song: Song?,
currentlyListening: String
) {
when (which) {
0 -> {
startActivity(Intent.createChooser(song?.let {
MusicUtil.createShareSongFileIntent(
it,
requireContext()
)
}, null))
}
1 -> {
startActivity(
Intent.createChooser(
Intent()
.setAction(Intent.ACTION_SEND)
.putExtra(Intent.EXTRA_TEXT, currentlyListening)
.setType("text/plain"),
null
)
)
}
2 -> {
if (song != null) {
startActivity(
Intent(
requireContext(),
ShareInstagramStory::class.java
).putExtra(
ShareInstagramStory.EXTRA_SONG,
song
)
2020-02-01 17:53:26 +00:00
)
2019-05-15 20:55:57 +00:00
}
2020-02-01 17:53:26 +00:00
}
2020-05-23 21:47:23 +00:00
}
2018-12-06 08:52:57 +00:00
}
companion object {
fun create(song: Song): SongShareDialog {
2020-12-05 13:32:49 +00:00
return SongShareDialog().apply {
arguments = bundleOf(
EXTRA_SONG to song
)
}
2018-12-06 08:52:57 +00:00
}
}
}