Working on implementing SQLite Database #16
This commit is contained in:
@@ -1,21 +1,25 @@
|
||||
package com.example.mear
|
||||
|
||||
import android.graphics.Bitmap
|
||||
import android.content.Context
|
||||
import android.database.sqlite.SQLiteDatabase
|
||||
import android.database.sqlite.SQLiteQuery
|
||||
import android.graphics.BitmapFactory
|
||||
import android.media.MediaPlayer
|
||||
import android.os.Bundle
|
||||
import android.os.Environment
|
||||
import android.support.design.widget.Snackbar
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
|
||||
import java.lang.Exception
|
||||
import kotlinx.android.synthetic.main.activity_main.*
|
||||
import kotlinx.android.synthetic.main.content_main.*
|
||||
import kotlinx.coroutines.selects.select
|
||||
import kotlin.io.*
|
||||
import kotlin.random.Random
|
||||
|
||||
import org.jetbrains.anko.*
|
||||
import org.jetbrains.anko.db.*
|
||||
|
||||
import com.example.mear.management.TrackRepository
|
||||
import com.example.mear.management.DatabaseManager
|
||||
import com.example.mear.management.MusicFiles
|
||||
import com.example.mear.management.TrackManager
|
||||
import com.example.mear.models.Track
|
||||
@@ -173,13 +177,65 @@ class MainActivity : AppCompatActivity() {
|
||||
return allSongs!!
|
||||
}
|
||||
private fun loadTracks(allSongs: MutableList<String>) {
|
||||
val trackMgr = TrackManager(allSongs!!)
|
||||
var trackMgr = TrackManager(allSongs!!)
|
||||
trackMgr.configureTracks()
|
||||
allTracks = trackMgr.allTracks
|
||||
val allTracks = trackMgr.allTracks
|
||||
trackMgr = TrackManager( listOf())
|
||||
this.allTracks = allTracks
|
||||
|
||||
databaseInit(allTracks!!)
|
||||
val obk = TrackRepository(this).getAll()
|
||||
|
||||
currentSong = fetchSongIndex(PlayTypes.PlaySong)
|
||||
}
|
||||
|
||||
private fun databaseInit(allTracks: MutableList<Track>) {
|
||||
/**
|
||||
database.use {
|
||||
dropTable("Track")
|
||||
createTable("Track", true,
|
||||
"Id" to INTEGER + PRIMARY_KEY + UNIQUE,
|
||||
"Title" to INTEGER, "Album" to TEXT, "Artist" to TEXT,
|
||||
"Duration" to INTEGER, "Cover" to BLOB, "SongPath" to TEXT
|
||||
)
|
||||
for (songData in allTracks) {
|
||||
insert("Track",
|
||||
"id" to songData.id,
|
||||
"Title" to songData.title,
|
||||
"Album" to songData.album,
|
||||
"Artist" to songData.artist,
|
||||
"Duration" to songData.length,
|
||||
"Cover" to songData.TrackCover,
|
||||
"FilePath" to songData.songPath)
|
||||
}
|
||||
}
|
||||
*/
|
||||
/**
|
||||
myDB.createTable("Track", true,
|
||||
"Id" to INTEGER + UNIQUE,
|
||||
"Title" to TEXT, "Artist" to TEXT, "Album" to TEXT,
|
||||
"Duration" to INTEGER, "Cover" to BLOB, "SongPath" to TEXT)
|
||||
*/
|
||||
|
||||
|
||||
TrackRepository(this).delete()
|
||||
for (songData in allTracks) {
|
||||
TrackRepository(this).create(songData)
|
||||
}
|
||||
}
|
||||
/**
|
||||
private fun databaseGetTracks(db: ManagedSQLiteOpenHelper): List<Track> = db.use {
|
||||
val trackParser = classParser<Track>()
|
||||
var dbObj = db.readableDatabase
|
||||
dbObj.use {
|
||||
select("Track").exec {
|
||||
val ss = this
|
||||
val tracks: List<Track> = parseList()
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
private var TrackPayer: MediaPlayer? = null
|
||||
private var allTracks: MutableList<Track>? = null
|
||||
private var currentSong: Int? = null
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.example.mear
|
||||
|
||||
import android.content.Context
|
||||
|
||||
import com.example.mear.management.DatabaseManager
|
||||
|
||||
val Context.database: DatabaseManager
|
||||
get() = DatabaseManager.getInstance(applicationContext)
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.example.mear.management
|
||||
|
||||
import android.content.Context
|
||||
import android.database.sqlite.SQLiteDatabase
|
||||
|
||||
import org.jetbrains.anko.db.*
|
||||
import org.jetbrains.anko.db.ManagedSQLiteOpenHelper
|
||||
|
||||
class DatabaseManager(ctx: Context): ManagedSQLiteOpenHelper(ctx, "Mear",
|
||||
null, 1) {
|
||||
companion object {
|
||||
private var instance: DatabaseManager? = null
|
||||
|
||||
@Synchronized
|
||||
fun getInstance(ctx: Context): DatabaseManager {
|
||||
if (instance == null) {
|
||||
instance = DatabaseManager(ctx.applicationContext)
|
||||
}
|
||||
return instance!!
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
override fun onCreate(db: SQLiteDatabase?) {
|
||||
//db!!.dropTable("Track")
|
||||
db!!.createTable("Track", true,
|
||||
"Id" to INTEGER + PRIMARY_KEY + UNIQUE,
|
||||
"Title" to TEXT, "Album" to TEXT, "Artist" to TEXT,
|
||||
"Cover" to BLOB, "Duration" to INTEGER,
|
||||
"FilePath" to TEXT)
|
||||
}
|
||||
|
||||
override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
|
||||
db!!.dropTable("Track")
|
||||
}
|
||||
|
||||
|
||||
private fun createSettingsTable() {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.example.mear.management
|
||||
|
||||
import android.os.Environment
|
||||
import java.io.File
|
||||
import java.lang.Exception
|
||||
import kotlin.io.*
|
||||
@@ -19,25 +18,22 @@ class MusicFiles (val demoPath: File) {
|
||||
val listOfFiles = folder.listFiles()
|
||||
|
||||
for (i in listOfFiles) {
|
||||
if (allSongs!!.count() < musicSongLimit) {
|
||||
for (j in i.listFiles()) {
|
||||
if (j.absolutePath.toString().contains("zip")) {
|
||||
println("zip file")
|
||||
}
|
||||
for (k in j.listFiles()) {
|
||||
println("What's good?")
|
||||
if (k.absolutePath.toString().contains("mp3")) {
|
||||
for (j in i.listFiles()) {
|
||||
if (j.absolutePath.toString().contains("zip")) {
|
||||
}
|
||||
for (k in j.listFiles()) {
|
||||
if (k.absolutePath.toString().contains("mp3")) {
|
||||
if (count < musicSongLimit) {
|
||||
allSongs!!.add(k.absolutePath.toString())
|
||||
count++
|
||||
} else {
|
||||
println("Stranger")
|
||||
}
|
||||
else
|
||||
{
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
break
|
||||
}
|
||||
}
|
||||
songCount = count
|
||||
}
|
||||
@@ -49,5 +45,5 @@ class MusicFiles (val demoPath: File) {
|
||||
|
||||
var allSongs: MutableList<String>?= null
|
||||
var songCount: Int? = null
|
||||
val musicSongLimit = 50
|
||||
val musicSongLimit = 120
|
||||
}
|
||||
@@ -1,17 +1,10 @@
|
||||
package com.example.mear.management
|
||||
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.BitmapFactory
|
||||
import android.media.MediaMetadataRetriever
|
||||
|
||||
import java.io.File
|
||||
import java.lang.Exception
|
||||
|
||||
import ealvatag.audio.AudioFile
|
||||
import ealvatag.audio.AudioFileIO
|
||||
import ealvatag.tag.FieldKey
|
||||
import ealvatag.tag.NullTag
|
||||
|
||||
import com.example.mear.models.Track
|
||||
|
||||
class TrackManager(val allSongPath: List<String>) {
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.example.mear.management
|
||||
|
||||
import android.content.Context
|
||||
import com.example.mear.database
|
||||
|
||||
import java.util.ArrayList
|
||||
|
||||
import org.jetbrains.anko.db.*
|
||||
|
||||
import com.example.mear.management.*
|
||||
import com.example.mear.models.Track
|
||||
|
||||
class TrackRepository(val context: Context) {
|
||||
|
||||
fun getAll(): List<Track> = context.database.use {
|
||||
val tracks = mutableListOf<Track>()
|
||||
|
||||
select("Track", "id", "Title")
|
||||
.parseList(object : MapRowParser<Track>{
|
||||
override fun parseRow(columns: Map<String, Any?>): Track {
|
||||
val id = columns.getValue("Id")
|
||||
val title = columns.getValue("Title")
|
||||
|
||||
val track = Track(id.toString().toInt(), title.toString(),"","",0,
|
||||
ByteArray(0), "")
|
||||
tracks.add(track)
|
||||
|
||||
return track
|
||||
}
|
||||
})
|
||||
tracks
|
||||
}
|
||||
|
||||
fun create(track: Track) = context.database.use {
|
||||
insert("Track",
|
||||
"Id" to track.id,
|
||||
"Title" to track.title,
|
||||
"Album" to track.album,
|
||||
"Artist" to track.artist,
|
||||
"Duration" to track.length,
|
||||
"SongPath" to track.songPath,
|
||||
"Cover" to track.TrackCover)
|
||||
}
|
||||
|
||||
fun delete(track: Track) = context.database.use {
|
||||
delete("Track", whereClause = "id = {$track.id}")
|
||||
}
|
||||
fun delete() = context.database.use {
|
||||
delete("Track")
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,15 @@
|
||||
package com.example.mear.models
|
||||
|
||||
class Track (val id: Int, val title: String, val artist: String, val album: String,
|
||||
data class Track (val id: Int, val title: String, val artist: String, val album: String,
|
||||
val length: Int, val TrackCover: ByteArray, val songPath: String) {
|
||||
companion object {
|
||||
//val Track.COLUMN_ID = "id"
|
||||
val TABLE_NAME = "Track"
|
||||
val COLUMN_TITLE = "Title"
|
||||
val COLUMN_ARTIST = "Artist"
|
||||
val COLUMN_ALBUM = "Album"
|
||||
val COLUMN_DURATION = "Duration"
|
||||
val COLUMN_COVER = "Cover"
|
||||
val COLUMN_SONGPATH = "SongPath"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user