Skip to content

Commit

Permalink
added option to copy and share magnet links
Browse files Browse the repository at this point in the history
close #311
  • Loading branch information
LivingWithHippos committed Aug 6, 2023
1 parent 8a00d65 commit 2e3952b
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fun getFilesNodes(
): Node<TorrentFileItem> {
val rootFolder = Node(TorrentFileItem(TYPE_FOLDER, "", 0, selected = false, "/"))

if (item.files != null && item.files.isNotEmpty()) {
if (!item.files.isNullOrEmpty()) {
val files = if (selectedOnly) item.files.filter { it.selected == 1 } else item.files
for (file in files) {
val paths = file.path.split("/").drop(1)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.livingwithhippos.unchained.torrentdetails.view

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.Menu
Expand Down Expand Up @@ -31,6 +32,7 @@ import com.github.livingwithhippos.unchained.torrentdetails.model.getFilesNodes
import com.github.livingwithhippos.unchained.torrentdetails.viewmodel.TorrentDetailsViewModel
import com.github.livingwithhippos.unchained.utilities.EventObserver
import com.github.livingwithhippos.unchained.utilities.Node
import com.github.livingwithhippos.unchained.utilities.extension.copyToClipboard
import com.github.livingwithhippos.unchained.utilities.extension.getApiErrorMessage
import com.github.livingwithhippos.unchained.utilities.extension.showToast
import com.github.livingwithhippos.unchained.utilities.loadingStatusList
Expand Down Expand Up @@ -216,10 +218,26 @@ class TorrentDetailsFragment : UnchainedFragment(), TorrentDetailsListener {
override fun onDeleteClick(id: String) {
viewModel.deleteTorrent(id)
}

override fun onShareMagnetClick() {
val shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.type = "text/plain"
shareIntent.putExtra(Intent.EXTRA_TEXT, "magnet:?xt=urn:btih:${args.item.hash}")
startActivity(Intent.createChooser(shareIntent, getString(R.string.share_with)))
}

override fun onCopyMagnetClick() {
copyToClipboard("Real-Debrid Magnet", "magnet:?xt=urn:btih:${args.item.hash}")
context?.showToast(R.string.link_copied)
}
}

interface TorrentDetailsListener {
fun onDownloadClick(item: TorrentItem)

fun onDeleteClick(id: String)

fun onShareMagnetClick()

fun onCopyMagnetClick()
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.livingwithhippos.unchained.torrentfilepicker.view

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.MenuItem
Expand Down Expand Up @@ -27,6 +28,7 @@ import com.github.livingwithhippos.unchained.torrentfilepicker.viewmodel.Torrent
import com.github.livingwithhippos.unchained.torrentfilepicker.viewmodel.TorrentProcessingViewModel
import com.github.livingwithhippos.unchained.utilities.Node
import com.github.livingwithhippos.unchained.utilities.beforeSelectionStatusList
import com.github.livingwithhippos.unchained.utilities.extension.copyToClipboard
import com.github.livingwithhippos.unchained.utilities.extension.isMagnet
import com.github.livingwithhippos.unchained.utilities.extension.isTorrent
import com.github.livingwithhippos.unchained.utilities.extension.showToast
Expand All @@ -51,6 +53,11 @@ class TorrentProcessingFragment : UnchainedFragment() {

private var cachedTorrent: CachedTorrent? = null

/**
* Save the torrent/magnet has when loaded
*/
private var torrentHash: String? = null

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand All @@ -74,10 +81,12 @@ class TorrentProcessingFragment : UnchainedFragment() {
content.item.files?.size == 1 &&
"waiting_files_selection".equals(content.item.status, ignoreCase = true)
) {
// todo: make this configurable in settings
context?.showToast(R.string.single_torrent_file_available)
viewModel.triggerTorrentEvent(TorrentEvent.DownloadAll)
viewModel.startSelectionLoop()
} else {
torrentHash = content.item.hash
// torrent loaded
if (
activityViewModel.getCurrentTorrentCachePick()?.first != content.item.id
Expand Down Expand Up @@ -269,6 +278,11 @@ class TorrentProcessingFragment : UnchainedFragment() {
val lastSelection: Node<TorrentFileItem>? = viewModel.structureLiveData.value?.peekContent()
if (lastSelection == null) popup.menu.findItem(R.id.manual_pick).isEnabled = false

if (torrentHash==null) {
popup.menu.findItem(R.id.copy_magnet).isVisible = false
popup.menu.findItem(R.id.share_magnet).isVisible = false
}

popup.setOnMenuItemClickListener { menuItem: MenuItem ->
// Respond to menu item click.
when (menuItem.itemId) {
Expand Down Expand Up @@ -326,6 +340,16 @@ class TorrentProcessingFragment : UnchainedFragment() {
Timber.e("Last files selection should not have been null")
}
}
R.id.copy_magnet -> {
copyToClipboard("Real-Debrid Magnet", "magnet:?xt=urn:btih:$torrentHash")
context?.showToast(R.string.link_copied)
}
R.id.share_magnet -> {
val shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.type = "text/plain"
shareIntent.putExtra(Intent.EXTRA_TEXT, "magnet:?xt=urn:btih:$torrentHash")
startActivity(Intent.createChooser(shareIntent, getString(R.string.share_with)))
}
else -> {
Timber.e("Unknown menu button pressed: $menuItem")
}
Expand Down
63 changes: 63 additions & 0 deletions app/app/src/main/res/layout/fragment_torrent_details.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,69 @@
android:maxLines="3"
android:text="@{torrent.filename, default=`File Name`}" />


<com.google.android.flexbox.FlexboxLayout
android:id="@+id/actionsLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
app:alignItems="flex_start"
app:flexWrap="wrap"
app:justifyContent="space_around">

<LinearLayout
android:layout_width="@dimen/fab_download_details_width"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:layout_marginBottom="10dp">

<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fabShareMagnet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/share_magnet"
android:onClick="@{() -> listener.onShareMagnetClick()}"
app:srcCompat="@drawable/icon_share" />

<TextView
android:textAppearance="?attr/textAppearanceLabelSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:focusable="false"
android:text="@string/share_magnet"
android:textAlignment="center" />
</LinearLayout>

<LinearLayout
android:layout_width="@dimen/fab_download_details_width"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:layout_marginBottom="10dp">

<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fabCopyMagnet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/copy_magnet"
android:onClick="@{() -> listener.onCopyMagnetClick()}"
app:srcCompat="@drawable/icon_open_external" />

<TextView
android:textAppearance="?attr/textAppearanceLabelSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:focusable="false"
android:text="@string/copy_magnet"
android:textAlignment="center" />
</LinearLayout>

</com.google.android.flexbox.FlexboxLayout>


<com.google.android.material.card.MaterialCardView
style="?attr/materialCardViewElevatedStyle"
android:layout_width="match_parent"
Expand Down
4 changes: 4 additions & 0 deletions app/app/src/main/res/menu/download_mode_picker.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@
android:title="@string/download_all" />
<item android:id="@+id/manual_pick"
android:title="@string/download_selected_files" />
<item android:id="@+id/copy_magnet"
android:title="@string/copy_magnet" />
<item android:id="@+id/share_magnet"
android:title="@string/share_magnet" />
</menu>
2 changes: 2 additions & 0 deletions app/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -696,4 +696,6 @@
<string name="select">Select</string>
<string name="selected_theme">Selected theme</string>
<string name="apply">Apply</string>
<string name="copy_magnet">Copy Magnet</string>
<string name="share_magnet">Share Magnet</string>
</resources>

1 comment on commit 2e3952b

@Invictaz
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LivingWithHippos I still cannot see the hash info. Even when pressing share or copy magnet

Screenshot_20240218_221053_Unchained-dev

Please sign in to comment.