Skip to content

Commit

Permalink
✨ Check Connection
Browse files Browse the repository at this point in the history
  • Loading branch information
adlerluiz committed Jun 26, 2019
1 parent fd7a01f commit 253f5b9
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 15 deletions.
4 changes: 1 addition & 3 deletions assets/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ html, body {
color: #AAA;
}

.center-loading {
.center-flex {
width: 100vw;
min-height: 100vh;
text-align: center;
Expand All @@ -74,8 +74,6 @@ html, body {
}

.is-offline {
bottom: 20px;
position: fixed;
width: 100%;
text-align: center;
color: #EEE;
Expand Down
10 changes: 6 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@

</div>

<div class="center-loading">
<div class="preloader-wrapper big active">
<div class="center-flex">

<div id="center-loading" class="preloader-wrapper big active">
<div class="spinner-layer spinner-blue-only">
<div class="circle-clipper left">
<div class="circle"></div>
Expand All @@ -41,9 +42,10 @@
</div>
</div>
</div>
</div>

<div id="is-offline" class="hide is-offline"> <i class="material-icons">signal_wifi_off</i> </div>
<div id="is-offline" class="hide is-offline"> <i class="material-icons medium">signal_wifi_off</i> </div>

</div>

<script>
require('./renderer.js');
Expand Down
48 changes: 40 additions & 8 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ let lastSongAuthor;
let likeStatus;
let doublePressPlayPause;
let lastConnectionStatusIsOnline = false;
let hasLoadedUrl;

let mainWindowUrl = 'https://music.youtube.com';

Expand Down Expand Up @@ -113,32 +114,63 @@ function createWindow() {
width: mainWindowSize.width - 2,
height: mainWindowSize.height - 30
});
view.webContents.loadURL( mainWindowUrl );

if (store.get('settings-continue-where-left-of') && store.get('window-url')) {
mainWindowUrl = store.get('window-url');
}

view.webContents.loadURL( mainWindowUrl );

async function checkConnection() {
/**
* Check if is online
*/
var is_online = await isOnline();

/**
* If online, consider that already loaded the url
* If offline, mark the variable that the url was not read
*/
if (hasLoadedUrl == undefined) {
hasLoadedUrl = is_online;
}

/**
* Emmit is online or offline to render.js
*/
mainWindow.send( 'is-online', is_online );

/**
* If online and lastConnectionStatusIsOnline is false, set BrowserView and check hasLoadedUrl to loadURL
* else set BrowserView to null to show Loading circle and show icon that not have connection
*/
if (is_online == true) {
if (lastConnectionStatusIsOnline == false) {
mainWindow.setBrowserView( view );
view.webContents.loadURL( mainWindowUrl );
if ( hasLoadedUrl == false ) {
view.webContents.loadURL( mainWindowUrl );
hasLoadedUrl = true;
}
}
} else {
mainWindow.setBrowserView( null );
if ( lastConnectionStatusIsOnline == true ) {
mainWindow.setBrowserView( null );
mediaControl.createThumbar(mainWindow, 'play', likeStatus);
mediaControl.stopTrack(view);
}
}

lastConnectionStatusIsOnline = is_online;
}

/**
* Check connection every 30 seconds
*/
setTimeout( function() {
checkConnection();
}, 10 * 1000 );
}

checkConnection();
setInterval( function() {
checkConnection();
}, 10 * 1000 );
checkConnection();

// Open the DevTools.
// mainWindow.webContents.openDevTools({ mode: 'detach' });
Expand Down
2 changes: 2 additions & 0 deletions renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ ipc.on( 'is-online', function( e, isOnline ) {

if ( isOnline ) {
document.getElementById( 'is-offline' ).classList.add( 'hide' );
document.getElementById( 'center-loading' ).classList.remove( 'hide' );
} else {
document.getElementById( 'is-offline' ).classList.remove( 'hide' );
document.getElementById( 'center-loading' ).classList.add( 'hide' );
}

} );
Expand Down

0 comments on commit 253f5b9

Please sign in to comment.