settings/do_not_disturb/
do_not_disturb_controller.rs

1// Copyright 2019 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::base::SettingInfo;
6use crate::do_not_disturb::types::DoNotDisturbInfo;
7use crate::handler::base::Request;
8use crate::handler::setting_handler::persist::{controller as data_controller, ClientProxy};
9use crate::handler::setting_handler::{
10    controller, ControllerError, IntoHandlerResult, SettingHandlerResult,
11};
12use async_trait::async_trait;
13use settings_storage::device_storage::{DeviceStorage, DeviceStorageCompatible};
14use settings_storage::storage_factory::{NoneT, StorageAccess};
15
16impl DeviceStorageCompatible for DoNotDisturbInfo {
17    type Loader = NoneT;
18    const KEY: &'static str = "do_not_disturb_info";
19}
20
21impl Default for DoNotDisturbInfo {
22    fn default() -> Self {
23        DoNotDisturbInfo::new(false, false)
24    }
25}
26
27impl From<DoNotDisturbInfo> for SettingInfo {
28    fn from(info: DoNotDisturbInfo) -> SettingInfo {
29        SettingInfo::DoNotDisturb(info)
30    }
31}
32
33pub struct DoNotDisturbController {
34    client: ClientProxy,
35}
36
37impl StorageAccess for DoNotDisturbController {
38    type Storage = DeviceStorage;
39    type Data = DoNotDisturbInfo;
40    const STORAGE_KEY: &'static str = DoNotDisturbInfo::KEY;
41}
42
43#[async_trait(?Send)]
44impl data_controller::Create for DoNotDisturbController {
45    async fn create(client: ClientProxy) -> Result<Self, ControllerError> {
46        Ok(DoNotDisturbController { client })
47    }
48}
49
50#[async_trait(?Send)]
51impl controller::Handle for DoNotDisturbController {
52    async fn handle(&self, request: Request) -> Option<SettingHandlerResult> {
53        match request {
54            Request::SetDnD(dnd_info) => {
55                let id = fuchsia_trace::Id::new();
56                let mut stored_value = self.client.read_setting::<DoNotDisturbInfo>(id).await;
57                if dnd_info.user_dnd.is_some() {
58                    stored_value.user_dnd = dnd_info.user_dnd;
59                }
60                if dnd_info.night_mode_dnd.is_some() {
61                    stored_value.night_mode_dnd = dnd_info.night_mode_dnd;
62                }
63                Some(self.client.write_setting(stored_value.into(), id).await.into_handler_result())
64            }
65            Request::Get => Some(
66                self.client
67                    .read_setting_info::<DoNotDisturbInfo>(fuchsia_trace::Id::new())
68                    .await
69                    .into_handler_result(),
70            ),
71            _ => None,
72        }
73    }
74}