Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better Organization and Formatting Regarding Sleeping Permissions #23

Merged
merged 39 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
e8ed1c0
Update app.json
EsBarkley Nov 20, 2024
f5bcaab
Update index.js
EsBarkley Nov 20, 2024
9cfb44c
Update index.r.layout.js
EsBarkley Nov 20, 2024
bd28f32
Add files via upload
EsBarkley Nov 20, 2024
32d839b
Update index.r.layout.js
EsBarkley Nov 24, 2024
bfec022
Merge branch 'main' into sleepstuff
EsBarkley Nov 26, 2024
375ff15
Fix Prettier formatting
EsBarkley Nov 26, 2024
52f7962
Resolved merge conflicts
EsBarkley Nov 26, 2024
3287b53
Fixed the Prettier Error
EsBarkley Nov 26, 2024
f34697b
Delete app/page/rescuePlanPage.js
EsBarkley Nov 26, 2024
2923d58
Update index.js
EsBarkley Nov 26, 2024
a6e7ef7
Fixed prettier formatting issues
EsBarkley Nov 26, 2024
fd62a67
Merge conflicts
EsBarkley Nov 26, 2024
4210f34
Missed semicolon
EsBarkley Nov 26, 2024
73b5aed
Prettier Error Fix
EsBarkley Nov 26, 2024
5a51663
Update app.json
EsBarkley Nov 26, 2024
2e786c6
Update index.r.layout.js
EsBarkley Nov 26, 2024
31f4d8f
Change that separates the sleep permissions stuff to their own separa…
EsBarkley Jan 18, 2025
a412ecf
Merge branch 'sleepstuff' of https://github.com/MyAlyce/ASU_OD_detect…
EsBarkley Jan 18, 2025
c0b2bfe
updated app.json
EsBarkley Jan 18, 2025
b83f0f5
Separate the sleep functions from the index.js file
EsBarkley Jan 19, 2025
a88714c
Created the sleepFunctions.js file, which separates the sleep functio…
EsBarkley Jan 19, 2025
71250ab
changed file name
EsBarkley Jan 19, 2025
bfe4bb6
Merge branch 'sleepstuff' of https://github.com/MyAlyce/ASU_OD_detect…
EsBarkley Jan 19, 2025
b6aec08
added google-api-constants.example.js file
EsBarkley Jan 19, 2025
1f61a55
added google-api-constants.example.js file
EsBarkley Jan 19, 2025
5dca960
Merge branch 'sleepstuff' of https://github.com/MyAlyce/ASU_OD_detect…
EsBarkley Jan 19, 2025
e2857ea
added file
EsBarkley Jan 19, 2025
9fcc287
Fixed formatting in sleepFunctions.js
EsBarkley Jan 19, 2025
2e20c0b
Fixed possible merge conflicts between 'main' and 'sleepstuff'
EsBarkley Jan 19, 2025
fe46e34
Fixed possible merge conflicts between branches 'main' and 'sleepstuff'
EsBarkley Jan 19, 2025
d943a2c
Merge branch 'sleepstuff' of https://github.com/MyAlyce/ASU_OD_detect…
EsBarkley Jan 19, 2025
9f59b0d
Update index.r.layout.js
EsBarkley Jan 20, 2025
743bfd5
Separated some widget created in permissionsPage to be created in per…
EsBarkley Jan 20, 2025
2bd0ded
Merge branch 'sleepstuff' of https://github.com/MyAlyce/ASU_OD_detect…
EsBarkley Jan 20, 2025
dff1ec5
Fixed formatting in index.js
EsBarkley Jan 20, 2025
e105535
Fixed formatting in index.js
EsBarkley Jan 20, 2025
437f181
Prettier format fix
EsBarkley Jan 20, 2025
aa6613f
Merge branch 'main' of https://github.com/MyAlyce/ASU_OD_detection_pr…
EsBarkley Jan 26, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"page": {
"pages": [
"page/index",
"page/permissionsPage"
"page/permissionsPage",
"page/sleepFunctions"
]
},
"app-side": {
Expand Down
102 changes: 16 additions & 86 deletions app/page/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ import {
STOP_BUTTON,
} from 'zosLoader:./index.[pf].layout.js';

import {
onClickSleepButton,
getSleepInfo,
getStageConstantObj,
} from './sleepFunctions.js';

const permissions = ['device:os.bg_service'];
const service = 'app-service/service';
const storage = getApp().globals.storage;
Expand All @@ -21,10 +27,10 @@ Page(
BasePage({
state: {
temp: null,
permissions: {},
permissions: {}, // Will hold the permissions data
},
onInit(params) {
console.log('Index Page onInit invoked');
console.log('Index PageonInit invoked');

// Log the entire params object to see the received data
console.log('Received params:', params);
Expand Down Expand Up @@ -138,7 +144,14 @@ Page(

hmUI.createWidget(hmUI.widget.BUTTON, {
...SLEEP_BUTTON,
click_func: this.onClickSleepButton.bind(this),
click_func: () => {
const jsonstringPermissions = JSON.stringify(
this?.state?.permissions,
);
console.log('JSON string of permissions:', jsonstringPermissions);

onClickSleepButton(jsonstringPermissions);
},
});

hmUI.createWidget(hmUI.widget.BUTTON, {
Expand Down Expand Up @@ -173,89 +186,6 @@ Page(
storage.setKey('expiresAt', req.params.expiresAt);
}
},

onClickSleepButton() {
console.log('Sleep button pressed');

// Log the current state and permissions to see what data is available
console.log('Current state:', JSON.stringify(this.state));

// Access permissions from the state
if (this.state.permissions) {
Object.entries(this.state.permissions).forEach(([key, value]) => {
if (value === true) {
console.log(`Permission for ${key} is granted.`);

// Actual data extraction based on the granted permission
switch (key) {
case 'sleepScore':
// Get the sleep score using the `getInfo` method
this.getSleepInfo('score');
break;
case 'startEndTime':
// Get the start and end times (assuming `getInfo` provides this)
this.getSleepInfo('startTime');
this.getSleepInfo('endTime');
break;
case 'deepSleepTime':
// Get the deep sleep time using `getInfo`
this.getSleepInfo('deepTime');
break;
case 'totalSleepTime':
// Get the total sleep time using `getInfo`
this.getSleepInfo('totalTime');
break;
case 'wakeStage':
// Get the wake stage using `getStageConstantObj`
this.getStageConstantObj('WAKE_STAGE');
break;
case 'remStage':
// Get the REM stage using `getStageConstantObj`
this.getStageConstantObj('REM_STAGE');
break;
case 'lightStage':
// Get the light sleep stage using `getStageConstantObj`
this.getStageConstantObj('LIGHT_STAGE');
break;
case 'deepStage':
// Get the deep sleep stage using `getStageConstantObj`
this.getStageConstantObj('DEEP_STAGE');
break;
default:
console.log(`No action defined for permission: ${key}`);
}
} else {
console.log(`Permission for ${key} is denied.`);
}
});
} else {
console.log('No permissions found in state.');
}
},

// Extract sleep info (getInfo method)
getSleepInfo(infoKey) {
// Using the ZeppOS Sleep module to fetch the sleep info
const info = sleep.getInfo();

if (info && info.hasOwnProperty(infoKey)) {
console.log(`${infoKey}: ${info[infoKey]}`);
} else {
console.log(`No data for ${infoKey}`);
}
},

// Extract stage constant
getStageConstantObj(stageKey) {
// Using the ZeppOS Sleep module to fetch stage constants
const sleepStageConstants = sleep.getStageConstantObj();

if (sleepStageConstants && sleepStageConstants.hasOwnProperty(stageKey)) {
console.log(`${stageKey}: ${sleepStageConstants[stageKey]}`);
} else {
console.log(`No data for ${stageKey}`);
}
},
}),
);

Expand Down
23 changes: 6 additions & 17 deletions app/page/permissionsPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { push } from '@zos/router';
import {
TOGGLE_OPTIONS,
BACK_BUTTON,
titleText,
createToggleLabel,
} from 'zosLoader:./permissionsPage.[pf].layout.js';
import { LocalStorage } from '@zos/storage'; // Import LocalStorage only once

Expand Down Expand Up @@ -31,15 +33,9 @@ Page({
console.log('Permissions Page Build Called');

// Display the main title
// Need to add to permissionsPage layout file
hmUI.createWidget(hmUI.widget.TEXT, {
text: this.state.message,
x: 100,
y: 20,
w: 200,
h: 50,
text_size: 24,
color: 0xffffff,
align_h: hmUI.align.CENTER_H,
...titleText,
});

// Create toggle switches with their corresponding names
Expand All @@ -62,16 +58,9 @@ Page({
},
});

// Create the label (name) for each toggle switch next to it
// Create the label for each toggle switch next to it
hmUI.createWidget(hmUI.widget.TEXT, {
text: option.label,
x: option.x, // Position the label next to the toggle
y: option.y + 5, // Adjust the Y position slightly to match the center of the toggle
w: px(100), // Adjust the width for the label
h: option.h, // Keep the same height as the toggle
text_size: px(16), // Smaller text size for the label
color: 0xffffff, // Text color for better visibility
align_h: hmUI.align.LEFT, // Align the text to the left
...createToggleLabel(option),
});

console.log(`Created toggle widget and label for: ${option.label}`);
Expand Down
35 changes: 35 additions & 0 deletions app/page/permissionsPage.r.layout.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,40 @@
import { px } from '@zos/utils';
import { BUTTON_TEXT, NORMAL_COLOR, PRESS_COLOR } from '../utils/constants';
import hmUI, { createWidget, widget, align, text_style, prop } from '@zos/ui';

/*export const createToggleSwitch = (option, checkedValue, handleToggleChange) => ({
x: px(option.x + 100), // Position the toggle switch slightly to the right
y: px(option.y),
w: px(option.w),
h: px(option.h),
checked: px(checkedValue),
color: 0xffffff, // Text color for better visibility
text_size: px(16), // Smaller text size
});*/

// Function to create a label for a toggle switch
export const createToggleLabel = (option) => ({
text: option.label,
x: px(option.x), // Position the label next to the toggle
y: px(option.y + 5), // Adjust the Y position slightly to match the center of the toggle
w: px(100), // Adjust the width for the label
h: px(option.h), // Keep the same height as the toggle
text_size: px(16), // Smaller text size for the label
color: 0xffffff, // Text color for better visibility
align_h: hmUI.align.LEFT, // Align the text to the left
});

export const titleText = createWidget(widget.TEXT, {
text: 'Permissions Page',
x: px(100),
y: px(20),
w: px(200),
h: px(50),
text_size: px(24),
color: 0xffffff,
align_h: hmUI.align.CENTER_H,
text_style: hmUI.text_style.WRAP,
});

export const TOGGLE_OPTIONS = [
{
Expand Down
90 changes: 90 additions & 0 deletions app/page/sleepFunctions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { Sleep } from '@zos/sensor'; // Import the Sleep module

const sleep = new Sleep();

// Function to handle the sleep button click and permissions
export function onClickSleepButton(jsonstringPermissions) {
console.log('Sleep button pressed. in sleepFunctions.js');

// Log the received JSON string of permissions
console.log('Received JSON string of permissions:', jsonstringPermissions);

try {
// Parse the JSON string to access the permissions as an object
const permissions = JSON.parse(jsonstringPermissions);

// Log the parsed permissions object
console.log('Parsed permissions:', permissions);

if (permissions) {
Object.entries(permissions).forEach(([key, value]) => {
if (value === true) {
console.log(`Permission for ${key} is granted.`);

// Actual data extraction based on the granted permission
switch (key) {
case 'sleepScore':
getSleepInfo('score');
break;
case 'startEndTime':
getSleepInfo('startTime');
getSleepInfo('endTime');
break;
case 'deepSleepTime':
getSleepInfo('deepTime');
break;
case 'totalSleepTime':
getSleepInfo('totalTime');
break;
case 'wakeStage':
getStageConstantObj('WAKE_STAGE');
break;
case 'remStage':
getStageConstantObj('REM_STAGE');
break;
case 'lightStage':
getStageConstantObj('LIGHT_STAGE');
break;
case 'deepStage':
getStageConstantObj('DEEP_STAGE');
break;
default:
console.log(`No action defined for permission: ${key}`);
}
} else {
console.log(`Permission for ${key} is denied.`);
}
});
} else {
console.log('No permissions found in the parsed object.');
}
} catch (error) {
console.error('Error parsing permissions JSON:', error);
}
}

// Extract sleep info (getInfo method)
export function getSleepInfo(infoKey) {
const info = sleep.getInfo();

if (info) {
if (info.hasOwnProperty(infoKey)) {
console.log(`${infoKey}: ${info[infoKey]}`);
} else {
console.log(`No data for ${infoKey}`);
}
} else {
console.log('No sleep data available');
}
}

// Extract stage constant
export function getStageConstantObj(stageKey) {
const sleepStageConstants = sleep.getStageConstantObj();

if (sleepStageConstants && sleepStageConstants.hasOwnProperty(stageKey)) {
console.log(`${stageKey}: ${sleepStageConstants[stageKey]}`);
} else {
console.log(`No data for ${stageKey}`);
}
}
Loading