Skip to content

Commit

Permalink
#9 minor fix
Browse files Browse the repository at this point in the history
  • Loading branch information
farhansolodev committed Dec 25, 2020
1 parent 03ee0de commit 8487e0c
Showing 1 changed file with 166 additions and 166 deletions.
332 changes: 166 additions & 166 deletions www/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,225 +20,225 @@
// Wait for the deviceready event before using any of Cordova's device APIs.
// See https://cordova.apache.org/docs/en/latest/cordova/events/events.html#deviceready

var system_month // to store the current month
var system_month; // to store the current month

var geodata // to store the location data
var geodata; // to store the location data

// ------------------------------------------------------------------------------------------------------------
// ------------------------------------------------FILE SECTION------------------------------------------------
// ------------------------------------------------------------------------------------------------------------
function writeFile(fileEntry, dataObj) {
// Create a FileWriter object for our FileEntry (log.txt).
fileEntry.createWriter(function (fileWriter) {
fileWriter.onwriteend = function () {
console.log("Successful file write...")
// readFile(fileEntry)
}

fileWriter.onerror = function (e) {
console.log("Failed file write: " + e.toString())
}

// If data object is not passed in,
// create a new Blob instead.
if (!dataObj) {
dataObj = new Blob(["some file data"], { type: "application/json" })
}

fileWriter.write(dataObj)
})
// Create a FileWriter object for our FileEntry (log.txt).
fileEntry.createWriter(function (fileWriter) {
fileWriter.onwriteend = function () {
console.log("Successful file write...");
// readFile(fileEntry)
};

fileWriter.onerror = function (e) {
console.log("Failed file write: " + e.toString());
};

// If data object is not passed in,
// create a new Blob instead.
if (!dataObj) {
dataObj = new Blob(["some file data"], { type: "application/json" });
}

fileWriter.write(dataObj);
});
}

// This function creates fileEntry obj and calls writeFile()
function createwriteFile(data, fname) {
window.requestFileSystem(
LocalFileSystem.PERSISTENT,
100000,
function (fs) {
fs.root.getFile(fname, { create: true, exclusive: false }, function (fileEntry) {
if (fname.localeCompare("saved-month.json") == 0) {
console.log("Writing file saved-month")
writeFile(fileEntry, { month: system_month })
} else {
console.log("Writing file salah-times")
writeFile(fileEntry, data)
}
})
},
function (error) {
console.log("WRITE ERROR:", error)
}
)
window.requestFileSystem(
LocalFileSystem.PERSISTENT,
100000,
function (fs) {
fs.root.getFile(fname, { create: true, exclusive: false }, function (fileEntry) {
if (fname.localeCompare("saved-month.json") == 0) {
console.log("Writing file saved-month");
writeFile(fileEntry, { month: system_month });
} else {
console.log("Writing file salah-times");
writeFile(fileEntry, data);
}
});
},
function (error) {
console.log("WRITE ERROR:", error);
}
);
}

// this method gets checks for the month and if the times don't need changing, it will do what we want to with salah data via funcs
function displayData(fileEntry, fdata) {
data = JSON.parse(fdata)
if (fileEntry.name.localeCompare("saved-month.json") == 0) {
console.log("Displaying saved month: " + data.month)
console.log(system_month)
if (system_month != data.month) {
console.log("month is diff, making api req")
reqAPI()
}
} else {
// Here do whatever we need to with the stored salah data (display/call funstion/etc)
console.log("Displaying saved salah times: " + JSON.stringify(data))

// Display Salah Times for the month
console.log(data.data.length)
for (var i = 0; i < data.data.length; i++) {
console.log(JSON.stringify(data.data[i].timings))
}
}
data = JSON.parse(fdata);
if (fileEntry.name.localeCompare("saved-month.json") == 0) {
console.log("Displaying saved month: " + data.month);
console.log(system_month);
if (system_month != data.month) {
console.log("month is diff, making api req");
reqAPI();
}
} else {
// Here do whatever we need to with the stored salah data (display/call funstion/etc)
console.log("Displaying saved salah times: " + JSON.stringify(data));

// Display Salah Times for the month
console.log(data.data.length);
for (var i = 0; i < data.data.length; i++) {
console.log(JSON.stringify(data.data[i].timings));
}
}
}

// This function reads the file data and calls displayData()
function getFileData(fileEntry) {
fileEntry.file(
function (file) {
var reader = new FileReader()
reader.onloadend = function () {
console.log("Successful file read: " + this.result)
displayData(fileEntry, this.result)
}

reader.readAsText(file)
},
function () {
console.log("File Read Error")
}
)
fileEntry.file(
function (file) {
var reader = new FileReader();
reader.onloadend = function () {
console.log("Successful file read: " + this.result);
displayData(fileEntry, this.result);
};

reader.readAsText(file);
},
function () {
console.log("File Read Error");
}
);
}

// This function gets fileWrite obj for reading and calls getFileData()
function readFile(fname) {
window.requestFileSystem(
LocalFileSystem.PERSISTENT,
0,
function (fs) {
fs.root.getFile(fname, { create: false, exclusive: false }, function (fileEntry) {
if (fname.localeCompare("saved-month.json") == 0) {
console.log("Reading saved-month.json")
getFileData(fileEntry)
} else {
console.log("Reading salah-times.json")
getFileData(fileEntry)
}
})
},
function (error) {
console.log("WRITE ERROR:", error)
}
)
window.requestFileSystem(
LocalFileSystem.PERSISTENT,
0,
function (fs) {
fs.root.getFile(fname, { create: false, exclusive: false }, function (fileEntry) {
if (fname.localeCompare("saved-month.json") == 0) {
console.log("Reading saved-month.json");
getFileData(fileEntry);
} else {
console.log("Reading salah-times.json");
getFileData(fileEntry);
}
});
},
function (error) {
console.log("WRITE ERROR:", error);
}
);
}

