diff --git a/package-lock.json b/package-lock.json index 4adb0e19..344251cc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13318,6 +13318,11 @@ "integrity": "sha1-SLtQiECfFvGCFmZkHETdGqrjzYg=", "dev": true }, + "lodash.uniqwith": { + "version": "4.5.0", + "resolved": "https://registry.npmjs.org/lodash.uniqwith/-/lodash.uniqwith-4.5.0.tgz", + "integrity": "sha1-egy/ZfQ7WShiWp1NDcVLGMrcfvM=" + }, "log-symbols": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/log-symbols/-/log-symbols-4.0.0.tgz", diff --git a/package.json b/package.json index a0801978..752f94ad 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "js-yaml": "^3.14.0", "lodash.debounce": "^4.0.8", "lodash.isequal": "^4.5.0", + "lodash.uniqwith": "^4.5.0", "material-design-icons": "^3.0.1", "mediaelement": "^4.2.16", "nedb": "^1.8.0", diff --git a/src/renderer/App.js b/src/renderer/App.js index 609c375a..0e4e2922 100644 --- a/src/renderer/App.js +++ b/src/renderer/App.js @@ -33,6 +33,7 @@ export default Vue.extend({ mounted: function () { this.$store.dispatch('grabUserSettings') this.$store.dispatch('grabHistory') + this.$store.dispatch('grabAllProfiles', this.$t('Profile.All Channels')) this.$store.commit('setUsingElectron', useElectron) this.checkThemeSettings() this.checkLocale() diff --git a/src/renderer/store/modules/profile.js b/src/renderer/store/modules/profile.js new file mode 100644 index 00000000..dc4bfd7a --- /dev/null +++ b/src/renderer/store/modules/profile.js @@ -0,0 +1,106 @@ +import Datastore from 'nedb' + +let dbLocation + +if (window && window.process && window.process.type === 'renderer') { + // Electron is being used + /* let dbLocation = localStorage.getItem('dbLocation') + + if (dbLocation === null) { + const electron = require('electron') + dbLocation = electron.remote.app.getPath('userData') + } */ + + const electron = require('electron') + dbLocation = electron.remote.app.getPath('userData') + + dbLocation = dbLocation + '/profiles.db' +} else { + dbLocation = 'profiles.db' +} + +const profileDb = new Datastore({ + filename: dbLocation, + autoload: true +}) + +const state = { + profileList: [], + activeProfile: 'allChannels' +} + +const getters = { + getProfileList: () => { + return state.historyCache + } +} + +const actions = { + grabAllProfiles ({ dispatch, commit }, defaultName = null) { + profileDb.find({}, (err, results) => { + if (!err) { + if (results.length === 0) { + dispatch('createDefaultProfile', defaultName) + } else { + commit('setProfileList', results) + } + } + }) + }, + + async createDefaultProfile ({ dispatch }, defaultName) { + const randomColor = await dispatch('getRandomColor') + const textColor = await dispatch('calculateColorLuminance', randomColor) + const defaultProfile = { + _id: 'allChannels', + name: defaultName, + bgColor: randomColor, + textColor: textColor, + subscriptions: [] + } + console.log(defaultProfile) + return + profileDb.update({ _id: 'allChannels' }, defaultProfile, { upsert: true }, (err, numReplaced) => { + if (!err) { + dispatch('grabAllProfiles') + } + }) + }, + + updateProfile ({ dispatch }, profile) { + profileDb.update({ name: profile.name }, profile, { upsert: true }, (err, numReplaced) => { + if (!err) { + dispatch('grabAllProfiles') + } + }) + }, + + removeFromHistory ({ dispatch }, videoId) { + historyDb.remove({ videoId: videoId }, (err, numReplaced) => { + if (!err) { + dispatch('grabHistory') + } + }) + }, + + updateWatchProgress ({ dispatch }, videoData) { + historyDb.update({ videoId: videoData.videoId }, { $set: { watchProgress: videoData.watchProgress } }, { upsert: true }, (err, numReplaced) => { + if (!err) { + dispatch('grabHistory') + } + }) + } +} + +const mutations = { + setHistoryCache (state, historyCache) { + state.historyCache = historyCache + } +} + +export default { + state, + getters, + actions, + mutations +} diff --git a/src/renderer/store/modules/utils.js b/src/renderer/store/modules/utils.js index 788c081d..2e888be0 100644 --- a/src/renderer/store/modules/utils.js +++ b/src/renderer/store/modules/utils.js @@ -28,6 +28,24 @@ const state = { 'mainAmber', 'mainOrange', 'mainDeepOrange' + ], + colorValues: [ + '#d50000', + '#C51162', + '#AA00FF', + '#6200EA', + '#304FFE', + '#2962FF', + '#0091EA', + '#00B8D4', + '#00BFA5', + '#00C853', + '#64DD17', + '#AEEA00', + '#FFD600', + '#FFAB00', + '#FF6D00', + '#DD2C00' ] } @@ -63,6 +81,26 @@ const actions = { return state.colorClasses[randomInt] }, + getRandomColor () { + const randomInt = Math.floor(Math.random() * state.colorValues.length) + return state.colorValues[randomInt] + }, + + calculateColorLuminance (_, colorValue) { + const cutHex = colorValue.substring(1, 7) + const colorValueR = parseInt(cutHex.substring(0, 2), 16) + const colorValueG = parseInt(cutHex.substring(2, 4), 16) + const colorValueB = parseInt(cutHex.substring(4, 6), 16) + + const luminance = (0.299 * colorValueR + 0.587 * colorValueG + 0.114 * colorValueB) / 255 + + if (luminance > 0.5) { + return '#000000' + } else { + return '#FFFFFF' + } + }, + getVideoIdFromUrl (_, url) { /** @type {URL} */ let urlObject diff --git a/src/renderer/views/ProfileSettings/ProfileSettings.vue b/src/renderer/views/ProfileSettings/ProfileSettings.vue index 0e840e9e..0bbb9867 100644 --- a/src/renderer/views/ProfileSettings/ProfileSettings.vue +++ b/src/renderer/views/ProfileSettings/ProfileSettings.vue @@ -1,7 +1,7 @@