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 @@ ...@@ -29,6 +29,8 @@
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Compile Include="NutriQuestions\Message.cs" />
<Compile Include="NutriQuestions\Conversation.cs" />
<Compile Include="Questions\Message.cs" /> <Compile Include="Questions\Message.cs" />
<Compile Include="Utils\BoolConverter.cs" /> <Compile Include="Utils\BoolConverter.cs" />
<Compile Include="Menu\LocalMenu.cs" /> <Compile Include="Menu\LocalMenu.cs" />
......
...@@ -106,8 +106,9 @@ namespace inutralia.API ...@@ -106,8 +106,9 @@ namespace inutralia.API
{ {
JsonConvert.PopulateObject(responseContent, item, _serializerSettings); JsonConvert.PopulateObject(responseContent, item, _serializerSettings);
} }
catch (Exception) catch (Exception e)
{ {
var error = e;
return false; 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 @@ ...@@ -23,15 +23,15 @@
Margin="15,15,0,15" Margin="15,15,0,15"
TextColor="#ffffff" TextColor="#ffffff"
FontAttributes="Bold" FontAttributes="Bold"
Text="Asunto mi consulta con un texto de longitud media"/> Text="{ Binding Conversation.Subject }"/>
</StackLayout> </StackLayout>
<!--lista con conversación chat--> <!--lista con conversación chat-->
<ListView Grid.Row="1" Grid.Column="0" <ListView Grid.Row="1" Grid.Column="0"
x:Name="MyListView" x:Name="MyListView"
ItemsSource="{Binding Bubbles}" ItemsSource="{Binding Conversation.Messages}"
ItemTapped="Handle_ItemTapped"
CachingStrategy="RecycleElement" CachingStrategy="RecycleElement"
HasUnevenRows ="True"> HasUnevenRows ="True">
...@@ -52,31 +52,32 @@ ...@@ -52,31 +52,32 @@
<!-- row 3 span, col 0 icono --> <!-- row 3 span, col 0 icono -->
<Image Grid.RowSpan="2" Grid.Column="0" <Image Grid.RowSpan="2" Grid.Column="0"
Source="{Binding Icono}" Source="user_chat.png"
Aspect="AspectFit" Aspect="AspectFit"
WidthRequest="45" WidthRequest="45"
HorizontalOptions="Start" HorizontalOptions="Start"
VerticalOptions="CenterAndExpand"/> VerticalOptions="CenterAndExpand"/>
<!-- row 1 span, col 0 icono --> <!-- row 1 span, col 0 icono -->
<Label Grid.Row="0" Grid.Column="1" <Label Grid.Row="0" Grid.Column="1"
TextColor="#000000" TextColor="#000000"
FontAttributes="Bold" FontAttributes="Bold"
Text="{Binding Mensaje}"/> Text="{Binding Text}"/>
<Label Grid.Row="1" Grid.Column="1" <Label Grid.Row="1" Grid.Column="1"
FontSize="Micro" FontSize="Micro"
TextColor="#000000" TextColor="#000000"
Text="{Binding Fecha}"/> Text="{Binding Created_at }"/>
<Label Grid.Row="1" Grid.Column="1" <Label Grid.Row="1" Grid.Column="1"
XAlign="End" XAlign="End"
FontSize="Micro" FontSize="Micro"
TextColor="#000000" TextColor="#000000"
Text="{Binding Leida}"/> Text="{Binding Read_message}"/>
</Grid> </Grid>
</ViewCell> </ViewCell>
...@@ -84,6 +85,25 @@ ...@@ -84,6 +85,25 @@
</ListView.ItemTemplate> </ListView.ItemTemplate>
</ListView> </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" > <StackLayout Grid.Row="2" Grid.Column="0" >
<Editor Text="Escribe un mensaje..." <Editor Text="Escribe un mensaje..."
......
using System; 
using System.Collections.Generic; using inutralia.ViewModels;
using inutralia.Views.NutriQuestion;
using Xamarin.Forms; 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 namespace inutralia.Views.NutriQuestion
{ {
public partial class DetailsNutriQuestionListView : ContentPage 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 public DetailsNutriQuestionListView()
{
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"
}); InitializeComponent();
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 protected override async void OnAppearing()
{ {
Id = "6", base.OnAppearing();
Mensaje = "ultima respuesta del nutricionista, fin de la converasación, cerrada ", await ViewModel.RefreshData();
Icono = "nutricionist_chat.png",
Fecha = "02/11/2019 16:17:48",
Leida = "Leída"
});
BindingContext = this;
} }
async void Handle_ItemTapped(object sender, ItemTappedEventArgs e)
{
if (e.Item == null)
return;
await DisplayAlert("Item Tapped", "An item was tapped.", "OK");
//Deselect Item
((ListView)sender).SelectedItem = null;
}
} }
} }
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="inutralia.Views.NutriQuestion.NutriQuestionListView" x:Class="inutralia.Views.NutriQuestion.NutriQuestionListView"
x:Name="NutriQuestionListView"
Title="Consultas"> Title="Consultas">
<StackLayout Padding="15,15,15,30"> <StackLayout Padding="15,15,15,30">
...@@ -23,10 +24,11 @@ ...@@ -23,10 +24,11 @@
</StackLayout> </StackLayout>
<!-- listView--> <!-- listView-->
<ListView x:Name="ConsultasListView" <ListView x:Name="listConversations"
ItemsSource="{Binding Monkeys}" ItemsSource="{Binding Conversations}"
ItemTapped="Handle_ItemTapped" ItemTapped="Handle_ItemTapped"
CachingStrategy="RecycleElement" CachingStrategy="RecycleElement"
HasUnevenRows ="True"> HasUnevenRows ="True">
<ListView.ItemTemplate> <ListView.ItemTemplate>
...@@ -50,7 +52,7 @@ ...@@ -50,7 +52,7 @@
<Label Grid.Row="0" Grid.ColumnSpan="2" <Label Grid.Row="0" Grid.ColumnSpan="2"
TextColor="#000000" TextColor="#000000"
FontAttributes="Bold" FontAttributes="Bold"
Text="{Binding Asunto}"/> Text="{Binding Subject}"/>
<!-- row 0 Right column --> <!-- row 0 Right column -->
<!--<Label Grid.Row="0" Grid.Column="1" <!--<Label Grid.Row="0" Grid.Column="1"
...@@ -61,7 +63,7 @@ ...@@ -61,7 +63,7 @@
<Label Grid.Row="1" Grid.Column="0" <Label Grid.Row="1" Grid.Column="0"
FontSize="Micro" FontSize="Micro"
TextColor="#000000" TextColor="#000000"
Text="{Binding Fecha}"/> Text="{Binding Created_at}"/>
...@@ -70,8 +72,7 @@ ...@@ -70,8 +72,7 @@
XAlign="End" XAlign="End"
FontSize="Micro" FontSize="Micro"
TextColor="#000000" TextColor="#000000"
Text="{Binding Estado}"/> Text="{Binding State}"/>
</Grid> </Grid>
</ViewCell> </ViewCell>
</DataTemplate> </DataTemplate>
......
using System; using inutralia.Models;
using System.Collections.Generic; using inutralia.ViewModels;
using inutralia.Views.NutriQuestion; using System;
using Xamarin.Forms; 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 namespace inutralia.Views.NutriQuestion
{ {
public partial class NutriQuestionListView : ContentPage public partial class NutriQuestionListView : ContentPage
{ {
public IList<Monkey> Monkeys { get; private set; }
protected NutriQuestionListViewModel ViewModel => BindingContext as NutriQuestionListViewModel;
public NutriQuestionListView() public NutriQuestionListView()
{ {
InitializeComponent(); InitializeComponent();
BindingContext = new NutriQuestionListViewModel();
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;
} }
//click item list /// <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) void Handle_ItemTapped(object sender, ItemTappedEventArgs e)
{ {
if (e.Item == null) // e.Item apunta a la notificación seleccionada. A partir de ella se crea su ViewModel
return; // y con él se crea la página de detalle y se navega a ella
//ABRO VISTA DETALLES DE LA CONSULTA Navigation.PushAsync(
(App.Current.MainPage as RootPage).Navigate<DetailsNutriQuestionListView>(); new DetailsNutriQuestionListView()
{
BindingContext = new DetailsNutriQuestionListViewModel(e.Item as Conversation)
}
);
//Deselect Item // Deselecciona el item para que no le cambie el color de fondo
((ListView)sender).SelectedItem = null; ((ListView)sender).SelectedItem = null;
} }
//click button create question //click button create question
private void NewQuestion_Clicked(object sender, EventArgs e) private void NewQuestion_Clicked(object sender, EventArgs e)
...@@ -240,7 +50,16 @@ namespace inutralia.Views.NutriQuestion ...@@ -240,7 +50,16 @@ namespace inutralia.Views.NutriQuestion
} }
/// <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 @@ ...@@ -54,6 +54,8 @@
<DesignTime>True</DesignTime> <DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon> <DependentUpon>Resources.resx</DependentUpon>
</Compile> </Compile>
<Compile Include="ViewModels\NutriQuestions\DetailsNutriQuestionListViewModel.cs" />
<Compile Include="ViewModels\NutriQuestions\NutriQuestionListViewModel.cs" />
<Compile Include="ViewModels\Menus\CustomMenuViewModel.cs" /> <Compile Include="ViewModels\Menus\CustomMenuViewModel.cs" />
<Compile Include="ViewModels\Menus\RecipeListViewModel.cs" /> <Compile Include="ViewModels\Menus\RecipeListViewModel.cs" />
<Compile Include="ViewModels\QuestionsSpecialist\NewQuestionViewModel.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