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
{
using MvvmHelpers;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
......@@ -10,6 +9,14 @@
public class Message : ObservableEntityData
{
public enum Estatus
{
Pendiente,
Respondida_por_cliente,
Respondida_por_nutricionista,
Cerrado
};
public string StatusImg => imgStatus[StateId];
[JsonProperty("text", Required = Required.Always)]
......@@ -46,7 +53,8 @@
public int StateId { get; set; }
[JsonProperty("status", Required = Required.Always)]
public string State { get; set; }
[JsonConverter(typeof(StringEnumConverter))]
public Estatus State { get; set; }
[JsonProperty("subject", Required = Required.Always)]
public string Subject
......@@ -57,11 +65,11 @@
string _subject;
string[] imgStatus = new string[2]
string[] imgStatus = new string[4]
{
"pendiente.png", // 1 - Pendiente de responder
//"respondida_por_cliente.png", // 2 - Respondida por cliente
//"respondida_por_nutricionista", // 3- Respondida por nutricionista
"respondida_por_cliente.png", // 2 - Respondida por cliente
"respondida_por_nutricionista", // 3- Respondida por nutricionista
"cerrada" // 4 - Conversación cerrada
};
}
......
......@@ -3,14 +3,40 @@
using inutralia.Models.Questions;
using MvvmHelpers;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using System.Windows.Input;
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 ObservableRangeCollection<Message> _ListMessages;
// LOADING LIST
Command _LoadNewQuestionCommand;
// UODATE LIST
Command _RefreshNewQuestionCommand;
// DELETE LIST
Command _DeleteNewQuestionCommand;
public NewQuestionViewModel()
{
IsBusy = true;
......@@ -28,11 +54,9 @@
MessageDateTime = DateTime.Now
};
ListMessages.Add(message);
OutText = "";
}
});
IsBusy = false;
......@@ -44,5 +68,140 @@
set { SetProperty(ref _outText, value); }
}
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 @@
{
using System;
using System.Threading.Tasks;
using inutralia.Models;
using inutralia.Models.Questions;
public class PendingReadViewModel : BaseNavigationViewModel
......@@ -18,7 +17,7 @@
public PendingReadViewModel(Message message)
{
Message = message;
Title = message?.State;
Title = message?.StatusImg;
}
public PendingReadViewModel() { }
......@@ -33,7 +32,8 @@
public DateTime MessageDateTime => (DateTime)Message?.MessageDateTime;
public async Task RefresData()
// REFRESH
public async Task RefreshData()
{
if (Message == null)
return;
......@@ -42,7 +42,7 @@
if (await App.API.RefreshItemAsync(Message))
{
Title = Message.State;
Title = Message.StatusImg;
OnPropertyChanged("State");
}
......
......@@ -19,7 +19,7 @@
protected async void ItemTapped(object sender, ItemTappedEventArgs e)
{
var respon = e.Item as Message;
if (respon.State == "Respondida")
if (respon.StatusImg == "Respondida")
{
await DisplayAlert("ERROR", "La pregunta ya está respondida", "Entendido");
}
......
......@@ -35,14 +35,13 @@
Footer=""/>
</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"
Placeholder=" Escriba aquí su mensaje"
Text="{Binding OutText}"
Keyboard="Chat"
Margin="4"/>
Text="{Binding OutText}"
HeightRequest="10"/>
<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>
<TapGestureRecognizer
Command="{Binding SendCommand}" />
......
......@@ -21,82 +21,12 @@
{
var target = vm.ListMessages[vm.ListMessages.Count - 1];
MessagesListView.ScrollTo(target, ScrollToPosition.End, true);
};
//newQuest.IsEnabled = true;
//buttonNewQuestion.IsEnabled = false;
//buttonNewQuestion.Clicked += ButtonNewQuestion_Clicked;
};
}
protected void ItemTapped(object sender, ItemTappedEventArgs e)
{
((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 @@
protected override async void 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