Skip to content

Commit

Permalink
Satisfy all CI requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
Archrahkshi committed Dec 1, 2020
1 parent 47fa999 commit 5b0d418
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 80 deletions.
23 changes: 14 additions & 9 deletions app/src/main/java/com/archrahkshi/spotifine/data/Constants.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package com.archrahkshi.spotifine.data

const val CLIENT_ID = "fbe0ec189f0247f99909e75530bac38e"
const val REDIRECT_URI = "http://localhost:8888/callback/"
const val REQUEST_CODE = 1337
const val ACCESS_TOKEN = "token"
const val URL = "url"
const val ALBUM_FROM_PLAYLIST_DISTINCTION = 5
const val ALBUMS = "albums"
const val ARTIST_FROM_ME_DISTINCTION = 2
const val ARTISTS = "artists"
const val CLIENT_ID = "fbe0ec189f0247f99909e75530bac38e"
const val DURATION = "duration"
const val FROM_ARTIST = "from_artist"
const val FROM_ME = "from_me"
const val ID = "id"
const val IMAGE = "image"
const val LIST_TYPE = "list_type"
const val PLAYLISTS = "playlists"
const val ARTISTS = "artists"
const val ALBUMS = "albums"
const val NAME = "name"
const val ID = "id"
const val DURATION = "duration"
const val ONE_DIGIT = 9
const val PLAYLISTS = "playlists"
const val REDIRECT_URI = "http://localhost:8888/callback/"
const val REQUEST_CODE = 1337
const val URL = "url"
2 changes: 0 additions & 2 deletions app/src/main/java/com/archrahkshi/spotifine/data/Entities.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package com.archrahkshi.spotifine.data
import androidx.room.Entity
import androidx.room.PrimaryKey

// TODO: при необходимости - добавить поля

