How to create a pdf from data in android? - android

I'm building a resume builder. I've made everything else that I'm not able to write the resume data to a pdf and give it to the use.
I have a class user and an object constants that contains data.
data class User(
var id : String? = null,
var email : String? = null,
var profileHeadline : String? = "",
var profileSummary : String? = "",
var profilePhoto : String? = "",
var personalInfo: PersonalInfo? = PersonalInfo(),
var education: MutableList<Education>? = null,
var experience: MutableList<Experience>? = null,
var skills : MutableList<Skills>? = null,
var languages: MutableList<Languages>? = null,
var interest : MutableList<Interest>? = null,
var awardAndAchievements: MutableList<AwardAndAchievements>? = null,
var licenceAndCertification: MutableList<LicenceAndCertification>? = null,
var projects : MutableList<Projects>? = null,
var publications: MutableList<Publications>? = null,
var patents : MutableList<Patents>? = null,
var volunteerExperience : MutableList<VolunteerExperience>? = null,
var recommendations: MutableList<Recommendations>? = null
) : Parcelable
I want to create a well structured pdf with the data. A well structured pdf means Think about your own resume.
I know some about canvas and pdfdocument in kotlin - Android and also about creating pdfs with activities.
But i don't know which approach should I use and how to make it happen.
Please help!
It will be so nice of you if you can give me an example by creating some resume pdf.

Related

Firebase Realtime Database has a different data key from the Kotlin data class [duplicate]

This question already has answers here:
Drop "is" prefix for Firebase Firestore fields for Boolean Values
(2 answers)
Prevent firebase from taking the method name as variable in Firebase
(2 answers)
Closed 5 months ago.
I have multiple data classes and all upload fine to Firebase Realtime database except for this one key "isCompleted" gets changed to "completed" in the database.
My data class:
data class MaintenanceLog(
val isCompleted: Boolean? = null,
val brand: String? = null,
val clientCode: String? = null,
val dateOfSubmission: Double? = null,
val details: String? = null,
val equipmentID: String? = null,
val id: String? = null,
val model: String? = null,
val name: String? = null,
val photosArray: List<String>? = null,
val refNum: String? = null,
val restaurantID: String? = null,
var hidden: Boolean? = null,
val userSelectedDate: Double? = null,
val wasFutureMaintCreated: Boolean? = null,
val workLogRef: String? = null,
val contractorCode: String? = null,
val isCalendarEvent: Boolean? = null,
val calendarEvent_venueName: String? = null,
val createdBy: String? = null
)
Implementation:
val demoLog = MaintenanceLog(
isCompleted = true,
id = "DemoLog22222",
equipmentID = "demo_equipID_123456",
refNum = "DemoRefNum_123456",
dateOfSubmission = APSDate.getCurrentDatabaseFormattedTime(),
details = "Maintenance Log created from Work Log - Demo Description",
photosArray = null,
workLogRef = "DemoRefNum_123456",
createdBy = "Demo User",
clientCode = "Demo Client",
restaurantID = "Demo Restaurant",
brand = "Demo Brand",
model = "Demo Model",
name = "Demo User",
userSelectedDate = APSDate.dateFromComponents(timeIn_Year, timeIn_Month, timeIn_Day, timeIn_Hour, timeIn_Minute),
wasFutureMaintCreated = false)
dbRef.child(FirebaseLocations.MAINTENANCE_RECORDS.code).child("111_demo_path").setValue(demoLog)
The demo log when setting the value:
And the firebase value:
Why is "isCompleted" getting changed to "completed" when uploaded to the database?

How to parse this specific json object to display driver name and lastname on my ui?

