Skip to content

Commit

Permalink
showing downloading when app closed fixed
Browse files Browse the repository at this point in the history
super quick downloading added by canceling and sending xhr again
  • Loading branch information
ShivamJoker committed Sep 6, 2019
1 parent 78994d5 commit b855fb7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
9 changes: 6 additions & 3 deletions src/components/CurrentSection.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ import { GlobalContext } from "./GlobalState";
import {
getHistory,
getLikedSongs,
getDownloadedSongs
getDownloadedSongs,
removeDownloadingState,
db
} from "../external/saveSong";

import { db } from "../external/saveSong";
import SettingsPage from "./sections/SettingsPage";
// import the db from save song

Expand Down Expand Up @@ -150,6 +151,8 @@ const CurrentSection = ({ history, location }) => {
db.on("changes", () => {
setUpdateCount(c => c + 1);
});
// will remove all the songs which are downloading in the first place
removeDownloadingState();

const isThisNewUser = localStorage.getItem("isThisNew");
if (isThisNewUser === "no") {
Expand Down Expand Up @@ -237,7 +240,7 @@ const CurrentSection = ({ history, location }) => {
return <div>Redirecting you to play store</div>;
}}
/>

<Route path="/settings" component={SettingsPage} />
<Route path="/privacy" component={PrivacyPage} />

Expand Down
22 changes: 20 additions & 2 deletions src/external/saveSong.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,24 @@ export const getLikedSongs = async () => {

export const getDownloadedSongs = async () => {
const downloadedSongs = await db.songs
.where("[downloadState+timestamp]") //this will filter song based on time and liked
.where("[downloadState+timestamp]") //this will filter song based on time and downloaded
.between(["downloaded", Dexie.minKey], ["downloaded", Dexie.maxKey])
.reverse()
.toArray();
return downloadedSongs;
};

export const removeDownloadingState = async () => {
// find all the downloadState which is downloading and remove that
const songs = await db.songs
.where("[downloadState+timestamp]")
.between(["downloading", Dexie.minKey], ["downloading", Dexie.maxKey])
.modify(x => {
delete x.downloadState;
});
console.log(songs);
};

export const downloadSong = async (id, url) => {
try {
db.songs.update(id, {
Expand Down Expand Up @@ -108,9 +119,10 @@ export const deleteSongAudio = async id => {
};

function fetchProxiedBlob(url) {
const URL = url;
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://server.ylight.xyz/proxy/" + url);
xhr.open("GET", "https://server.ylight.xyz/proxy/" + URL);
xhr.responseType = "blob";
xhr.onload = function() {
var status = xhr.status;
Expand All @@ -124,5 +136,11 @@ function fetchProxiedBlob(url) {
}
};
xhr.send();
setTimeout(() => {
xhr.abort();
xhr.open("GET", "https://server.ylight.xyz/proxy/" + URL);

xhr.send();
}, 1000);
});
}

0 comments on commit b855fb7

Please sign in to comment.