Skip to content
This repository has been archived by the owner on Oct 6, 2023. It is now read-only.

Commit

Permalink
update touchbar with data
Browse files Browse the repository at this point in the history
  • Loading branch information
kremalicious committed May 22, 2019
1 parent 9954165 commit d5b43be
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 82 deletions.
1 change: 1 addition & 0 deletions src/app/components/Balance.jsx
Expand Up @@ -5,6 +5,7 @@ import { AppContext } from '../store/createContext'
import { cryptoFormatter } from '../../utils'
import './Balance.css'
import { fadeIn } from './Animations'
import { cryptoFormatter } from '../../utils'

const Animation = posed.h1(fadeIn)

Expand Down
1 change: 1 addition & 0 deletions src/app/components/Ticker.jsx
Expand Up @@ -4,6 +4,7 @@ import { AppContext } from '../store/createContext'
import { cryptoFormatter } from '../../utils'
import './Ticker.css'
import { fadeIn } from './Animations'
import { cryptoFormatter } from '../../utils'

const Item = posed.div(fadeIn)

Expand Down
49 changes: 0 additions & 49 deletions src/app/components/Touchbar.js

This file was deleted.

28 changes: 17 additions & 11 deletions src/app/store/AppProvider.jsx
Expand Up @@ -3,9 +3,8 @@ import PropTypes from 'prop-types'
import ms from 'ms'
import { ipcRenderer } from 'electron'
import Store from 'electron-store'
import { ipcRenderer } from 'electron'
import { AppContext } from './createContext'
import fetchData from '../util/fetch'
import fetchData from '../utils/fetch'
import { refreshInterval, prices, oceanTokenContract } from '../config'

export default class AppProvider extends PureComponent {
Expand All @@ -21,29 +20,32 @@ export default class AppProvider extends PureComponent {
currency: 'ocean',
needsConfig: false,
prices: Object.assign(...prices.map(key => ({ [key]: 0 }))),
toggleCurrencies: currency => this.setState({ currency }),
toggleCurrencies: currency => this.toggleCurrencies(currency),
setBalances: () => this.setBalances(),
accentColor: ''
}

async componentDidMount() {
// listener for accent color
ipcRenderer.on('accent-color', (event, accentColor) => {
this.setState({ accentColor })
})

await this.fetchAndSetPrices()
// listener for touchbar
ipcRenderer.on('setCurrency', (evt, currency) =>
this.state.toggleCurrencies(currency)
)

const newPrizes = await this.fetchAndSetPrices()
this.setState({ prices: newPrizes })
ipcRenderer.send('prices-updated', newPrizes)

await this.setBalances()

setInterval(this.fetchAndSetPrices, ms(refreshInterval))
setInterval(this.setBalances, ms(refreshInterval))

this.setState({ isLoading: false })

// listener for touchbar
ipcRenderer.on('setCurrency', (evt, currency) => {
console.log('pong')
this.context.toggleCurrencies(currency)
})
}

getAccounts() {
Expand Down Expand Up @@ -85,7 +87,7 @@ export default class AppProvider extends PureComponent {
}))
)

this.setState({ prices: newPrizes })
return newPrizes
}

setBalances = async () => {
Expand Down Expand Up @@ -118,6 +120,10 @@ export default class AppProvider extends PureComponent {
}
}

toggleCurrencies(currency) {
this.setState({ currency })
}

render() {
return (
<AppContext.Provider value={this.state}>
Expand Down
File renamed without changes.
13 changes: 10 additions & 3 deletions src/main.js
@@ -1,9 +1,10 @@
const path = require('path')
const { app, BrowserWindow, systemPreferences } = require('electron')
const { app, BrowserWindow, systemPreferences, ipcMain } = require('electron')
const pkg = require('../package.json')
const buildMenu = require('./menu')
const buildTouchbar = require('./touchbar')
const { buildTouchbar, updateTouchbar } = require('./touchbar')
const { rgbaToHex } = require('./utils')
const { prices } = require('./app/config')

let mainWindow

Expand Down Expand Up @@ -109,7 +110,13 @@ const createWindow = async () => {
buildMenu(mainWindow)
// Load touchbar
process.platform === 'darwin' &&
buildTouchbar(mainWindow, app.getLocale())
const systemAccentColor = systemPreferences.getAccentColor()
buildTouchbar(prices, mainWindow, systemAccentColor)

ipcMain.on('prices-updated', (event, pricesNew) => {
updateTouchbar(pricesNew, mainWindow, systemAccentColor)
})
}
}

app.on('ready', () => {
Expand Down
43 changes: 24 additions & 19 deletions src/touchbar.js
@@ -1,37 +1,42 @@
const { TouchBar } = require('electron')
const { formatCurrency } = require('@coingecko/cryptoformat')
const { prices } = require('./app/config')
const { cryptoFormatter } = require('./utils')

const { TouchBarButton } = TouchBar

// const currency = ipc...
// const prices = ipc...

const createButton = (price, key, locale, mainWindow) => {
const formattedPrice = formatCurrency(price, key.toUpperCase(), locale)
.replace(/OCEAN/, 'Ọ')
.replace(/BTC/, 'Ƀ')
.replace(/ETH/, 'Ξ')

return new TouchBarButton({
label: formattedPrice,
click: () => {
console.log('ping')
mainWindow.webContents.send('setCurrency', key)
}
// backgroundColor: currency === 'ocean' ? '#f6388a' : '#141414'
const createButton = (value, key, mainWindow, systemAccentColor) =>
new TouchBarButton({
label: cryptoFormatter(value, key.toUpperCase()),
click: () => mainWindow.webContents.send('setCurrency', key),
backgroundColor: key === 'ocean' ? systemAccentColor : '#141414'
})

const buildTouchbar = (prices, mainWindow, systemAccentColor) => {
const touchBar = new TouchBar({
items: [
createButton(1, 'ocean', mainWindow, systemAccentColor),
...prices.map(key => createButton(0, key, mainWindow, systemAccentColor))
]
})

mainWindow.setTouchBar(touchBar)
}

const buildTouchbar = (mainWindow, locale) => {
const updateTouchbar = (prices, mainWindow, systemAccentColor) => {
const touchBar = new TouchBar({
items: [
createButton(1, 'ocean', locale, mainWindow),
...prices.map(key => createButton(0, key, locale, mainWindow))
createButton(1, 'ocean', mainWindow, systemAccentColor),
...Object.entries(prices)
.filter(([key]) => key !== 'ocean')
.map(([key, value]) =>
createButton(value, key, mainWindow, systemAccentColor)
)
]
})

mainWindow.setTouchBar(touchBar)
}

module.exports = buildTouchbar
module.exports = { buildTouchbar, updateTouchbar }

0 comments on commit d5b43be

Please sign in to comment.