This repository has been archived by the owner on Jan 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #277 from MaximeZmt/tsathogguaa/offline_mode
Offline mode implementation
- Loading branch information
Showing
9 changed files
with
231 additions
and
13 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
app/src/androidTest/java/ch/sdp/vibester/model/OfflineSongListTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package ch.sdp.vibester.model | ||
|
||
import android.os.Environment | ||
import androidx.test.platform.app.InstrumentationRegistry | ||
import ch.sdp.vibester.api.LastfmMethod | ||
import org.junit.Assert | ||
import org.junit.Test | ||
import java.io.File | ||
|
||
class OfflineSongListTest { | ||
|
||
@Test | ||
fun noSongsAvailableInitialization() { | ||
val context = InstrumentationRegistry.getInstrumentation().targetContext.applicationContext | ||
val mySongsList = OfflineSongList(context) | ||
|
||
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()) | ||
} | ||
|
||
@Test | ||
fun addSpecificSongToList() { | ||
val context = InstrumentationRegistry.getInstrumentation().targetContext.applicationContext | ||
var records = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "records.txt") | ||
records.createNewFile() | ||
records.appendText("bones - imagine dragons\n") | ||
|
||
val mySongsList = OfflineSongList(context) | ||
|
||
val songName = "bones" | ||
val artistName = "imagine dragons" | ||
val inputSongsList = mutableListOf(Pair(songName, artistName)) | ||
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()) | ||
|
||
records.delete() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
app/src/main/java/ch/sdp/vibester/model/OfflineSongList.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
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) { | ||
private var songList = mutableListOf<Pair<String, String>>() | ||
private val page = "1" | ||
private val songsPerPage = "100" | ||
private val totalPages = "20" | ||
private val totalSongs = "2000" | ||
private var context = ctx | ||
private var emptySongs: Boolean = false | ||
|
||
init { | ||
fillList() | ||
} | ||
|
||
/** | ||
* 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 are no downloaded songs, keep the song list empty | ||
emptySongs = true | ||
} else { | ||
var reader = BufferedReader(FileReader(records)) | ||
var currentLine = reader.readLine() | ||
|
||
while (currentLine != null) { | ||
var trimmed = currentLine.trim() | ||
if (trimmed.isNotEmpty()) { | ||
val split = trimmed.split("-") | ||
if (split.size == 2) { | ||
songList.add(Pair(split[0].trim(), split[1].trim())) | ||
} | ||
} | ||
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 | ||
} | ||
|
||
/** | ||
* Getter that return shuffled song list | ||
* @return MutableList<Pair<String,String>> of type Pair("$songName", "$artistName") | ||
*/ | ||
fun getShuffledDownloadedSongList(): 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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters