Songs sharing

This commit is contained in:
Koitharu 2023-05-18 10:23:34 +03:00
parent ded7956319
commit 7915655c78
No known key found for this signature in database
GPG key ID: 8E861F8CE6E7CE27
7 changed files with 90 additions and 0 deletions

View file

@ -32,6 +32,7 @@ import org.oxycblt.auxio.music.*
import org.oxycblt.auxio.navigation.MainNavigationAction
import org.oxycblt.auxio.navigation.NavigationViewModel
import org.oxycblt.auxio.util.logD
import org.oxycblt.auxio.util.shareSong
import org.oxycblt.auxio.util.showToast
/**
@ -99,6 +100,9 @@ abstract class ListFragment<in T : Music, VB : ViewBinding> :
R.id.action_go_album -> {
navModel.exploreNavigateTo(song.album)
}
R.id.action_song_share -> {
requireContext().shareSong(song)
}
R.id.action_playlist_add -> {
musicModel.addToPlaylist(song)
}

View file

@ -26,6 +26,7 @@ import org.oxycblt.auxio.R
import org.oxycblt.auxio.music.MusicViewModel
import org.oxycblt.auxio.playback.PlaybackViewModel
import org.oxycblt.auxio.ui.ViewBindingFragment
import org.oxycblt.auxio.util.shareSongs
import org.oxycblt.auxio.util.showToast
/**
@ -86,6 +87,10 @@ abstract class SelectionFragment<VB : ViewBinding> :
playbackModel.shuffle(selectionModel.take())
true
}
R.id.action_selection_share -> {
requireContext().shareSongs(selectionModel.take())
true
}
else -> false
}
}

View file

@ -212,4 +212,14 @@ data class MimeType(val fromExtension: String, val fromFormat: String?) {
MimeTypeMap.getSingleton().getExtensionFromMimeType(fromExtension)?.uppercase()
}
}
/**
* Return a mime-type such as "audio/ogg"
*
* @return A raw mime-type string. Will first try [fromFormat], then falling
* back to [fromExtension], and then null if that fails.
*/
fun getRawType(): String {
return fromFormat ?: fromExtension
}
}

View file

@ -0,0 +1,63 @@
/*
* Copyright (c) 2023 Auxio Project
* ShareUtil.kt is part of Auxio.
*
* This program 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.
*
* This program 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.oxycblt.auxio.util
import android.content.Context
import androidx.core.app.ShareCompat
import org.oxycblt.auxio.music.Song
private const val MIME_TYPE_FALLBACK = "audio/*"
/**
* Show system share sheet to share song
*
* @param song the [Song] to share
*/
fun Context.shareSong(song: Song) {
ShareCompat.IntentBuilder(this)
.setStream(song.uri)
.setType(song.mimeType.getRawType())
.startChooser()
}
/**
* Show system share sheet to share multiple song
*
* @param songs the collection of [Song] to share
*/
fun Context.shareSongs(songs: Collection<Song>) {
if (songs.isEmpty()) {
return
}
if (songs.size == 1) {
shareSong(songs.first())
return
}
val type = songs.mapTo(HashSet(songs.size)) {
it.mimeType.getRawType()
}.singleOrNull() ?: MIME_TYPE_FALLBACK
ShareCompat.IntentBuilder(this)
.apply {
for (song in songs) {
addStream(song.uri)
}
}
.setType(type)
.startChooser()
}

View file

@ -21,4 +21,8 @@
android:id="@+id/action_selection_shuffle"
android:title="@string/lbl_shuffle_selected"
app:showAsAction="never"/>
<item
android:id="@+id/action_selection_share"
android:title="@string/lbl_share"
app:showAsAction="never"/>
</menu>

View file

@ -15,6 +15,9 @@
<item
android:id="@+id/action_playlist_add"
android:title="@string/lbl_playlist_add" />
<item
android:id="@+id/action_song_share"
android:title="@string/lbl_share" />
<item
android:id="@+id/action_song_detail"
android:title="@string/lbl_song_detail" />

View file

@ -118,6 +118,7 @@
<string name="lbl_go_artist">Go to artist</string>
<string name="lbl_go_album">Go to album</string>
<string name="lbl_song_detail">View properties</string>
<string name="lbl_share">Share</string>
<string name="lbl_props">Song properties</string>
<string name="lbl_file_name">File name</string>