Indicator as Clean Architecture

jh-changes
Shin'ya Ueoka 6 years ago
parent b130fd5268
commit 42d902982a
  1. 11
      src/background/controllers/addon-enabled.js
  2. 2
      src/background/index.js
  3. 13
      src/background/infrastructures/content-message-client.js
  4. 8
      src/background/infrastructures/content-message-listener.js
  5. 12
      src/background/presenters/indicator.js
  6. 4
      src/background/presenters/tab.js
  7. 29
      src/background/usecases/addon-enabled.js

@ -0,0 +1,11 @@
import AddonEnabledInteractor from '../usecases/addon-enabled';
export default class AddonEnabledController {
constructor() {
this.addonEnabledInteractor = new AddonEnabledInteractor();
}
indicate(enabled) {
this.addonEnabledInteractor.indicate(enabled);
}
}

@ -1,7 +1,6 @@
import BackgroundComponent from 'background/components/background'; import BackgroundComponent from 'background/components/background';
import OperationComponent from 'background/components/operation'; import OperationComponent from 'background/components/operation';
import TabComponent from 'background/components/tab'; import TabComponent from 'background/components/tab';
import IndicatorComponent from 'background/components/indicator';
import reducers from 'background/reducers'; import reducers from 'background/reducers';
import { createStore, applyMiddleware } from 'redux'; import { createStore, applyMiddleware } from 'redux';
import promise from 'redux-promise'; import promise from 'redux-promise';
@ -19,7 +18,6 @@ const store = createStore(
const backgroundComponent = new BackgroundComponent(store); const backgroundComponent = new BackgroundComponent(store);
const operationComponent = new OperationComponent(store); const operationComponent = new OperationComponent(store);
const tabComponent = new TabComponent(store); const tabComponent = new TabComponent(store);
const indicatorComponent = new IndicatorComponent(store);
/* eslint-enable no-unused-vars */ /* eslint-enable no-unused-vars */
new SettingController().reload(); new SettingController().reload();

@ -9,4 +9,17 @@ export default class ContentMessageClient {
}); });
} }
} }
async getAddonEnabled(tabId) {
let { enabled } = await browser.tabs.sendMessage(tabId, {
type: messages.ADDON_ENABLED_QUERY,
});
return enabled;
}
toggleAddonEnabled(tabId) {
return browser.tabs.sendMessage(tabId, {
type: messages.ADDON_TOGGLE_ENABLED,
});
}
} }

@ -2,12 +2,14 @@ import messages from '../../shared/messages';
import CompletionsController from '../controllers/completions'; import CompletionsController from '../controllers/completions';
import SettingController from '../controllers/setting'; import SettingController from '../controllers/setting';
import FindController from '../controllers/find'; import FindController from '../controllers/find';
import AddonEnabledController from '../controllers/addon-enabled';
export default class ContentMessageListener { export default class ContentMessageListener {
constructor() { constructor() {
this.settingController = new SettingController(); this.settingController = new SettingController();
this.completionsController = new CompletionsController(); this.completionsController = new CompletionsController();
this.findController = new FindController(); this.findController = new FindController();
this.addonEnabledController = new AddonEnabledController();
} }
run() { run() {
@ -35,6 +37,8 @@ export default class ContentMessageListener {
return this.onFindGetKeyword(); return this.onFindGetKeyword();
case messages.FIND_SET_KEYWORD: case messages.FIND_SET_KEYWORD:
return this.onFindSetKeyword(message.keyword); return this.onFindSetKeyword(message.keyword);
case messages.ADDON_ENABLED_RESPONSE:
return this.onAddonEnabledResponse(message.enabled);
} }
} }
@ -58,4 +62,8 @@ export default class ContentMessageListener {
onFindSetKeyword(keyword) { onFindSetKeyword(keyword) {
return this.findController.setKeyword(keyword); return this.findController.setKeyword(keyword);
} }
onAddonEnabledResponse(enabled) {
return this.addonEnabledController.indicate(enabled);
}
} }

@ -0,0 +1,12 @@
export default class IndicatorPresenter {
indicate(enabled) {
let path = enabled
? 'resources/enabled_32x32.png'
: 'resources/disabled_32x32.png';
return browser.browserAction.setIcon({ path });
}
onClick(listener) {
browser.browserAction.onClicked.addListener(listener);
}
}

@ -2,4 +2,8 @@ export default class TabPresenter {
create(url) { create(url) {
browser.tabs.create({ url, }); browser.tabs.create({ url, });
} }
onSelected(listener) {
browser.tabs.onActivated.addListener(listener);
}
} }

@ -0,0 +1,29 @@
import IndicatorPresenter from '../presenters/indicator';
import TabPresenter from '../presenters/tab';
import ContentMessageClient from '../infrastructures/content-message-client';
export default class AddonEnabledInteractor {
constructor() {
this.indicatorPresentor = new IndicatorPresenter();
this.indicatorPresentor.onClick(tab => this.onIndicatorClick(tab.id));
this.tabPresenter = new TabPresenter();
this.tabPresenter.onSelected(info => this.onTabSelected(info.tabId));
this.contentMessageClient = new ContentMessageClient();
}
indicate(enabled) {
this.indicatorPresentor.indicate(enabled);
}
onIndicatorClick(tabId) {
return this.contentMessageClient.toggleAddonEnabled(tabId);
}
async onTabSelected(tabId) {
let enabled = await this.contentMessageClient.getAddonEnabled(tabId);
return this.indicatorPresentor.indicate(enabled);
}
}