|
| 1 | +package com.gravatar.app.homeUi.presentation.home.share.model |
| 2 | + |
| 3 | +internal class VCard private constructor( |
| 4 | + val firstName: String? = null, |
| 5 | + val lastName: String? = null, |
| 6 | + val nickname: String? = null, |
| 7 | + val organization: String? = null, |
| 8 | + val title: String? = null, |
| 9 | + val profileUrl: String? = null, |
| 10 | + val note: String? = null, |
| 11 | + val phoneNumber: String? = null, |
| 12 | + val email: String? = null, |
| 13 | +) { |
| 14 | + |
| 15 | + override fun toString(): String { |
| 16 | + val contentBuilder = StringBuilder().append("BEGIN:VCARD\n") |
| 17 | + .append("VERSION:3.0\n") |
| 18 | + .append("PRODID:Gravatar Android\n") |
| 19 | + |
| 20 | + val firstName = firstName.orEmpty() |
| 21 | + val lastName = lastName.orEmpty() |
| 22 | + val nickname = nickname.orEmpty() |
| 23 | + if (firstName.isNotEmpty() || lastName.isNotEmpty()) { |
| 24 | + contentBuilder.append("N:$lastName;$firstName;;;\n") |
| 25 | + .append("FN:${("$firstName $lastName".trim()).ifEmpty { nickname }}\n") |
| 26 | + } |
| 27 | + val calculatedNickname = nickname.ifEmpty { "$firstName $lastName".trim() } |
| 28 | + |
| 29 | + calculatedNickname.takeIf { it.isNotEmpty() }?.let { contentBuilder.append("NICKNAME:$it\n") } |
| 30 | + organization?.takeIf { it.isNotEmpty() }?.let { contentBuilder.append("ORG:$it\n") } |
| 31 | + title?.takeIf { it.isNotEmpty() }?.let { contentBuilder.append("TITLE:$it\n") } |
| 32 | + profileUrl?.takeIf { it.isNotEmpty() }?.let { contentBuilder.append("URL:$it\n") } |
| 33 | + note?.takeIf { it.isNotEmpty() }?.let { contentBuilder.append("NOTE:$it\n") } |
| 34 | + phoneNumber?.takeIf { it.isNotEmpty() }?.let { contentBuilder.append("TEL;TYPE=cell:$it\n") } |
| 35 | + email?.takeIf { it.isNotEmpty() }?.let { contentBuilder.append("EMAIL:$it\n") } |
| 36 | + |
| 37 | + contentBuilder.append("END:VCARD") |
| 38 | + return contentBuilder.toString() |
| 39 | + } |
| 40 | + |
| 41 | + class Builder( |
| 42 | + private var firstName: String? = null, |
| 43 | + private var lastName: String? = null, |
| 44 | + private var nickname: String? = null, |
| 45 | + private var organization: String? = null, |
| 46 | + private var title: String? = null, |
| 47 | + private var profileUrl: String? = null, |
| 48 | + private var note: String? = null, |
| 49 | + private var phoneNumber: String? = null, |
| 50 | + private var email: String? = null, |
| 51 | + ) { |
| 52 | + fun firstName(firstName: String?) = apply { this.firstName = firstName } |
| 53 | + fun lastName(lastName: String?) = apply { this.lastName = lastName } |
| 54 | + fun nickname(nickname: String?) = apply { this.nickname = nickname } |
| 55 | + fun organization(organization: String?) = apply { this.organization = organization } |
| 56 | + fun title(title: String?) = apply { this.title = title } |
| 57 | + fun profileUrl(url: String?) = apply { this.profileUrl = url } |
| 58 | + fun note(description: String?) = apply { this.note = description } |
| 59 | + fun phoneNumber(phone: String?) = apply { this.phoneNumber = phone } |
| 60 | + fun email(email: String?) = apply { this.email = email } |
| 61 | + |
| 62 | + fun build() = VCard( |
| 63 | + firstName = firstName, |
| 64 | + lastName = lastName, |
| 65 | + nickname = nickname, |
| 66 | + organization = organization, |
| 67 | + title = title, |
| 68 | + profileUrl = profileUrl, |
| 69 | + note = note, |
| 70 | + phoneNumber = phoneNumber, |
| 71 | + email = email |
| 72 | + ) |
| 73 | + } |
| 74 | +} |
0 commit comments