Skip to content

Commit

Permalink
✨ Now shows song list with nice colors
Browse files Browse the repository at this point in the history
  • Loading branch information
yhs0602 committed Jan 27, 2020
1 parent f5a4947 commit 5f0f5f0
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 57 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ dependencies {
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.3'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.3'
implementation 'com.github.tingyik90:snackprogressbar:6.2.0'
implementation 'com.github.doyaaaaaken:kotlin-csv-jvm:0.7.3'

Expand Down
83 changes: 60 additions & 23 deletions app/src/main/java/com/kyhsgeekcode/dereinfo/SongListActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,27 @@ package com.kyhsgeekcode.dereinfo

import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.snackbar.Snackbar
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView

import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.snackbar.Snackbar
import com.kyhsgeekcode.dereinfo.dummy.DummyContent
import com.kyhsgeekcode.dereinfo.model.DereDatabaseHelper
import com.kyhsgeekcode.dereinfo.model.MusicInfo
import com.kyhsgeekcode.dereinfo.model.getColor
import com.kyhsgeekcode.dereinfo.model.makeRGB
import com.tingyik90.snackprogressbar.SnackProgressBar
import com.tingyik90.snackprogressbar.SnackProgressBarManager
import kotlinx.android.synthetic.main.activity_song_list.*
import kotlinx.android.synthetic.main.song_list_content.view.*
import kotlinx.android.synthetic.main.song_list.*
import kotlinx.coroutines.*
import kotlinx.android.synthetic.main.song_list_content.view.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**
* An activity representing a list of Pings. This activity
Expand All @@ -26,8 +33,17 @@ import kotlinx.coroutines.*
* item details side-by-side using two vertical panes.
*/
class SongListActivity : AppCompatActivity() {
private val snackProgressBarManager by lazy {
SnackProgressBarManager(
mainListLayout,
lifecycleOwner = this
)
}
val circularType =
SnackProgressBar(SnackProgressBar.TYPE_CIRCULAR, "Loading...")
.setIsIndeterminate(false)
.setAllowUserInput(true)
private lateinit var dereDatabaseHelper: DereDatabaseHelper

/**
* Whether or not the activity is in two-pane mode, i.e. running on a tablet
* device.
Expand All @@ -36,9 +52,6 @@ class SongListActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
CoroutineScope(Dispatchers.IO).launch {
dereDatabaseHelper = DereDatabaseHelper(this@SongListActivity)
}
setContentView(R.layout.activity_song_list)


Expand All @@ -57,34 +70,51 @@ class SongListActivity : AppCompatActivity() {
// activity should be in two-pane mode.
twoPane = true
}
setupRecyclerView(song_list)
val adapter = setupRecyclerView(song_list)
snackProgressBarManager.show(circularType, SnackProgressBarManager.LENGTH_INDEFINITE)
CoroutineScope(Dispatchers.IO).launch {
dereDatabaseHelper.parseDatabases({ current, total ->

}) {}
dereDatabaseHelper = DereDatabaseHelper(this@SongListActivity)
dereDatabaseHelper.parseDatabases({ current, total, musicInfo ->
CoroutineScope(Dispatchers.Main).launch {
adapter.addItem(musicInfo)
}
circularType.setProgressMax(total)
CoroutineScope(Dispatchers.Main).launch {
snackProgressBarManager.setProgress(current)
}
}) {
snackProgressBarManager.dismiss()
}
}
}

private fun setupRecyclerView(recyclerView: RecyclerView) {
recyclerView.adapter = SongRecyclerViewAdapter(this, DummyContent.ITEMS, twoPane)
override fun onDestroy() {
super.onDestroy()
snackProgressBarManager.disable()
}

private fun setupRecyclerView(recyclerView: RecyclerView): SongRecyclerViewAdapter {
val adapter = SongRecyclerViewAdapter(this, twoPane)
recyclerView.adapter = adapter
return adapter
}

class SongRecyclerViewAdapter(
private val parentActivity: SongListActivity,
private val values: List<DummyContent.DummyItem>,
private val twoPane: Boolean
) :
RecyclerView.Adapter<SongRecyclerViewAdapter.ViewHolder>() {

private val onClickListener: View.OnClickListener
private val values: MutableList<MusicInfo> = ArrayList()

init {
onClickListener = View.OnClickListener { v ->
val item = v.tag as DummyContent.DummyItem
val item = v.tag as MusicInfo
if (twoPane) {
val fragment = SongDetailFragment().apply {
arguments = Bundle().apply {
putString(SongDetailFragment.ARG_ITEM_ID, item.id)
putString(SongDetailFragment.ARG_ITEM_ID, item.name)
}
}
parentActivity.supportFragmentManager
Expand All @@ -93,7 +123,7 @@ class SongListActivity : AppCompatActivity() {
.commit()
} else {
val intent = Intent(v.context, SongDetailActivity::class.java).apply {
putExtra(SongDetailFragment.ARG_ITEM_ID, item.id)
putExtra(SongDetailFragment.ARG_ITEM_ID, item.name)
}
v.context.startActivity(intent)
}
Expand All @@ -108,20 +138,27 @@ class SongListActivity : AppCompatActivity() {

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = values[position]
holder.idView.text = item.id
holder.contentView.text = item.content

holder.idView.text = item.name.replace("\\n"," ")
holder.contentView.text = item.composer
holder.backgroundLayout.setBackgroundColor(makeRGB(getColor(item.circleType)))
with(holder.itemView) {
tag = item
setOnClickListener(onClickListener)
}
}


override fun getItemCount() = values.size

fun addItem(item: MusicInfo) {
values.add(item)
notifyDataSetChanged()
}

inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val idView: TextView = view.id_text
val contentView: TextView = view.content
val backgroundLayout : LinearLayout = view.listitem_background
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class DereDatabaseHelper(context: Context) {
var maxlen = 0L
var fumensDBFileTmp: File? = null
for (file in fumenFolder.listFiles()) {
Log.d(TAG,file.name)
//Log.d(TAG, file.name)
val len = file.length()
if (len > maxlen) {
if (len > 10000000) {
Expand All @@ -36,13 +36,13 @@ class DereDatabaseHelper(context: Context) {
break
}
}
Log.d(TAG, """$len""")
//Log.d(TAG, """$len""")
}
Log.d(TAG, "maxlen=${maxlen/1000}")
Log.d(TAG, "maxlen=${maxlen / 1000}")
fumensDBFile = fumensDBFileTmp ?: error("No fumen file found")
}

suspend fun parseDatabases(publisher: (Int, Int) -> Unit, onFinish: () -> Unit) {
suspend fun parseDatabases(publisher: (Int, Int, MusicInfo) -> Unit, onFinish: () -> Unit) {
val fumensDB =
SQLiteDatabase.openDatabase(fumensDBFile.path, null, SQLiteDatabase.OPEN_READONLY)

Expand All @@ -52,7 +52,7 @@ class DereDatabaseHelper(context: Context) {
val cursorLiveData =
fumensDB.query(
"live_data",
arrayOf("id", "music_data_id"),
arrayOf("id", "music_data_id","circle_type"),
null,
null,
null,
Expand All @@ -66,8 +66,6 @@ class DereDatabaseHelper(context: Context) {
val totalCount = cursorLiveData.count
var currentCount = 0
while (cursorLiveData.moveToNext()) {
currentCount++
publisher(currentCount, totalCount)
val musicDataId = cursorLiveData.getInt(musicDataIdIndex)
val cursorMusicData = fumensDB.query(
"music_data",
Expand All @@ -86,33 +84,43 @@ class DereDatabaseHelper(context: Context) {
null,
null
)
cursorMusicData.moveToFirst()
// Log.d(TAG, cursorMusicData.getColumnName(0))
// Log.d(TAG, cursorMusicData.getColumnName(1))
// Log.d(TAG, cursorMusicData.getColumnName(2))
// Log.d(TAG, cursorMusicData.getColumnName(3))
// Log.d(TAG, cursorMusicData.getColumnName(4))
// Log.d(TAG, cursorMusicData.getColumnName(5))

val musicNameIndex = cursorMusicData.getColumnIndex("name")
val name = cursorMusicData.getString(musicNameIndex)
val name = cursorMusicData.getString(1)
val composerIndex = cursorMusicData.getColumnIndex("composer")
val composer = cursorMusicData.getString(composerIndex)
val composer = cursorMusicData.getString(3)
val bpmIndex = cursorMusicData.getColumnIndex("bpm")
val bpm = cursorMusicData.getInt(bpmIndex)
val bpm = cursorMusicData.getInt(2)
val lyricistIndex = cursorMusicData.getColumnIndex("lyricist")
val lyricist = cursorMusicData.getString(lyricistIndex)
val lyricist = cursorMusicData.getString(4)
val soundOffsetIndex = cursorMusicData.getColumnIndex("sound_offset")
val soundOffset = cursorMusicData.getInt(soundOffsetIndex)
val soundOffset = cursorMusicData.getInt(5)
val soundLengthIndex = cursorMusicData.getColumnIndex("sound_length")
val soundLength = cursorMusicData.getInt(soundLengthIndex)
val soundLength = cursorMusicData.getInt(6)
cursorMusicData.close()
val liveDataId = cursorLiveData.getInt(liveDataIdIndex)
val circleType = cursorLiveData.getInt(circleTypeIndex)
fumenIDToMusicID[liveDataId] = musicDataId
musicIDToInfo[musicDataId] =
MusicInfo(
musicDataId,
name,
bpm,
composer,
lyricist,
soundOffset,
soundLength,
circleType
)
val musicInfo = MusicInfo(
musicDataId,
name,
bpm,
composer,
lyricist,
soundOffset,
soundLength,
circleType
)
musicIDToInfo[musicDataId] = musicInfo
currentCount++
publisher(currentCount, totalCount, musicInfo)
}
cursorLiveData.close()
onFinish()
Expand Down
16 changes: 10 additions & 6 deletions app/src/main/java/com/kyhsgeekcode/dereinfo/model/DereToTW.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.kyhsgeekcode.dereinfo.model

import android.graphics.Color

//None left right up down
fun getTW5Flick(status: Int): Int {
return when (status) {
Expand All @@ -25,10 +27,12 @@ fun getTWMode(mode: Int): Int {

fun getColor(circleType: Int): Array<Int> {
return when (circleType) {
1 -> arrayOf(255, 0, 0, 255) // red
2 -> arrayOf(0, 0, 255, 255) // blue
3 -> arrayOf(255, 255, 0, 255) // yellow
4 -> arrayOf(255, 255, 255, 255) // white
else -> arrayOf(0,0,0,255)
1 -> arrayOf(255, 0x74, 0x77, 255) // red
2 -> arrayOf(0x53, 0x6A, 0xDE, 255) // blue
3 -> arrayOf(0xFA, 0xCC, 0x43, 255) // yellow
4 -> arrayOf(0xC7, 0xF9, 0xF4, 255) // white
else -> arrayOf(0, 0, 0, 255)
}
}
}

fun makeRGB(color: Array<Int>): Int = Color.argb(color[3],color[0],color[1],color[2])
1 change: 1 addition & 0 deletions app/src/main/res/layout/activity_song_list.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainListLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
Expand Down
9 changes: 5 additions & 4 deletions app/src/main/res/layout/song_list_content.xml
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:id="@+id/listitem_background"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:orientation="vertical">

<TextView
android:id="@+id/id_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:textAppearance="?attr/textAppearanceListItem" />
android:textAppearance="@style/TextAppearance.AppCompat.Large" />

<TextView
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:textAppearance="?attr/textAppearanceListItem" />
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
</LinearLayout>

0 comments on commit 5f0f5f0

Please sign in to comment.