mirror of
https://github.com/YGGverse/Yoda.git
synced 2025-01-15 17:20:08 +00:00
implement new profile init
This commit is contained in:
parent
f558940574
commit
091b7da7b8
@ -177,7 +177,7 @@ impl Browser {
|
|||||||
|
|
||||||
// Show welcome dialog on profile not selected yet (e.g. first launch)
|
// Show welcome dialog on profile not selected yet (e.g. first launch)
|
||||||
if self.profile.database.selected().is_none() {
|
if self.profile.database.selected().is_none() {
|
||||||
// @TODO Welcome::new(self.profile.clone(), self.widget.gobject().clone()).present();
|
Welcome::new(self.profile.clone(), self.widget.gobject().clone()).present();
|
||||||
}
|
}
|
||||||
|
|
||||||
self
|
self
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
mod widget;
|
mod widget;
|
||||||
|
use gtk::glib::DateTime;
|
||||||
use widget::Widget;
|
use widget::Widget;
|
||||||
|
|
||||||
use crate::profile::Profile;
|
use crate::profile::Profile;
|
||||||
@ -19,13 +20,19 @@ impl Welcome {
|
|||||||
let widget = Rc::new(Widget::new(parent));
|
let widget = Rc::new(Widget::new(parent));
|
||||||
|
|
||||||
// Init events
|
// Init events
|
||||||
widget.connect_response(|value| {
|
widget.connect_response({
|
||||||
|
let profile = profile.clone();
|
||||||
|
move |value| {
|
||||||
match value {
|
match value {
|
||||||
Some(id) => {
|
Some(id) => {
|
||||||
// Select profile by record ID @TODO
|
// Select profile by record ID @TODO
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
// Create new profile @TODO
|
// Create and select new profile
|
||||||
|
let _ = profile
|
||||||
|
.database
|
||||||
|
.add(true, &DateTime::now_local().unwrap(), None);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -28,7 +28,7 @@ impl Database {
|
|||||||
pub fn records(&self) -> Vec<Table> {
|
pub fn records(&self) -> Vec<Table> {
|
||||||
let readable = self.connection.read().unwrap();
|
let readable = self.connection.read().unwrap();
|
||||||
let tx = readable.unchecked_transaction().unwrap();
|
let tx = readable.unchecked_transaction().unwrap();
|
||||||
records(&tx).unwrap()
|
select(&tx).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get selected profile record if exist
|
/// Get selected profile record if exist
|
||||||
@ -44,13 +44,31 @@ impl Database {
|
|||||||
// Setters
|
// Setters
|
||||||
|
|
||||||
pub fn add(&self, is_active: bool, time: &DateTime, name: Option<&str>) -> Result<i64, ()> {
|
pub fn add(&self, is_active: bool, time: &DateTime, name: Option<&str>) -> Result<i64, ()> {
|
||||||
|
// Begin new transaction
|
||||||
let mut writable = self.connection.write().unwrap();
|
let mut writable = self.connection.write().unwrap();
|
||||||
let tx = writable.transaction().unwrap();
|
let tx = writable.transaction().unwrap();
|
||||||
|
|
||||||
add(&tx, is_active, time, name).unwrap();
|
// New record has active status
|
||||||
|
if is_active {
|
||||||
|
// Deactivate other records as only one profile should be active
|
||||||
|
for record in select(&tx).unwrap() {
|
||||||
|
let _ = update(
|
||||||
|
&tx,
|
||||||
|
record.id,
|
||||||
|
false,
|
||||||
|
&record.time,
|
||||||
|
record.name.as_ref().map(|x| x.as_str()),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create new record
|
||||||
|
insert(&tx, is_active, time, name).unwrap();
|
||||||
|
|
||||||
|
// Hold insert ID for result
|
||||||
let id = last_insert_id(&tx);
|
let id = last_insert_id(&tx);
|
||||||
|
|
||||||
|
// Done
|
||||||
match tx.commit() {
|
match tx.commit() {
|
||||||
Ok(_) => Ok(id),
|
Ok(_) => Ok(id),
|
||||||
Err(_) => Err(()), // @TODO
|
Err(_) => Err(()), // @TODO
|
||||||
@ -73,16 +91,36 @@ pub fn init(tx: &Transaction) -> Result<usize, Error> {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add(
|
pub fn insert(
|
||||||
tx: &Transaction,
|
tx: &Transaction,
|
||||||
is_active: bool,
|
is_active: bool,
|
||||||
time: &DateTime,
|
time: &DateTime,
|
||||||
name: Option<&str>,
|
name: Option<&str>,
|
||||||
) -> Result<usize, Error> {
|
) -> Result<usize, Error> {
|
||||||
tx.execute("INSERT INTO `profile`", (is_active, time.to_unix(), name))
|
tx.execute(
|
||||||
|
"INSERT INTO `profile` (
|
||||||
|
`is_active`,
|
||||||
|
`time`,
|
||||||
|
`name`
|
||||||
|
) VALUES (?, ?, ?)",
|
||||||
|
(is_active, time.to_unix(), name),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn records(tx: &Transaction) -> Result<Vec<Table>, Error> {
|
pub fn update(
|
||||||
|
tx: &Transaction,
|
||||||
|
id: i64,
|
||||||
|
is_active: bool,
|
||||||
|
time: &DateTime,
|
||||||
|
name: Option<&str>,
|
||||||
|
) -> Result<usize, Error> {
|
||||||
|
tx.execute(
|
||||||
|
"UPDATE `profile` SET `is_active` = ?, `time` = ?, `name` = ? WHERE `id` = ? LIMIT 1",
|
||||||
|
(is_active, time.to_unix(), name, id),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn select(tx: &Transaction) -> Result<Vec<Table>, Error> {
|
||||||
let mut stmt = tx.prepare("SELECT `id`, `is_active`, `time`, `name` FROM `profile`")?;
|
let mut stmt = tx.prepare("SELECT `id`, `is_active`, `time`, `name` FROM `profile`")?;
|
||||||
let result = stmt.query_map([], |row| {
|
let result = stmt.query_map([], |row| {
|
||||||
Ok(Table {
|
Ok(Table {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user