Commit 22e5b72e by Javier Piris

Merge branch 'dev_javi' of http://git.setisl.com/inutraliaapp/2018-app-inutralia into dev_javi

parents 8b0af342 fc4efc82
namespace inutralia.Models.Questions namespace inutralia.Models.Questions
{ {
using MvvmHelpers;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Converters; using Newtonsoft.Json.Converters;
using System; using System;
...@@ -10,6 +9,14 @@ ...@@ -10,6 +9,14 @@
public class Message : ObservableEntityData public class Message : ObservableEntityData
{ {
public enum Estatus
{
Pendiente,
Respondida_por_cliente,
Respondida_por_nutricionista,
Cerrado
};
public string StatusImg => imgStatus[StateId]; public string StatusImg => imgStatus[StateId];
[JsonProperty("text", Required = Required.Always)] [JsonProperty("text", Required = Required.Always)]
...@@ -46,7 +53,8 @@ ...@@ -46,7 +53,8 @@
public int StateId { get; set; } public int StateId { get; set; }
[JsonProperty("status", Required = Required.Always)] [JsonProperty("status", Required = Required.Always)]
public string State { get; set; } [JsonConverter(typeof(StringEnumConverter))]
public Estatus State { get; set; }
[JsonProperty("subject", Required = Required.Always)] [JsonProperty("subject", Required = Required.Always)]
public string Subject public string Subject
...@@ -57,11 +65,11 @@ ...@@ -57,11 +65,11 @@
string _subject; string _subject;
string[] imgStatus = new string[2] string[] imgStatus = new string[4]
{ {
"pendiente.png", // 1 - Pendiente de responder "pendiente.png", // 1 - Pendiente de responder
//"respondida_por_cliente.png", // 2 - Respondida por cliente "respondida_por_cliente.png", // 2 - Respondida por cliente
//"respondida_por_nutricionista", // 3- Respondida por nutricionista "respondida_por_nutricionista", // 3- Respondida por nutricionista
"cerrada" // 4 - Conversación cerrada "cerrada" // 4 - Conversación cerrada
}; };
} }
......
...@@ -3,14 +3,40 @@ ...@@ -3,14 +3,40 @@
using inutralia.Models.Questions; using inutralia.Models.Questions;
using MvvmHelpers; using MvvmHelpers;
using System; using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using System.Windows.Input; using System.Windows.Input;
using Xamarin.Forms; using Xamarin.Forms;
public class NewQuestionViewModel : BaseViewModel public class NewQuestionViewModel : BaseNavigationViewModel
{ {
public ObservableRangeCollection<Message> ListMessages { get; } public enum Estatus
{
Pendiente = 1,
Respondida_por_cliente = 2,
Respondida_por_nutricionista = 3,
Cerrado = 4
}
public Estatus State => (Estatus)Message?.StateId;
public Message Message { private set; get; }
public ICommand SendCommand { get; set; } public ICommand SendCommand { get; set; }
public ObservableRangeCollection<Message> _ListMessages;
// LOADING LIST
Command _LoadNewQuestionCommand;
// UODATE LIST
Command _RefreshNewQuestionCommand;
// DELETE LIST
Command _DeleteNewQuestionCommand;
public NewQuestionViewModel() public NewQuestionViewModel()
{ {
IsBusy = true; IsBusy = true;
...@@ -28,11 +54,9 @@ ...@@ -28,11 +54,9 @@
MessageDateTime = DateTime.Now MessageDateTime = DateTime.Now
}; };
ListMessages.Add(message); ListMessages.Add(message);
OutText = ""; OutText = "";
} }
}); });
IsBusy = false; IsBusy = false;
...@@ -44,5 +68,140 @@ ...@@ -44,5 +68,140 @@
set { SetProperty(ref _outText, value); } set { SetProperty(ref _outText, value); }
} }
string _outText = string.Empty; string _outText = string.Empty;
// LIST MESSAGES
public ObservableRangeCollection<Message> ListMessages
{
get { return _ListMessages ?? (_ListMessages = new ObservableRangeCollection<Message>()); }
set
{
_ListMessages = value;
OnPropertyChanged("ListMessages");
}
}
// LOAD
public Command LoadNewQuestionCommand
{
get { return _LoadNewQuestionCommand ?? (_LoadNewQuestionCommand = new Command(async () => await ExecuteLoadNewQuestionCommand())); }
}
public async Task ExecuteLoadNewQuestionCommand()
{
LoadNewQuestionCommand.ChangeCanExecute();
//if (Authorizations.Count < 1)
await FetchNewQuestion();
LoadNewQuestionCommand.ChangeCanExecute();
}
// REFRESH
public Command RefreshNewQuestionCommand
{
get { return _RefreshNewQuestionCommand ?? (_RefreshNewQuestionCommand = new Command(async () => await ExecuteRefreshNewQuestionCommand())); }
}
public async Task ExecuteRefreshNewQuestionCommand()
{
try
{
RefreshNewQuestionCommand.ChangeCanExecute();
await FetchNewQuestion();
RefreshNewQuestionCommand.ChangeCanExecute();
}
catch (Exception e)
{
string err = e.Message;
}
}
// DELETE
public Command DeleteNewQuestionCommand
{
get
{
return _DeleteNewQuestionCommand ?? (_DeleteNewQuestionCommand = new Command(async (parameter) => await ExecuteDeleteNewQuestionCommand(parameter as Message)));
}
}
async Task ExecuteDeleteNewQuestionCommand(Message message)
{
if (message != null)
{
IsBusy = true;
await App.API.DeleteItemAsync(message);
await FetchNewQuestion();
IsBusy = false;
}
}
// FETCH
async Task FetchNewQuestion()
{
IsBusy = true;
try
{
ListMessages = new ObservableRangeCollection<Message>(await App.API.RefreshListAsync<Message>());
}
catch (Exception e)
{
string err = e.Message;
}
}
// SAVE MESSAGE
public async Task SaveMessage(string text)
{
var q = Message;
HttpMethod method = HttpMethod.Post;
IsBusy = true;
if (q.Id < 0)
{
var data = new Dictionary<string, string>()
{
{"subject", q.Subject },{"message", text}
};
await App.API.RawMessage(method, string.Format("messages"), data, q);
}
IsBusy = false;
ListMessages = new ObservableRangeCollection<Message>();
}
// UPDATE MESSAGE
public async Task UpdateMessage()
{
var q = Message;
HttpMethod method = HttpMethod.Post;
IsBusy = true;
await App.API.UpdateItemAsync(q);
if (await App.API.RefreshItemAsync(Message))
{
Title = Message.StatusImg;
OnPropertyChanged("State");
}
IsBusy = false;
}
public bool IsClosedOrPending => (State == Estatus.Pendiente ||
State == Estatus.Cerrado ||
State == Estatus.Respondida_por_cliente ||
State == Estatus.Respondida_por_nutricionista);
public bool MayBeClosed => !IsClosedOrPending;
} }
} }
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
{ {
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using inutralia.Models;
using inutralia.Models.Questions; using inutralia.Models.Questions;
public class PendingReadViewModel : BaseNavigationViewModel public class PendingReadViewModel : BaseNavigationViewModel
...@@ -18,7 +17,7 @@ ...@@ -18,7 +17,7 @@
public PendingReadViewModel(Message message) public PendingReadViewModel(Message message)
{ {
Message = message; Message = message;
Title = message?.State; Title = message?.StatusImg;
} }
public PendingReadViewModel() { } public PendingReadViewModel() { }
...@@ -33,7 +32,8 @@ ...@@ -33,7 +32,8 @@
public DateTime MessageDateTime => (DateTime)Message?.MessageDateTime; public DateTime MessageDateTime => (DateTime)Message?.MessageDateTime;
public async Task RefresData() // REFRESH
public async Task RefreshData()
{ {
if (Message == null) if (Message == null)
return; return;
...@@ -42,7 +42,7 @@ ...@@ -42,7 +42,7 @@
if (await App.API.RefreshItemAsync(Message)) if (await App.API.RefreshItemAsync(Message))
{ {
Title = Message.State; Title = Message.StatusImg;
OnPropertyChanged("State"); OnPropertyChanged("State");
} }
......
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
protected async void ItemTapped(object sender, ItemTappedEventArgs e) protected async void ItemTapped(object sender, ItemTappedEventArgs e)
{ {
var respon = e.Item as Message; var respon = e.Item as Message;
if (respon.State == "Respondida") if (respon.StatusImg == "Respondida")
{ {
await DisplayAlert("ERROR", "La pregunta ya está respondida", "Entendido"); await DisplayAlert("ERROR", "La pregunta ya está respondida", "Entendido");
} }
......
...@@ -35,14 +35,13 @@ ...@@ -35,14 +35,13 @@
Footer=""/> Footer=""/>
</ScrollView> </ScrollView>
<StackLayout Orientation="Horizontal" Grid.Row="1" BackgroundColor="White" Padding="0,0,0,10"> <StackLayout Orientation="Horizontal" Grid.Row="1" BackgroundColor="White" Padding="10,0,10,10" HeightRequest="40">
<Entry HorizontalOptions="FillAndExpand" <Entry HorizontalOptions="FillAndExpand"
Placeholder=" Escriba aquí su mensaje" Placeholder=" Escriba aquí su mensaje"
Text="{Binding OutText}" Text="{Binding OutText}"
Keyboard="Chat" HeightRequest="10"/>
Margin="4"/>
<Image Source="btn_enviar_chat.png" WidthRequest="45" HeightRequest="5" Margin="4" BackgroundColor="#FFA2C300"> <Image Source="btn_enviar_chat.png" WidthRequest="40" VerticalOptions="FillAndExpand" BackgroundColor="#FFA2C300" Aspect="AspectFit">
<Image.GestureRecognizers> <Image.GestureRecognizers>
<TapGestureRecognizer <TapGestureRecognizer
Command="{Binding SendCommand}" /> Command="{Binding SendCommand}" />
......
...@@ -21,82 +21,12 @@ ...@@ -21,82 +21,12 @@
{ {
var target = vm.ListMessages[vm.ListMessages.Count - 1]; var target = vm.ListMessages[vm.ListMessages.Count - 1];
MessagesListView.ScrollTo(target, ScrollToPosition.End, true); MessagesListView.ScrollTo(target, ScrollToPosition.End, true);
}; };
//newQuest.IsEnabled = true;
//buttonNewQuestion.IsEnabled = false;
//buttonNewQuestion.Clicked += ButtonNewQuestion_Clicked;
} }
protected void ItemTapped(object sender, ItemTappedEventArgs e) protected void ItemTapped(object sender, ItemTappedEventArgs e)
{ {
((ListView)sender).SelectedItem = null; ((ListView)sender).SelectedItem = null;
} }
//protected override void OnBindingContextChanged()
//{
// base.OnBindingContextChanged();
// ViewModel.PropertyChanged += ViewModel_PropertyChanged;
//}
//private void ViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
//{
// OnDataRefreshed();
//}
//private async void ButtonNewQuestion_Clicked(object sender, EventArgs e)
//{
// if (String.IsNullOrWhiteSpace(newQuest.Text))
// {
// newQuest.Text = "";
// return;
// }
// await ViewModel.SaveQuestion(newQuest.Text);
// listNewConsultation.SelectedItem = null;
// //OnDataRefreshed();
//}
///// <summary>
///// Método llamado cada vez que una página pasa a ser visible
///// </summary>
//protected override async void OnAppearing()
//{
// base.OnAppearing();
// await ViewModel.RefreshData();
// OnDataRefreshed();
//}
///// <summary>
///// Método llamado al hacer tap en un elemento de la lista. Navega a la página de detalle
///// de la notificación seleccionada
///// </summary>
///// <param name="sender">La ListiView</param>
///// <param name="e">Argumentos del evento</param>
//void ItemTapped(object sender, ItemTappedEventArgs e)
//{
// if (e == null) return; // has been set to null, do not 'process' tapped event
// QuestionMessage item = (QuestionMessage)e.Item;
// if (ViewModel.NewQuestion.Last() == item && item.FromUser)
// {
// buttonNewQuestion.IsEnabled = true;
// newQuest.IsEnabled = true;
// newQuest.Text = item.Text;
// }
// else
// {
// ((ListView)sender).SelectedItem = null; // de-select the row
// }
//}
//private void OnDataRefreshed()
//{
// newQuest.Text = "";
// newQuest.IsEnabled = !(ViewModel.IsCloseOrPending);
// newQuest.Placeholder = (ViewModel.IsCloseOrPending) ? ((ViewModel.QuestionSpecialist.State == QuestionSpecialist.Estatus.Closed) ? "La pregunta está cerrada" : "Esperando respuesta") : "Realiza una pregunta";
// buttonNewQuestion.IsEnabled = !(ViewModel.IsCloseOrPending);
//}
} }
} }
\ No newline at end of file
...@@ -40,7 +40,7 @@ ...@@ -40,7 +40,7 @@
protected override async void OnAppearing() protected override async void OnAppearing()
{ {
base.OnAppearing(); base.OnAppearing();
await ViewModel.RefresData(); await ViewModel.RefreshData();
} }
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment