2023-08-06 11:51:03 +08:00
|
|
|
use std::sync::Weak;
|
|
|
|
|
2023-05-17 16:33:44 +08:00
|
|
|
use flowy_error::{FlowyError, FlowyResult};
|
2024-06-30 17:38:39 +08:00
|
|
|
use flowy_sqlite::kv::KVStorePreferences;
|
2023-08-06 11:51:03 +08:00
|
|
|
use lib_dispatch::prelude::{data_result_ok, AFPluginData, AFPluginState, DataResult};
|
2023-05-17 16:33:44 +08:00
|
|
|
|
2023-07-29 09:46:24 +08:00
|
|
|
use crate::entities::{KeyPB, KeyValuePB};
|
2023-05-17 16:33:44 +08:00
|
|
|
|
2023-08-06 11:51:03 +08:00
|
|
|
pub(crate) async fn set_key_value_handler(
|
2024-06-30 17:38:39 +08:00
|
|
|
store_preferences: AFPluginState<Weak<KVStorePreferences>>,
|
2023-08-06 11:51:03 +08:00
|
|
|
data: AFPluginData<KeyValuePB>,
|
|
|
|
) -> FlowyResult<()> {
|
2023-05-17 16:33:44 +08:00
|
|
|
let data = data.into_inner();
|
2023-08-06 11:51:03 +08:00
|
|
|
|
|
|
|
if let Some(store_preferences) = store_preferences.upgrade() {
|
|
|
|
match data.value {
|
|
|
|
None => store_preferences.remove(&data.key),
|
|
|
|
Some(value) => {
|
|
|
|
store_preferences.set_str(&data.key, value);
|
|
|
|
},
|
|
|
|
}
|
2023-05-17 16:33:44 +08:00
|
|
|
}
|
2023-08-06 11:51:03 +08:00
|
|
|
|
2023-05-17 16:33:44 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn get_key_value_handler(
|
2024-06-30 17:38:39 +08:00
|
|
|
store_preferences: AFPluginState<Weak<KVStorePreferences>>,
|
2023-05-17 16:33:44 +08:00
|
|
|
data: AFPluginData<KeyPB>,
|
|
|
|
) -> DataResult<KeyValuePB, FlowyError> {
|
2023-08-06 11:51:03 +08:00
|
|
|
match store_preferences.upgrade() {
|
2023-08-22 00:19:15 +08:00
|
|
|
None => Err(FlowyError::internal().with_context("The store preferences is already drop"))?,
|
2023-08-06 11:51:03 +08:00
|
|
|
Some(store_preferences) => {
|
|
|
|
let data = data.into_inner();
|
|
|
|
let value = store_preferences.get_str(&data.key);
|
|
|
|
data_result_ok(KeyValuePB {
|
|
|
|
key: data.key,
|
|
|
|
value,
|
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|
2023-05-17 16:33:44 +08:00
|
|
|
}
|
|
|
|
|
2023-08-06 11:51:03 +08:00
|
|
|
pub(crate) async fn remove_key_value_handler(
|
2024-06-30 17:38:39 +08:00
|
|
|
store_preferences: AFPluginState<Weak<KVStorePreferences>>,
|
2023-08-06 11:51:03 +08:00
|
|
|
data: AFPluginData<KeyPB>,
|
|
|
|
) -> FlowyResult<()> {
|
|
|
|
match store_preferences.upgrade() {
|
2023-08-22 00:19:15 +08:00
|
|
|
None => Err(FlowyError::internal().with_context("The store preferences is already drop"))?,
|
2023-08-06 11:51:03 +08:00
|
|
|
Some(store_preferences) => {
|
|
|
|
let data = data.into_inner();
|
|
|
|
store_preferences.remove(&data.key);
|
|
|
|
Ok(())
|
|
|
|
},
|
|
|
|
}
|
2023-05-17 16:33:44 +08:00
|
|
|
}
|