Skip to content

Commit

Permalink
Merge pull request #4 from Archrahkshi/feature
Browse files Browse the repository at this point in the history
Feature
  • Loading branch information
Archrahkshi authored Dec 2, 2020
2 parents 6254d0e + 037dc45 commit 80fc457
Show file tree
Hide file tree
Showing 18 changed files with 363 additions and 279 deletions.
6 changes: 5 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ android {

defaultConfig {
applicationId "com.archrahkshi.spotifine"
minSdkVersion 26
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
Expand All @@ -35,8 +35,10 @@ android {
}

repositories {
mavenCentral()
google()
jcenter()
maven { url 'https://jitpack.io' }
}

dependencies {
Expand Down Expand Up @@ -71,5 +73,7 @@ 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 'com.ibm.watson:ibm-watson:8.6.3'
}
5 changes: 4 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package="com.archrahkshi.spotifine">

<application
android:name=".App"
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
Expand All @@ -15,7 +16,9 @@
<activity
android:name="com.spotify.sdk.android.auth.LoginActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<activity android:name=".ui.PlayerActivity" />
<activity
android:name=".ui.PlayerActivity"
android:screenOrientation="portrait" />
<activity android:name=".ui.LibraryActivity" />
<activity android:name=".ui.MainActivity">
<intent-filter>
Expand Down
30 changes: 30 additions & 0 deletions app/src/main/java/com/archrahkshi/spotifine/App.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.archrahkshi.spotifine

import android.app.Application
import android.os.StrictMode

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

if (BuildConfig.DEBUG) {
StrictMode.setThreadPolicy(
StrictMode.ThreadPolicy.Builder()
// .detectDiskReads() - Not detecting disk reads cuz Xiaomi ¯\_(ツ)_/¯
.detectDiskWrites()
.detectNetwork()
.penaltyLog()
.penaltyDeath()
.build()
)
StrictMode.setVmPolicy(
StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects()
.detectLeakedClosableObjects()
.penaltyLog()
.penaltyDeath()
.build()
)
}
}
}
7 changes: 4 additions & 3 deletions app/src/main/java/com/archrahkshi/spotifine/data/Entities.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,18 @@ data class Artist(
data class Album(
@PrimaryKey(autoGenerate = true)
var id: Int = 0,
val artists: String,
val image: String,
val name: String,
val artists: String,
val size: Int,
val url: String,
)

@Entity
data class Track(
@PrimaryKey(autoGenerate = true)
var id: String,
val name: String,
val artist: String,
val artists: String,
val duration: Long,
val name: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.archrahkshi.spotifine.R
import com.archrahkshi.spotifine.util.setWordTracks
import com.bumptech.glide.Glide
import kotlinx.android.synthetic.main.item_library_list.view.imageViewListPic
import kotlinx.android.synthetic.main.item_library_list.view.layoutLibraryList
Expand Down Expand Up @@ -33,7 +34,7 @@ class LibraryListsAdapter<ListType>(
private val layoutItemList = view.layoutLibraryList
private val textViewListInfo = view.textViewListInfo
private val textViewListName = view.textViewListName
private val resources = view.context.resources
private val context = view.context
private val viewTest = view

fun bind(listType: ListType, clickListener: (ListType) -> Unit) {
Expand All @@ -46,7 +47,7 @@ class LibraryListsAdapter<ListType>(
textViewListInfo.text = when (listType) {
is Playlist -> {
val size = listType.size
"$size ${setWordTracks(size)}"
"$size ${setWordTracks(context, size)}"
}
is Artist -> ""
is Album -> listType.artists
Expand All @@ -62,16 +63,5 @@ class LibraryListsAdapter<ListType>(
).into(imageViewListPic)
layoutItemList.setOnClickListener { clickListener(listType) }
}

private fun setWordTracks(size: Int) = resources.getString(
with(size.toString()) {
when {
endsWith('1') -> R.string.tracks_singular
endsWith('2') || endsWith('3') || endsWith('4') ->
R.string.tracks_paucal
else -> R.string.tracks_plural
}
}
)
}
}
13 changes: 3 additions & 10 deletions app/src/main/java/com/archrahkshi/spotifine/data/TracksAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.archrahkshi.spotifine.R
import com.archrahkshi.spotifine.util.formatDuration
import kotlinx.android.synthetic.main.item_track.view.layoutItemList
import kotlinx.android.synthetic.main.item_track.view.textViewTrackArtist
import kotlinx.android.synthetic.main.item_track.view.textViewTrackDuration
import kotlinx.android.synthetic.main.item_track.view.textViewTrackName
import kotlin.time.ExperimentalTime
import kotlin.time.milliseconds

@ExperimentalTime
class TracksAdapter(
Expand All @@ -35,16 +35,9 @@ class TracksAdapter(

fun bind(track: Track, clickListener: (Track) -> Unit) {
textViewTrackName.text = track.name
textViewTrackArtist.text = track.artist
textViewTrackArtist.text = track.artists
// textViewTrackDuration.text = SimpleDateFormat("HH:mm:ss").format(track.duration)
textViewTrackDuration.text = track.duration.milliseconds.toComponents { HH, mm, ss, _ ->
var duration = ""
if (HH > 0) duration += "$HH:"
if (duration.isNotEmpty() && mm <= ONE_DIGIT) duration += '0'
duration += "$mm:"
if (duration.isNotEmpty() && ss <= ONE_DIGIT) duration += '0'
"$duration$ss"
}
textViewTrackDuration.text = formatDuration(track.duration)
layoutItemList.setOnClickListener { clickListener(track) }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package com.archrahkshi.spotifine.ui
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.archrahkshi.spotifine.R
import com.archrahkshi.spotifine.data.ACCESS_TOKEN
import com.archrahkshi.spotifine.data.ALBUMS
import com.archrahkshi.spotifine.data.ARTISTS
import com.archrahkshi.spotifine.data.LIST_TYPE
import com.archrahkshi.spotifine.data.PLAYLISTS
import com.archrahkshi.spotifine.util.ACCESS_TOKEN
import com.archrahkshi.spotifine.util.ALBUMS
import com.archrahkshi.spotifine.util.ARTISTS
import com.archrahkshi.spotifine.util.LIST_TYPE
import com.archrahkshi.spotifine.util.PLAYLISTS
import kotlinx.android.synthetic.main.activity_library.buttonAlbums
import kotlinx.android.synthetic.main.activity_library.buttonArtists
import kotlinx.android.synthetic.main.activity_library.buttonPlaylists
Expand All @@ -18,7 +18,8 @@ class LibraryActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_library)

replaceFragmentWith(PLAYLISTS)
if (savedInstanceState == null)
replaceFragmentWith(PLAYLISTS)

buttonPlaylists.setOnClickListener {
replaceFragmentWith(PLAYLISTS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,23 @@ import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.archrahkshi.spotifine.R
import com.archrahkshi.spotifine.data.ACCESS_TOKEN
import com.archrahkshi.spotifine.data.ALBUMS
import com.archrahkshi.spotifine.data.ARTISTS
import com.archrahkshi.spotifine.data.ARTIST_FROM_ME_DISTINCTION
import com.archrahkshi.spotifine.data.Album
import com.archrahkshi.spotifine.data.Artist
import com.archrahkshi.spotifine.data.FROM_ARTIST
import com.archrahkshi.spotifine.data.FROM_ME
import com.archrahkshi.spotifine.data.IMAGE
import com.archrahkshi.spotifine.data.LIST_TYPE
import com.archrahkshi.spotifine.data.LibraryListsAdapter
import com.archrahkshi.spotifine.data.NAME
import com.archrahkshi.spotifine.data.PLAYLISTS
import com.archrahkshi.spotifine.data.Playlist
import com.archrahkshi.spotifine.data.URL
import com.archrahkshi.spotifine.util.ACCESS_TOKEN
import com.archrahkshi.spotifine.util.ALBUMS
import com.archrahkshi.spotifine.util.ARTISTS
import com.archrahkshi.spotifine.util.ARTIST_FROM_USER_DISTINCTION
import com.archrahkshi.spotifine.util.FROM_ARTIST
import com.archrahkshi.spotifine.util.FROM_USER
import com.archrahkshi.spotifine.util.IMAGE
import com.archrahkshi.spotifine.util.LIST_TYPE
import com.archrahkshi.spotifine.util.NAME
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 kotlinx.android.synthetic.main.fragment_library_lists.recyclerViewLists
Expand Down Expand Up @@ -50,10 +52,8 @@ class LibraryListsFragment(
val accessToken = arguments?.getString(ACCESS_TOKEN)

launch {
recyclerViewLists.adapter = LibraryListsAdapter(
createLibraryLists(accessToken)!!
) {
Log.i("Item clicked", it.toString())
val libraryLists = createLibraryLists(accessToken)!!
recyclerViewLists.adapter = LibraryListsAdapter(libraryLists) {
fragmentManager?.beginTransaction()?.replace(
R.id.frameLayoutLibrary,
when (it) {
Expand All @@ -62,6 +62,7 @@ class LibraryListsFragment(
putString(URL, it.url)
putString(IMAGE, it.image)
putString(NAME, it.name)
putInt(SIZE, it.size)
putString(ACCESS_TOKEN, accessToken)
}
}
Expand All @@ -78,6 +79,8 @@ class LibraryListsFragment(
putString(URL, it.url)
putString(IMAGE, it.image)
putString(NAME, it.name)
putString(ARTISTS, it.artists)
putInt(SIZE, it.size)
putString(ACCESS_TOKEN, accessToken)
}
}
Expand All @@ -92,28 +95,15 @@ class LibraryListsFragment(
accessToken: String?
) = withContext(Dispatchers.IO) {
when (arguments?.getString(LIST_TYPE)) {
PLAYLISTS -> {
val libraryLists = mutableListOf<Playlist>()
getJsonFromApi(
"me/playlists",
accessToken
)["items"].asJsonArray.forEach {
libraryLists.add(createPlaylist(it.asJsonObject))
}
libraryLists
}
ARTISTS -> {
val libraryLists = mutableListOf<Artist>()
getJsonFromApi(
"me/following?type=artist",
accessToken
)["artists"].asJsonObject["items"].asJsonArray.forEach {
libraryLists.add(createArtist(it.asJsonObject))
}
libraryLists
}
PLAYLISTS -> getJsonFromApi(
"me/playlists",
accessToken
)["items"].asJsonArray.map { createPlaylist(it.asJsonObject) }
ARTISTS -> getJsonFromApi(
"me/following?type=artist",
accessToken
)["artists"].asJsonObject["items"].asJsonArray.map { createArtist(it.asJsonObject) }
ALBUMS -> {
val libraryLists = mutableListOf<Album>()
val json = getJsonFromApi(
"${arguments?.getString(URL) ?: "me"}/albums",
accessToken
Expand All @@ -122,24 +112,14 @@ class LibraryListsFragment(
when (
json["href"]
.asString
.removePrefix("https://api.spotify.com/v1/")
.take(ARTIST_FROM_ME_DISTINCTION)
.removePrefix(SPOTIFY_PREFIX)
.take(ARTIST_FROM_USER_DISTINCTION)
) {
"ar" -> {
items.forEach {
libraryLists.add(createAlbum(it.asJsonObject, FROM_ARTIST))
}
libraryLists
"ar" -> items.map { createAlbum(it.asJsonObject, FROM_ARTIST) }
"me" -> items.map {
createAlbum(it.asJsonObject["album"].asJsonObject, FROM_USER)
}
"me" -> {
items.forEach {
libraryLists.add(
createAlbum(it.asJsonObject["album"].asJsonObject, FROM_ME)
)
}
libraryLists
}
else -> libraryLists
else -> listOf()
}
}
else -> null
Expand All @@ -163,10 +143,11 @@ class LibraryListsFragment(
)

private fun createAlbum(item: JsonObject, type: String) = Album(
image = item["images"].asJsonArray[1].asJsonObject["url"].asString,
name = item["name"].asString,
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
Expand All @@ -177,14 +158,12 @@ class LibraryListsFragment(
try {
OkHttpClient().newCall(
Request.Builder()
.url("https://api.spotify.com/v1/$requestPostfix")
.url("$SPOTIFY_PREFIX$requestPostfix")
.header("Authorization", "Bearer $accessToken")
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.build()
).execute().body?.string().also {
Log.i("JSON string", it ?: "no JSON string")
}
).execute().body?.string()
} catch (e: IOException) {
Log.wtf("getJsonFromApi", e)
null
Expand Down
Loading

0 comments on commit 80fc457

Please sign in to comment.