Commit 4c75320c by gema

carga de datos en la vista detalles de una conversación. han sido necesarios…

carga de datos en la vista detalles de una conversación. han sido necesarios pequeños datos en la api.
parent b0fa5866
namespace inutralia.Models
{
using Newtonsoft.Json;
using System.Collections.Generic;
[JsonObject(MemberSerialization.OptIn)]
[DataPath("conversation")]
public class Conversation : ObservableEntityData
{
[JsonProperty("subject", Required = Required.Always)]
public string Subject { get; set; }
[JsonProperty("created_at")]
public string Created_at { get; set; }
[JsonProperty("state_ds")]
public string State { get; set; }
[JsonProperty("messages")]
public IList<Message> Messages { get; set; }
public Conversation() { }
/*public string ExcerptCompress => Excerpt == null ? "..." : Excerpt.Substring(0, Math.Min(80, Excerpt.Length)).Length < 80 ? Excerpt : Excerpt.Substring(0, Excerpt.Substring(0, 80).LastIndexOf(" ")) + ((Excerpt.Length > 80) ? "..." : "");*/
}
}
namespace inutralia.Models
{
using Newtonsoft.Json;
[JsonObject(MemberSerialization.OptIn)]
[DataPath("conversation")]
public class Message : ObservableEntityData
{
[JsonProperty("message", Required = Required.Always)]
public string Text { get; set; }
[JsonProperty("read")]
public string Read_message { get; set; }
[JsonProperty("created_at")]
public string Created_at { get; set; }
public Message() { }
}
}
......@@ -29,6 +29,8 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Compile Include="NutriQuestions\Message.cs" />
<Compile Include="NutriQuestions\Conversation.cs" />
<Compile Include="Questions\Message.cs" />
<Compile Include="Utils\BoolConverter.cs" />
<Compile Include="Menu\LocalMenu.cs" />
......
......@@ -106,8 +106,9 @@ namespace inutralia.API
{
JsonConvert.PopulateObject(responseContent, item, _serializerSettings);
}
catch (Exception)
catch (Exception e)
{
var error = e;
return false;
}
......
namespace inutralia.ViewModels
{
using inutralia.Models;
using System.Threading.Tasks;
public class DetailsNutriQuestionListViewModel : BaseNavigationViewModel
{
public DetailsNutriQuestionListViewModel(Conversation conversation)
{
Conversation = conversation;
Title = Conversation.Subject;
if (Conversation.Messages == null)
Conversation.Messages = new Message[0];
}
public Conversation Conversation { get; private set; }
public async Task RefreshData()
{
if (Conversation == null)
return;
IsBusy = true;
if (await App.API.RefreshItemAsync(Conversation))
{
OnPropertyChanged("Conversation");
// Cambiar título
Title = Conversation.Subject;
} //endif
IsBusy = false;
}
}
}
\ No newline at end of file
namespace inutralia.ViewModels
{
using inutralia.Models;
using System.Threading.Tasks;
using MvvmHelpers;
using System.Diagnostics;
using Xamarin.Forms;
public class NutriQuestionListViewModel : BaseNavigationViewModel
{
public NutriQuestionListViewModel() {
}
// Lista de conversaciones
ObservableRangeCollection<Conversation> _Conversations;
// Comando de update del listadd
Command _RefreshConversationsCommand;
// Acceso a la lista de conversaciones
public ObservableRangeCollection<Conversation> Conversations
{
// Getter (lazy load), crea la lista si no existe
get { return _Conversations ?? (_Conversations = new ObservableRangeCollection<Conversation>()); }
// Setter. cambiar el valor y notifica a la vista de dicho cambio
set
{
_Conversations = value;
OnPropertyChanged("Conversations");
}
}
/// <summary>
/// Método que realiza la carga inicial del listado
/// </summary>
public async Task ExecuteLoadConversationsCommand()
{
// Realiza el proceso de actualización si hay menos de un
// elemento en el listado
if (Conversations.Count < 1)
await FetchConversations();
}
/// <summary>
/// Acceso al comando de actualización del listado
/// </summary>
public Command RefreshConversationsCommand
{
// Getter (lazy load), crea el comando si no existe
get { return _RefreshConversationsCommand ?? (_RefreshConversationsCommand = new Command(async () => await ExecuteRefreshConversationsCommand())); }
}
/// <summary>
/// Proceso de ejecución del comando de actualización del listado
/// </summary>
async Task ExecuteRefreshConversationsCommand()
{
// Hace que el comando no se pueda ejecutar de nuevo
RefreshConversationsCommand.ChangeCanExecute();
// Realiza el proceso de actualización
await FetchConversations();
// Hace que el comando pueda volver a ejecutarse
RefreshConversationsCommand.ChangeCanExecute();
}
/// <summary>
/// Proceso de actualización del listado
/// </summary>
async Task FetchConversations()
{
// Indicamos que estamos ocupados (provoca que aparezca el indicador de carga)
IsBusy = true;
// Llamada al API para coger el listado (provoca que se actualize la vista del listado)
// Nota: Al obtener el listado, el controlador Rest del servidor no retorna el cuerpo de
// la notificación (campo Body)
try
{
Conversations = new ObservableRangeCollection<Conversation>(await App.API.RefreshListAsync<Conversation>());
}
catch (System.Exception e)
{
Debug.WriteLine(e.Message);
Conversations.Clear();
}
// Indicamos que ya no estamos ocupados (provoca que desaparezca el indicador de carga)
IsBusy = false;
}
}
}
\ No newline at end of file
......@@ -23,15 +23,15 @@
Margin="15,15,0,15"
TextColor="#ffffff"
FontAttributes="Bold"
Text="Asunto mi consulta con un texto de longitud media"/>
Text="{ Binding Conversation.Subject }"/>
</StackLayout>
<!--lista con conversación chat-->
<ListView Grid.Row="1" Grid.Column="0"
x:Name="MyListView"
ItemsSource="{Binding Bubbles}"
ItemTapped="Handle_ItemTapped"
ItemsSource="{Binding Conversation.Messages}"
CachingStrategy="RecycleElement"
HasUnevenRows ="True">
......@@ -47,36 +47,37 @@
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20*" />
<ColumnDefinition Width="80*" />
</Grid.ColumnDefinitions>
</Grid.ColumnDefinitions>
<!-- row 3 span, col 0 icono -->
<Image Grid.RowSpan="2" Grid.Column="0"
Source="{Binding Icono}"
Source="user_chat.png"
Aspect="AspectFit"
WidthRequest="45"
HorizontalOptions="Start"
VerticalOptions="CenterAndExpand"/>
<!-- row 1 span, col 0 icono -->
<Label Grid.Row="0" Grid.Column="1"
TextColor="#000000"
FontAttributes="Bold"
Text="{Binding Mensaje}"/>
Text="{Binding Text}"/>
<Label Grid.Row="1" Grid.Column="1"
FontSize="Micro"
TextColor="#000000"
Text="{Binding Fecha}"/>
Text="{Binding Created_at }"/>
<Label Grid.Row="1" Grid.Column="1"
XAlign="End"
FontSize="Micro"
TextColor="#000000"
Text="{Binding Leida}"/>
Text="{Binding Read_message}"/>
</Grid>
</ViewCell>
......@@ -84,6 +85,25 @@
</ListView.ItemTemplate>
</ListView>
<!-- prueba hasta que cargue todos mensajes lista-->
<!-- <StackLayout Grid.Row="1" Grid.Column="0"
BackgroundColor="Pink">
<Label
Margin="15,15,0,15"
TextColor="#ffffff"
FontAttributes="Bold"
Text="{ Binding Conversation.Subject }"/>
<Label
Margin="15,15,0,15"
TextColor="#ffffff"
FontAttributes="Bold"
Text="{ Binding Conversation.Created_at }"/>
</StackLayout>-->
<StackLayout Grid.Row="2" Grid.Column="0" >
<Editor Text="Escribe un mensaje..."
......
using System;
using System.Collections.Generic;
using inutralia.Views.NutriQuestion;

