mirror of
https://github.com/mgroves/MonodroidStockPortfolio.git
synced 2024-12-24 12:03:02 +00:00
implemented 'edit' long click action and validation
This commit is contained in:
parent
f98d073569
commit
1e7dde035b
5 changed files with 122 additions and 61 deletions
|
@ -63,19 +63,14 @@ namespace MonoStockPortfolio.Core.PortfolioRepositories
|
|||
_db.Insert(POSITION_TABLE_NAME, null, GetPositionContentValues(position));
|
||||
}
|
||||
|
||||
|
||||
public void DeletePortfolio(string portfolioName)
|
||||
{
|
||||
_db.BeginTransaction();
|
||||
try
|
||||
{
|
||||
var portfolio = GetPortfolioByName(portfolioName);
|
||||
Log.E("DeletePortfolio", "Name: " + portfolio.Name);
|
||||
Log.E("DeletePortfolio", "Name: " + portfolio.ID);
|
||||
_db.RawQuery("DELETE FROM " + PORTFOLIO_TABLE_NAME + " WHERE id = " + portfolio.ID, null);
|
||||
_db.RawQuery("DELETE FROM " + POSITION_TABLE_NAME + " WHERE ContainingPortfolioID = " + portfolio.ID, null);
|
||||
//_db.Delete(PORTFOLIO_TABLE_NAME, " WHERE id = " + portfolio.ID, null);
|
||||
//_db.Delete(POSITION_TABLE_NAME, " WHERE ContainingPortfolioID = " + portfolio.ID, null);
|
||||
_db.Delete(PORTFOLIO_TABLE_NAME, "id = " + portfolio.ID, null);
|
||||
_db.Delete(POSITION_TABLE_NAME, "ContainingPortfolioID = " + portfolio.ID, null);
|
||||
_db.SetTransactionSuccessful();
|
||||
}
|
||||
catch (SQLiteException ex)
|
||||
|
@ -137,12 +132,12 @@ namespace MonoStockPortfolio.Core.PortfolioRepositories
|
|||
private void UpdateExistingPortfolio(Portfolio portfolio)
|
||||
{
|
||||
var portfolioID = portfolio.ID ?? -1;
|
||||
_db.Update(PORTFOLIO_TABLE_NAME, GetPortfolioContentValues(portfolio), " WHERE id = " + portfolioID, null);
|
||||
Log.E("UpdateExistingPortfolio", "Portfolios updated: " + _db.Update(PORTFOLIO_TABLE_NAME, GetPortfolioContentValues(portfolio), "id = " + portfolioID, null));
|
||||
}
|
||||
|
||||
private void InsertNewPortfolio(Portfolio portfolio)
|
||||
{
|
||||
_db.Insert(PORTFOLIO_TABLE_NAME, null, GetPortfolioContentValues(portfolio));
|
||||
Log.E("InsertNewPortfolio", "Portfolios inserted: " + _db.Insert(PORTFOLIO_TABLE_NAME, null, GetPortfolioContentValues(portfolio)));
|
||||
}
|
||||
|
||||
private static ContentValues GetPortfolioContentValues(Portfolio portfolio)
|
||||
|
|
|
@ -1,47 +0,0 @@
|
|||
using System;
|
||||
using Android.App;
|
||||
using Android.Content;
|
||||
using Android.OS;
|
||||
using Android.Widget;
|
||||
using MonoStockPortfolio.Core.PortfolioRepositories;
|
||||
using MonoStockPortfolio.Entities;
|
||||
|
||||
namespace MonoStockPortfolio
|
||||
{
|
||||
[Activity(Label = "Add Portfolio", MainLauncher = false)]
|
||||
public class AddPortfolioActivity : Activity
|
||||
{
|
||||
public static string ClassName { get { return "monostockportfolio.AddPortfolioActivity"; } }
|
||||
private IPortfolioRepository _repo;
|
||||
|
||||
protected override void OnCreate(Bundle bundle)
|
||||
{
|
||||
base.OnCreate(bundle);
|
||||
|
||||
_repo = new AndroidSqlitePortfolioRepository(this);
|
||||
|
||||
SetContentView(Resource.layout.addportfolio);
|
||||
|
||||
WireUpEvents();
|
||||
}
|
||||
|
||||
private void WireUpEvents()
|
||||
{
|
||||
var saveButton = FindViewById<Button>(Resource.id.btnSave);
|
||||
saveButton.Click += saveButton_Click;
|
||||
}
|
||||
|
||||
private void saveButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
var portfolioName = FindViewById<EditText>(Resource.id.portfolioName);
|
||||
|
||||
_repo.SavePortfolio(new Portfolio {Name = portfolioName.Text.ToString()});
|
||||
|
||||
Toast.MakeText(this, "You saved: " + portfolioName.Text, ToastLength.Short).Show();
|
||||
|
||||
var intent = new Intent();
|
||||
SetResult(Result.Ok, intent);
|
||||
Finish();
|
||||
}
|
||||
}
|
||||
}
|
108
MonoStockPortfolio/EditPortfolioActivity.cs
Normal file
108
MonoStockPortfolio/EditPortfolioActivity.cs
Normal file
|
@ -0,0 +1,108 @@
|
|||
using System;
|
||||
using Android.App;
|
||||
using Android.Content;
|
||||
using Android.OS;
|
||||
using Android.Widget;
|
||||
using MonoStockPortfolio.Core.PortfolioRepositories;
|
||||
using MonoStockPortfolio.Entities;
|
||||
using MonoStockPortfolio.Validation;
|
||||
|
||||
namespace MonoStockPortfolio
|
||||
{
|
||||
[Activity(Label = "Add Portfolio", MainLauncher = false)]
|
||||
public class EditPortfolioActivity : Activity
|
||||
{
|
||||
public static string ClassName { get { return "monostockportfolio.EditPortfolioActivity"; } }
|
||||
public static string Extra_PortfolioID { get { return "monoStockPortfolio.EditPortfolioActivity.PortfolioID"; } }
|
||||
|
||||
private Button SaveButton { get { return FindViewById<Button>(Resource.id.btnSave); } }
|
||||
private EditText PortfolioName { get { return FindViewById<EditText>(Resource.id.portfolioName); } }
|
||||
|
||||
private IPortfolioRepository _repo;
|
||||
|
||||
protected override void OnCreate(Bundle bundle)
|
||||
{
|
||||
base.OnCreate(bundle);
|
||||
|
||||
_repo = new AndroidSqlitePortfolioRepository(this);
|
||||
|
||||
SetContentView(Resource.layout.addportfolio);
|
||||
|
||||
WireUpEvents();
|
||||
|
||||
var portfolioId = Intent.GetLongExtra(Extra_PortfolioID, -1);
|
||||
if(portfolioId != -1)
|
||||
{
|
||||
this.Title = "Edit Portfolio";
|
||||
PopulateForm(portfolioId);
|
||||
}
|
||||
}
|
||||
|
||||
private void PopulateForm(long portfolioId)
|
||||
{
|
||||
var portfolio = _repo.GetPortfolioById(portfolioId);
|
||||
PortfolioName.Text = portfolio.Name;
|
||||
}
|
||||
|
||||
private void WireUpEvents()
|
||||
{
|
||||
SaveButton.Click += saveButton_Click;
|
||||
}
|
||||
|
||||
private void saveButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
Portfolio portfolioToSave = GetPortfolioToSave();
|
||||
|
||||
if (Validate(portfolioToSave))
|
||||
{
|
||||
_repo.SavePortfolio(portfolioToSave);
|
||||
|
||||
Toast.MakeText(this, "You saved: " + PortfolioName.Text, ToastLength.Short).Show();
|
||||
|
||||
var intent = new Intent();
|
||||
SetResult(Result.Ok, intent);
|
||||
Finish();
|
||||
}
|
||||
}
|
||||
|
||||
private bool Validate(Portfolio portfolioToSave)
|
||||
{
|
||||
var validator = new FormValidator();
|
||||
validator.AddValidation(PortfolioName, () => IsDuplicateName(portfolioToSave));
|
||||
|
||||
var result = validator.Apply();
|
||||
if(result != string.Empty)
|
||||
{
|
||||
Toast.MakeText(this, result, ToastLength.Long).Show();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private string IsDuplicateName(Portfolio portfolioToSave)
|
||||
{
|
||||
var portfolio = _repo.GetPortfolioByName(portfolioToSave.Name);
|
||||
if(portfolio != null && portfolio.ID != portfolioToSave.ID)
|
||||
{
|
||||
return "Portfolio name is already taken";
|
||||
}
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
private Portfolio GetPortfolioToSave()
|
||||
{
|
||||
Portfolio portfolioToSave;
|
||||
var portfolioId = Intent.GetLongExtra(Extra_PortfolioID, -1);
|
||||
if (portfolioId != -1)
|
||||
{
|
||||
portfolioToSave = new Portfolio(portfolioId);
|
||||
}
|
||||
else
|
||||
{
|
||||
portfolioToSave = new Portfolio();
|
||||
}
|
||||
portfolioToSave.Name = PortfolioName.Text.ToString();
|
||||
return portfolioToSave;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -56,7 +56,8 @@ namespace MonoStockPortfolio
|
|||
void PortfolioListView_ItemLongClick(object sender, ItemEventArgs e)
|
||||
{
|
||||
_longClickOptions = new[] {"Edit", "Delete"};
|
||||
var selectedPortfolio = ((TextView) e.View).Text.ToS();
|
||||
var selectedPortfolioName = ((TextView) e.View).Text.ToS();
|
||||
var selectedPortfolio = _repo.GetPortfolioByName(selectedPortfolioName);
|
||||
var dialogBuilder = new AlertDialog.Builder(this);
|
||||
dialogBuilder.SetTitle("Options");
|
||||
dialogBuilder.SetItems(_longClickOptions,
|
||||
|
@ -64,17 +65,21 @@ namespace MonoStockPortfolio
|
|||
dialogBuilder.Create().Show();
|
||||
}
|
||||
|
||||
private void tr_LongClick_Options(object sender, DialogClickEventArgs e, string selectedPortfolio)
|
||||
private void tr_LongClick_Options(object sender, DialogClickEventArgs e, Portfolio selectedPortfolio)
|
||||
{
|
||||
//Toast.MakeText(this, "Option: " + _longClickOptions[e.Which], ToastLength.Long).Show();
|
||||
if(_longClickOptions[e.Which] == "Edit")
|
||||
{
|
||||
// Edit
|
||||
var intent = new Intent();
|
||||
intent.SetClassName(this, EditPortfolioActivity.ClassName);
|
||||
intent.PutExtra(EditPortfolioActivity.Extra_PortfolioID, selectedPortfolio.ID ?? -1);
|
||||
StartActivityForResult(intent, 0);
|
||||
}
|
||||
else if (_longClickOptions[e.Which] == "Delete")
|
||||
{
|
||||
// Delete
|
||||
_repo.DeletePortfolio(selectedPortfolio);
|
||||
_repo.DeletePortfolio(selectedPortfolio.Name);
|
||||
RefreshList();
|
||||
}
|
||||
}
|
||||
|
@ -90,7 +95,7 @@ namespace MonoStockPortfolio
|
|||
private void addPortfolioButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
var intent = new Intent();
|
||||
intent.SetClassName(this, AddPortfolioActivity.ClassName);
|
||||
intent.SetClassName(this, EditPortfolioActivity.ClassName);
|
||||
StartActivityForResult(intent, 0);
|
||||
}
|
||||
|
||||
|
|
|
@ -44,7 +44,7 @@
|
|||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="AddPortfolioActivity.cs" />
|
||||
<Compile Include="EditPortfolioActivity.cs" />
|
||||
<Compile Include="PortfolioActivity.cs" />
|
||||
<Compile Include="MainActivity.cs" />
|
||||
<Compile Include="AddPositionActivity.cs" />
|
||||
|
|
Loading…
Reference in a new issue