# ACM Construction - Estimation ## 6.1 ![](https://i.imgur.com/69ptTVg.png) There are two different controllers (yellow and red). Will use event bus to communicate between two controllers which are `EitObservationDetails.controller.ts`, `WorkOnDailyReport.controller.ts`. --- #### We will take `Refresh Button` as example: and for `Nav back button`, `list item` would be similar. --- #### Basic Idea: **Event Bus** `C1: controller1`, `C2: controller2`. 1. C1 ----save message----> C2 2. C2 check if needs to save. If yes, pop up window to ask confirm. If confirm save, do save. 3. C2 ----do refresh message----> C1 4. C1 Do refresh --- #### Detail Code: in `EditObservationDetails.controller.ts`: ```typescript= onInit() { ... const busSave = this.getEventBus(); busSave.subscribe("save", "save", this.onSaveBus, this); } saveButtonEnabled() { const tableModeValue = this.tableModeSwitch.getState(); let enableSaveButton = false; if (this.observation.status === "New") { return true; } if (this.original.category !== this.categorySelect.getSelectedItem().getText()) { enableSaveButton = true; } if (this.original.label !== this.topicInput.getValue()) { enableSaveButton = true; } if (!tableModeValue) { if (this.original.value !== this.commentsText) { enableSaveButton = true; } } else { const data = this.tableModel.getData(); const rows = data.rows as MapToString[]; const columns = data.columns as string[]; const csv = CsvUtil.json2csv(rows, columns); this.observation.value = csv; if (this.original.value !== this.observation.value) { enableSaveButton = true; } } if (this.original.constructionFK !== this.observation.constructionFK) { enableSaveButton = true; } if (this.original.status === "New") { enableSaveButton = true; } return enableSaveButton; } async onSaveBus(sChannel, sEvent, oData) { if(this.saveButtonEnabled()) { // change data console.log("change data"); const confirmed = await ConfirmDialog.openAndAwaitAnswer({ title: "Are you sure leave without save?", text: "Are you sure no save?", yesText: "NO, please save", }); if(confirmed) { await this.onSavePress(null); } } // // send event that keep going const bus = this.getEventBus(); bus.publish(oData.text, oData.text); } ``` in `WorkOnDailyReport.controller.ts`: ```typescript= onInit() { ... const busRefresh = this.getEventBus(); busRefresh.subscribe("refresh", "refresh", this.onRefreshBus, this); } @WrapBusy("Refreshing report.") @CatchAndFlash() protected async onReportRefresh() { this.publishSave("refresh"); } onRefreshBus() { this.doRefresh(); } protected async doRefresh() { const report = this.getReport(); if (Device.isHybridOrLocalhost()) { await syncSingleReport(report.reportID); } const fullReport = await ReportsHelper.retrieveReportData(report.reportID); Models.reportModel.setData(fullReport); Models.reportModel.refresh(); this.checkIfReportIsPublishable(); if (!Device.system.phone) { this.getRouter().navTo({ target: PageTarget.Blank }); } } publishSave(text: string) { const bus = this.getEventBus(); bus.publish('save', 'save', { text }); } ``` ## 6.2 There are two `Delete` buttons. ### 1. ![](https://i.imgur.com/nxPRGn4.png) in **EditObservationDetails.controller.ts** ```typescript= async onDeletePress() { const confirmed = await ConfirmDialog.openAndAwaitAnswer({ title: "Are you sure?", text: "Are you sure delete?", yesText: "OK", }); if(!confirmed) return; ``` ### 2. ![](https://i.imgur.com/GXDU9Cp.png) in **WorkOnDailyReport.controller.ts** ```typescript= @WrapViewBusy() async onDeleteObservationPress(_evt: ControlEvent) { const confirmed = await ConfirmDialog.openAndAwaitAnswer({ title: "Are you sure?", text: "Are you sure delete?", yesText: "OK", }); if(!confirmed) return; ```