From 2ccd9e3b79d48f0d877773b5d6f3c2f8244caff1 Mon Sep 17 00:00:00 2001
From: mukae1997 <444652692@qq.com>
Date: Sun, 14 May 2017 22:34:26 +0800
Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9ECategoryTableManager=EF=BC=8C?=
=?UTF-8?q?=E4=BF=AE=E6=94=B9categoryViewModel=E5=92=8CMainPag?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- CategoryTableManager:负责categoryTable在数据库中的管理
- categoryViewModel的修改:将文件读入改成数据库操作
---
ShowMeMyMoney/MainPage.xaml | 304 ++++++++++--------
ShowMeMyMoney/MainPage.xaml.cs | 272 ++++++++++++++--
.../Services/CategoryTableManager.cs | 142 ++++++++
ShowMeMyMoney/Services/DBManager.cs | 51 ++-
ShowMeMyMoney/ViewModel/ViewModel.cs | 40 ++-
ShowMeMyMoney/ViewModel/categoryViewModel.cs | 86 +++--
6 files changed, 687 insertions(+), 208 deletions(-)
create mode 100644 ShowMeMyMoney/Services/CategoryTableManager.cs
diff --git a/ShowMeMyMoney/MainPage.xaml b/ShowMeMyMoney/MainPage.xaml
index 139f541..3b4a449 100644
--- a/ShowMeMyMoney/MainPage.xaml
+++ b/ShowMeMyMoney/MainPage.xaml
@@ -7,6 +7,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
x:Class="ShowMeMyMoney.MainPage"
mc:Ignorable="d">
+
-
-
+
-
+
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
+ VerticalAlignment="Center" Width="Auto" >
+
+
+
+
+
+
+
+
+
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
-
-
-
+
+
-
-
-
-
-
-
-
+
+
+
+
+
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
-
-
-
+
+
-
-
-
-
-
-
+
+
+
+
+
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
-
-
-
-
+
+
-
-
-
+
-
@@ -206,10 +243,7 @@
-
-
-
-
+
\ No newline at end of file
diff --git a/ShowMeMyMoney/MainPage.xaml.cs b/ShowMeMyMoney/MainPage.xaml.cs
index 4f9b63e..1a0a6ef 100644
--- a/ShowMeMyMoney/MainPage.xaml.cs
+++ b/ShowMeMyMoney/MainPage.xaml.cs
@@ -1,4 +1,4 @@
-using Newtonsoft.Json;
+using Newtonsoft.Json;
using ShowMeMyMoney.Model;
using System;
using System.Collections;
@@ -20,6 +20,14 @@
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Shapes;
+using Windows.UI.Notifications;
+using NotificationsExtensions.Tiles;
+using NotificationsExtensions;
+using Windows.UI.Xaml.Media.Imaging;
+using Windows.ApplicationModel.DataTransfer;
+using Windows.Storage;
+using System.Text.RegularExpressions;
+
//“空白页”项模板在 http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 上有介绍
namespace ShowMeMyMoney
@@ -41,20 +49,46 @@ public MainPage()
remainedProportion = 100;
makeColorPicker();
initializeShareSlider();
- initializeShareBar();
-
-
+ initializeShareBar();
+ createTile();
+ DataTransferManager DataTrans = DataTransferManager.GetForCurrentView();
+ DataTrans.DataRequested += OnShareDataRequested;
+ }
+ async void OnShareDataRequested(DataTransferManager sender, DataRequestedEventArgs args)
+ {
+ var dp = args.Request.Data;
+ var deferral = args.Request.GetDeferral();
+ string uri = "ms-appx://" + ((BitmapImage)pic.Source).UriSource.LocalPath;
+ var img = await StorageFile.GetFileFromApplicationUriAsync(new Uri(uri));
+ dp.Properties.Title = "【紧急求助】";
+ dp.SetText("本月" + TotalBudgetProportion.Text + "," + totalExpense + "元," + "\nFrom ShowMeMyMoney");
+ dp.SetStorageItems(new List { img });
+ deferral.Complete();
}
+
private void updateMetadataViews()
+
{
TotalExpenseAmount.Text = totalExpense.ToString();
TotalIncomeAmount.Text = totalIncome.ToString();
PocketMoneyAmount.Text = totalPocketMoney.ToString();
- TotalBudgetProportion.Text = "已使用" + totalExpense/monthlyBudget + "%";
-
+ TotalBudgetProportion.Text = "已使用" + Math.Truncate(totalExpense / monthlyBudget * 100) + "%";
+ if (totalExpense / monthlyBudget < 20)
+ {
+ pic.Source = new BitmapImage(new Uri("ms-appx:///Assets/pic3.jpg", UriKind.RelativeOrAbsolute));
+ }
+ else if (totalExpense / monthlyBudget < 50)
+ {
+ pic.Source = new BitmapImage(new Uri("ms-appx:///Assets/pic2.jpg", UriKind.RelativeOrAbsolute));
+ }
+ else
+ {
+ pic.Source = new BitmapImage(new Uri("ms-appx:///Assets/pic1.jpg", UriKind.RelativeOrAbsolute));
+ }
+ createTile();
}
/*------ metadata -----*/
@@ -83,6 +117,8 @@ private void maintainMetadata()
writeMetadataToFile();
updateMetadataViews();
}
+
+
private async void readMetadataFromFile()
{
try
@@ -116,9 +152,9 @@ private async void readMetadataFromFile()
Dictionary p = JsonConvert.DeserializeObject>(text);
metadata = p;
- totalExpense = metadata["totalExpense"];
- totalIncome = metadata["totalIncome"] ;
- totalPocketMoney = metadata["totalPocketMoney"] ;
+ totalExpense = metadata["totalExpense"];
+ totalIncome = metadata["totalIncome"];
+ totalPocketMoney = metadata["totalPocketMoney"];
monthlyBudget = metadata["monthlyBudget"];
@@ -133,7 +169,7 @@ private async void readMetadataFromFile()
throw e;
}
}
-
+
private async void writeMetadataToFile()
{
try
@@ -189,7 +225,7 @@ protected override void OnNavigatedTo(NavigationEventArgs e)
if (e.Parameter == null)
{
-
+
}
if (e.Parameter != null)
{
@@ -208,7 +244,7 @@ protected override void OnNavigatedTo(NavigationEventArgs e)
accountViewModel.SelectedItem = null;
-
+
}
/* 希望只读一次 */
@@ -216,15 +252,22 @@ protected override void OnNavigatedTo(NavigationEventArgs e)
{
readMetadataFromFile();
}
+ DataTransferManager DataTrans = DataTransferManager.GetForCurrentView();
+ DataTrans.DataRequested += OnShareDataRequested;
+ }
+ protected override void OnNavigatedFrom(NavigationEventArgs e)
+ {
+ DataTransferManager DataTrans = DataTransferManager.GetForCurrentView();
+ DataTrans.DataRequested -= OnShareDataRequested;
}
-
private void ShowCategory_Click(object sender, ItemClickEventArgs e)
{
/* 将分类item发送到accountsListViewPage */
categoryItem citem = ((categoryItem)(e.ClickedItem));
- Frame.Navigate(typeof(AccountsListViewPage), citem);
+ categoryViewModel.SelectedCategory = citem;
+ Frame.Navigate(typeof(AccountsListViewPage), categoryViewModel);
}
@@ -234,28 +277,17 @@ private async void AddNewCategoryButton_Click(object sender, RoutedEventArgs e)
{
ContentDialog dialog = AddNewCategoryDialog;
shareSlider.Header = "# 所占预算比例,还剩" + remainedProportion + "%可用";
+ expenseButton.IsChecked = false;
+ incomeButton.IsChecked = false;
+ categoryName.Text = "";
+ shareSlider.Value = 0;
+ ColorPallete_Combo.SelectedIndex = -1;
+
await dialog.ShowAsync();
}
private async void AddNewCategoryDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
{
- /*
- if (remainedProportion < Convert.ToDouble(categoryShare.Text))
- {
- // /* 判断新添加的分类比例是否合法
- AddNewCategoryDialog.Hide();
- ContentDialog alert = new ContentDialog()
- {
- Title = "错误提示",
- Content = "新设置的比例太大啦!只剩" + remainedProportion + "%可用。",
- IsPrimaryButtonEnabled = true,
- PrimaryButtonText = "OK"
-
- };
- await alert.ShowAsync();
- return;
- }
- */
/* 确定收入还是支出 */
bool incomeOrExpense = incomeButton.IsChecked == true ? true : false;
@@ -298,7 +330,8 @@ private async void AddNewCategoryDialog_PrimaryButtonClick(ContentDialog sender,
/* 添加新分类 */
categoryViewModel.AddCategoryItem(newCategory);
categoryCount++;
- categoryViewModel.saveCategoryTable(incomeOrExpense);
+
+
AddNewCategoryDialog.Hide();
}
@@ -343,11 +376,54 @@ private void shareSlider_ValueChanged(object sender, RangeBaseValueChangedEventA
{
/* 好像不需要干嘛 */
}
+ private void viewStatisticsButton_Click(object sender, RoutedEventArgs e)
+ {
+ /* 跳转到统计页 */
+ Frame.Navigate(typeof(statistics));
+ }
private void initializeShareSlider()
{
shareSlider.Maximum = remainedProportion;
}
+ private void Button_Click_1(object sender, RoutedEventArgs e)
+ {
+ if (Regex.IsMatch(newAmount.Text, @"^(-?\d+)(\.\d+)?$"))
+ {
+ BudgetAmount.Text = newAmount.Text;
+ monthlyBudget = Double.Parse(BudgetAmount.Text);
+ writeMetadataToFile();
+ updateMetadataViews();
+ BudgetFlyout.Hide();
+ newAmount.Text = "";
+ }
+ }
+ private void Button_Click_2(object sender, RoutedEventArgs e)
+ {
+ if (Regex.IsMatch(PocketAmount.Text, @"^(-?\d+)(\.\d+)?$"))
+ {
+ PocketMoneyAmount.Text = PocketAmount.Text;
+ totalPocketMoney = Double.Parse(PocketMoneyAmount.Text);
+ writeMetadataToFile();
+ updateMetadataViews();
+ PocketMoneyFlyout.Hide();
+ PocketAmount.Text = "";
+ }
+ }
+ private void cancl_Button_Click_1(object sender, RoutedEventArgs e)
+ {
+ newAmount.Text = "";
+ BudgetFlyout.Hide();
+ }
+ private void cancl_Button_Click_2(object sender, RoutedEventArgs e)
+ {
+ PocketAmount.Text = "";
+ PocketMoneyFlyout.Hide();
+ }
+ private void Button_Click(object sender, RoutedEventArgs e)
+ {
+ DataTransferManager.ShowShareUI();
+ }
private void out_Checked(object sender, RoutedEventArgs e)
{
@@ -358,6 +434,136 @@ private void in_Checked(object sender, RoutedEventArgs e)
{
shareSlider.Visibility = Visibility.Collapsed;
}
+
+ // 创建磁贴
+ public void createTile()
+ {
+ string from = (monthlyBudget + totalIncome - totalExpense).ToString();
+ string subject = "本月余额";
+ string body = "点此来记账";
+ string picSource = ((BitmapImage)pic.Source).UriSource.LocalPath;
+ picSource = picSource.Substring(1);
+
+
+ // Construct the tile content
+ TileContent content = new TileContent()
+ {
+ Visual = new TileVisual()
+ {
+ TileMedium = new TileBinding()
+ {
+ Content = new TileBindingContentAdaptive()
+ {
+ PeekImage = new TilePeekImage()
+ {
+ Source = picSource
+ },
+ Children =
+ {
+ new AdaptiveText()
+ {
+ Text = subject,
+ HintStyle = AdaptiveTextStyle.SubtitleSubtle,
+ HintAlign = AdaptiveTextAlign.Center
+ },
+ new AdaptiveText()
+ {
+ Text = from,
+ HintStyle = AdaptiveTextStyle.Title,
+ HintAlign = AdaptiveTextAlign.Center
+ },
+
+
+ new AdaptiveText()
+ {
+ Text = body,
+ HintStyle = AdaptiveTextStyle.CaptionSubtle,
+ HintAlign = AdaptiveTextAlign.Center
+ }
+ }
+ }
+ },
+
+ TileWide = new TileBinding()
+ {
+ Content = new TileBindingContentAdaptive()
+ {
+ Children =
+ {
+ new AdaptiveText()
+ {
+ Text = subject,
+ HintStyle = AdaptiveTextStyle.SubtitleSubtle,
+ HintAlign = AdaptiveTextAlign.Center
+ },
+ new AdaptiveText()
+ {
+ Text = from,
+ HintStyle = AdaptiveTextStyle.Title,
+ HintAlign = AdaptiveTextAlign.Center
+ },
+
+ new AdaptiveText()
+ {
+ Text = body,
+ HintStyle = AdaptiveTextStyle.CaptionSubtle,
+ HintAlign = AdaptiveTextAlign.Center
+ }
+ }
+ }
+ },
+ TileLarge = new TileBinding()
+ {
+ Content = new TileBindingContentAdaptive()
+ {
+ TextStacking = TileTextStacking.Center,
+ Children =
+ {
+ new AdaptiveGroup()
+ {
+ Children =
+ {
+ new AdaptiveSubgroup() { HintWeight = 1 },
+ new AdaptiveSubgroup()
+ {
+ HintWeight = 2,
+ Children =
+ {
+ new AdaptiveImage()
+ {
+ Source = picSource,
+ HintCrop = AdaptiveImageCrop.Circle
+ }
+ }
+ },
+ new AdaptiveSubgroup() { HintWeight = 1 }
+ }
+ },
+ new AdaptiveText()
+ {
+ Text = from,
+ HintStyle = AdaptiveTextStyle.Title,
+ HintAlign = AdaptiveTextAlign.Center
+ },
+ new AdaptiveText()
+ {
+ Text = body,
+ HintStyle = AdaptiveTextStyle.SubtitleSubtle,
+ HintAlign = AdaptiveTextAlign.Center
+ }
+ }
+ }
+ }
+ }
+ };
+
+ //TileUpdateManager.CreateTileUpdaterForApplication().Clear();
+ var notification = new TileNotification(content.GetXml());
+ // And send the notification
+ TileUpdateManager.CreateTileUpdaterForApplication().Update(notification);
+
+ }
+
}
-}
+}
\ No newline at end of file
diff --git a/ShowMeMyMoney/Services/CategoryTableManager.cs b/ShowMeMyMoney/Services/CategoryTableManager.cs
new file mode 100644
index 0000000..d2b6b76
--- /dev/null
+++ b/ShowMeMyMoney/Services/CategoryTableManager.cs
@@ -0,0 +1,142 @@
+using ShowMeMyMoney.Model;
+using SQLitePCL;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ShowMeMyMoney.Services
+{
+ public class CategoryTableManager
+ {
+ public CategoryTableManager()
+ {
+ LoadDatabase();
+ }
+ private void LoadDatabase()
+ {
+ // Get a reference to the SQLite database
+ /*
+ Practically this TABLE is UNCOMPLETED
+ */
+ string sql = @"CREATE TABLE IF NOT EXISTS
+ categories (number VARCHAR(30) PRIMARY KEY NOT NULL,
+ name VARCHAR(100),
+ color VARCHAR(20),
+ share REAL,
+ amount REAL,
+ inOrOut BOOLEAN
+ );";
+ /* SQL not complete yet */
+ using (var statement = App.conn.Prepare(sql))
+ {
+ statement.Step();
+ }
+ }
+ // UPDATE
+ public void UpdateItemInDatabase(categoryItem item)
+ {
+ // See if the customer already exists
+ var exist = SearchDatabaseByNumber(item.number);
+
+ var db = App.conn;
+ if (exist == true)
+ {
+ using (var todostmt = db.Prepare("UPDATE categories SET name = ?, color = ?, share = ?, amount = ?, inOrOut = ? WHERE number =?"))
+ {
+ // NOTE when using anonymous parameters the first has an index of 1, not 0.
+
+ todostmt.Bind(1, item.name);
+ todostmt.Bind(2, item.color);
+ todostmt.Bind(3, item.share);
+ todostmt.Bind(4, item.amount);
+ todostmt.Bind(5, Convert.ToInt16(item.inOrOut));
+ todostmt.Bind(6, item.number);
+ todostmt.Step();
+ }
+ }
+
+ }
+ public void InsertIntoDatabase(categoryItem item)
+ {
+ var db = App.conn;
+ string SQLstmt = @"INSERT INTO categories (number, name, color, share, amount, inOrOut)" +
+ " VALUES(?,?,?,?,?,?)";
+
+ try
+ {
+ using (var todostmt = db.Prepare(SQLstmt))
+ {
+ int i = 1;
+ todostmt.Bind(i++, item.number);
+ todostmt.Bind(i++, item.name);
+ todostmt.Bind(i++, item.color);
+ todostmt.Bind(i++, item.share);
+ todostmt.Bind(i++, item.amount);
+ todostmt.Bind(i++, Convert.ToInt16(item.inOrOut));
+ var a = todostmt.Step();
+ var b = 1;
+ }
+ }
+ catch (Exception ex)
+ {
+ // TODO: Handle error
+ }
+ }
+
+ // QUERY BY ID
+ public bool SearchDatabaseByNumber(long idOfItem)
+ {
+ var dbconn = App.conn;
+ categoryItem item = null;
+ string SQLstmt = "SELECT * FROM categories WHERE number = ?";
+ using (var statement = dbconn.Prepare(SQLstmt))
+ {
+ statement.Bind(1, idOfItem);
+
+ if (SQLiteResult.DONE == statement.Step())
+ {
+ return true;
+ }
+ }
+ return false;
+
+ }
+ // 返回所有项
+ public List GetWholeTable()
+ {
+ // 返回的TodoItem是不完整的:没有filePath 和 BitmapImage 对象
+ var dbconn = App.conn;
+ List itemList = new List();
+ string SQLstmt = "SELECT * FROM categories";
+
+ using (var statement = dbconn.Prepare(SQLstmt))
+ {
+ /*if (!wildcardFlag)
+ statement.Bind(1, ti);*/
+
+ while (statement.Step() == SQLiteResult.ROW)
+ {
+ int i = 0;
+ var number = (long)statement[i++];
+ var name = (string)statement[i++];
+ var color = (string)statement[i++];
+ var share = (double)statement[i++];
+ var amount = (double)statement[i++];
+ var inOrOut = (bool)statement[i++];
+
+ categoryItem item = new categoryItem(number, name, share, color, inOrOut);
+ item.number = number;
+ item.amount = amount;
+
+ itemList.Add(item);
+
+ }
+ }
+ return itemList;
+
+
+ }
+ }
+}
diff --git a/ShowMeMyMoney/Services/DBManager.cs b/ShowMeMyMoney/Services/DBManager.cs
index a2ad3ed..36b3986 100644
--- a/ShowMeMyMoney/Services/DBManager.cs
+++ b/ShowMeMyMoney/Services/DBManager.cs
@@ -2,6 +2,7 @@
using SQLitePCL;
using System;
using System.Collections.Generic;
+using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@@ -51,10 +52,11 @@ public void InsertIntoDatabase(Model.accountItem item)
todostmt.Bind(2, item.amount);
todostmt.Bind(3, item.createDate.ToString());
todostmt.Bind(4, item.category);
- todostmt.Bind(5, item.isPocketMoney);
- todostmt.Bind(6, item.inOrOut);
+ todostmt.Bind(5, Convert.ToInt16(item.isPocketMoney));
+ todostmt.Bind(6, Convert.ToInt16(item.inOrOut));
todostmt.Bind(7, item.description);
var a = todostmt.Step();
+ var b = 1;
}
}
catch (Exception ex)
@@ -72,7 +74,7 @@ public void DeleteItemInDatabase(string idOfItem)
}
}
// QUERY BY categoryNumber
- public List SearchDatabaseById(int numberOfCategory)
+ public List SearchDatabaseById(long numberOfCategory)
{
var dbconn = App.conn;
List li = new List();
@@ -82,15 +84,19 @@ public List SearchDatabaseById(int numberOfCategory)
while (statement.Step() == SQLiteResult.ROW)
{
- accountItem i = new accountItem();
+ accountItem i = new accountItem((string)statement[0]);
+/*<<<<<<< HEAD
int k = 0;
+=======*/
+ int k = 1;
+//>>>>>> 714ed49f59e4e0edee427eaacafa0d48b29c3316
i.amount = (double)statement[k++];
i.createDate = DateTimeOffset.Parse((string)statement[k++]);
- i.category = (int)statement[k++];
- i.isPocketMoney = (bool)statement[k++];
- i.inOrOut = (bool)statement[k++];
+ i.category = (long)statement[k++];
+ i.isPocketMoney = ((long)statement[k++] == 0)?false:true;
+ i.inOrOut = ((long)statement[k++] == 0) ? false : true;
i.description = (string)statement[k++];
@@ -101,5 +107,36 @@ public List SearchDatabaseById(int numberOfCategory)
}
+
+ public ObservableCollection LoadAllItems()
+ {
+ var dbconn = App.conn;
+ ObservableCollection li = new ObservableCollection();
+ using (var statement = dbconn.Prepare("SELECT * FROM accounts"))
+ {
+
+ while (statement.Step() == SQLiteResult.ROW)
+ {
+ accountItem i = new accountItem((string)statement[0]);
+
+
+ /*<<<<<<< HEAD
+ int k = 0;
+ =======*/
+ int k = 1;
+ //>>>>>> 714ed49f59e4e0edee427eaacafa0d48b29c3316
+ i.amount = (double)statement[k++];
+ i.createDate = DateTimeOffset.Parse((string)statement[k++]);
+ i.category = (long)statement[k++];
+ i.isPocketMoney = ((long)statement[k++] == 0) ? false : true;
+ i.inOrOut = ((long)statement[k++] == 0) ? false : true;
+ i.description = (string)statement[k++];
+
+
+ li.Add(i);
+ }
+ }
+ return li;
+ }
}
}
diff --git a/ShowMeMyMoney/ViewModel/ViewModel.cs b/ShowMeMyMoney/ViewModel/ViewModel.cs
index 2bb50cc..f4ae7c0 100644
--- a/ShowMeMyMoney/ViewModel/ViewModel.cs
+++ b/ShowMeMyMoney/ViewModel/ViewModel.cs
@@ -15,25 +15,42 @@ public class ViewModel
private ObservableCollection allItems = new ObservableCollection();
public ObservableCollection AllItems { get { return this.allItems; } set { this.allItems = value; } }
+ private ObservableCollection displayItems = new ObservableCollection();
+ public ObservableCollection DisplayItems { get { return this.displayItems; } set { this.displayItems = value; } }
+
private accountItem selectedItem = default(accountItem);
public accountItem SelectedItem { get { return selectedItem; } set { this.selectedItem = value; } }
public DBManager dbManager;
+
public ViewModel()
{
dbManager = new DBManager();
- // AllCatagoryItem.Add(new categoryItem("play", 1, "red"));
- // public categoryItem(int i, string s, double _share, string c)
+
+ /* 从数据库读取全部items */
+ if (allItems.Count == 0)
+ {
+ allItems = dbManager.LoadAllItems();
+ }
}
- public async void getItemsFromDB(categoryItem ci)
+
+ /* 从allItems中选出特定类别的items */
+ public void queryDisplayItems(categoryItem ci)
{
- /* 初始载入时连接到数据库,加载数据 */
+ displayItems.Clear();
+ for (int i = 0; i < allItems.Count; i++)
+ {
+ if (allItems[i].category == ci.number)
+ {
+ displayItems.Add(allItems[i]);
+ }
+ }
}
/* public async void AddAccountItem(accountItem item) {
/* 添加item并插入到数据库 */
//}
- public async void AddAccountItem(int categoryNum, DateTimeOffset date, double amount,
+ public async void AddAccountItem(long categoryNum, DateTimeOffset date, double amount,
bool isPocketMoney, bool inOrOut, string description)
{
accountItem accountItem = new accountItem(categoryNum, date, amount, isPocketMoney, inOrOut, description);
@@ -43,9 +60,16 @@ public async void AddAccountItem(int categoryNum, DateTimeOffset date, double am
public async void RemoveAccountItem(string id)
{
-
- /*删除item并同步数据库*/
-
+ foreach (var item in displayItems)
+ {
+ if (item.id == id)
+ {
+ displayItems.Remove(item);
+ allItems.Remove(item);
+ dbManager.DeleteItemInDatabase(id);
+ break;
+ }
+ }
}
}
}
diff --git a/ShowMeMyMoney/ViewModel/categoryViewModel.cs b/ShowMeMyMoney/ViewModel/categoryViewModel.cs
index 9b69471..c943196 100644
--- a/ShowMeMyMoney/ViewModel/categoryViewModel.cs
+++ b/ShowMeMyMoney/ViewModel/categoryViewModel.cs
@@ -1,5 +1,6 @@
-using Newtonsoft.Json;
+using Newtonsoft.Json;
using ShowMeMyMoney.Model;
+using ShowMeMyMoney.Services;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
@@ -23,47 +24,77 @@ class categoryViewModel
public ObservableCollection AllIncomeCatagoryItems { get { return this.allIncomeCatagoryItems; } set { this.allIncomeCatagoryItems = value; } }
public ObservableCollection allIncomeCatagoryItems = new ObservableCollection();
- static bool EXPENSE = false, INCOME = true;
+ private categoryItem selectedCategory = default(categoryItem);
+ public categoryItem SelectedCategory { get { return selectedCategory; } set { this.selectedCategory = value; } }
+
+ public CategoryTableManager CTM;
+
+ static bool EXPENSE = false, INCOME = true;
+
public double pocketMoneyAmount;
public categoryViewModel()
{
- /* 读入本地json文件 */
+ CTM = new CategoryTableManager();
+
initializeCategoryTable();
/* todo : 从本地读入私房钱数额 */
pocketMoneyAmount = 800;
+
+
}
private void initializeCategoryTable()
{
+ /* 读入本地json文件 */
+ /*
readFromTable("ExpenseCategoryTable");
readFromTable("IncomeCategoryTable");
+ */
+
+ /* 从数据库读入 */
+ List li = CTM.GetWholeTable();
+ foreach(var item in li)
+ {
+ if (item.inOrOut == EXPENSE)
+ {
+ allExpenseCatagoryItems.Add(item);
+ } else
+ {
+ allIncomeCatagoryItems.Add(item);
+ }
+ }
+
}
- public void AddCategoryItem(int index, string name, double _share, string color, bool b)
+ public void AddCategoryItem(long index, string name, double _share, string color, bool b)
{
- categoryItem categoryItem = new categoryItem(index, name, _share, color, b);
+ categoryItem categoryItem = new categoryItem(index, name, _share, color, b);
AddCategoryItem(categoryItem);
+ ///////////////////// DATABASE
}
public void AddCategoryItem(categoryItem newCategory)
{
if (newCategory.inOrOut == EXPENSE)
{
allExpenseCatagoryItems.Add(newCategory);
- } else
+ }
+ else
{
allIncomeCatagoryItems.Add(newCategory);
- }
+ }
+
+
+ CTM.InsertIntoDatabase(newCategory);
+ // saveCategoryTable(newCategory.inOrOut);
}
-
- public int getCategoryNum(string categoryName, bool expOrInc)
+ public long getCategoryNum(string categoryName, bool expOrInc)
{
var items = expOrInc ? allIncomeCatagoryItems : allExpenseCatagoryItems;
foreach (var item in items)
{
if (item.name.Equals(categoryName))
- /* 要用.Equals判断才是判断内容 */
{
return item.number;
}
@@ -72,8 +103,8 @@ public int getCategoryNum(string categoryName, bool expOrInc)
}
private async void readFromTable(string nameOfTable)
{
- try
- {
+ // try
+ //{
/* 读取json文件 */
var Folder = Windows.Storage.ApplicationData.Current.LocalFolder;
var item = await Folder.TryGetItemAsync(nameOfTable + ".json");
@@ -81,7 +112,6 @@ private async void readFromTable(string nameOfTable)
{
/* 第一次打开应用, 则无需进行后续操作 */
/* 手动添加一个代表总开支的item */
- // allCatagoryItems.Add(new categoryItem(-1, "Total", 100, "Black"));
return;
}
var file = await Folder.GetFileAsync(nameOfTable + ".json");
@@ -106,29 +136,36 @@ private async void readFromTable(string nameOfTable)
foreach (var i in p)
allIncomeCatagoryItems.Add(i);
}
-
}
- /* TODO: 去掉Total */
-
- }
- catch (Exception e)
+ // }
+ /* catch (Exception e)
{
throw e;
- }
+ }*/
}
- internal void UpdateCategoryByAccount(accountItem account)
+ internal void UpdateCategoryByAccount(accountItem account, bool addOrDel)
{
bool expenseOrIncome = account.inOrOut;
- var items = expenseOrIncome ? allIncomeCatagoryItems : allExpenseCatagoryItems;
+ var items = expenseOrIncome ? allIncomeCatagoryItems : allExpenseCatagoryItems;
/* 如果是私房钱,不增加开支,只增加收入 */
if (account.isPocketMoney == true && expenseOrIncome == EXPENSE) return;
foreach (var i in items)
{
if (account.category == i.number)
{
- i.amount += account.amount;
- saveCategoryTable(expenseOrIncome);
+ if (addOrDel)
+ {
+ i.amount += account.amount;
+ }
+ else
+ {
+ i.amount -= account.amount;
+ }
+ // local file IO
+ /* saveCategoryTable(expenseOrIncome);*/
+ // DB
+ CTM.UpdateItemInDatabase(i);
return;
}
}
@@ -139,7 +176,7 @@ internal void UpdateCategoryByAccount(accountItem account)
static string INCOME_TABLE = "IncomeCategoryTable";
public async void saveCategoryTable(Boolean incomeOrExpense)
{
- string nameOfTable = incomeOrExpense? INCOME_TABLE: EXPENSE_TABLE;
+ string nameOfTable = incomeOrExpense ? INCOME_TABLE : EXPENSE_TABLE;
ObservableCollection table = incomeOrExpense ? allIncomeCatagoryItems : allExpenseCatagoryItems;
try
{
@@ -149,7 +186,6 @@ public async void saveCategoryTable(Boolean incomeOrExpense)
using (StreamWriter r = new StreamWriter(data))
{
-
var serelizedfile = JsonConvert.SerializeObject(table);
r.Write(serelizedfile);
}