// ------------------------------------------------------------------------------------------------------------
// ------------------------------------------------API SECTION------------------------------------------------
// ------------------------------------------------------------------------------------------------------------
function reqAPI() {
const AdhanAPIParams = {
latitude: "51.508515",
longitude: "-0.1254872",
method: "2",
}
// this is a new month OR no prayer info saved prior
cordova.plugin.http.get(
"https://api.aladhan.com/v1/calendar",
AdhanAPIParams,
{ Authorization: "OAuth2: token" },
function (response) {
createwriteFile(response.data, "salah-times.json")
createwriteFile(system_month, "saved-month.json")
},
function (response) {
console.log(response.error)
}
)
const AdhanAPIParams = {
latitude: "51.508515",
longitude: "-0.1254872",
method: "2",
};
// this is a new month OR no prayer info saved prior
cordova.plugin.http.get(
"https://api.aladhan.com/v1/calendar",
AdhanAPIParams,
{ Authorization: "OAuth2: token" },
function (response) {
createwriteFile(response.data, "salah-times.json");
createwriteFile(system_month, "saved-month.json");
},
function (response) {
console.log(response.error);
}
);
}

// ------------------------------------------------------------------------------------------------------------
// ------------------------------------------------CHECK FILE EXISTS SECTION-----------------------------------
// ------------------------------------------------------------------------------------------------------------

function checkIfSalahFileExists() {
window.requestFileSystem(
LocalFileSystem.PERSISTENT,
0,
function (fileSystem) {
fileSystem.root.getFile("salah-times.json", { create: false }, fileExists, fileDoesNotExist)
},
getFSFail
)

function fileExists(fileEntry) {
console.log("File " + fileEntry.fullPath + " exists!")
}
function fileDoesNotExist() {
console.log("salah file doesn't exist, make api call, calling reqAPI()")
reqAPI() //If salah file doesn't exist, make API req
}
function getFSFail(evt) {
console.log(evt.target.error.code)
}
window.requestFileSystem(
LocalFileSystem.PERSISTENT,
0,
function (fileSystem) {
fileSystem.root.getFile("salah-times.json", { create: false }, fileExists, fileDoesNotExist);
},
getFSFail
);

function fileExists(fileEntry) {
console.log("File " + fileEntry.fullPath + " exists!");
}
function fileDoesNotExist() {
console.log("salah file doesn't exist, make api call, calling reqAPI()");
reqAPI(); //If salah file doesn't exist, make API req
}
function getFSFail(evt) {
console.log(evt.target.error.code);
}
}

function checkIfMonthFileExists() {
window.requestFileSystem(
LocalFileSystem.PERSISTENT,
0,
function (fileSystem) {
fileSystem.root.getFile("saved-month.json", { create: false }, fileExists, fileDoesNotExist)
},
getFSFail
)

function fileExists(fileEntry) {
console.log("File " + fileEntry.fullPath + " exists!")
}
function fileDoesNotExist() {
console.log("month file doesn't exist, creating month file by calling createwrite()")
createwriteFile(new Date().getMonth() + 1, "saved-month.json")
}
function getFSFail(evt) {
console.log(evt.target.error.code)
}
window.requestFileSystem(
LocalFileSystem.PERSISTENT,
0,
function (fileSystem) {
fileSystem.root.getFile("saved-month.json", { create: false }, fileExists, fileDoesNotExist);
},
getFSFail
);

function fileExists(fileEntry) {
console.log("File " + fileEntry.fullPath + " exists!");
}
function fileDoesNotExist() {
console.log("month file doesn't exist, creating month file by calling createwrite()");
createwriteFile(system_month, "saved-month.json");
}
function getFSFail(evt) {
console.log(evt.target.error.code);
}
}

// ------------------------------------------------------------------------------------------------------------
// ------------------------------------------------MAIN SECTION------------------------------------------------
// ------------------------------------------------------------------------------------------------------------
document.addEventListener("deviceready", onDeviceReady, false)
document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
system_month = new Date().getMonth() + 1
// navigator.geolocation.getCurrentPosition(onSuccess, onError)
onSuccess("test")
system_month = new Date().getMonth() + 1;
// navigator.geolocation.getCurrentPosition(onSuccess, onError)
onSuccess("test");
}

// onSuccess Callback
// This method accepts a Position object, which contains the
// current GPS coordinates
function onSuccess(position) {
// Get postion data and store in geodata

// checkIfSalahFileExists
checkIfSalahFileExists()
// checkIfMonthFileExists
checkIfMonthFileExists()

// now read both files
var delayInMilliseconds = 4000
setTimeout(function () {
readFile("saved-month.json")
}, delayInMilliseconds)

setTimeout(function () {
readFile("salah-times.json")
}, delayInMilliseconds)
// Get postion data and store in geodata

// checkIfSalahFileExists
checkIfSalahFileExists();
// checkIfMonthFileExists
checkIfMonthFileExists();

// now read both files
var delayInMilliseconds = 4000;
setTimeout(function () {
readFile("saved-month.json");
}, delayInMilliseconds);

setTimeout(function () {
readFile("salah-times.json");
}, delayInMilliseconds);
}

// onError Callback receives a PositionError object
function onError(error) {
console.log(error.message)
console.log(error.message);
}

0 comments on commit 8487e0c

Please sign in to comment.