Skip to content

Commit

Permalink
added most used app wip
Browse files Browse the repository at this point in the history
  • Loading branch information
lunokjod committed Jun 23, 2023
1 parent bc07009 commit e67389e
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 0 deletions.
127 changes: 127 additions & 0 deletions src/app/MostUsedApps.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
//
// LunokWatch, a open source smartwatch software
// Copyright (C) 2022,2023 Jordi Rubió <jordi@binarycell.org>
// This file is part of LunokWatch.
//
// LunokWatch is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.
//
// LunokWatch is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along with
// LunokWatch. If not, see <https://www.gnu.org/licenses/>.
//

#include "../UI/AppLuITemplate.hpp"
#include "MostUsedApps.hpp"
#include "../UI/controls/IconMenu.hpp"
#include "../UI/controls/base/Container.hpp"
#include "../UI/controls/Text.hpp"
#include "../UI/controls/Button.hpp"
#include "../UI/controls/XBM.hpp"
#include "LogView.hpp"

#include <LilyGoWatch.h>
#include "../system/Datasources/database.hpp"
#include "../resources.hpp"

using namespace LuI;

LuIMostUsedApplication::LuIMostUsedApplication() {
directDraw=false; // disable direct draw meanwhile build the UI
// fill view with background color
canvas->fillSprite(ThCol(background)); // use theme colors

/// create root container with two slots horizontal
Container * screen = new Container(LuI_Horizontal_Layout,2);
// bottom buttons container
Container * bottomButtonContainer = new Container(LuI_Vertical_Layout,2);

// main view space
Container * viewContainer = new Container(LuI_Horizontal_Layout,1);

// if use quota, all the slot quotas must sum equal the total number of elements
// example: default quota in 2 controls result 50/50% of view space with 1.0 of quota everyone (2.0 in total)

screen->AddChild(viewContainer,1.65);
// add bottom button bar shirnked
screen->AddChild(bottomButtonContainer,0.35);

// add back button to dismiss
Button *backButton = new Button(LuI_Vertical_Layout,1,NO_DECORATION);
backButton->border=10;
backButton->tapCallback=[](void * obj){ LaunchWatchface(); }; // callback when tap
// load icon in XBM format
XBM * backButtonIcon = new XBM(img_backscreen_24_width,img_backscreen_24_height,img_backscreen_24_bits);
// put the icon inside the button
backButton->AddChild(backButtonIcon);
// shrink button to left and empty control oversized (want button on left bottom)
bottomButtonContainer->AddChild(backButton,0.35);
bottomButtonContainer->AddChild(new Text("Most used apps"),1.65);

AddChild(screen);

if ( nullptr != systemDatabase ) {
// build empty tree
entriesCounter = JSON.parse("{}");
//systemDatabase->SendSQL("SELECT message FROM rawlogSession WHERE message LIKE 'Application: %%:%%';");
systemDatabase->SendSQL("SELECT message FROM rawlogSession WHERE message LIKE 'Application: %%:%%';",[](void *data, int argc, char **argv, char **azColName) {
LuIMostUsedApplication *self=(LuIMostUsedApplication*)data;
for (int i = 0; i<argc; i++) {
if ( 0 == strcmp(azColName[i],"message")) {
//lSysLog("-> %s\n", (argv[i] ? argv[i] : "NULL"));
// search app name
char * strFrom=nullptr;
int currentOffset=strlen(argv[i]);
while(currentOffset>-1) {
if ( ':' == argv[i][currentOffset] ) {
strFrom = argv[i]+currentOffset+1;
//lSysLog("CURRENT APP STRING OFFSET: %d as: '%s'\n",currentOffset,strFrom);
break;
}
currentOffset--;
}
if ( nullptr != strFrom ) {
// YEAH! FOUND!!
//lSysLog("'%s' app found from log\n",strFrom);
bool alreadyExists = self->entriesCounter.hasOwnProperty(strFrom);
if ( alreadyExists ) {
// GET VALUE
int currVal=(int)self->entriesCounter[(const char*)strFrom];
currVal++; // increment counter for app
lSysLog("Updating entry: '%s' to: %d\n",strFrom,currVal);
// update value on json property
self->entriesCounter[(const char*)strFrom]= currVal; //JSONVar(currVal);
if ( self->HighCount < currVal ) { self->HighCount = currVal; }
} else {
// create entry with 1 as value
lSysLog("Adding entry: '%s'\n",strFrom);
self->entriesCounter[(const char*)strFrom] = 1; //JSONVar(1);
}
}
}
}
return 0;
},this);
}

}
bool LuIMostUsedApplication::Tick() {
if ( lastHighCount != HighCount ) {
lastHighCount=HighCount;
lAppLog("JSON: '%s'\n",JSON.stringify(entriesCounter).c_str());
/*
int numEntries = entriesCounter.length();
lAppLog("HIGHTER VAL: %d entries: %d\n",HighCount,numEntries);
for(int currentEntry=0; numEntries>currentEntry;currentEntry++) {
JSONVar entryLaunches = entriesCounter[currentEntry];
lAppLog(" KEY NAMES: '%s' launches %d\n",(const char*)entryLaunches, (int)entryLaunches);
}*/
}
return TemplateLuIApplication::Tick();
}
38 changes: 38 additions & 0 deletions src/app/MostUsedApps.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// LunokWatch, a open source smartwatch software
// Copyright (C) 2022,2023 Jordi Rubió <jordi@binarycell.org>
// This file is part of LunokWatch.
//
// LunokWatch is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.
//
// LunokWatch is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along with
// LunokWatch. If not, see <https://www.gnu.org/licenses/>.
//

#ifndef __LUNOKIOT__APPLICATION__LUI_MOST_USED_APPS__
#define __LUNOKIOT__APPLICATION__LUI_MOST_USED_APPS__

#include "../UI/AppLuITemplate.hpp"
#include <Arduino_JSON.h>

class LuIMostUsedApplication : public TemplateLuIApplication {
private:
int lastHighCount=0;
public:
int HighCount=0;
JSONVar entriesCounter;
// max apps used
LuIMostUsedApplication();
const char *AppName() override { return "Most used"; };
bool Tick();
};

#endif

0 comments on commit e67389e

Please sign in to comment.