Skip to content

Commit

Permalink
v 0.3.4
Browse files Browse the repository at this point in the history
  • Loading branch information
AbiruzzamanMolla committed Oct 6, 2024
1 parent acc79fd commit 1fc52ed
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 40 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

All notable changes to the **[Prayer Timer Bangladesh](https://marketplace.visualstudio.com/items?itemName=azmolla.prayer-timer-bangladesh)** extension will be documented here.

### Changelog for Version 0.3.4
- **New Feature**: Added a notification to display a random hadith related to prayer 5 minutes before each prayer time.
- **Data Storage**: Introduced a `hadith.json` file to store hadiths and their references.
- **Dependency Update**: Included the `fs` module for reading JSON files.
- **Error Handling**: Added error handling for loading and parsing hadiths.
- **Code Optimization**: Refactored relevant functions for better clarity and maintainability.


## [0.3.2] - 2024-10-06

### Fixed
Expand Down
32 changes: 32 additions & 0 deletions hadith.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[
{
"hadith": "The Prophet Muhammad (peace be upon him) said, 'Prayer is the pillar of religion. Whoever establishes it has established religion, and whoever destroys it has destroyed religion.'",
"reference": "Sunan Ibn Majah, Book 5, Hadith 224",
"status": "Sahih"
},
{
"hadith": "The Prophet Muhammad (peace be upon him) said, 'The most beloved of deeds to Allah is the prayer at its proper time.'",
"reference": "Sahih Bukhari, Book 10, Hadith 503",
"status": "Sahih"
},
{
"hadith": "The Prophet Muhammad (peace be upon him) said, 'Your prayer in your house is better than your prayer in this mosque, except for the obligatory prayer.'",
"reference": "Sahih Muslim, Book 4, Hadith 773",
"status": "Sahih"
},
{
"hadith": "The Prophet Muhammad (peace be upon him) said, 'Perform the prayer as if it is your last prayer.'",
"reference": "Sunan Ibn Majah, Book 5, Hadith 433",
"status": "Daif"
},
{
"hadith": "The Prophet Muhammad (peace be upon him) said, 'The first deed for which a person will be questioned on the Day of Resurrection is prayer.'",
"reference": "Sunan Al-Kubra, Hadith 5100",
"status": "Sahih"
},
{
"hadith": "The Prophet Muhammad (peace be upon him) said, 'Whoever prays twelve rak'ahs in a day and night, a house will be built for him in Paradise.'",
"reference": "Sahih Muslim, Book 4, Hadith 759",
"status": "Sahih"
}
]
13 changes: 10 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"name": "prayer-timer-bangladesh",
"displayName": "Prayer Timer Bangladesh",
"description": "The Prayer Timer Bangladesh extension provides an easy and convenient way to track prayer times based on your GPS location within Bangladesh. With this extension, you can seamlessly integrate prayer time notifications into your Visual Studio Code environment. \n\n### Key Features:\n- **Dynamic GPS-Based Prayer Times**: Automatically retrieves prayer times based on your current latitude and longitude, ensuring accurate timings for your location.\n- **Customizable Settings**: Configure your preferred latitude, longitude, and timezone name directly in the settings.\n- **Status Bar Integration**: Displays the current prayer time directly in the status bar, allowing you to keep track without disrupting your workflow.\n- **Command Options**: Easily access commands to display current prayer times or show all prayer times with simple keyboard shortcuts or command palette options.\n- **User-Friendly Interface**: Simple configuration options for adjusting the display position (left or right) and toggling the visibility of prayer times.\n\n### Configuration Options:\n- **Latitude and Longitude**: Set default values for your geographic location (default: 24.0298, 90.7061).\n- **Timezone**: Automatically adjusts to your local timezone (default: Asia/Dhaka).\n- **Display Position**: Choose to display prayer times on the left or right side of the status bar (default: right).\n- **Active Status**: Toggle the display of prayer times in the status bar on or off.\n\nThis extension is designed for users who wish to maintain their prayer schedules while working in Visual Studio Code, offering a streamlined way to stay connected with their spiritual practices.",
"version": "0.3.3",
"preview": true,
"version": "0.3.4",
"preview": false,
"publisher": "azmolla",
"icon": "icon.png",
"engines": {
Expand All @@ -27,7 +27,8 @@
],
"activationEvents": [
"onStartupFinished",
"onCommand:prayer-timer-bangladesh.showPrayerTimes"
"onCommand:prayer-timer-bangladesh.showPrayerTimes",
"onCommand:prayer-timer-bangladesh.showAllPrayerTimes"
],
"main": "./out/extension.js",
"contributes": {
Expand Down Expand Up @@ -101,6 +102,7 @@
"typescript": "^5.6.2"
},
"dependencies": {
"axios": "^1.7.7"
"axios": "^1.7.7",
"fs": "0.0.1-security"
}
}
84 changes: 51 additions & 33 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as vscode from "vscode";
import axios from "axios";
import * as fs from "fs";
import * as path from "path";

