convert data of type Array to MutabaleLiveData and LiveData - mvvm

I want to convert this whole bunch of data into live data.
fun loadQuoteFromAssets(): Array: loads the json from asset folder and converted using gson.
fun getQuote() = quoteList[index]: get the quotes from array
fun nextQuote() = quoteList[++index % quoteList.size]: when clicking next, it gives me the next quote
fun previousQuote() = quoteList[(--index + quoteList.size) % quoteList.size]: when clicking previous, it gives me previous quote.
code is working fine, I just want to convert it into Mutable and live data so that i can implement databinding and lifecycle owner

Related

Using Setters and Getters from a Class

I have an class called TestData that houses a private property called data, which I define as a numeric array. Its goal is to take in data from various other .m files and extract the data and place it in the specified format (numeric array. So, a random_data.m file that I am currently working with spits out a 1X13 double array called Avec. I generate and instance of the class myTestData = TestData(); however, because the member variables are private I need to have getData and setData functions. The only idea I have is to pass Avec into getData (e.g. myTestData.getData(Avec)) and then store it in a temporary array that could then be used by setData to write into data; but I feel like this is bad practice as that array would need to be public. Also, would it make sense to pass the entire array in or should I pass each element in individually. I would like to have it check the data to make sure that it is in proper format, as well.
I guess in general my concept of how the class works in MATLAB may be flawed.
Thanks for your help in advance and if there is anything else that I can provide, please let me know. Below is some code. The first snippet above the class is from the separate .m file.
%Write data to file using the TestData Object
Avec = [some 1X13 double array]
myTestData=TestData; % Generate an instance of the object
myTestData.getData(Avec);
classdef TestData
properties (Access = private)
metaData % stores meta data in Nx2 array
data % stores data in PxQ array
colLabels % labels columns
colUnits % provides units
metaPrint % used to print metaData
temp % debugging purposes only
end
methods
%****************************************************************************%
%Function: TestData
%Purpose: Constructor used to allocate data arrays
%****************************************************************************%
function this = TestData() %constructor
this.metaData = [];
this.data = [];
this.colLabels = [];
this.colUnits = [];
this.metaPrint = [];
this.temp = [];
end %TestData()
%%
%****************************************************************************%
%Function:
%Inputs:
%Purpose:
%****************************************************************************%
function this = getData(this, someArray)
????
end %getData
I think you're misunderstanding the idea of getters and setters. A get function is designed to take something from the object and return it, while a setter is designed to put something into a property of the object. You would want something like:
function data = getData(this)
data = this.data;
% Do any processing to put data into a different format for output
end
function this = setData(this, data)
% Check the data input to make sure it is the right format, etc.
this.data = data;
end
You may also want to design setData to take different types of arguments, like a file name that it can use to load the matrix itself. You could also design your constructor to accept a matrix or file name and initialize data as well.
Also, as Cris alludes to in his comment, if the reason you were making data private was to control how the user could access and modify it, it's enough to just have getters and setters. You can make data public and your property access methods will still be called when accessing the object like Avec = myTestData.data or myTestData.data = Avec.

One way data binding not working when using date pipe

I'm using a date object to keep track of the current date in an application.
In my view I have a one way binding like this:
<h3>{{ currentDate | date }}</h3>
And in the component, I have functions to change this date, like this:
previousMonth(){
this.currentDate.setMonth(this.currentDate.getMonth() - 1);
}
nextMonth(){
this.currentDate.setMonth(this.currentDate.getMonth() + 1);
}
But when these functions are triggered, the currentDate value doesn't update on the view.
I made sure the date object is being updated, just not on the view.
Whenever I remove the date pipe, it works.
Anyone has any idea how to fix this?
Thanks!
The value is not updating in the view because the pipes in angular are so called pure (or, stateless) by default. That means that the input will not be re-evaluated if the input object changes, but only if it's replaced.
From the documentation (see section Pure and Impure pipes):
Angular executes a pure pipe only when it detects a pure change to the
input value. A pure change is either a change to a primitive input
value (String, Number, Boolean, Symbol) or a changed object reference
(Date, Array, Function, Object).
Try the following code instead:
previousMonth(){
this.currentDate.setMonth(this.currentDate.getMonth() - 1);
this.currentDate = new Date(this.currentDate);
}
nextMonth(){
this.currentDate.setMonth(this.currentDate.getMonth() + 1);
this.currentDate = new Date(this.currentDate);
}

D3 invalid character in element

I'm getting a response in JSON format, which contains an _id that is stored as an ObjectID in Mongodb on the server side. However, I change it into a String, and it still won't let me add it. Is it because it has numbers? I need the element to be identifiable by the id, so if I can't append this way, is there any other way I can reference the element by the id?
var group = d3.select("#containerthing");
var id = response._id.toString();
console.log(id);
//5802bc044f6313c1097de4a2
var responseNode = group.append(id).attr("fill","black").attr("x", 15).attr("y", 15).attr("width", 190).attr("height", 90);
//InvalidCharacterError: String contains an invalid character
I believe that I understand your problem.
D3's .append():
If the specified type is a string, appends a new element of this type (tag name) as the last child of each selected element, or the next following sibling in the update selection if this is an enter selection. [...] This function should return an element to be appended. (The function typically creates a new element, but it may instead return an existing element.
Why .append() work fine if you pass 'foo'? Because D3 append a custom tag element. If you see in your console I'am sure that you will see <foo>...</foo>
Why .append() work wrong if you pass '5802bc044f6313c1097de4a2'? A custom tag element can't start with a number. You don't use _id, you should try to find another pattern for identify your element.
I hope that helps
The reason was that you can't start elements with numbers. I had to do:
var responseNode = group.append("n"+id).attr("fill","black").attr("x", 15).attr("y", 15).attr("width", 190).attr("height", 90);
to get it to work.

MATLAB - Adding a field to the begining of an existing struct

I have a 1x100000 struct in MATLAB. It occurs to me that I need to add a field to it, which is easy and fine.. however i can't seem to add the field to the beginning i.e. make the new field the first field.
my struct looks like this
DB(kk).StudyDate
DB(kk).StudyTime
DB(kk).PatientName
i wish to make it
DB(kk).PatientID <---- new field
DB(kk).StudyDate
DB(kk).StudyTime
DB(kk).PatientName
and not
DB(kk).StudyDate
DB(kk).StudyTime
DB(kk).PatientName
DB(kk).PatientID <---- new field
this is more for aesthetics and presentation purposes than anything else as it won't really affect how the struct is used whether the new field is at the beginning or the end.
The orderfields function exists for this purpose:
% Order based on permuting current field ordering
DB = struct('StudyDate','2015/04/27','StudyTime',now(),'PatientName','Baz Bar');
DB.PatientID = dec2hex(randi([1,2^32]));
DB = orderfields(DB,[4,1,2,3]);
% Does the same with explicit fieldnames
DB = struct('StudyDate','2015/04/27','StudyTime',now(),'PatientName','Baz Bar');
DB.PatientID = dec2hex(randi([1,2^32]));
DB = orderfields(DB,{'PatientID','StudyDate','StudyTime','PatientName'});
The only way (AFAIK) to do this is to make a brand new struct and copy all the fields into it in the order you would like them displayed.

Viewing a data matrix in Eclipse (RCP)

This is how I update my TableViewer that displays a list:
public void view(MyListClass list) {
ViewerSupport.bind(
this,
new WritableList(list, controller.theClass()),
BeanProperties.values(controller.theClass(), controller.strings())
);
}
controller is an instance of a class that encapsulates my glue code (it is a different class for the two listings; as is controller.theClass()). strings() is an array of property names. MyListClass descends from ArrayList<MyListEntryObject>. That works fine. But, I want to display a matrix of data. MyMatrixClass is a HashMap<Point, MyMatrixEntryObject>. This is what I've tried:
private void view(MyMatrixClass matrix) {
columns(matrix.columns());
List<WritableList> lists = new ArrayList<WritableList>(matrix.rows());
for (MyEntityClass list : matrix.children())
if (list instanceof MyListClass)
lists.add(new WritableList((MyListClass) list, controller.theClass()));
WritableList[] alists = lists.toArray(new WritableList[0]);
MultiList mlist = new MultiList(alists);
ViewerSupport.bind(
this,
mlist,
BeanProperties.value(controller.theClass(), "value")
);
}
This doesn't work. I get null argument popup errors. (Every data model class implements MyEntityClass. Class names have been altered due to this being a proprietary program I'm being employed to develop.)
Long story short, how do I use ViewerSupport and BeanProperties to display a matrix of data in a TableViewer?
As the JFace table viewer is line-based, you have to provide your matrix in a line-based way. You have to create a collection of the lines of the matrix, and then set this list as the input of the viewer. After that, you could create BeanProperties, that display the columns of the selected rows.