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
Offline mode implementation #277
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
826092f
Edited downloadActivity to store the API call result
Tsathogguaa 5c09b57
Commented the parts related to the offline mode for now under 'TODO: …
Tsathogguaa 89e1ad2
Added necessary code to use downloaded media. Some code are commented.
Tsathogguaa 08c7156
Taking care of merge, hopefully
Tsathogguaa 2c8a341
Improved readability and resolved merge conflicts(hopefully)
Tsathogguaa 2e149d8
Improved test coverage
Tsathogguaa 1c418ce
Debugged the test lmao
Tsathogguaa 8b297d0
Got rid of useless try catch block and improved readability
Tsathogguaa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Similar blocks of code found in 2 locations. Consider refactoring.