I am trying to parse the following api endpoint.
http://ergast.com/api/f1/current/driverStandings.json
I'm confused about the structure of the json response. specificaly i cant find a way to display driver's name and last name in a recycler view.
i've managed to display data from another json file of the same endpoint, but not from this one.
spent 2-3 days researching and googling.
There is a Plugin in Android Studio called JSON to Kotlin Class. You just have to copy the JSON reponse and paste it there in the plugin and it will generate the necessary classes for you, so you can visualize it more clearly. So what you would end up is:
data class ExampleJson2KtKotlin (
#SerializedName("MRData" ) var MRData : MRData? = MRData()
)
data class MRData (
#SerializedName("xmlns" ) var xmlns : String? = null,
#SerializedName("series" ) var series : String? = null,
#SerializedName("url" ) var url : String? = null,
#SerializedName("limit" ) var limit : String? = null,
#SerializedName("offset" ) var offset : String? = null,
#SerializedName("total" ) var total : String? = null,
#SerializedName("StandingsTable" ) var StandingsTable : StandingsTable? = StandingsTable()
)
data class StandingsTable (
#SerializedName("season" ) var season : String? = null,
#SerializedName("StandingsLists" ) var StandingsLists : ArrayList<StandingsLists> = arrayListOf()
)
data class StandingsLists (
#SerializedName("season" ) var season : String? = null,
#SerializedName("round" ) var round : String? = null,
#SerializedName("DriverStandings" ) var DriverStandings : ArrayList<DriverStandings> = arrayListOf()
)
data class DriverStandings (
#SerializedName("position" ) var position : String? = null,
#SerializedName("positionText" ) var positionText : String? = null,
#SerializedName("points" ) var points : String? = null,
#SerializedName("wins" ) var wins : String? = null,
#SerializedName("Driver" ) var Driver : Driver? = Driver(),
#SerializedName("Constructors" ) var Constructors : ArrayList<Constructors> = arrayListOf()
)
data class Driver (
#SerializedName("driverId" ) var driverId : String? = null,
#SerializedName("permanentNumber" ) var permanentNumber : String? = null,
#SerializedName("code" ) var code : String? = null,
#SerializedName("url" ) var url : String? = null,
#SerializedName("givenName" ) var givenName : String? = null,
#SerializedName("familyName" ) var familyName : String? = null,
#SerializedName("dateOfBirth" ) var dateOfBirth : String? = null,
#SerializedName("nationality" ) var nationality : String? = null
)
data class Constructors (
#SerializedName("constructorId" ) var constructorId : String? = null,
#SerializedName("url" ) var url : String? = null,
#SerializedName("name" ) var name : String? = null,
#SerializedName("nationality" ) var nationality : String? = null
)
And then your response will become like this if you use retrofit
interface RetrofitInterface {
#GET("example-endpoint")
suspend fun getDataList(): MRData
}
Then you just collect the data in your ViewModel and display it in the RecycleView.
The basic parsing can be achieved by this way.
val mrDataJsonObj = response.getJSONObject("MRData")
val standingTableObj = mrDataJsonObj.getJSONObject("StandingsTable")
val standingListJsonArray = standingTableObj.getJSONArray("StandingsLists")
for (i in 0 until standingListJsonArray.length()) {
val seasonObj = standingListJsonArray.get(i) as JSONObject
val driverStandingsArray = seasonObj.getJSONArray("DriverStandings")
for ( j in 0 until driverStandingsArray.length()){
val driverStandingObj = driverStandingsArray.get(j) as JSONObject
val driverObj = driverStandingObj.getJSONObject("Driver")
// Get All the Driver Attributes from driverObj
if(driverObj.has("givenName"))
Log.d(TAG, "dummyData: Given Name : " +driverObj.getString("givenName"))
if(driverObj.has("familyName"))
Log.d(TAG, "dummyData: Family Name " +driverObj.getString("familyName"))
}
}

Firebase RealtimeDatabase retrieve snapshot object exception

This is my model class
#Parcel
data class ClientModel(
var name: String? = "",
var phone: String? = "",
var princpalAddresse: String? = "",
var homeAddresse: String? = "",
var travailleAddresse: String? = "",
var email: String? = "",
var userToken: String? = "",
var principalAddresseCoords: Pair<Double, Double>? = null,
var homeAddresseCoords: Pair<Double, Double>?= null,
var workAddresseCoords: Pair<Double, Double>? = null,
)
My proGuard file keep the class :
-keep class com.olivier.oplivre.models.ClientModel
But! when I try to get the snapshot with a singleValueEventListener I got exception because of the Pair<Double,Double> variables
val utilisationInfo = snapshot.getValue(ClientModel::class.java) //todo CRASH
Exception :
com.google.firebase.database.DatabaseException: Class kotlin.Pair does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped.
Database Structure :
I think firebase Realtime database treat your principalAddresseCoords as a list of long so in your ClientModel change the value of principalAddresseCoords to emptyList() and the type List
As #Sami Shorman said , firebase took my Pair instance and transform it but not as list, as Hashmap<,> ! so I changed my class model like that :
var principalAddresseCoords: HashMap<String,Double>? = null,
var homeAddresseCoords: HashMap<String,Double >? = null,
var workAddresseCoords: HashMap<String,Double >? = null,
To put the data as Hashmap I just had to do :
clientModel.workAddresseCoords = HashMap<String,Double>().apply {
put("lat",lat)
put("long",long)
}

