Skip to content

Commit

Permalink
Merge pull request #5 from Archrahkshi/feature
Browse files Browse the repository at this point in the history
Massive changes

ktlint problem: pinterest/ktlint#527
ktlint import ordering ignored, using Android Studio default settings
  • Loading branch information
Archrahkshi authored Dec 3, 2020
2 parents 80fc457 + 22bff4e commit 5478cfa
Show file tree
Hide file tree
Showing 19 changed files with 364 additions and 302 deletions.
4 changes: 3 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ dependencies {
implementation "com.squareup.okhttp3:okhttp:4.9.0"
implementation 'com.squareup.okhttp3:okhttp-urlconnection:4.4.1'

implementation 'org.jsoup:jsoup:1.10.3'
implementation 'org.jsoup:jsoup:1.13.1'

implementation 'com.jakewharton.timber:timber:4.7.1'

implementation 'com.ibm.watson:ibm-watson:8.6.3'
}
3 changes: 3 additions & 0 deletions app/src/main/java/com/archrahkshi/spotifine/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package com.archrahkshi.spotifine

import android.app.Application
import android.os.StrictMode
import timber.log.Timber
import timber.log.Timber.DebugTree

class App : Application() { // Some weird warning which is certainly false
override fun onCreate() {
super.onCreate()

if (BuildConfig.DEBUG) {
Timber.plant(DebugTree())
StrictMode.setThreadPolicy(
StrictMode.ThreadPolicy.Builder()
// .detectDiskReads() - Not detecting disk reads cuz Xiaomi ¯\_(ツ)_/¯
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@ import kotlinx.android.synthetic.main.item_library_list.view.layoutLibraryList
import kotlinx.android.synthetic.main.item_library_list.view.textViewListInfo
import kotlinx.android.synthetic.main.item_library_list.view.textViewListName

class LibraryListsAdapter<ListType>(
class LibraryListsAdapter(
private val libraryLists: List<ListType>,
private val clickListener: (ListType) -> Unit
) : RecyclerView.Adapter<LibraryListsAdapter.ViewHolder<ListType>>() {
) : RecyclerView.Adapter<LibraryListsAdapter.ViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ViewHolder<ListType>(
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ViewHolder(
LayoutInflater
.from(parent.context)
.inflate(R.layout.item_library_list, parent, false)
)

override fun getItemCount() = libraryLists.size

override fun onBindViewHolder(holder: ViewHolder<ListType>, position: Int) {
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bind(libraryLists[position], clickListener)
}

class ViewHolder<ListType>(view: View) : RecyclerView.ViewHolder(view) {
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
private val imageViewListPic = view.imageViewListPic
private val layoutItemList = view.layoutLibraryList
private val textViewListInfo = view.textViewListInfo
Expand All @@ -42,7 +42,6 @@ class LibraryListsAdapter<ListType>(
is Playlist -> listType.name
is Artist -> listType.name
is Album -> listType.name
else -> null
}
textViewListInfo.text = when (listType) {
is Playlist -> {
Expand All @@ -51,14 +50,12 @@ class LibraryListsAdapter<ListType>(
}
is Artist -> ""
is Album -> listType.artists
else -> null
}
Glide.with(viewTest).load(
when (listType) {
is Playlist -> listType.image
is Artist -> listType.image
is Album -> listType.image
else -> null
}
).into(imageViewListPic)
layoutItemList.setOnClickListener { clickListener(listType) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.archrahkshi.spotifine.data
import androidx.room.Entity
import androidx.room.PrimaryKey

sealed class ListType

@Entity
data class Playlist(
@PrimaryKey(autoGenerate = true)
Expand All @@ -11,7 +13,7 @@ data class Playlist(
val name: String,
val size: Int,
val url: String,
)
) : ListType()

@Entity
data class Artist(
Expand All @@ -20,7 +22,7 @@ data class Artist(
val image: String,
val name: String,
val url: String,
)
) : ListType()

@Entity
data class Album(
Expand All @@ -31,7 +33,7 @@ data class Album(
val name: String,
val size: Int,
val url: String,
)
) : ListType()

@Entity
data class Track(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.archrahkshi.spotifine.ui

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand All @@ -24,16 +23,15 @@ import com.archrahkshi.spotifine.util.PLAYLISTS
import com.archrahkshi.spotifine.util.SIZE
import com.archrahkshi.spotifine.util.SPOTIFY_PREFIX
import com.archrahkshi.spotifine.util.URL
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import com.archrahkshi.spotifine.util.createAlbum
import com.archrahkshi.spotifine.util.createArtist
import com.archrahkshi.spotifine.util.createPlaylist
import com.archrahkshi.spotifine.util.getJsonFromApi
import kotlinx.android.synthetic.main.fragment_library_lists.recyclerViewLists
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.OkHttpClient
import okhttp3.Request
import java.io.IOException
import kotlin.coroutines.CoroutineContext

class LibraryListsFragment(
Expand All @@ -52,60 +50,56 @@ class LibraryListsFragment(
val accessToken = arguments?.getString(ACCESS_TOKEN)

launch {
val libraryLists = createLibraryLists(accessToken)!!
recyclerViewLists.adapter = LibraryListsAdapter(libraryLists) {
recyclerViewLists.adapter = LibraryListsAdapter(createLibraryLists(accessToken)) {
fragmentManager?.beginTransaction()?.replace(
R.id.frameLayoutLibrary,
when (it) {
is Playlist -> TracksFragment().apply {
arguments = Bundle().apply {
putString(URL, it.url)
putString(ACCESS_TOKEN, accessToken)
putString(IMAGE, it.image)
putString(NAME, it.name)
putInt(SIZE, it.size)
putString(ACCESS_TOKEN, accessToken)
putString(URL, it.url)
}
}
is Artist -> LibraryListsFragment().apply {
arguments = Bundle().apply {
putString(ACCESS_TOKEN, accessToken)
putString(IMAGE, it.image)
putString(LIST_TYPE, ALBUMS)
putString(URL, it.url)
putString(IMAGE, it.image)
putString(ACCESS_TOKEN, accessToken)
}
}
is Album -> TracksFragment().apply {
arguments = Bundle().apply {
putString(URL, it.url)
putString(ACCESS_TOKEN, accessToken)
putString(ARTISTS, it.artists)
putString(IMAGE, it.image)
putString(NAME, it.name)
putString(ARTISTS, it.artists)
putInt(SIZE, it.size)
putString(ACCESS_TOKEN, accessToken)
putString(URL, it.url)
}
}
else -> null
}!!
}
)?.addToBackStack(null)?.commit()
}
}
}

private suspend fun createLibraryLists(
accessToken: String?
) = withContext(Dispatchers.IO) {
private suspend fun createLibraryLists(accessToken: String?) = withContext(Dispatchers.IO) {
when (arguments?.getString(LIST_TYPE)) {
PLAYLISTS -> getJsonFromApi(
"me/playlists",
"${SPOTIFY_PREFIX}me/playlists",
accessToken
)["items"].asJsonArray.map { createPlaylist(it.asJsonObject) }
ARTISTS -> getJsonFromApi(
"me/following?type=artist",
"${SPOTIFY_PREFIX}me/following?type=artist",
accessToken
)["artists"].asJsonObject["items"].asJsonArray.map { createArtist(it.asJsonObject) }
ALBUMS -> {
val json = getJsonFromApi(
"${arguments?.getString(URL) ?: "me"}/albums",
"$SPOTIFY_PREFIX${arguments?.getString(URL) ?: "me"}/albums",
accessToken
)
val items = json["items"].asJsonArray
Expand All @@ -122,51 +116,7 @@ class LibraryListsFragment(
else -> listOf()
}
}
else -> null
else -> listOf()
}
}

private fun createPlaylist(item: JsonObject): Playlist {
val tracks = item["tracks"].asJsonObject
return Playlist(
image = item["images"].asJsonArray.first().asJsonObject["url"].asString,
name = item["name"].asString,
size = tracks["total"].asInt,
url = tracks["href"].asString
)
}

private fun createArtist(item: JsonObject) = Artist(
image = item["images"].asJsonArray[1].asJsonObject["url"].asString,
name = item["name"].asString,
url = "artists/${item["id"].asString}"
)

private fun createAlbum(item: JsonObject, type: String) = Album(
artists = item["artists"].asJsonArray
.joinToString { it.asJsonObject["name"].asString },
image = item["images"].asJsonArray[1].asJsonObject["url"].asString,
name = item["name"].asString,
size = item["total_tracks"].asInt,
url = if (type == FROM_ARTIST)
"${item["href"].asString}/tracks"
else
item["tracks"].asJsonObject["href"].asString
)

private fun getJsonFromApi(requestPostfix: String, accessToken: String?) = JsonParser().parse(
try {
OkHttpClient().newCall(
Request.Builder()
.url("$SPOTIFY_PREFIX$requestPostfix")
.header("Authorization", "Bearer $accessToken")
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.build()
).execute().body?.string()
} catch (e: IOException) {
Log.wtf("getJsonFromApi", e)
null
}
).asJsonObject
}
Loading

0 comments on commit 5478cfa

Please sign in to comment.