Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
zaednasr committed Nov 26, 2023
1 parent 3b3873d commit ef5b68f
Show file tree
Hide file tree
Showing 30 changed files with 980 additions and 165 deletions.
598 changes: 598 additions & 0 deletions app/schemas/com.deniscerri.ytdlnis.database.DBManager/13.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import com.deniscerri.ytdlnis.database.models.*
LogItem::class,
TerminalItem::class
],
version = 12,
version = 13,
autoMigrations = [
AutoMigration (from = 1, to = 2),
AutoMigration (from = 2, to = 3),
Expand All @@ -35,7 +35,8 @@ import com.deniscerri.ytdlnis.database.models.*
AutoMigration (from = 8, to = 9),
AutoMigration (from = 9, to = 10),
AutoMigration (from = 10, to = 11),
AutoMigration (from = 11, to = 12)
AutoMigration (from = 11, to = 12),
AutoMigration (from = 12, to = 13)
]
)
abstract class DBManager : RoomDatabase(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ interface DownloadDao {
@Query("SELECT * FROM downloads WHERE status in('Active','Queued','QueuedPaused','ActivePaused','PausedReQueued')")
fun getActiveAndQueuedDownloads() : Flow<List<DownloadItem>>

@RewriteQueriesToDropUnusedColumns
@Query("SELECT * FROM downloads WHERE status in('Queued','QueuedPaused') ORDER BY downloadStartTime, id")
fun getQueuedDownloads() : PagingSource<Int, DownloadItemSimple>

Expand All @@ -55,6 +56,7 @@ interface DownloadDao {
@Query("SELECT * FROM downloads WHERE status in('Queued','QueuedPaused') ORDER BY downloadStartTime, id")
fun getQueuedDownloadsList() : List<DownloadItem>

@RewriteQueriesToDropUnusedColumns
@Query("SELECT * FROM downloads WHERE status='Cancelled' ORDER BY id DESC")
fun getCancelledDownloads() : PagingSource<Int, DownloadItemSimple>

Expand All @@ -64,12 +66,14 @@ interface DownloadDao {
@Query("SELECT * FROM downloads WHERE status LIKE '%Paused%'")
fun getPausedDownloadsList() : List<DownloadItem>

@RewriteQueriesToDropUnusedColumns
@Query("SELECT * FROM downloads WHERE status='Error' ORDER BY id DESC")
fun getErroredDownloads() : PagingSource<Int, DownloadItemSimple>

@Query("SELECT * FROM downloads WHERE status='Error' ORDER BY id DESC")
fun getErroredDownloadsList() : List<DownloadItem>

@RewriteQueriesToDropUnusedColumns
@Query("SELECT * FROM downloads WHERE status='Saved' ORDER BY id DESC")
fun getSavedDownloads() : PagingSource<Int, DownloadItemSimple>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ interface ResultDao {
suspend fun delete(id: Long)

@Query("SELECT * FROM results WHERE url=:url LIMIT 1")
fun getResultByURL(url: String) : ResultItem
fun getResultByURL(url: String) : ResultItem?

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,9 @@ data class DownloadItem(
var status: String,
@ColumnInfo(defaultValue = "0")
var downloadStartTime: Long,
var logID: Long?
var logID: Long?,
@ColumnInfo(defaultValue = "")
var playlistURL: String? = "",
@ColumnInfo(defaultValue = "")
var playlistIndex: Int? = null
) : Parcelable
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,9 @@ data class ResultItem(
@ColumnInfo(defaultValue = "")
var urls: String,
var chapters: MutableList<ChapterItem>?,
@ColumnInfo(defaultValue = "")
var playlistURL: String? = "",
@ColumnInfo(defaultValue = "")
var playlistIndex: Int? = null,
var creationTime: Long = System.currentTimeMillis() / 1000,
) : Parcelable
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ class HistoryRepository(private val historyDao: HistoryDao) {
}
}

suspend fun deleteAll(){
suspend fun deleteAll(deleteFile: Boolean = false){
if (deleteFile){
historyDao.getAllHistoryList().forEach { item ->
FileUtil.deleteFile(item.downloadPath)
}
}
historyDao.deleteAll()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class ResultRepository(private val resultDao: ResultDao, private val context: Co
deleteAll()
itemCount.value = v.size
}else{
v.forEach { it?.playlistTitle = "ytdlnis-Search" }
v.filter { it?.playlistTitle.isNullOrBlank() }.forEach { it?.playlistTitle = "ytdlnis-Search" }
}
resultDao.insertMultiple(v)
return ArrayList(v)
Expand Down Expand Up @@ -77,7 +77,7 @@ class ResultRepository(private val resultDao: ResultDao, private val context: Co
deleteAll()
itemCount.value = items.size
}else{
items.forEach { it!!.playlistTitle = "ytdlnis-Search" }
items.filter { it?.playlistTitle.isNullOrBlank() }.forEach { it!!.playlistTitle = "ytdlnis-Search" }
}
resultDao.insertMultiple(items.toList())
return items
Expand All @@ -96,7 +96,7 @@ class ResultRepository(private val resultDao: ResultDao, private val context: Co
resultDao.update(item)
}

fun getItemByURL(url: String): ResultItem {
fun getItemByURL(url: String): ResultItem? {
return resultDao.getResultByURL(url)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,7 @@ class DownloadViewModel(application: Application) : AndroidViewModel(application

audioContainer = sharedPreferences.getString("audio_format", "mp3")
bestAudioFormat = if (audioFormatIDPreference.isEmpty()){
Format(
"best",
audioContainer!!,
"",
"",
"",
0,
"best"
)
infoUtil.getGenericAudioFormats(resources).last()
}else{
Format(
audioFormatIDPreference.first().split("+").first(),
Expand Down Expand Up @@ -260,23 +252,8 @@ class DownloadViewModel(application: Application) : AndroidViewModel(application

val audioPreferences = AudioPreferences(embedThumb, cropThumb,false, ArrayList(sponsorblock!!))

val preferredAudioFormats = arrayListOf<String>()
for (f in resultItem.formats.sortedBy { it.format_id }){
val fId = audioFormatIDPreference.sorted().find { it.contains(f.format_id) }
if (fId != null) {
if (fId.split("+").all { resultItem.formats.map { f-> f.format_id }.contains(it) }){
preferredAudioFormats.addAll(fId.split("+"))
break
}
}
}

if (preferredAudioFormats.isEmpty()){
val audioF = getFormat(resultItem.formats, Type.audio)
if (!infoUtil.getGenericAudioFormats(resources).contains(audioF)){
preferredAudioFormats.add(audioF.format_id)
}
}
val preferredAudioFormats = getPreferredAudioFormats(resultItem.formats)

val videoPreferences = VideoPreferences(
embedSubs,
Expand All @@ -303,7 +280,15 @@ class DownloadViewModel(application: Application) : AndroidViewModel(application
container!!,
"",
resultItem.formats,
downloadPath!!, resultItem.website, "", resultItem.playlistTitle, audioPreferences, videoPreferences, extraCommands, customFileNameTemplate!!, saveThumb, DownloadRepository.Status.Queued.toString(), 0, null
downloadPath!!, resultItem.website,
"",
resultItem.playlistTitle,
audioPreferences,
videoPreferences,
extraCommands,
customFileNameTemplate!!,
saveThumb,
DownloadRepository.Status.Queued.toString(), 0, null, playlistURL = resultItem.playlistURL, playlistIndex = resultItem.playlistIndex
)

}
Expand All @@ -321,6 +306,8 @@ class DownloadViewModel(application: Application) : AndroidViewModel(application
downloadItem.allFormats,
"",
arrayListOf(),
downloadItem.playlistURL,
downloadItem.playlistIndex,
System.currentTimeMillis()
)

Expand All @@ -339,6 +326,8 @@ class DownloadViewModel(application: Application) : AndroidViewModel(application
arrayListOf(),
"",
arrayListOf(),
"",
null,
System.currentTimeMillis()
)

Expand All @@ -357,6 +346,8 @@ class DownloadViewModel(application: Application) : AndroidViewModel(application
arrayListOf(),
"",
arrayListOf(),
"",
null,
System.currentTimeMillis()
)

Expand Down Expand Up @@ -493,6 +484,26 @@ class DownloadViewModel(application: Application) : AndroidViewModel(application
}
}

fun getPreferredAudioFormats(formats: List<Format>) : ArrayList<String>{
val preferredAudioFormats = arrayListOf<String>()
for (f in formats.sortedBy { it.format_id }){
val fId = audioFormatIDPreference.sorted().find { it.contains(f.format_id) }
if (fId != null) {
if (fId.split("+").all { formats.map { f-> f.format_id }.contains(it) }){
preferredAudioFormats.addAll(fId.split("+"))
break
}
}
}
if (preferredAudioFormats.isEmpty()){
val audioF = getFormat(formats, Type.audio)
if (!infoUtil.getGenericAudioFormats(resources).contains(audioF)){
preferredAudioFormats.add(audioF.format_id)
}
}
return preferredAudioFormats
}

fun generateCommandFormat(c: CommandTemplate) : Format {
return Format(
c.title,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ class HistoryViewModel(application: Application) : AndroidViewModel(application)
repository.delete(item, deleteFile)
}

fun deleteAll() = viewModelScope.launch(Dispatchers.IO) {
repository.deleteAll()
fun deleteAll(deleteFile: Boolean = false) = viewModelScope.launch(Dispatchers.IO) {
repository.deleteAll(deleteFile)
}

fun deleteDuplicates() = viewModelScope.launch(Dispatchers.IO) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class ResultViewModel(application: Application) : AndroidViewModel(application)
repository.update(item)
}

fun getItemByURL(url: String) : ResultItem {
fun getItemByURL(url: String) : ResultItem? {
return repository.getItemByURL(url)
}

Expand Down Expand Up @@ -233,11 +233,12 @@ class ResultViewModel(application: Application) : AndroidViewModel(application)
val formats = infoUtil.getFormats(result.url)
updatingFormats.emit(false)
if (formats.isNotEmpty() && isActive) {
val res = getItemByURL(result.url)
res.formats = formats.toMutableList()
update(res)
getItemByURL(result.url)?.apply {
this.formats = formats.toMutableList()
update(this)
}
updateFormatsResultData.emit(formats.toMutableList())
}
updateFormatsResultData.emit(formats.toMutableList())
}
updateFormatsResultDataJob?.start()
updateFormatsResultDataJob?.invokeOnCompletion {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class HomeAdapter(onItemClickListener: OnItemClickListener, activity: Activity)

// TITLE ----------------------------------
val videoTitle = card.findViewById<TextView>(R.id.result_title)
var title = video!!.title
var title = video!!.title.ifBlank { video.url }
if (title.length > 100) {
title = title.substring(0, 40) + "..."
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,24 +198,27 @@ class DownloadAudioFragment(private var resultItem: ResultItem? = null, private
val chosenFormat = downloadItem.format
UiUtil.populateFormatCard(requireContext(), formatCard, chosenFormat, null)
val listener = object : OnFormatClickListener {
override fun onFormatClick(allFormats: List<List<Format>>, item: List<FormatTuple>) {
override fun onFormatClick(item: List<FormatTuple>) {
downloadItem.format = item.first().format
UiUtil.populateFormatCard(requireContext(), formatCard, item.first().format, null)
lifecycleScope.launch {
withContext(Dispatchers.IO){
resultItem?.formats?.removeAll(formats.toSet())
resultItem?.formats?.addAll(allFormats.first().filter { !genericAudioFormats.contains(it) })
if (resultItem != null){
resultViewModel.update(resultItem!!)
kotlin.runCatching {
val f1 = fragmentManager?.findFragmentByTag("f1") as DownloadVideoFragment
f1.updateUI(resultItem)
}
}

override fun onFormatsUpdated(allFormats: List<List<Format>>) {
lifecycleScope.launch(Dispatchers.IO) {
resultItem?.formats?.removeAll(formats.toSet())
resultItem?.formats?.addAll(allFormats.first().filter { !genericAudioFormats.contains(it) })
if (resultItem != null){
resultViewModel.update(resultItem!!)
kotlin.runCatching {
val f1 = fragmentManager?.findFragmentByTag("f1") as DownloadVideoFragment
f1.updateUI(resultItem)
}
}
}
formats = allFormats.first().filter { !genericAudioFormats.contains(it) }.toMutableList()
formats.removeAll(genericAudioFormats)
val preferredFormat = downloadViewModel.getFormat(formats, Type.audio)
UiUtil.populateFormatCard(requireContext(), formatCard, preferredFormat, null)
}
}
formatCard.setOnClickListener{
Expand Down
Loading

0 comments on commit ef5b68f

Please sign in to comment.