parent
a1e5e97200
commit
87ed1f43a9
11 changed files with 104 additions and 114 deletions
@ -0,0 +1,11 @@ |
||||
import VersionInteractor from '../usecases/version'; |
||||
|
||||
export default class VersionController { |
||||
constructor() { |
||||
this.versionInteractor = new VersionInteractor(); |
||||
} |
||||
|
||||
notifyIfUpdated() { |
||||
this.versionInteractor.notifyIfUpdated(); |
||||
} |
||||
} |
@ -0,0 +1,23 @@ |
||||
const NOTIFICATION_ID = 'vimvixen-update'; |
||||
|
||||
export default class Notifier { |
||||
notify(title, message, onclick) { |
||||
const listener = (id) => { |
||||
if (id !== NOTIFICATION_ID) { |
||||
return; |
||||
} |
||||
|
||||
onclick(); |
||||
|
||||
browser.notifications.onClicked.removeListener(listener); |
||||
}; |
||||
browser.notifications.onClicked.addListener(listener); |
||||
|
||||
return browser.notifications.create(NOTIFICATION_ID, { |
||||
'type': 'basic', |
||||
'iconUrl': browser.extension.getURL('resources/icon_48x48.png'), |
||||
title, |
||||
message, |
||||
}); |
||||
} |
||||
} |
@ -0,0 +1,5 @@ |
||||
export default class TabRepository { |
||||
create(url) { |
||||
browser.tabs.create({ url, }); |
||||
} |
||||
} |
@ -0,0 +1,10 @@ |
||||
export default class VersionRepository { |
||||
async get() { |
||||
let { version } = await browser.storage.local.get('version'); |
||||
return version; |
||||
} |
||||
|
||||
update(version) { |
||||
return browser.storage.local.set({ version }); |
||||
} |
||||
} |
@ -1,38 +0,0 @@ |
||||
import * as storage from './storage'; |
||||
import * as releaseNotes from './release-notes'; |
||||
import manifest from '../../../../manifest.json'; |
||||
|
||||
const NOTIFICATION_ID = 'vimvixen-update'; |
||||
|
||||
const notificationClickListener = (id) => { |
||||
if (id !== NOTIFICATION_ID) { |
||||
return; |
||||
} |
||||
|
||||
browser.tabs.create({ url: releaseNotes.url(manifest.version) }); |
||||
browser.notifications.onClicked.removeListener(notificationClickListener); |
||||
}; |
||||
|
||||
const checkUpdated = async() => { |
||||
let prev = await storage.load(); |
||||
if (!prev) { |
||||
return true; |
||||
} |
||||
return manifest.version !== prev; |
||||
}; |
||||
|
||||
const notify = () => { |
||||
browser.notifications.onClicked.addListener(notificationClickListener); |
||||
return browser.notifications.create(NOTIFICATION_ID, { |
||||
'type': 'basic', |
||||
'iconUrl': browser.extension.getURL('resources/icon_48x48.png'), |
||||
'title': 'Vim Vixen ' + manifest.version + ' has been installed', |
||||
'message': 'Click here to see release notes', |
||||
}); |
||||
}; |
||||
|
||||
const commit = () => { |
||||
storage.save(manifest.version); |
||||
}; |
||||
|
||||
export { checkUpdated, notify, commit }; |
@ -1,8 +0,0 @@ |
||||
const url = (version) => { |
||||
if (version) { |
||||
return 'https://github.com/ueokande/vim-vixen/releases/tag/' + version; |
||||
} |
||||
return 'https://github.com/ueokande/vim-vixen/releases/'; |
||||
}; |
||||
|
||||
export { url }; |
@ -1,10 +0,0 @@ |
||||
const load = async() => { |
||||
let { version } = await browser.storage.local.get('version'); |
||||
return version; |
||||
}; |
||||
|
||||
const save = (version) => { |
||||
return browser.storage.local.set({ version }); |
||||
}; |
||||
|
||||
export { load, save }; |
@ -0,0 +1,41 @@ |
||||
import manifest from '../../../manifest.json'; |
||||
import VersionRepository from '../repositories/version'; |
||||
import TabRepository from '../repositories/tab'; |
||||
import Notifier from '../infrastructures/notifier'; |
||||
|
||||
export default class VersionInteractor { |
||||
constructor() { |
||||
this.versionRepository = new VersionRepository(); |
||||
this.tabRepository = new TabRepository(); |
||||
this.notifier = new Notifier(); |
||||
} |
||||
|
||||
async notifyIfUpdated() { |
||||
if (!await this.checkUpdated()) { |
||||
return; |
||||
} |
||||
|
||||
let title = 'Vim Vixen ' + manifest.version + ' has been installed'; |
||||
let message = 'Click here to see release notes'; |
||||
this.notifier.notify(title, message, () => { |
||||
let url = this.releaseNoteUrl(manifest.version); |
||||
this.tabRepository.create(url); |
||||
}); |
||||
this.versionRepository.update(manifest.version); |
||||
} |
||||
|
||||
async checkUpdated() { |
||||
let prev = await this.versionRepository.get(); |
||||
if (!prev) { |
||||
return true; |
||||
} |
||||
return manifest.version !== prev; |
||||
} |
||||
|
||||
releaseNoteUrl(version) { |
||||
if (version) { |
||||
return 'https://github.com/ueokande/vim-vixen/releases/tag/' + version; |
||||
} |
||||
return 'https://github.com/ueokande/vim-vixen/releases/'; |
||||
} |
||||
} |
@ -1,40 +0,0 @@ |
||||
import * as versions from 'background/shared/versions'; |
||||
import manifest from '../../../../manifest.json'; |
||||
|
||||
describe("shared/versions/storage", () => { |
||||
describe('#checkUpdated', () => { |
||||
beforeEach(() => { |
||||
return browser.storage.local.remove('version'); |
||||
}); |
||||
|
||||
it('return true if no previous versions', async() => { |
||||
let updated = await versions.checkUpdated(); |
||||
expect(updated).to.be.true; |
||||
}); |
||||
|
||||
it('return true if updated', async() => { |
||||
await browser.storage.local.set({ version: '0.001' }); |
||||
let updated = await versions.checkUpdated(); |
||||
expect(updated).to.be.true; |
||||
}); |
||||
|
||||
it('return false if not updated', async() => { |
||||
await browser.storage.local.set({ version: manifest.version }); |
||||
let updated = await versions.checkUpdated(); |
||||
expect(updated).to.be.false; |
||||
}); |
||||
}); |
||||
|
||||
describe('#commit', () => { |
||||
beforeEach(() => { |
||||
return browser.storage.local.remove('version'); |
||||
}); |
||||
|
||||
it('saves current version from manifest.json', async() => { |
||||
await versions.commit(); |
||||
let { version } = await browser.storage.local.get('version'); |
||||
expect(version).to.be.a('string'); |
||||
expect(version).to.equal(manifest.version); |
||||
}); |
||||
}); |
||||
}); |
Reference in new issue