-
Notifications
You must be signed in to change notification settings - Fork 0
Offline mode implementation #277
Changes from 3 commits
826092f
5c09b57
89e1ad2
08c7156
2c8a341
2e149d8
1c418ce
8b297d0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package ch.sdp.vibester.model | ||
|
||
import androidx.test.platform.app.InstrumentationRegistry | ||
import ch.sdp.vibester.api.LastfmMethod | ||
import org.junit.Assert | ||
import org.junit.Test | ||
|
||
class OfflineSongListTest { | ||
|
||
@Test | ||
fun noSongsAvailableInitialization() { | ||
val context = InstrumentationRegistry.getInstrumentation().targetContext.applicationContext | ||
val mySongsList = OfflineSongList(context, LastfmMethod.BY_TAG.method) | ||
|
||
val inputSongsList = mutableListOf<Pair<String, String>>() | ||
val page = "1" | ||
val songsPerPage = "100" | ||
val totalPages = "20" | ||
val totalSongs = "2000" | ||
|
||
|
||
Assert.assertEquals(inputSongsList, mySongsList.getSongList()) | ||
Assert.assertEquals(page, mySongsList.getPage()) | ||
Assert.assertEquals(songsPerPage, mySongsList.getSongsPerPage()) | ||
Assert.assertEquals(totalPages, mySongsList.getTotalPages()) | ||
Assert.assertEquals(totalSongs, mySongsList.getTotalSongs()) | ||
Assert.assertTrue(mySongsList.getEmptySongs()) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -204,8 +204,30 @@ class DownloadActivity : AppCompatActivity() { | |
records.createNewFile() | ||
} | ||
records.appendText("$songName\n") | ||
/* TODO: OFFLINE | ||
recordProperties() | ||
*/ | ||
} | ||
|
||
/** | ||
* Records the properties of a song. | ||
* Order of storage: Track name - artist name - artwork URL - preview URL. | ||
*/ | ||
/* TODO: OFFLINE | ||
private fun recordProperties() { | ||
var properties = File(applicationContext.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "properties.txt") | ||
|
||
if(!properties.exists()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a space after if |
||
properties.createNewFile() | ||
} | ||
|
||
val trackName = song.getTrackName() | ||
val artistName = song.getArtistName() | ||
val artworkURL = song.getArtworkUrl() | ||
val previewURL = song.getPreviewUrl() | ||
properties.appendText("$trackName - $artistName - $artworkURL - $previewURL\n") | ||
} | ||
*/ | ||
|
||
fun switchToDeleteSongs(view: View) { | ||
val intent = Intent(this, DeleteSongsActivity::class.java) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ class BuzzerGameManager: GameManager() { | |
lateinit var score: BuzzerScoreUpdater | ||
private lateinit var mediaPlayer: CompletableFuture<MediaPlayer> | ||
lateinit var scoreUpdater: BuzzerScoreUpdater | ||
private var hasInternet: Boolean = true | ||
|
||
/** | ||
* Check if mediaPlayer is initialized | ||
|
@@ -22,7 +23,15 @@ class BuzzerGameManager: GameManager() { | |
* Play current song with media player. | ||
*/ | ||
fun playSong() { | ||
mediaPlayer = AudioPlayer.playAudio(currentSong.getPreviewUrl()) | ||
if(hasInternet) { | ||
mediaPlayer = AudioPlayer.playAudio(currentSong.getPreviewUrl()) | ||
} else { | ||
mediaPlayer = AudioPlayer.playAudio(currentSong.getPreviewUrl()) | ||
/* TODO: OFFLINE | ||
var media = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "extract_of_$currentSong.getTrackName()") | ||
mediaPlayer = AudioPlayer.playAudio(media.absolutePath) | ||
*/ | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you just put these modifications into GameManager instead? I deleted BuzzerGameManager when refactoring and put all the equivalent functions into GameManager instead so you should find it there There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done on local, will push soon once I make sure the merge was successful |
||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package ch.sdp.vibester.model | ||
|
||
import android.content.Context | ||
import android.os.Environment | ||
import ch.sdp.vibester.api.LastfmMethod | ||
import java.io.BufferedReader | ||
import java.io.File | ||
import java.io.FileReader | ||
|
||
/** | ||
* Process the fetched data from the external storage of the app. | ||
* Mainly, it creates a list of songs in the form Pair("$songName", "$artistName") | ||
* @param ctx: Context of the caller, to fetch the downloaded folder | ||
* @param method: Chosen playlist type | ||
*/ | ||
class OfflineSongList(ctx: Context, method: String) { | ||
private var songList = mutableListOf<Pair<String, String>>() | ||
private var page = "" | ||
private var songsPerPage = "" | ||
private var totalPages = "" | ||
private var totalSongs = "" | ||
private var context = ctx | ||
private var emptySongs: Boolean = false | ||
|
||
init { | ||
try { | ||
var tracksField = "tracks" | ||
if (method == LastfmMethod.BY_ARTIST.method) { | ||
tracksField = "toptracks" | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. extra spaces |
||
fillList() | ||
|
||
page = "1" | ||
songsPerPage = "100" | ||
totalPages = "20" | ||
totalSongs = "2000" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These vars don't seem to be modified anywhere in the class: maybe it'd be more readable to turn them into private vals / constants that you directly initialise as they're declared. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True, will do it |
||
|
||
} catch (e: Exception) { | ||
throw IllegalArgumentException("OfflineSongList constructor, failed reading from file or empty file!") | ||
} | ||
} | ||
|
||
/** | ||
* Adds from the downloads to the list of songs ("$songName", "$artistName") | ||
* Saves the list of songs in songList | ||
*/ | ||
private fun fillList() { | ||
var records = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "records.txt") | ||
|
||
|
||
if(!records.exists() || records.length() == 0L) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a space after if for readability There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done on local |
||
//There are no downloaded songs, keep the song list empty | ||
emptySongs = true | ||
} else { | ||
var reader = BufferedReader(FileReader(records)) | ||
var currentLine = reader.readLine() | ||
|
||
while(currentLine != null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a space after while |
||
var trimmed = currentLine.trim() | ||
if(trimmed.isNotEmpty()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a space after if |
||
val split = trimmed.split("-") | ||
if(split.size == 2) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a space after if |
||
songList.add(Pair(split[0], split[1])) | ||
} | ||
} | ||
currentLine = reader.readLine() | ||
} | ||
reader.close() | ||
emptySongs = false | ||
} | ||
} | ||
|
||
/** | ||
* Getter that return songs for the given tag | ||
* @return MutableList<Pair<String,String>> of type Pair("$songName", "$artistName") | ||
*/ | ||
fun getSongList(): MutableList<Pair<String, String>> { | ||
return songList | ||
} | ||
|
||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar blocks of code found in 2 locations. Consider refactoring. |
||
* Getter that return shuffled song list | ||
* @return MutableList<Pair<String,String>> of type Pair("$songName", "$artistName") | ||
*/ | ||
fun getShuffledSongList(): MutableList<Pair<String, String>> { | ||
return songList.asSequence().shuffled().toMutableList() | ||
} | ||
|
||
/** | ||
* Getter that return page number from the query | ||
* @return String page | ||
*/ | ||
fun getPage(): String { | ||
return page | ||
} | ||
|
||
/** | ||
* Getter that return total number of songs in the tag | ||
* @return String totalSongs | ||
*/ | ||
fun getTotalSongs(): String { | ||
return totalSongs | ||
} | ||
|
||
/** | ||
* Getter that return the total number of pages with songs by tag | ||
* @return String totalPages | ||
*/ | ||
fun getTotalPages(): String { | ||
return totalPages | ||
} | ||
|
||
/** | ||
* Getter that return the total number of songs in the page | ||
* @return String perPage | ||
*/ | ||
fun getSongsPerPage(): String { | ||
return songsPerPage | ||
} | ||
|
||
/** | ||
* Getter that returns whether there are downloaded songs available or not | ||
* @return Boolean emptySongs | ||
*/ | ||
fun getEmptySongs(): Boolean { | ||
return emptySongs | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extra space