Play position in stuff like podcasts can now be permanently persisted.
This commit is contained in:
@@ -104,10 +104,41 @@ Playlist* PlaylistManager::get_playlist_for_id(String id) {
|
||||
}
|
||||
|
||||
Playlist* PlaylistManager::get_playlist_for_folder(String folder) {
|
||||
Playlist* p;
|
||||
if (!_playlists.count(folder)) {
|
||||
_playlists[folder] = new Playlist(folder);
|
||||
p = new Playlist(folder);
|
||||
_playlists[folder] = p;
|
||||
if (p->persistence == PERSIST_PERMANENTLY) {
|
||||
// TODO Load persistence from file
|
||||
String search = folder;
|
||||
search += "=";
|
||||
SPIMaster::select_sd();
|
||||
if (SD.exists("/_positions.txt")) {
|
||||
File f = SD.open("/_positions.txt", "r");
|
||||
while (true) {
|
||||
String s = f.readStringUntil('\n');
|
||||
if (s.length()==0) break;
|
||||
if (s.startsWith(search)) {
|
||||
s = s.substring(search.length());
|
||||
int idx = s.indexOf(',');
|
||||
String title_index = s.substring(0, idx);
|
||||
uint32_t position = s.substring(idx+1).toInt();
|
||||
p->set_track_by_id(title_index);
|
||||
p->set_position(position);
|
||||
break;
|
||||
}
|
||||
}
|
||||
f.close();
|
||||
}
|
||||
SPIMaster::select_sd(false);
|
||||
}
|
||||
} else {
|
||||
p = _playlists[folder];
|
||||
if (p->persistence == PERSIST_NONE) {
|
||||
p->reset();
|
||||
}
|
||||
}
|
||||
return _playlists[folder];
|
||||
return p;
|
||||
}
|
||||
|
||||
void PlaylistManager::dump_ids() {
|
||||
@@ -164,3 +195,40 @@ String PlaylistManager::create_mapping_txt() {
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
void PlaylistManager::persist(Playlist* p) {
|
||||
if (p->persistence != PERSIST_PERMANENTLY) return;
|
||||
|
||||
String search = p->path();
|
||||
search += '=';
|
||||
|
||||
bool old_file_existed = false;
|
||||
|
||||
SPIMaster::select_sd();
|
||||
if (SD.exists("_positions.txt")) {
|
||||
SD.rename("/_positions.txt", "/_positions.temp.txt");
|
||||
old_file_existed = true;
|
||||
}
|
||||
File dst = SD.open("/_positions.txt", "w");
|
||||
|
||||
if (old_file_existed) {
|
||||
File src = SD.open("/_positions.temp.txt", "r");
|
||||
|
||||
while (true) {
|
||||
String line = src.readStringUntil('\n');
|
||||
line.trim();
|
||||
if (line.startsWith(search)) continue;
|
||||
dst.println(line);
|
||||
}
|
||||
|
||||
src.close();
|
||||
SD.remove("/_positions.temp.txt");
|
||||
}
|
||||
|
||||
dst.print(search);
|
||||
dst.print(p->get_current_track_id());
|
||||
dst.print(',');
|
||||
dst.println(p->get_position());
|
||||
dst.close();
|
||||
SPIMaster::select_sd(false);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user