Implemented functionality to store the repeat settings #35
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* Automatically generated file. DO NOT MODIFY
|
||||
*/
|
||||
package com.example.mear;
|
||||
|
||||
public final class BuildConfig {
|
||||
public static final boolean DEBUG = Boolean.parseBoolean("true");
|
||||
public static final String APPLICATION_ID = "com.example.mear";
|
||||
public static final String BUILD_TYPE = "debug";
|
||||
public static final String FLAVOR = "";
|
||||
public static final int VERSION_CODE = 1;
|
||||
public static final String VERSION_NAME = "1.0";
|
||||
}
|
||||
@@ -35,14 +35,17 @@ import com.example.mear.playback.service.MusicService
|
||||
import com.example.mear.models.PlayControls
|
||||
import com.example.mear.R
|
||||
import com.example.mear.constants.ControlTypes
|
||||
import com.example.mear.repositories.RepeatRepository
|
||||
import com.example.mear.repositories.ShuffleRepository
|
||||
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
|
||||
private var musicService: MusicService? = null
|
||||
private var musicHandler: Handler? = Handler()
|
||||
private var serviceBinded: Boolean? = false
|
||||
private var repeatOn: Boolean? = false
|
||||
private var shuffleOn: Boolean? = false
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
@@ -86,6 +89,7 @@ class MainActivity : AppCompatActivity() {
|
||||
try {
|
||||
window.statusBarColor = resources.getColor(R.color.track_seek)
|
||||
initializeShuffle()
|
||||
initializeRepeat()
|
||||
initializeServices()
|
||||
initializeClickListeners()
|
||||
}
|
||||
@@ -136,6 +140,17 @@ class MainActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
}
|
||||
private fun initializeRepeat() {
|
||||
val repeatMode = RepeatRepository(this).getRepeatMode()
|
||||
when (repeatMode) {
|
||||
ControlTypes.REPEAT_ON -> {
|
||||
RepeatTrack.text = resources.getText(R.string.repeat_on)
|
||||
}
|
||||
ControlTypes.REPEAT_OFF -> {
|
||||
RepeatTrack.text = resources.getText(R.string.repeat_off)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun toggleShuffle() {
|
||||
val shuffleText = ShuffleTracks.text.toString()
|
||||
@@ -176,6 +191,8 @@ class MainActivity : AppCompatActivity() {
|
||||
RepeatTrack.text = off
|
||||
}
|
||||
}
|
||||
val playC = PlayControls(null, repeatOn)
|
||||
RepeatRepository(this).updateRepeatMode(playC)
|
||||
}
|
||||
|
||||
private fun playSongTrack() {
|
||||
@@ -196,8 +213,6 @@ class MainActivity : AppCompatActivity() {
|
||||
private fun playNextSongTrack() {
|
||||
NextTrack.isEnabled = false
|
||||
try {
|
||||
//val controls = PlayControls(shuffleOn!!, repeatOn!!)
|
||||
//musicService!!.playNextTrack(controls)
|
||||
musicService!!.playNextTrack()
|
||||
|
||||
configureTrackDisplay()
|
||||
@@ -211,8 +226,6 @@ class MainActivity : AppCompatActivity() {
|
||||
private fun playPreviousSongTrack() {
|
||||
PreviousTrack.isEnabled = false
|
||||
try {
|
||||
//val controls = PlayControls(shuffleOn!!, repeatOn!!)
|
||||
//musicService!!.playPreviousTrack(controls)
|
||||
musicService!!.playPreviousTrack()
|
||||
|
||||
configureTrackDisplay()
|
||||
@@ -247,7 +260,6 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
resetControls()
|
||||
|
||||
|
||||
TrackTitle.text = trackTitle
|
||||
ArtistTitle.text = artistTitle
|
||||
AlbumTitle.text = albumTitle
|
||||
@@ -365,9 +377,4 @@ class MainActivity : AppCompatActivity() {
|
||||
else {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
private var musicHandler: Handler? = Handler()
|
||||
private var shuffleOn: Boolean? = false
|
||||
}
|
||||
|
||||
@@ -3,4 +3,6 @@ package com.example.mear.constants
|
||||
object ControlTypes {
|
||||
const val SHUFFLE_ON = "Shuffle On"
|
||||
const val SHUFFLE_OFF = "Shuffle Off"
|
||||
const val REPEAT_ON = "Repeat On"
|
||||
const val REPEAT_OFF = "Repeat Off"
|
||||
}
|
||||
@@ -56,19 +56,25 @@ class DatabaseManager(ctx: Context): ManagedSQLiteOpenHelper(ctx, "Mear",
|
||||
"Id" to INTEGER + PRIMARY_KEY + UNIQUE,
|
||||
"Mode" to TEXT
|
||||
)
|
||||
db!!.createTable(
|
||||
"Repeat", true,
|
||||
"Id" to INTEGER + PRIMARY_KEY + UNIQUE,
|
||||
"Mode" to TEXT)
|
||||
}
|
||||
catch (ex: Exception) {
|
||||
}
|
||||
initializeShuffle(db)
|
||||
initializeRepeat(db)
|
||||
}
|
||||
|
||||
override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
|
||||
try {
|
||||
db!!.dropTable("Track")
|
||||
db!!.dropTable("TrackCount")
|
||||
db!!.dropTable("PlayCount")
|
||||
db!!.dropTable("Settings")
|
||||
db!!.dropTable("Shuffle")
|
||||
db!!.dropTable("Track")
|
||||
db!!.dropTable("TrackCount")
|
||||
db!!.dropTable("PlayCount")
|
||||
db!!.dropTable("Settings")
|
||||
db!!.dropTable("Shuffle")
|
||||
db!!.dropTable("Repeat")
|
||||
}
|
||||
catch (ex: Exception) {
|
||||
}
|
||||
@@ -76,13 +82,12 @@ class DatabaseManager(ctx: Context): ManagedSQLiteOpenHelper(ctx, "Mear",
|
||||
|
||||
|
||||
private fun initializeShuffle(db: SQLiteDatabase?) {
|
||||
try {
|
||||
db!!.insert("Shuffle",
|
||||
"Mode" to ControlTypes.SHUFFLE_OFF)
|
||||
}
|
||||
catch (ex: Exception) {
|
||||
val exMsg = ex.message
|
||||
}
|
||||
db!!.insert("Shuffle",
|
||||
"Mode" to ControlTypes.SHUFFLE_OFF)
|
||||
}
|
||||
private fun initializeRepeat(db: SQLiteDatabase?) {
|
||||
db!!.insert("Repeat",
|
||||
"Mode" to ControlTypes.REPEAT_OFF)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import android.content.Intent
|
||||
import android.media.MediaPlayer
|
||||
import android.os.Binder
|
||||
import android.os.IBinder
|
||||
import android.support.v4.media.VolumeProviderCompat
|
||||
import android.widget.Toast
|
||||
|
||||
import java.lang.Exception
|
||||
@@ -15,8 +16,10 @@ import com.example.mear.management.MusicFiles
|
||||
import com.example.mear.management.TrackManager
|
||||
import com.example.mear.models.PlayControls
|
||||
import com.example.mear.models.Track
|
||||
import com.example.mear.repositories.RepeatRepository
|
||||
import com.example.mear.repositories.ShuffleRepository
|
||||
import com.example.mear.repositories.TrackRepository
|
||||
import java.util.*
|
||||
|
||||
|
||||
class MusicService: Service() {
|
||||
@@ -178,6 +181,7 @@ class MusicService: Service() {
|
||||
|
||||
private fun fetchSongIndex(playType: PlayTypes): Int {
|
||||
var songIndex: Int? = currentSongIndex
|
||||
repeatOn = retrieveRepeatMode()
|
||||
if (repeatOn!!) {
|
||||
return songIndex!!
|
||||
}
|
||||
@@ -261,6 +265,7 @@ class MusicService: Service() {
|
||||
val trackMgr = TrackManager(paths!!)
|
||||
trackMgr.configureTracks(this)
|
||||
initializeShuffleMode()
|
||||
initializeRepeatMode()
|
||||
}
|
||||
private fun initializeShuffleMode() {
|
||||
try {
|
||||
@@ -281,6 +286,25 @@ class MusicService: Service() {
|
||||
val exMsg = ex.message
|
||||
}
|
||||
}
|
||||
private fun initializeRepeatMode() {
|
||||
try {
|
||||
val repeatMode = RepeatRepository(this).getRepeatMode()
|
||||
when (repeatMode) {
|
||||
ControlTypes.REPEAT_ON -> {
|
||||
repeatOn = true
|
||||
}
|
||||
ControlTypes.REPEAT_OFF -> {
|
||||
repeatOn = false
|
||||
}
|
||||
null -> {
|
||||
repeatOn = false
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (ex: Exception) {
|
||||
val exMsg = ex.message
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private fun retrieveShuffleMode(): Boolean? {
|
||||
@@ -300,6 +324,23 @@ class MusicService: Service() {
|
||||
}
|
||||
}
|
||||
}
|
||||
private fun retrieveRepeatMode(): Boolean? {
|
||||
val repeatMode = RepeatRepository(this).getRepeatMode()
|
||||
when (repeatMode) {
|
||||
ControlTypes.REPEAT_ON -> {
|
||||
return true
|
||||
}
|
||||
ControlTypes.REPEAT_OFF -> {
|
||||
return false
|
||||
}
|
||||
null -> {
|
||||
return null
|
||||
}
|
||||
else -> {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun trackRepositoryEmpty(): Boolean? {
|
||||
val trackCount = retrieveSongCount()
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.example.mear.repositories
|
||||
|
||||
import android.content.Context
|
||||
|
||||
import org.jetbrains.anko.db.*
|
||||
|
||||
import com.example.mear.constants.ControlTypes
|
||||
import com.example.mear.database
|
||||
import com.example.mear.models.PlayControls
|
||||
|
||||
class RepeatRepository {
|
||||
var context: Context? = null
|
||||
|
||||
|
||||
constructor(context: Context) {
|
||||
this.context = context
|
||||
}
|
||||
|
||||
|
||||
fun getRepeatMode(): String = context!!.database.use {
|
||||
select("Repeat").limit(1)
|
||||
.parseSingle(object: MapRowParser<String>{
|
||||
override fun parseRow(columns: Map<String, Any?>): String {
|
||||
|
||||
return columns.getValue("Mode").toString()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fun updateRepeatMode(playControls: PlayControls?) = context!!.database.use {
|
||||
var repeatMode = ControlTypes.REPEAT_OFF
|
||||
if (playControls!!.repeatOn!!) {
|
||||
repeatMode = ControlTypes.REPEAT_ON
|
||||
}
|
||||
update("Repeat",
|
||||
"Mode" to repeatMode).exec()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user