ui: clean up share impl
Clean up the sharing implementation to be more in-line with how I would write it.
This commit is contained in:
parent
eac03f702a
commit
2d0a74122f
9 changed files with 47 additions and 45 deletions
|
@ -1,5 +1,10 @@
|
|||
# Changelog
|
||||
|
||||
## dev
|
||||
|
||||
#### What's New
|
||||
- Added ability to share a track
|
||||
|
||||
## 3.1.0
|
||||
|
||||
#### What's New
|
||||
|
|
|
@ -36,12 +36,10 @@ import androidx.recyclerview.widget.GridLayoutManager
|
|||
import androidx.recyclerview.widget.RecyclerView
|
||||
import androidx.viewbinding.ViewBinding
|
||||
import java.lang.IllegalArgumentException
|
||||
import org.oxycblt.auxio.music.Music
|
||||
import org.oxycblt.auxio.R
|
||||
import org.oxycblt.auxio.music.MusicParent
|
||||
import org.oxycblt.auxio.music.Song
|
||||
|
||||
private const val MIME_TYPE_FALLBACK = "audio/*"
|
||||
|
||||
/**
|
||||
* Get if this [View] contains the given [PointF], with optional leeway.
|
||||
*
|
||||
|
@ -271,35 +269,31 @@ fun WindowInsets.replaceSystemBarInsetsCompat(
|
|||
}
|
||||
|
||||
/**
|
||||
* Show system share sheet to share songs
|
||||
* Share a single [Song].
|
||||
*
|
||||
* @param music the [Music] to share
|
||||
* @param song
|
||||
*/
|
||||
fun Context.share(music: Music) = share(
|
||||
when (music) {
|
||||
is Song -> listOf(music)
|
||||
is MusicParent -> music.songs
|
||||
}
|
||||
)
|
||||
fun Context.share(song: Song) = share(listOf(song))
|
||||
|
||||
/**
|
||||
* Show system share sheet to share multiple song
|
||||
* Share all songs in a [MusicParent].
|
||||
*
|
||||
* @param songs the collection of [Song] to share
|
||||
* @param parent The [MusicParent] to share.
|
||||
*/
|
||||
fun Context.share(songs: Collection<Song>) {
|
||||
if (songs.isEmpty()) {
|
||||
return
|
||||
fun Context.share(parent: MusicParent) = share(parent.songs)
|
||||
|
||||
/**
|
||||
* Share an arbitrary list of [Song]s.
|
||||
* @param songs The [Song]s to share.
|
||||
*/
|
||||
fun Context.share(songs: List<Song>) {
|
||||
if (songs.isEmpty()) return
|
||||
val builder = ShareCompat.IntentBuilder(this)
|
||||
val mimeTypes = mutableSetOf<String>()
|
||||
for (song in songs) {
|
||||
builder.addStream(song.uri)
|
||||
mimeTypes.add(song.mimeType.fromFormat ?: song.mimeType.fromExtension)
|
||||
}
|
||||
val type = songs.mapTo(HashSet(songs.size)) {
|
||||
it.mimeType.raw
|
||||
}.singleOrNull() ?: MIME_TYPE_FALLBACK
|
||||
ShareCompat.IntentBuilder(this)
|
||||
.apply {
|
||||
for (song in songs) {
|
||||
addStream(song.uri)
|
||||
}
|
||||
}
|
||||
.setType(type)
|
||||
.startChooser()
|
||||
|
||||
builder.setType(mimeTypes.singleOrNull() ?: "audio/*").startChooser()
|
||||
}
|
||||
|
|
|
@ -9,10 +9,10 @@
|
|||
<item
|
||||
android:id="@+id/action_go_artist"
|
||||
android:title="@string/lbl_go_artist" />
|
||||
<item
|
||||
android:id="@+id/action_share"
|
||||
android:title="@string/lbl_share" />
|
||||
<item
|
||||
android:id="@+id/action_playlist_add"
|
||||
android:title="@string/lbl_playlist_add" />
|
||||
<item
|
||||
android:id="@+id/action_share"
|
||||
android:title="@string/lbl_share" />
|
||||
</menu>
|
|
@ -12,10 +12,10 @@
|
|||
<item
|
||||
android:id="@+id/action_playlist_add"
|
||||
android:title="@string/lbl_playlist_add" />
|
||||
<item
|
||||
android:id="@+id/action_share"
|
||||
android:title="@string/lbl_share" />
|
||||
<item
|
||||
android:id="@+id/action_song_detail"
|
||||
android:title="@string/lbl_song_detail" />
|
||||
<item
|
||||
android:id="@+id/action_share"
|
||||
android:title="@string/lbl_share" />
|
||||
</menu>
|
|
@ -12,10 +12,10 @@
|
|||
<item
|
||||
android:id="@+id/action_playlist_add"
|
||||
android:title="@string/lbl_playlist_add" />
|
||||
<item
|
||||
android:id="@+id/action_share"
|
||||
android:title="@string/lbl_share" />
|
||||
<item
|
||||
android:id="@+id/action_song_detail"
|
||||
android:title="@string/lbl_song_detail" />
|
||||
<item
|
||||
android:id="@+id/action_share"
|
||||
android:title="@string/lbl_share" />
|
||||
</menu>
|
|
@ -12,10 +12,10 @@
|
|||
<item
|
||||
android:id="@+id/action_queue_add"
|
||||
android:title="@string/lbl_queue_add" />
|
||||
<item
|
||||
android:id="@+id/action_share"
|
||||
android:title="@string/lbl_share" />
|
||||
<item
|
||||
android:id="@+id/action_playlist_add"
|
||||
android:title="@string/lbl_playlist_add" />
|
||||
<item
|
||||
android:id="@+id/action_share"
|
||||
android:title="@string/lbl_share" />
|
||||
</menu>
|
|
@ -14,9 +14,6 @@
|
|||
android:id="@+id/action_go_album"
|
||||
android:title="@string/lbl_go_album"
|
||||
app:showAsAction="never" />
|
||||
<item
|
||||
android:id="@+id/action_share"
|
||||
android:title="@string/lbl_share" />
|
||||
<item
|
||||
android:id="@+id/action_playlist_add"
|
||||
android:title="@string/lbl_playlist_add" />
|
||||
|
@ -24,4 +21,7 @@
|
|||
android:id="@+id/action_song_detail"
|
||||
android:title="@string/lbl_song_detail"
|
||||
app:showAsAction="never" />
|
||||
<item
|
||||
android:id="@+id/action_share"
|
||||
android:title="@string/lbl_share" />
|
||||
</menu>
|
|
@ -15,4 +15,7 @@
|
|||
<item
|
||||
android:id="@+id/action_song_detail"
|
||||
android:title="@string/lbl_song_detail" />
|
||||
<item
|
||||
android:id="@+id/action_share"
|
||||
android:title="@string/lbl_share" />
|
||||
</menu>
|
|
@ -15,10 +15,10 @@
|
|||
<item
|
||||
android:id="@+id/action_playlist_add"
|
||||
android:title="@string/lbl_playlist_add" />
|
||||
<item
|
||||
android:id="@+id/action_share"
|
||||
android:title="@string/lbl_share" />
|
||||
<item
|
||||
android:id="@+id/action_song_detail"
|
||||
android:title="@string/lbl_song_detail" />
|
||||
<item
|
||||
android:id="@+id/action_share"
|
||||
android:title="@string/lbl_share" />
|
||||
</menu>
|
Loading…
Reference in a new issue