Remove more unwrap
This commit is contained in:
1 parent
b5b1eb768c
commit
d836b57d2a
5 files changed
+74
-62
No files matched your search
+30
-24
@@ -1,11 +1,12 @@
|
||||
use crate::app::{fill_state, AppState};
|
||||
use crate::data::Id;
|
||||
use crate::data_provider::history::get_history_of_set;
|
||||
use crate::data_provider::sqlite::{create_db, default_connection};
|
||||
use crate::data_provider::web_api::{
|
||||
get_local_version, get_web_version, load_data, set_local_version,
|
||||
};
|
||||
use crate::fs::*;
|
||||
use crate::message_navigation;
|
||||
use crate::{message_navigation, push_error, push_error_task};
|
||||
use crate::navigation::Page::*;
|
||||
use crate::navigation::RootMessage::{
|
||||
DataLoaded, Keyboard, Notification, PushNotification, UpdateData,
|
||||
@@ -40,7 +41,7 @@ use parking_lot::Mutex;
|
||||
use rusqlite::Connection;
|
||||
use std::sync::Arc;
|
||||
use std::time::Instant;
|
||||
use crate::data::Id;
|
||||
use crate::notification::NotificationData::PermissionDenied;
|
||||
|
||||
impl Default for ScreenState {
|
||||
fn default() -> Self {
|
||||
@@ -110,12 +111,13 @@ impl ScreenState {
|
||||
let state = ScreenState::default();
|
||||
let reading_additional_task = Task::perform(Self::load_additional_data(), |data| data);
|
||||
|
||||
let final_task;
|
||||
let mut final_task = reading_additional_task;
|
||||
let state_guard = state.app_state.lock();
|
||||
if state_guard.sync_data.auto_web_fetch {
|
||||
let key = state_guard.sync_data.key.clone().unwrap();
|
||||
if state_guard.sync_data.auto_web_fetch
|
||||
&& let Some(key) = state_guard.sync_data.key.clone()
|
||||
{
|
||||
final_task = Task::batch([
|
||||
reading_additional_task,
|
||||
final_task,
|
||||
Task::perform(Self::load_web_backup(key), |result| match result {
|
||||
Ok(update) => {
|
||||
if update {
|
||||
@@ -127,8 +129,6 @@ impl ScreenState {
|
||||
Err(err) => PushNotification(NotificationRequest::Warning(err)),
|
||||
}),
|
||||
]);
|
||||
} else {
|
||||
final_task = reading_additional_task;
|
||||
}
|
||||
|
||||
drop(state_guard);
|
||||
@@ -138,21 +138,23 @@ impl ScreenState {
|
||||
|
||||
async fn load_additional_data() -> RootMessage {
|
||||
let directory = history_dir();
|
||||
|
||||
if let Ok(directory) = directory {
|
||||
let mut map = HashMap::new();
|
||||
for file in directory.read_dir().unwrap().flatten() {
|
||||
for file in directory
|
||||
.read_dir()
|
||||
.expect("Cant read history directory")
|
||||
.flatten()
|
||||
{
|
||||
let mut vec = vec![];
|
||||
let history_file_name = match file.file_name().into_string() {
|
||||
Err(_) => continue,
|
||||
Ok(v) => v,
|
||||
};
|
||||
|
||||
let id = history_file_name[4..history_file_name.len() - 12]
|
||||
.parse::<Id>()
|
||||
.unwrap();
|
||||
|
||||
let history = get_history_of_set(id);
|
||||
if let Ok(history) = history {
|
||||
if let Ok(id) = history_file_name[4..history_file_name.len() - 12].parse::<Id>()
|
||||
&& let Ok(history) = get_history_of_set(id)
|
||||
{
|
||||
let by_date = history.chunk_by(|x, x1| {
|
||||
x.timestamp.naive_local().date() == x1.timestamp.naive_local().date()
|
||||
});
|
||||
@@ -161,7 +163,7 @@ impl ScreenState {
|
||||
}
|
||||
map.insert(id, vec);
|
||||
} else {
|
||||
return PushNotification(NotificationRequest::Warning(history.err().unwrap()));
|
||||
return PushNotification(NotificationRequest::Warning(PermissionDenied));
|
||||
}
|
||||
}
|
||||
return DataLoaded(map);
|
||||
@@ -203,7 +205,9 @@ impl ScreenState {
|
||||
}
|
||||
let db_file = db_path();
|
||||
let temp_db_file = temp_db_path();
|
||||
std::fs::rename(temp_db_file, &db_file).unwrap();
|
||||
if std::fs::rename(temp_db_file, &db_file).is_err(){
|
||||
return push_error_task!(PermissionDenied)
|
||||
}
|
||||
|
||||
state.connection = default_connection();
|
||||
fill_state(&mut state);
|
||||
@@ -290,11 +294,13 @@ impl ScreenState {
|
||||
}
|
||||
|
||||
fn handle_keyboard(&mut self, message: Event) -> Task<RootMessage> {
|
||||
let page = self.stack.last_mut().unwrap();
|
||||
match page {
|
||||
Repetition(page) => page.press(&message),
|
||||
_ => Task::none(),
|
||||
if let Some(page) = self.stack.last_mut(){
|
||||
return match page {
|
||||
Repetition(page) => page.press(&message),
|
||||
_ => Task::none(),
|
||||
}
|
||||
}
|
||||
Task::none()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -306,7 +312,7 @@ pub struct AppSettings {
|
||||
#[macro_export]
|
||||
macro_rules! view_navigation {
|
||||
($stack:expr, $($e:ident), *) => {
|
||||
match &$stack.last().unwrap() {
|
||||
match &$stack.last().expect("Unexpected stack state") {
|
||||
$(
|
||||
$e(s) => s.view().map(RootMessage::$e),
|
||||
)*
|
||||
@@ -320,7 +326,7 @@ macro_rules! state_update {
|
||||
match $message {
|
||||
$(
|
||||
RootMessage::$e(msg) => {
|
||||
if let $e(s) = $stack.last_mut().unwrap() {
|
||||
if let $e(s) = $stack.last_mut().expect("Unexpected stack state") {
|
||||
message_navigation!(msg, $stack, s)
|
||||
}
|
||||
else {
|
||||
@@ -355,7 +361,7 @@ macro_rules! message_navigation {
|
||||
#[macro_export]
|
||||
macro_rules! state_navigate_handle {
|
||||
($stack:expr, $($e:ident), *) => {
|
||||
match $stack.last_mut().unwrap() {
|
||||
match $stack.last_mut().expect("Unexpected stack state") {
|
||||
$(
|
||||
Page::$e(page) => {
|
||||
page.navigated();
|
||||
|
||||
Reference in new issue
Block a user