Skip to content

Commit

Permalink
Checklisted items come first, checklist improvements
Browse files Browse the repository at this point in the history
Signed-off-by: AndreaCioccarelli <[email protected]>
  • Loading branch information
cioccarellia committed Aug 6, 2019
1 parent 0b743a5 commit 256a3ab
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 28 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ android {
applicationId "com.andreacioccarelli.musicdownloader"
minSdkVersion 21
targetSdkVersion 28
versionCode 24
versionName "1.3.14"
versionCode 25
versionName "1.3.15"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
Expand Down
11 changes: 6 additions & 5 deletions app/src/main/kotlin/com/andreacioccarelli/musicdownloader/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import com.andreacioccarelli.musicdownloader.extensions.Delegates
import com.andreacioccarelli.musicdownloader.ui.typeface.Typefaces
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import okhttp3.OkHttpClient
import uk.co.chrisjenx.calligraphy.CalligraphyConfig
import kotlin.LazyThreadSafetyMode.NONE

/**
* Designed and developed by Andrea Cioccarelli
Expand All @@ -22,12 +23,13 @@ class App : Application() {

companion object {
var context by Delegates.ctx<Application>()
val prefs by lazy { CryptoPrefs(context.applicationContext, FILE, KEY, false) }
val client by lazy(NONE) { OkHttpClient() }
val prefs by lazy { CryptoPrefs(context, FILE, KEY, shouldEncrypt = false) }

val checklist by lazy {
val db = Room.databaseBuilder(
context,
ChecklistDatabase::class.java, "checklist"
context,
ChecklistDatabase::class.java, "checklist"
)

with(db) {
Expand Down Expand Up @@ -61,7 +63,6 @@ class App : Application() {
)

CoroutineScope(Dispatchers.Default).launch {
delay(107)
::checklist.get()
::checklistedIds.get()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import com.andreacioccarelli.logkit.loge
import com.andreacioccarelli.musicdownloader.App
import com.andreacioccarelli.musicdownloader.App.Companion.checklist
import com.andreacioccarelli.musicdownloader.constants.*
import com.andreacioccarelli.musicdownloader.data.enums.Format
import com.andreacioccarelli.musicdownloader.data.enums.FailedConversionError
import com.andreacioccarelli.musicdownloader.data.enums.Format
import com.andreacioccarelli.musicdownloader.data.model.DownloadInfo
import com.andreacioccarelli.musicdownloader.data.requests.DownloadLinkRequestsBuilder
import com.andreacioccarelli.musicdownloader.data.serializers.DirectLinkResponse
Expand Down Expand Up @@ -132,9 +132,7 @@ class DownloadClient {
}

@UiThread
private fun downloadFileList(
totalVideos: List<DirectLinkResponse>
) {
private fun downloadFileList(totalVideos: List<DirectLinkResponse>) {
val convertedVideos = totalVideos.filter {
it.isSuccessful()
}
Expand All @@ -156,12 +154,13 @@ class DownloadClient {

@UiThread
private fun downloadFile(
downloadManager: DownloadManager = App.context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager,
response: DirectLinkResponse,
isSingle: Boolean = true
downloadManager: DownloadManager = App.context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager,
response: DirectLinkResponse,
isSingle: Boolean = true
) {
CoroutineScope(Dispatchers.Default).launch {
checklist.remove(response.videoId)
App.checklistedIds.add(response.videoId)
}

if (response.isUnsuccessful()) {
Expand Down Expand Up @@ -207,6 +206,7 @@ class DownloadClient {

downloadManager.enqueue(downloadRequest)
checklist.remove(response.videoId)
App.checklistedIds.add(response.videoId)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.customview.customView
import com.afollestad.materialdialogs.list.customListAdapter
import com.afollestad.materialdialogs.list.listItemsSingleChoice
import com.andreacioccarelli.musicdownloader.App
import com.andreacioccarelli.musicdownloader.App.Companion.checklist
import com.andreacioccarelli.musicdownloader.R
import com.andreacioccarelli.musicdownloader.client.DownloadClient
Expand All @@ -40,7 +41,6 @@ import jp.wasabeef.recyclerview.adapters.ScaleInAnimationAdapter
import kotlinx.android.synthetic.main.activity_content.*
import kotlinx.android.synthetic.main.activity_layout.*
import kotlinx.coroutines.*
import okhttp3.OkHttpClient

/**
* Designed and Developed by Andrea Cioccarelli
Expand Down Expand Up @@ -211,7 +211,9 @@ class MainActivity : BaseActivity() {

GlobalScope.launch(Dispatchers.IO + fetchExceptionHandler) {
val requestBuilder = YoutubeRequestBuilder.get(query)
val request = OkHttpClient().newCall(requestBuilder).execute()

val call = App.client.newCall(requestBuilder)
val request = call.execute()

val jsonRequest = request.body!!.string()
val response = Gson().fromJson(jsonRequest, YoutubeSearchResponse::class.java)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,22 @@ class SearchResultAdapter(

override fun getItemCount() = data.size

val data by lazy { ArrayList<Result>() }
val data = ArrayList<Result>()

init {
data.clear()
data.addAll(response.items)
data.sortByDescending { App.checklistedIds.contains(it.id.videoId) }
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ResultCardViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.result_item, parent, false)
return ResultCardViewHolder(v)
}

override fun onBindViewHolder(holder: ResultCardViewHolder, uncheckedIndex: Int) {
val i = holder.adapterPosition

override fun onBindViewHolder(holder: ResultCardViewHolder, i: Int) {
val videoId = data[i].id.videoId
Glide.with(activity)
.load(data[i].snippet.thumbnails.medium.url)
.thumbnail(0.1F)
Expand All @@ -63,7 +64,7 @@ class SearchResultAdapter(
title.text = data[i].snippet.title.breakHtml()

iconLayout.setOnClickListener {
YoutubeUtil.getVideoPreviewDialog(activity, data[i].id.videoId).show()
YoutubeUtil.getVideoPreviewDialog(activity, videoId).show()
}

card.setOnClickListener {
Expand All @@ -73,21 +74,21 @@ class SearchResultAdapter(

card.setOnLongClickListener {
VibrationUtil.medium()
if (checklist.contains(data[i].id.videoId)) {
if (checklist.contains(videoId)) {
ToastUtil.error("Removed from checklist", R.drawable.remove_outline, duration = Toast.LENGTH_SHORT)
title.applyChecklistBadge(false)

CoroutineScope(Dispatchers.Default).launch {
checklist.remove(data[i].id.videoId)
App.checklistedIds.remove(data[i].id.videoId)
checklist.remove(videoId)
App.checklistedIds.remove(videoId)
}
} else {
ToastUtil.success("Added to checklist", R.drawable.add_outline, duration = Toast.LENGTH_SHORT)
title.applyChecklistBadge(true)

CoroutineScope(Dispatchers.Default).launch {
checklist.add(ChecklistEntry(data[i]))
App.checklistedIds.add(data[i].id.videoId)
App.checklistedIds.add(videoId)
}
}

Expand All @@ -101,7 +102,7 @@ class SearchResultAdapter(

titleLayout.visibility = View.VISIBLE

if (App.checklistedIds.contains(data[i].id.videoId)) {
if (App.checklistedIds.contains(videoId)) {
title.applyChecklistBadge(true)
} else {
title.applyChecklistBadge(false)
Expand Down
Binary file modified bin/music-downloader.apk
Binary file not shown.
4 changes: 2 additions & 2 deletions bin/update.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"versionCode": 24,
"versionName": "1.3.14",
"versionCode": 25,
"versionName": "1.3.15",
"changelog": "UI improvements and bugfixes for summer :)",

"downloadInfo": {
Expand Down

0 comments on commit 256a3ab

Please sign in to comment.