// Default configuration keys
const CONFIG_KEY_LAT = "prayerTimerBangladesh.lat";
Expand All @@ -13,17 +15,18 @@ let prayerAlarmTimeouts: NodeJS.Timeout[] = [];
let allPrayerTimes: string[] = []; // To store all prayer times
let prayerTimes: any; // To store all prayer data
let locationInfo: any; // To store all prayer data
let hadiths: any[] = []; // To store hadiths

export function activate(context: vscode.ExtensionContext) {
// Register the command to show prayer times
loadHadiths(); // Load hadiths on activation

const showPrayerTimesCommand = vscode.commands.registerCommand(
"prayer-timer-bangladesh.showPrayerTimes",
async () => {
await fetchPrayerTimes();
}
);

// Register the command to show all prayer times
const showAllPrayerTimesCommand = vscode.commands.registerCommand(
"prayer-timer-bangladesh.showAllPrayerTimes",
() => {
Expand All @@ -34,7 +37,6 @@ export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(showPrayerTimesCommand);
context.subscriptions.push(showAllPrayerTimesCommand);

// Initialize status bar with alignment based on user preference
const position = vscode.workspace
.getConfiguration()
.get<string>(CONFIG_KEY_POSITION);
Expand All @@ -46,57 +48,60 @@ export function activate(context: vscode.ExtensionContext) {
);
context.subscriptions.push(prayerTimesStatusBar);

// Automatically load prayer times on startup
vscode.commands.executeCommand("prayer-timer-bangladesh.showPrayerTimes");
}

async function fetchPrayerTimes() {
try {
// Fetch settings from configuration
const lat = vscode.workspace.getConfiguration().get<number>(CONFIG_KEY_LAT);
const lng = vscode.workspace.getConfiguration().get<number>(CONFIG_KEY_LNG);
const tzname = vscode.workspace
.getConfiguration()
.get<string>(CONFIG_KEY_TZNAME);

// Build API URL using settings
const apiUrl = `https://salat.habibur.com/api/?lat=${lat}&lng=${lng}&tzoffset=360&tzname=${tzname}`;

const response = await axios.get(apiUrl);
prayerTimes = response.data.data; // Store all prayer times data
prayerTimes = response.data.data;

const prayerNames = ["Fajr", "Dhuhr", "Asr", "Maghrib", "Isha"];

allPrayerTimes = [
`${prayerTimes.fajar18.short}`, // Fajr
`${prayerTimes.noon.short}`, // Dhuhr
`${prayerTimes.asar1.short}`, // Asar start
`${prayerTimes.asar2.short}`, // Asar end
`${prayerTimes.magrib12.short}`, // Maghrib
`${prayerTimes.esha.short}`, // Isha
`${prayerTimes.fajar18.short}`,
`${prayerTimes.noon.short}`,
`${prayerTimes.asar1.short}`,
`${prayerTimes.asar2.short}`,
`${prayerTimes.magrib12.short}`,
`${prayerTimes.esha.short}`,
];

locationInfo = {
location: response.data.tzname,
name: response.data.name,
};

// Check if the display is active
const isActive = vscode.workspace
.getConfiguration()
.get<boolean>(CONFIG_KEY_ACTIVE);
if (isActive) {
// Display the active prayer and remaining time in the status bar
const currentPrayer = getCurrentPrayer(prayerNames, allPrayerTimes);
if (currentPrayer) {
updatePrayerTimesStatusBar(
currentPrayer.name,
currentPrayer.time,
currentPrayer.remainingTime
);

// Show hadith notification 5 minutes before the current prayer time
const currentTime = new Date();
const prayerTimeDate = new Date(currentPrayer.time); // Convert prayer time string to Date
const timeDiff = prayerTimeDate.getTime() - currentTime.getTime();
if (timeDiff <= 5 * 60 * 1000) {
// 5 minutes in milliseconds
showHadithNotification();
}
}

// Set alarms for the prayer times
setPrayerAlarms(allPrayerTimes);
} else {
prayerTimesStatusBar.hide(); // Hide if not active
Expand All @@ -106,6 +111,29 @@ async function fetchPrayerTimes() {
}
}

function loadHadiths() {
const hadithFilePath = path.join(__dirname, "hadith.json");
fs.readFile(hadithFilePath, "utf8", (err, data) => {
if (err) {
console.error("Error loading hadiths:", err);
return;
}
try {
hadiths = JSON.parse(data);
} catch (error) {
console.error("Error parsing hadiths:", error);
}
});
}

function showHadithNotification() {
const randomIndex = Math.floor(Math.random() * hadiths.length);
const hadith = hadiths[randomIndex];
vscode.window.showInformationMessage(
`${hadith.hadith} - ${hadith.reference}`
);
}

function showAllPrayerTimes() {
const locationName =
locationInfo?.name || locationInfo?.location || "Unknown Location";
Expand All @@ -124,17 +152,16 @@ function showAllPrayerTimes() {
`;
vscode.window.showInformationMessage(`Prayer Times:\n${message}`, {
modal: true,
}); // Show all times in an information message
});
}

function getCurrentPrayer(prayerNames: string[], times: string[]) {
const currentTime = new Date();
const currentSecs = Math.floor(currentTime.getTime() / 1000); // Get current time in seconds
const currentSecs = Math.floor(currentTime.getTime() / 1000);

for (let i = 0; i < times.length; i++) {
const prayerTimeSecs = getPrayerTimeSecs(i);

// Check if the current time is before the next prayer time
if (prayerTimeSecs > currentSecs) {
const remainingTime = prayerTimeSecs - currentSecs;
const hours = Math.floor(remainingTime / 3600);
Expand All @@ -147,15 +174,13 @@ function getCurrentPrayer(prayerNames: string[], times: string[]) {
}
}

// If no prayer is upcoming, return the last prayer
return {
name: prayerNames[prayerNames.length - 1],
time: times[times.length - 1],
remainingTime: "Prayer time is over.",
};
}


function getPrayerTimeSecs(index: number) {
switch (index) {
case 0:
Expand All @@ -171,45 +196,38 @@ function getPrayerTimeSecs(index: number) {
case 5:
return prayerTimes.esha.secs; // Isha
default:
return 0; // Default case
return 0;
}
}

function formatRemainingTime(seconds: number) {
const hours = Math.floor(seconds / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
return `${hours > 0 ? hours + "h " : ""}${minutes}m`;
}

function updatePrayerTimesStatusBar(
prayerName: string,
prayerTime: string,
remainingTime: string
) {
prayerTimesStatusBar.text = `Prayer Time: ${prayerName} (${remainingTime} left)`;
prayerTimesStatusBar.tooltip = "Click to see all prayer times";
prayerTimesStatusBar.command = "prayer-timer-bangladesh.showAllPrayerTimes"; // Link the command to the status bar item
prayerTimesStatusBar.command = "prayer-timer-bangladesh.showAllPrayerTimes";

prayerTimesStatusBar.show();
}

function setPrayerAlarms(times: string[]) {
// Clear previous alarms
prayerAlarmTimeouts.forEach((timeout) => clearTimeout(timeout));
prayerAlarmTimeouts = [];

const currentTime = new Date();
const currentSecs = Math.floor(currentTime.getTime() / 1000); // Get current time in seconds
const currentSecs = Math.floor(currentTime.getTime() / 1000);

for (let time of times) {
const prayerTimeSecs = getPrayerTimeSecs(times.indexOf(time)); // Get seconds for the prayer time
const prayerTimeSecs = getPrayerTimeSecs(times.indexOf(time));

if (prayerTimeSecs > currentSecs) {
const timeUntilAlarm = prayerTimeSecs - currentSecs;

const timeout = setTimeout(() => {
vscode.window.showInformationMessage(`It's time for prayer! (${time})`);
}, timeUntilAlarm * 1000); // Convert seconds to milliseconds
}, timeUntilAlarm * 1000);

prayerAlarmTimeouts.push(timeout);
}
Expand Down

0 comments on commit 1fc52ed

Please sign in to comment.