@Entity
data class Playlist(
@PrimaryKey(autoGenerate = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.archrahkshi.spotifine.R
import com.bumptech.glide.Glide
import kotlinx.android.synthetic.main.item_library_list.view.*
import kotlinx.android.synthetic.main.item_library_list.view.imageViewListPic
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>(
private val libraryLists: List<ListType>,
Expand All @@ -26,10 +29,11 @@ class LibraryListsAdapter<ListType>(
}

class ViewHolder<ListType>(view: View) : RecyclerView.ViewHolder(view) {
private val textViewListName = view.textViewListName
private val textViewListInfo = view.textViewListInfo
private val layoutItemList = view.layoutLibraryList
private val imageViewListPic = view.imageViewListPic
private val layoutItemList = view.layoutLibraryList
private val textViewListInfo = view.textViewListInfo
private val textViewListName = view.textViewListName
private val resources = view.context.resources
private val viewTest = view

fun bind(listType: ListType, clickListener: (ListType) -> Unit) {
Expand All @@ -40,7 +44,10 @@ class LibraryListsAdapter<ListType>(
else -> null
}
textViewListInfo.text = when (listType) {
is Playlist -> listType.size.toString() // TODO: поменять
is Playlist -> {
val size = listType.size
"$size ${setWordTracks(size)}"
}
is Artist -> ""
is Album -> listType.artists
else -> null
Expand All @@ -55,5 +62,16 @@ 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
}
}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.archrahkshi.spotifine.R
import kotlinx.android.synthetic.main.item_lyrics_line.view.*
import kotlinx.android.synthetic.main.item_lyrics_line.view.textViewLyricsLine

class LyricsAdapter(
private val lines: List<String>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.archrahkshi.spotifine.R
import kotlinx.android.synthetic.main.item_track.view.*
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

Expand Down Expand Up @@ -37,9 +40,9 @@ class TracksAdapter(
textViewTrackDuration.text = track.duration.milliseconds.toComponents { HH, mm, ss, _ ->
var duration = ""
if (HH > 0) duration += "$HH:"
if (duration.isNotEmpty() && mm < 9) duration += '0'
if (duration.isNotEmpty() && mm < ONE_DIGIT) duration += '0'
duration += "$mm:"
if (duration.isNotEmpty() && ss < 9) duration += '0'
if (duration.isNotEmpty() && ss < ONE_DIGIT) duration += '0'
"$duration$ss"
}
layoutItemList.setOnClickListener { clickListener(track) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ package com.archrahkshi.spotifine.ui
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.archrahkshi.spotifine.R
import com.archrahkshi.spotifine.data.*
import kotlinx.android.synthetic.main.activity_library.*
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 kotlinx.android.synthetic.main.activity_library.buttonAlbums
import kotlinx.android.synthetic.main.activity_library.buttonArtists
import kotlinx.android.synthetic.main.activity_library.buttonPlaylists

class LibraryActivity : AppCompatActivity() {

Expand Down
100 changes: 55 additions & 45 deletions app/src/main/java/com/archrahkshi/spotifine/ui/LibraryListsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,24 @@ import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.archrahkshi.spotifine.R
import com.archrahkshi.spotifine.data.*
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.google.gson.JsonObject
import com.google.gson.JsonParser
import kotlinx.android.synthetic.main.fragment_library_lists.*
import kotlinx.android.synthetic.main.fragment_library_lists.recyclerViewLists
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -66,31 +81,24 @@ class LibraryListsFragment(
putString(ACCESS_TOKEN, accessToken)
}
}
else -> null // TODO: разобраться с sealed классами и убрать этот костыль
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 -> {
val libraryLists = mutableListOf<Playlist>()
getJsonFromApi(
"me/playlists",
accessToken
)["items"].asJsonArray.forEach {
val item = it.asJsonObject
val tracks = item["tracks"].asJsonObject
libraryLists.add(
Playlist(
image = item["images"].asJsonArray.first().asJsonObject["url"].asString,
name = item["name"].asString,
size = tracks["total"].asInt,
url = tracks["href"].asString
)
)
libraryLists.add(createPlaylist(it.asJsonObject))
}
libraryLists
}
Expand All @@ -100,14 +108,7 @@ class LibraryListsFragment(
"me/following?type=artist",
accessToken
)["artists"].asJsonObject["items"].asJsonArray.forEach {
val item = it.asJsonObject
libraryLists.add(
Artist(
image = item["images"].asJsonArray[1].asJsonObject["url"].asString,
name = item["name"].asString,
url = "artists/${item["id"].asString}"
)
)
libraryLists.add(createArtist(it.asJsonObject))
}
libraryLists
}
Expand All @@ -122,38 +123,20 @@ class LibraryListsFragment(
json["href"]
.asString
.removePrefix("https://api.spotify.com/v1/")
.take(2)
.take(ARTIST_FROM_ME_DISTINCTION)
) {
"ar" -> {
items.forEach { element ->
val item = element.asJsonObject
libraryLists.add(
Album(
image = item["images"].asJsonArray[1].asJsonObject["url"].asString,
name = item["name"].asString,
artists = item["artists"].asJsonArray
.joinToString { it.asJsonObject["name"].asString },
url = "${item["href"].asString}/tracks"
)
)
items.forEach {
libraryLists.add(createAlbum(it.asJsonObject, FROM_ARTIST))
}
Log.wtf("Album (artist)", libraryLists.joinToString())
libraryLists
}
"me" -> {
items.forEach { element ->
val album = element.asJsonObject["album"].asJsonObject
items.forEach {
libraryLists.add(
Album(
image = album["images"].asJsonArray[1].asJsonObject["url"].asString,
name = album["name"].asString,
artists = album["artists"].asJsonArray
.joinToString { it.asJsonObject["name"].asString },
url = album["tracks"].asJsonObject["href"].asString
)
createAlbum(it.asJsonObject["album"].asJsonObject, FROM_ME)
)
}
Log.wtf("Album (me)", libraryLists.joinToString())
libraryLists
}
else -> libraryLists
Expand All @@ -163,6 +146,33 @@ class LibraryListsFragment(
}
}

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(
image = item["images"].asJsonArray[1].asJsonObject["url"].asString,
name = item["name"].asString,
artists = item["artists"].asJsonArray
.joinToString { it.asJsonObject["name"].asString },
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.archrahkshi.spotifine.R
import com.archrahkshi.spotifine.data.LyricsAdapter
import kotlinx.android.synthetic.main.fragment_lyrics.*
import kotlinx.android.synthetic.main.fragment_lyrics.buttonTranslate
import kotlinx.android.synthetic.main.fragment_lyrics.recyclerViewLyrics

class LyricsFragment(private var isLyricsTranslated: Boolean) : Fragment() {

Expand Down
10 changes: 7 additions & 3 deletions app/src/main/java/com/archrahkshi/spotifine/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,20 @@ class MainActivity : AppCompatActivity() {
if (requestCode == REQUEST_CODE) {
val response: AuthorizationResponse? =
AuthorizationClient.getResponse(resultCode, intent)
val accessToken = response?.accessToken
Log.i("Access token", accessToken.toString())
when (response?.type) {
AuthorizationResponse.Type.TOKEN -> {
Log.i("Token", "OK")
startActivity(
Intent(this, LibraryActivity::class.java).apply {
putExtra(ACCESS_TOKEN, accessToken)
putExtra(
ACCESS_TOKEN,
response.accessToken.also {
Log.i("Access token", it)
}
)
}
)
finish()
}
AuthorizationResponse.Type.ERROR -> Log.wtf("Token", response.error)
else -> Log.wtf("Token", "bullshit")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class PlayerActivity : AppCompatActivity() {
).commit()
}

override fun onStart() { // TODO: Когда-нибудь реализовать активный ползунок
override fun onStart() {
super.onStart()
val id = intent.getStringExtra(ID)
val duration = intent.getLongExtra(DURATION, 0)
Expand All @@ -56,7 +56,7 @@ class PlayerActivity : AppCompatActivity() {
seekBar.setOnSeekBarChangeListener(
object : OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar, i: Int, b: Boolean) {
if (flag == 0) { // TODO: Убрать этот кривой костыль
if (flag == 0) {
seekBar.progress = 0
} else {
appRemote.playerApi.seekTo(seekBar.progress.toLong())
Expand Down
21 changes: 18 additions & 3 deletions app/src/main/java/com/archrahkshi/spotifine/ui/TracksFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,20 @@ import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.archrahkshi.spotifine.R
import com.archrahkshi.spotifine.data.*
import com.archrahkshi.spotifine.data.ACCESS_TOKEN
import com.archrahkshi.spotifine.data.ALBUM_FROM_PLAYLIST_DISTINCTION
import com.archrahkshi.spotifine.data.DURATION
import com.archrahkshi.spotifine.data.ID
import com.archrahkshi.spotifine.data.IMAGE
import com.archrahkshi.spotifine.data.NAME
import com.archrahkshi.spotifine.data.Track
import com.archrahkshi.spotifine.data.TracksAdapter
import com.archrahkshi.spotifine.data.URL
import com.bumptech.glide.Glide
import com.google.gson.JsonParser
import kotlinx.android.synthetic.main.fragment_tracks.*
import kotlinx.android.synthetic.main.fragment_tracks.imageViewHeader
import kotlinx.android.synthetic.main.fragment_tracks.recyclerViewTracks
import kotlinx.android.synthetic.main.fragment_tracks.textViewHeaderLine1
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -82,7 +92,12 @@ class TracksFragment(
) = withContext(Dispatchers.IO) {
val tracks = mutableListOf<Track>()
val json = getJsonFromApi(url, accessToken)
when (json["href"].asString.removePrefix("https://api.spotify.com/v1/").take(5)) {
when (
json["href"]
.asString
.removePrefix("https://api.spotify.com/v1/")
.take(ALBUM_FROM_PLAYLIST_DISTINCTION)
) {
"album" -> {
json["items"].asJsonArray.forEach { element ->
val item = element.asJsonObject
Expand Down
Loading

0 comments on commit 5b0d418

Please sign in to comment.