Skip to content

Commit

Permalink
Solara
Browse files Browse the repository at this point in the history
  • Loading branch information
IdeaS0ft committed Sep 6, 2024
1 parent 0fac3fd commit f06683b
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 16 deletions.
44 changes: 37 additions & 7 deletions solara/lib/core/dashboard/brand/BrandController.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
// BrandController.js
import {DataSource} from './BrandModel.js';

class BrandController {
constructor(model, view) {
this.model = model;
this.view = view;
this.onSectionChanged = this.onSectionChanged.bind(this);
this.deleteField = this.deleteField.bind(this);
this.initializeEventListeners();
this.view.setOnSectionChangedHandler(this.onSectionChanged);
this.view.setOnDeleteFieldHandler(this.deleteField);
this.view.setOnSectionChangedHandler(this.onSectionChanged.bind(this));
this.view.setOnSelectBrandFolder(this.onLoadSections.bind(this));
this.view.setOnDeleteFieldHandler(this.deleteField.bind(this));
}

async initializeApp() {
try {
const {sectionItems, brandName} = await this.model.fetchSections();
this.view.updateAppNameTitle(brandName);
await this.loadSections(sectionItems);
switch (this.model.source) {
case DataSource.LOCAL:
return await this.initLoal();
case DataSource.REMOTE:
return await this.initRemote();
default:
throw new Error('Unknown data source');
}
}

async initRemote() {
this.view.initializeApp()
}

async initLoal() {
try {
const sections = await this.model.fetchSections();
await this.onLoadSections(sections);
const {isCurrentBrand, contentChanged} = await this.model.fetchCurrentBrand();

if (!isCurrentBrand) {
Expand All @@ -31,6 +46,21 @@ class BrandController {
}
}

async onLoadSections(sections) {
try {
const brandName = sections.brandName
const sectionItems = sections.sectionItems
console.log(brandName)
console.log(sectionItems)
console.log(this.view);
this.view.updateAppNameTitle(brandName);
await this.loadSections(sectionItems);
} catch (error) {
console.error('Error initializing app:', error);
alert(error.message);
}
}

initializeEventListeners() {
document.getElementById('allBrandsButton').addEventListener('click', () => {
window.location.href = `../brands/brands.html?source=${this.model.source}`;
Expand Down
4 changes: 2 additions & 2 deletions solara/lib/core/dashboard/brand/BrandModel.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import BrandLocalSource from './source/BrandLocalSource.js';
import BrandRemoteSource from './source/BrandRemoteSource.js';

const DataSource = {
export const DataSource = {
LOCAL: 'local',
REMOTE: 'remote',
};
Expand Down Expand Up @@ -77,4 +77,4 @@ class BrandModel {
}
}

export default BrandModel;
export default BrandModel;
93 changes: 91 additions & 2 deletions solara/lib/core/dashboard/brand/BrandView.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,96 @@
// BrandView.js
import {DataSource} from './BrandModel.js';

class BrandView {
constructor() {
constructor(model) {
this.model = model;
this.sectionsContainer = document.getElementById('sections');
this.addFieldSheet = document.getElementById('addFieldSheet');
this.overlay = document.getElementById('overlay');
this.confirmationDialog = document.getElementById('confirmationDialog');
this.messageBottomSheet = document.getElementById('messageBottomSheet');
this.selectFolderBtn = document.getElementById('selectFolder');
}

async initializeApp() {
switch (this.model.source) {
case DataSource.LOCAL:
return await this.initLoal();
case DataSource.REMOTE:
return await this.initRemote();
default:
throw new Error('Unknown data source');
}
}

async initRemote() {
this.selectFolderBtn.style.display = 'block';
this.selectFolderBtn.addEventListener('click', async () => {
try {
const dirHandle = await window.showDirectoryPicker();
const configList = await this.processFolder(dirHandle);
const json = JSON.stringify(configList, null, 2);
console.log(json, this);
const sections = {sectionItems: configList, brandName: dirHandle.name};
this.onSelectBrandFolder(sections)
} catch (error) {
console.error('Error:', error);
}
});
}

async processFolder(dirHandle) {
const configList = [];
const expectedFiles = [
{key: 'theme', name: 'Theme Configuration', input_type: 'color', filename: 'theme.json'},
{key: 'brand_config', name: 'Brand Configuration', input_type: 'text', filename: 'brand_config.json'},
{
key: 'android_config',
name: 'Android Platform Configuration',
input_type: 'text',
filename: 'android_config.json'
},
{key: 'android_signing', name: 'Android Signing', input_type: 'text', filename: 'android_signing.json'},
{key: 'ios_config', name: 'iOS Platform Configuration', input_type: 'text', filename: 'ios_config.json'},
{key: 'ios_signing', name: 'iOS Signing', input_type: 'text', filename: 'ios_signing.json'}
];

for (const file of expectedFiles) {
try {
const fileContent = await this.findAndReadFile(dirHandle, file.filename);
if (fileContent) {
configList.push({
key: file.key,
name: file.name,
inputType: file.input_type,
content: JSON.parse(fileContent)
});
}
} catch (error) {
console.warn(`File not found or invalid JSON: ${file.filename}`);
}
}

return configList;
}

async findAndReadFile(dirHandle, filename) {
const queue = [dirHandle];
while (queue.length > 0) {
const currentHandle = queue.shift();
for await (const entry of currentHandle.values()) {
if (entry.kind === 'file' && entry.name === filename) {
const file = await entry.getFile();
return await file.text();
} else if (entry.kind === 'directory') {
queue.push(entry);
}
}
}
return null;
}

async initLoal() {

}

updateAppNameTitle(brandName) {
Expand Down Expand Up @@ -265,6 +350,10 @@ class BrandView {
this.onSectionChanged = handler;
}

setOnSelectBrandFolder(handler) {
this.onSelectBrandFolder = handler;
}

setOnDeleteFieldHandler(handler) {
this.onDeleteField = handler;
}
Expand Down
4 changes: 4 additions & 0 deletions solara/lib/core/dashboard/brand/brand.html
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,9 @@
.close-message:hover {
background-color: #0056b3; /* Darker shade on hover */
}
.select-brand-folder {
display: none;
}
</style>
</head>
<body>
Expand All @@ -554,6 +557,7 @@ <h1><span id="brandNametitle"></span></h1>
</div>

<div class="container">
<button id="selectFolder" class="select-brand-folder">Select Brand Folder</button>
<div id="sections"></div>
</div>

Expand Down
6 changes: 3 additions & 3 deletions solara/lib/core/dashboard/brand/brand.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import BrandModel from './BrandModel.js';
import BrandView from './BrandView.js';
import BrandController from './BrandController.js';

document.addEventListener('DOMContentLoaded', () => {
document.addEventListener('DOMContentLoaded', async () => {
const model = new BrandModel();
const view = new BrandView();
const view = new BrandView(model);
const controller = new BrandController(model, view);
controller.initializeApp();
await controller.initializeApp();
});
3 changes: 1 addition & 2 deletions solara/lib/core/dashboard/brand/source/BrandRemoteSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ class BrandRemoteSource {
constructor() {
}

// Keep functions for future implementation
async fetchSections(brandKey) {
// Empty for now

}

async fetchCurrentBrand(brandKey) {
Expand Down

0 comments on commit f06683b

Please sign in to comment.