Customise single data class without creating multiple data classes in Kotlin

I have a scenario. I have created a data class in Kotlin like this:
data class AgentDetails(
val mobileNo: String,
val Name: String,
val Email: String,
val Password: String,
val Occupation: String,
val pincode: String,
val usertype: String,
val profilepic: String,
val AccountStatus: String
)
I want to send different type of objects of this data class to a web service:
1st object example:
val agentDetails = AgentDetails(mobileNo = mobileNumberText.text.toString(),
Name = userNameText.text.toString(),
Email = emailIdText.text.toString(),
Password = HashUtils.sha1(passwordText.text.toString()),
Occupation = item,
pincode = pinCodeText.text.toString(),
usertype = "Agent",
profilepic = "null", AccountStatus = "pending")
In 2nd object I only want to send mobile number. I dont wanna include any other field. Something like this:
val agentDetails = AgentDetails(mobileNo = mobileNumberText.text.toString())
And in 3rd object I only wanna send email id. Instead of creating multiple data classes. Can I use the same data class for multiple implementations?
Personally, I'd define three objects because they represent three different concepts (or projections of a concept). But if you make your properties nullable and provide a default value of null, you can get away with creating them as you want...
data class AgentDetails(
val mobileNo: String? = null,
val name: String? = null,
val email: String? = null,
val password: String? = null,
val occupation: String? = null,
val pincode: String? = null,
val usertype: String? = null,
val profilepic: String? = null,
val accountStatus: String? = null
)
Note: I've changed some of your property names to camelCase, as is the proper convention. And these all work fine:
AgentDetails(mobileNo = mobileNumberText.text.toString())
AgentDetails(email = "foo#example.com")
AgentDetails(name = "Foo", password = "Bar")
All of the other fields not provided will be null, and the types will be nullable, so you'll have to guard against that. Otherwise, I'd define three data classes for this.
Another solution would be to consider a sealed class structure:
sealed class AgentDetails
data class AgentByName(val name: String) : AgentDetails()
data class AgentByEmail(val email: String): AgentDetails()
// etc..
And then use it in a when expression:
fun doSomethingWithAgents(agentDetails: AgentDetails) {
when (agentDetails) {
is AgentByName -> // Do something
is AgentByEmail -> // Do Something
}
}
The easiest way is to make the fields nullable and provide default values:
data class AgentDetails(
val mobileNo: String? = null,
val Name: String? = null,
val Email: String? = null,
val Password: String? = null,
val Occupation: String? = null,
val pincode: String? = null,
val usertype: String? = null,
val profilepic: String? = null,
val AccountStatus: String? = null
)

Can I using one Model for two entities in Room persistence?

I would like to know that, is it possible to use one model for two entities while using Room Persistence in Kotlin?
For example, I have to create two pagers which are general page and star page.
Both of them have fields following..
id: String?, caption: String?, imageUrl:String?, isUserLike: Boolean
(And more)
Data came from two separate services and I want to save it separately.
How can I do this, or there have any ways to solve this problem?
Thank you
P.S. I'm the beginner of Kotlin and Room persistence.
You can create a separate abstract class that contains all those common fields
abstract class BaseModel(
#PrimaryKey
var id: String? = null,
var caption: String? = null,
var imageUrl: String? = null,
...
) {
...
}
then have that abstract class be extended on the other classes
#Entity(tableName = "a_model")
data class AModel (
mId: String? = null,
mCaption: String? = null,
mImageUrl: String? = null,
...
): BaseModel(id = mId,
caption = mCaption,
imageUrl = mImageUrl,
...) {
...
}
you can also add other fields that are exclusive to that entity
#Entity(tableName = "b_model")
data class BModel (
mId: String? = null,
mCaption: String? = null,
mImageUrl: String? = null,
...
var nonCommonField: String? = null, // like this
): BaseModel(id = mId,
caption = mCaption,
imageUrl = mImageUrl,
...) {
...
}
Android Room will require fields to have a default value for each field or an empty constructor, if I remember correctly.

Categories

Resources