I am trying to find a tool to automatically generate a set of view elements in the fragment.
The idea is to have a user input template file (xml or json), where the user can define the simple fields that he wants to see displayed in the fragment such as TextView, EditText, Buttons or spinner menus.
Then, the app can read that file and load/generate automatically the fields and show them in the fragment.
For example, a user defines the following json file, here define 2 TextFields and one checkbox with 2 options:
{
"sections": [
{
"rows": [
{
"items": [
{
"componentType": "TextField",
"inputType" "text",
"var": "user name"
}
]
}
],
"itemRank": "1",
"title": "User Login Name",
"shortTitle": "login"
},
{
"rows": [
{
"items": [
{
"componentType": "TextField",
"inputType" "float",
"var": "0.0",
"min": "0.0",
"max": "100.0",
"format":"3.3f"
}
]
}
],
"itemRank": "2",
"title": "Weather Temperature",
"shortTitle": "temp"
},
{
"rows": [
{
"items": [
{
"componentType": "multiSelect",
"inputType" "float",
"options": [
{
"text": "Checkbox option 1",
"var": "option1",
"selected": "false"
},
{
"text": "Checkbox option 2",
"var": "option2",
"selected": "false"
}
]
}
]
}
],
"itemRank": "3",
"title": "Weather Temperature",
"shortTitle": "temp"
},
"name": "My json template view elements",
"title": "My report"
}
Having that json file as an input, generate these fields to attach them on the fragment as views and process them accordingly.
Is there a library that can do that?
Related
I have a list of items, these items can have children, and the children can also have children. The API I'm using returns a list of these items in a single Array of Objects and provides a path property to consume the list and organise it how you choose. Each child adds an extra incrementing -000n to the path starting at 0001. I can't work out how to consume the path value to organise the list of Objects into nested Arrays where each item has it's own respective children[] Array.
I'm using Typescript/JS and Kotlin (Android Framework).
Been trying to figure this out for a while, I'd appreciate some community input on the problem. Hope I've explained it well enough, thanks!
Example:
|--group (0001)
|--item (0001-0001)
|--item (0001-0001-0001)
|--item (0001-0001-0001-0001)
|--item (0001-0001-0001-0002)
|--item (0001-0002)
|--item (0001-0002-0001)
|--item (0001-0002-0001-0001)
|--item (0001-0002-0001-0001-0001)
|--item (0001-0002-0001-0002)
|--item (0001-0003)
|--group (0002)
|--item (0002-0001)
Payload:
{
"items": [
{
"name": "cameras",
"type": "group",
"path": "0001"
},
{
"name": "camera-1",
"type": "equipment",
"path": "0001-0001"
},
{
"name": "charger",
"type": "power",
"path": "0001-0001-0001"
},
{
"name:": "cable",
"type": "power",
"path": "0001-0001-0001-0001"
},
{
"name": "adapter",
"type": "power",
"path": "0001-0001-0001-0002"
},
{
"name": "camera-2",
"type": "equipment",
"path": "0001-0002"
},
// etc
{
"name": "lights",
"type": "group",
"path": "0002"
}
// etc
]
}
Preferred outcome:
{
"items": [
{
"name": "cameras",
"type": "group",
"path": "0001",
"children": [
{
"name": "camera-1",
"type": "equipment",
"path": "0001-0001",
"children": [
{
"name": "charger",
"type": "power",
"path": "0001-0001-0001",
"children": [
{
"name:": "cable",
"type": "power",
"path": "0001-0001-0001-0001"
},
{
"name": "adapter",
"type": "power",
"path": "0001-0001-0001-0002"
}
]
}
]
},
{
"name": "camera-2",
"type": "equipment",
"path": "0001-0002",
// children as above
}
]
},
{
"name": "lights",
"type": "group",
"path": "0002",
// children as above
}
]
}
You could use a map to store the items by their path while you build the item tree. Here's an example:
data class Item(
val name: String,
val type: String,
val path: String,
val children: MutableList<Item> = mutableListOf(),
)
val itemsList = <flat list of items, parsed from json>
val pathMap = mutableMapOf<String, Item>()
val itemsTree = mutableListOf<Item>()
for (item in itemsList) {
pathMap[item.path] = item
if ('-' in item.path) {
val parentPath = item.path.substringBeforeLast('-')
val parent = pathMap[parentPath]
if (parent != null) {
parent.children += item
}
} else {
itemsTree += item
}
}
Here itemsList is the original list of items, in a flat hierarchy, and itemsTree is a list of top-level items only, each with a children attribute containing children items. This does require however that all "parents" appear before any of their children in itemsList, but could be easily modified to account for that not being the case.
I'm not sure if you wanted Kotlin or TypeScript, but the concept is easily interchangeable between the two, all you need is a map-like data structure.
I am integrating Paypal checkout in an Android App using REST APIs provided by Paypal and my country is India so I am following this guide from PayPal.
How I did as per docs:
Get access-token (/v1/oauth2/token) for further api calls.
Use the Create Order API to create a payment (v2/checkout/orders)and in the response we will get approval url at where you need to redirect user to make the payment.
Now my question is how do I know if payment transaction was successful or not in mobile app because I am using WebView in my app to load approval url.
Order is created like this and I load href inside webview:
{
"id": "1KK44573EX7352015",
"status": "CREATED",
"links": [
{
"href": "https://www.sandbox.paypal.com/checkoutnowtoken=1KK44573EX7352015",
"rel": "approve",
"method": "GET"
}
]
}
I did this way:
As soon as Payment is successfully completed by customer the return_url gets called with query parameters : PayerID & token(orderID). At that time we can update user's payment status in our database (Amount is not deducted yet still because order is yet not approved or captured).
After that we can capture our order (Make sure invoice-id is not duplicate) otherwise status will be not completed.
If order is not approved on the time of capture we get this kind of error:
{
"name": "UNPROCESSABLE_ENTITY",
"details": [
{
"issue": "ORDER_NOT_APPROVED",
"description": "Payer has not yet approved the Order for payment. Please redirect the payer to the 'rel':'approve' url returned as part of the HATEOAS links within the Create Order call or provide a valid payment_source in the request."
}
],
"message": "The requested action could not be performed, semantically incorrect, or failed business validation.",
"debug_id": "47af43e..",
"links": [
{
"href": "https://developer.paypal.com/docs/api/orders/v2/#error-ORDER_NOT_APPROVED",
"rel": "information_link",
"method": "GET"
}
]
}
If there is duplicate invoice-id you will see error at the time of capture:
{
"name": "UNPROCESSABLE_ENTITY",
"details": [
{
"issue": "DUPLICATE_INVOICE_ID",
"description": "Duplicate Invoice ID detected. To avoid a potential duplicate transaction your account setting requires that Invoice Id be unique for each transaction."
}
],
"message": "The requested action could not be performed, semantically incorrect, or failed business validation.",
"debug_id": "86e0cc7f....",
"links": [
{
"href": "https://developer.paypal.com/docs/api/orders/v2/#error-DUPLICATE_INVOICE_ID",
"rel": "information_link",
"method": "GET"
}
]
}
If there is currency based issue:
{
"name": "UNPROCESSABLE_ENTITY",
"details": [
{
"location": "body",
"issue": "CURRENCY_NOT_SUPPORTED",
"description": "Currency code is not currently supported. Please refer https://developer.paypal.com/docs/integration/direct/rest/currency-codes/ for list of supported currency codes."
}
],
"message": "The requested action could not be performed, semantically incorrect, or failed business validation.",
"debug_id": "d666b5e5eb0c0",
"links": [
{
"href": "https://developer.paypal.com/docs/api/orders/v2/#error-CURRENCY_NOT_SUPPORTED",
"rel": "information_link",
"method": "GET"
}
]
}
If your order is successfully captured with status as COMPLETED:
{
"id": "8G0042477K865063U",
"status": "COMPLETED",
"purchase_units": [
{
"reference_id": "default",
"shipping": {
"name": {
"full_name": "John Doe"
},
"address": {
"address_line_1": "10, east street",
"address_line_2": "first building",
"admin_area_2": "Mumbai",
"admin_area_1": "Maharashtra",
"postal_code": "400029",
"country_code": "NZ"
}
},
"payments": {
"captures": [
{
"id": "4K670967VH2547504",
"status": "PENDING",
"status_details": {
"reason": "RECEIVING_PREFERENCE_MANDATES_MANUAL_ACTION"
},
"amount": {
"currency_code": "NZD",
"value": "170.00"
},
"final_capture": true,
"seller_protection": {
"status": "ELIGIBLE",
"dispute_categories": [
"ITEM_NOT_RECEIVED",
"UNAUTHORIZED_TRANSACTION"
]
},
"invoice_id": "INV-1234567888",
"links": [
{
"href": "https://api.sandbox.paypal.com/v2/payments/captures/4K670967VH2547504",
"rel": "self",
"method": "GET"
},
{
"href": "https://api.sandbox.paypal.com/v2/payments/captures/4K670967VH2547504/refund",
"rel": "refund",
"method": "POST"
},
{
"href": "https://api.sandbox.paypal.com/v2/checkout/orders/8G0042477K865063U",
"rel": "up",
"method": "GET"
}
],
"create_time": "2020-10-31T13:35:58Z",
"update_time": "2020-10-31T13:35:58Z"
}
]
}
}
],
"payer": {
"name": {
"given_name": "Sumit",
"surname": "Shukla"
},
"email_address": "testg32#gmail.com",
"payer_id": "VW87TYSM2GMZ4",
"address": {
"address_line_1": "10, east street",
"admin_area_2": "Mumbai",
"admin_area_1": "Maharashtra",
"postal_code": "400029",
"country_code": "NZ"
}
},
"links": [
{
"href": "https://api.sandbox.paypal.com/v2/checkout/orders/8G0042477K865063U",
"rel": "self",
"method": "GET"
}
]
}
After that you can redirect user to thank you page and update mobile app screen based on database values.
I have a complex JSON which I need to parse and store the information that are inside the JSON data into Room database. Is it possible to store the whole JSON in Room? If that is possible I am up for that too. I am using Kotlin. Can anyone guide me to it?
Here's my JSON sample:
{
"chapters": [
{
"play": "manual",
"items": [
{
"image": "/path/to/image",
"background": "#ffffff",
"text": "text to show on image",
"points": 1,
"voice": {
"type": "tts"
},
"words": {
"type": "wav",
"file": "/path to words file",
"parts": [
{
"word": "text",
"from": 122,
"to": 116
}
]
}
},
{
"image": "/path/to/image",
"background": "#ffffff",
"text": "text to show on image",
"points": 1,
"voice": {
"type": "wav",
"file": "/path/to/file"
}
}
]
}
]
}
I have a class which is part of a school and this class has teachers and students, all of them has name and maybe has phone number , I want to get the full data for the classes
but firstly, could you advice me, what is the best for performance and maintaining from the following Dbs :
1st one
"schools":{
"school1":{
"class1":{
"name":"SC1",
"teachers":[{
"name":"T1"
}, {
"name":"T2"
}],
"students":[
{"name":"S1"},
{"name":"S2"}
]
}
}
.
.
.
.
.
.
.
}
and the 2nd
"school":{
"school1":{
"name":"SC1"
},
"school2":{
"name":"SC2"
}
},
"classes": {
"class1": {
"name": "C1"
},
"class2": {
"name": "C2"
}
},
"students": {
"student1": {
"name": "S1",
"phone":"123456789"
},
"student2": {
"name": "S2",
"phone":"123456789"
},
"student3": {
"name": "S3",
"phone":"123456789"
},
"student4": {
"name": "S4",
"phone":"123456789"
}
},
"teachers": {
"student1": {
"name": "T1",
"phone":"123456789"
},
"student2": {
"name": "T2",
"phone":"123456789"
},
"student3": {
"name": "T3",
"phone":"123456789"
},
"student4": {
"name": "T4",
"phone":"123456789"
}
},
"classes_enrollments": {
"class1": {
"teacher1": true,
"teacher3": true,
"student1": true,
"student2": true
},
"class2": {
"teacher2": true,
"teacher4": true,
"student3": true,
"student4": true
},
"class3": {
"teacher1": true,
"teacher2": true,
"student3": true,
"student4": true,
"student1": true,
"student2": true
}
},
"student_friends": {
"student1": {
"student2": true
},
"students2": {
"student1": true,
"student3": true
},
"students3": {
"student2": true
}
},
"teacher_friends": {
"teacher1": {
"teacher2": true
},
"teacher2": {
"teacher1": true,
"teacher3": true
},
"teacher3": {
"teacher2": true
}
}
and for the 2nd way how to get the full data for the class1: in which school and it's name and count of teachers and students and their names and phones
Thank you
I would mix those two.
For code simplicity and reading performance of individual class details, the 2nd scheme would indeed be messy. The 1st scheme would be better, but with some improvements.
Keep the teachers and students paths at root, just like in the 2nd scheme.
Add teacher_enrollments and student_enrollments path at root, to save the ids of the classes that each teacher/student is associated with.
Don't save class teachers and students as arrays inside classes, but use maps instead, similar to what you're saving in the root teachers and students path.
That way, when you edit a teacher from the root path, you can also get a list of all their associated classes (the ids) from the enrollments path, and do a multi-path update for these classes, to update the teacher/student details in each associated class.
If you have lots of data, you might want to maintain a separate path for class summaries, so that you can easily show a list of classes, without having to download the data for all included teachers and students (which would be present multiple times in all these classes).
When you delete a class, you would also want to do a multi-path update to delete all associated enrollments. If the total number of students and teachers is not too big, you can just delete the enrollments for ALL teacheres/students. If you have lots of teachers/students, you could keep your classes_enrollments path (but with intermediate teachers and students before the ids), so that you can make an update with only the required teacher/student ids. (it's actually a lot simpler. You already have the teacher/student IDs in the class info)
// How to delete a class in JavaScript.
// For Java, use updateChildren() instead of update(),
// and supply it with a HashMap instead of a plain object.
const classToDelete = { id: 'class1', teachers: ..., students: ..., school: ... };
const updateObject = {
['classes/'+classToDelete.id]: null },
['schools/'+classToDelete.school.id+'/classes/'+classToDelete.id]: null },
};
Object.keys(classToDelete.teachers).forEach(teacherId => {
updateObject['teachers/'+teacherId +'/classes/'+classToDelete.id] = null;
});
Object.keys(classToDelete.students).forEach(studentId=> {
updateObject['students/'+studentId+'/classes/'+classToDelete.id] = null;
});
dbRef.update(updateObject);
Example database structure (slightly different than instructed, but using the same concepts):
"schools": {
"school1": {
"id": "school1",
"name": "The best school",
"classes": {
"class1": {
"id": "class1",
"name": "The best class"
}
}
}
},
"classes": {
"class1": {
"id": "class1",
"name": "The best class",
"teachers": {
"teacher1": {
"id": "teacher1",
"name": "The best teacher",
"phone": "123"
}
},
"students": {
"student1": {
"id": "student1",
"name": "The best student",
"phone": "456"
}
},
"school": {
"id": "school1",
"name": "The best school"
}
}
},
"teachers": {
"teacher1": {
"id": "teacher1",
"name": "The best teacher",
"phone": "123",
"classes": {
"class1": {
"name": "The best class",
"school": {
"id": "school1",
"name": "The best school"
}
}
}
}
},
"students": {
"student1": {
"id": "student1",
"name": "The best student",
"phone": "456",
"classes": {
"class1": {
"name": "The best class",
"school": {
"id": "school1",
"name": "The best school"
}
}
}
}
}
Good luck!
I am using Firebase Database for my mobile and web application. I wanted to get advise for how to structure the JSON Tree. I have the following use case in mind:
Mobile app user logs in and gets all nearby restaurants in a list. User sets order on one restaurant. The restaurant owner uses web or mobile application to see incoming orders and accepts them. After accepting the order, the mobile app user gets response that his order has been accepted. Now my idea for the structure was the following:
SO we have one node at top level for each restaurant and each restaurant node contains a requests node which saves all the requests for this restaurants.
Is that structure ok or could this be structured better?
Consider a data structure like this, you don't want to retrieve all the request when you get a restaurant and this way, you can get all the requests for a restaurant and all the requests from a particular user.
{
"requests": {
"req1": {
"status": 0,
"time": 1473593287,
"user": { "u2": true }
},
"req2": {
"status": 0,
"time": 1473593227,
"user": { "u1": true }
},
"req3": {
"status": 0,
"time": 1473594287,
"user": { "u1": true }
},
"req4": {
"status": 0,
"time": 1473594227,
"user": { "u2": true }
},
},
"restaurant-requests": {
"resA": {
"req1": true,
"req2": true
},
"resB": {
"req3": true,
"req4": true
}
},
"restaurants": {
"resA": {
"name": "Example Restaurant A",
"address": "1 Example Street"
},
"resB": {
"name": "Example Restaurant B",
"address": "2 Example Street"
}
},
"user-requests": {
"u1": {
"req2": true,
"req3": true
},
"u2": {
"req1": true,
"req4": true
}
},
"users": {
"u1": {
"address": "123 Example Street"
},
"u2": {
"address": "124 Example Street"
},
},
}
That's what I would do..
good luck!