using inutralia.ViewModels;
using Xamarin.Forms;
public class Bubble
{
public string Id { get; set; }
public string Mensaje { get; set; }
public string Icono { get; set; }
public string Fecha { get; set; }
public string Leida { get; set; }
public override string ToString()
{
return Mensaje;
}
}
namespace inutralia.Views.NutriQuestion
{
public partial class DetailsNutriQuestionListView : ContentPage
{
public IList<Bubble> Bubbles { get; private set; }
protected DetailsNutriQuestionListViewModel ViewModel => BindingContext as DetailsNutriQuestionListViewModel;
public DetailsNutriQuestionListView()
{
InitializeComponent();
Bubbles = new List<Bubble>();
Bubbles.Add(new Bubble
{
Id = "1",
Mensaje = "Consulta del cliente ejemplo afas fdfasd fsadf sdfasdfsdf sdfasdfasdf asdfasdfasdfsdaf",
Icono = "user_chat.png",
Fecha = "29/10/2019 16:17:48",
Leida = "Leída"
});
Bubbles.Add(new Bubble
{
Id = "2",
Mensaje = "Respuesta del Nutricionista ejemplo afas fdfasd fsadf sdfasdfsdf sdfasdfasdf asdfasdfasdfsdaf",
Icono = "nutricionist_chat.png",
Fecha = "30/10/2019 16:17:48",
Leida = "No Leída"
});
Bubbles.Add(new Bubble
{
Id = "3",
Mensaje = "Respuesta del Cliente ejemplo afas fdfasd af sdafsaf adsfasdf asdfasdfasd sdfasdfas sdfasdf sdfdf asdfsafsafasdf dasfasdf dsfasdf sdafasdfasdf sdfasdfasdf sdfas dsfasdfasdf sadfasd fsadf",
Icono = "user_chat.png",
Fecha = "01/11/2019 16:17:48",
Leida = "Leída"
});
Bubbles.Add(new Bubble
{
Id = "4",
Mensaje = "Respuesta del Nutricionista ejemplo afas fdfasd fsadf sdfasdfsdf sdfasdfasdf asdfasdfasdfsdaf",
Icono = "nutricionist_chat.png",
Fecha = "02/11/2019 16:17:48",
Leida = "Leída"
});
Bubbles.Add(new Bubble
{
Id = "5",
Mensaje = "Respuesta del usuario ya par finalizar ",
Icono = "user_chat.png",
Fecha = "02/11/2019 16:17:48",
Leida = "Leída"
});
Bubbles.Add(new Bubble
{
Id = "6",
Mensaje = "ultima respuesta del nutricionista, fin de la converasación, cerrada ",
Icono = "nutricionist_chat.png",
Fecha = "02/11/2019 16:17:48",
Leida = "Leída"
});
BindingContext = this;
InitializeComponent();
}
async void Handle_ItemTapped(object sender, ItemTappedEventArgs e)
protected override async void OnAppearing()
{
if (e.Item == null)
return;
base.OnAppearing();
await ViewModel.RefreshData();
await DisplayAlert("Item Tapped", "An item was tapped.", "OK");
//Deselect Item
((ListView)sender).SelectedItem = null;
}
}
}
......@@ -2,6 +2,7 @@
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="inutralia.Views.NutriQuestion.NutriQuestionListView"
x:Name="NutriQuestionListView"
Title="Consultas">
<StackLayout Padding="15,15,15,30">
......@@ -23,10 +24,11 @@
</StackLayout>
<!-- listView-->
<ListView x:Name="ConsultasListView"
ItemsSource="{Binding Monkeys}"
<ListView x:Name="listConversations"
ItemsSource="{Binding Conversations}"
ItemTapped="Handle_ItemTapped"
CachingStrategy="RecycleElement"
HasUnevenRows ="True">
<ListView.ItemTemplate>
......@@ -50,7 +52,7 @@
<Label Grid.Row="0" Grid.ColumnSpan="2"
TextColor="#000000"
FontAttributes="Bold"
Text="{Binding Asunto}"/>
Text="{Binding Subject}"/>
<!-- row 0 Right column -->
<!--<Label Grid.Row="0" Grid.Column="1"
......@@ -61,18 +63,17 @@
<Label Grid.Row="1" Grid.Column="0"
FontSize="Micro"
TextColor="#000000"
Text="{Binding Fecha}"/>
Text="{Binding Created_at}"/>
<!-- row 1 Right column -->
<Label Grid.Row="1" Grid.Column="1"
<Label Grid.Row="1" Grid.Column="1"
XAlign="End"
FontSize="Micro"
TextColor="#000000"
Text="{Binding Estado}"/>
</Grid>
TextColor="#000000"
Text="{Binding State}"/>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
......
using System;
using System.Collections.Generic;
using inutralia.Views.NutriQuestion;
using inutralia.Models;
using inutralia.ViewModels;
using System;
using Xamarin.Forms;
public class Monkey
{
public string Id { get; set; }
public string Asunto { get; set; }
public string Fecha { get; set; }
public string Estado { get; set; }
public string Leida { get; set; }
public override string ToString()
{
return Asunto;
}
}
namespace inutralia.Views.NutriQuestion
{
public partial class NutriQuestionListView : ContentPage
{
public IList<Monkey> Monkeys { get; private set; }
protected NutriQuestionListViewModel ViewModel => BindingContext as NutriQuestionListViewModel;
public NutriQuestionListView()
{
InitializeComponent();
Monkeys = new List<Monkey>();
Monkeys.Add(new Monkey
{
Id="1",
Asunto = "Asunto question 1 que sea un poco larga de más a ver que pasa",
Fecha = "29/10/2019 16:17:48",
Estado="Cerrada",
Leida="Leída"
});
Monkeys.Add(new Monkey
{
Id = "2",
Asunto = "Asunto question 2 Lorem ipsum daet ejemplo de text largo otra vez",
Fecha = "28/10/2019 16:17:48",
Estado = "Pendiente",
Leida = "No Leída"
});
Monkeys.Add(new Monkey
{
Id = "3",
Asunto = "Asunto question 3",
Fecha = "27/10/2019 16:17:48",
Estado = "Respuesta",
Leida = "Leída"
});
Monkeys.Add(new Monkey
{
Id = "4",
Asunto = "Asunto question 4",
Fecha = "27/10/2019 16:17:48",
Estado = "Pendiente",
Leida = "No Leída"
});
Monkeys.Add(new Monkey
{
Id = "5",
Asunto = "Asunto question 5",
Fecha = "27/10/2019 16:17:48",
Estado = "Pendiente",
Leida = "No Leída"
});
Monkeys.Add(new Monkey
{
Id = "6",
Asunto = "Asunto question 6",
Fecha = "27/10/2019 16:17:48",
Estado = "Cerrada",
Leida = "Leída"
});
Monkeys.Add(new Monkey
{
Id = "7",
Asunto = "Asunto question 7",
Fecha = "27/10/2019 16:17:48",
Estado = "Respuesta",
Leida = "Leída"
});
Monkeys.Add(new Monkey
{
Id = "8",
Asunto = "Asunto question 8",
Fecha = "27/10/2019 16:17:48",
Estado = "Cerrada",
Leida = "Leída"
});
Monkeys.Add(new Monkey
{
Id = "9",
Asunto = "Asunto question 9",
Fecha = "27/10/2019 16:17:48",
Estado = "Cerrada",
Leida = "Leída"
});
Monkeys.Add(new Monkey
{
Id = "10",
Asunto = "Asunto question 10",
Fecha = "27/10/2019 16:17:48",
Estado = "Cerrada",
Leida = "Leída"
});
Monkeys.Add(new Monkey
{
Id = "11",
Asunto = "Asunto question 11",
Fecha = "27/10/2019 16:17:48",
Estado = "Cerrada",
Leida = "Leída"
});
Monkeys.Add(new Monkey
{
Id = "12",
Asunto = "Asunto question 12",
Fecha = "27/10/2019 16:17:48",
Estado = "Cerrada",
Leida = "Leída"
});
Monkeys.Add(new Monkey
{
Id = "13",
Asunto = "Asunto question 13",
Fecha = "27/10/2019 16:17:48",
Estado = "Cerrada",
Leida = "Leída"
});
Monkeys.Add(new Monkey
{
Id = "14",
Asunto = "Asunto question 14",
Fecha = "27/10/2019 16:17:48",
Estado = "Cerrada",
Leida = "Leída"
});
Monkeys.Add(new Monkey
{
Id = "15",
Asunto = "Asunto question 15",
Fecha = "27/10/2019 16:17:48",
Estado = "Cerrada",
Leida = "Leída"
});
Monkeys.Add(new Monkey
{
Id = "16",
Asunto = "Asunto question 16",
Fecha = "27/10/2019 16:17:48",
Estado = "Cerrada",
Leida = "Leída"
});
Monkeys.Add(new Monkey
{
Id = "17",
Asunto = "Asunto question 17",
Fecha = "27/10/2019 16:17:48",
Estado = "Cerrada",
Leida = "Leída"
});
Monkeys.Add(new Monkey
{
Id = "18",
Asunto = "Asunto question 18",
Fecha = "27/10/2019 16:17:48",
Estado = "Cerrada",
Leida = "Leída"
});
Monkeys.Add(new Monkey
{
Id = "19",
Asunto = "Asunto question 19",
Fecha = "27/10/2019 16:17:48",
Estado = "Cerrada",
Leida = "Leída,"
});
BindingContext = this;
BindingContext = new NutriQuestionListViewModel();
}
//click item list
void Handle_ItemTapped(object sender, ItemTappedEventArgs e)
/// <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 ListView</param>
/// <param name="e">Argumentos del evento</param>
void Handle_ItemTapped(object sender, ItemTappedEventArgs e)
{
if (e.Item == null)
return;
//ABRO VISTA DETALLES DE LA CONSULTA
(App.Current.MainPage as RootPage).Navigate<DetailsNutriQuestionListView>();
//Deselect Item
// e.Item apunta a la notificación seleccionada. A partir de ella se crea su ViewModel
// y con él se crea la página de detalle y se navega a ella
Navigation.PushAsync(
new DetailsNutriQuestionListView()
{
BindingContext = new DetailsNutriQuestionListViewModel(e.Item as Conversation)
}
);
// Deselecciona el item para que no le cambie el color de fondo
((ListView)sender).SelectedItem = null;
}
//click button create question
private void NewQuestion_Clicked(object sender, EventArgs e)
......@@ -239,8 +49,17 @@ namespace inutralia.Views.NutriQuestion
(App.Current.MainPage as RootPage).Navigate<CreateNutriQuestion>();
}
/// <summary>
/// Método llamado cada vez que una página pasa a ser visible
/// </summary>
protected override async void OnAppearing()
{
base.OnAppearing();
// Le decimos al ViewModel que realice la primera carga del listado
await ViewModel.ExecuteLoadConversationsCommand();
}
}
......
......@@ -54,6 +54,8 @@
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="ViewModels\NutriQuestions\DetailsNutriQuestionListViewModel.cs" />
<Compile Include="ViewModels\NutriQuestions\NutriQuestionListViewModel.cs" />
<Compile Include="ViewModels\Menus\CustomMenuViewModel.cs" />
<Compile Include="ViewModels\Menus\RecipeListViewModel.cs" />
<Compile Include="ViewModels\QuestionsSpecialist\NewQuestionViewModel.cs" />
......
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