|
| 1 | +package fr.free.nrw.commons.campaigns |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.net.Uri |
| 5 | +import android.util.AttributeSet |
| 6 | +import android.view.LayoutInflater |
| 7 | +import android.view.View |
| 8 | +import androidx.core.content.ContextCompat |
| 9 | +import fr.free.nrw.commons.R |
| 10 | +import fr.free.nrw.commons.Utils |
| 11 | +import fr.free.nrw.commons.campaigns.models.Campaign |
| 12 | +import fr.free.nrw.commons.contributions.MainActivity |
| 13 | +import fr.free.nrw.commons.databinding.LayoutCampaginBinding |
| 14 | +import fr.free.nrw.commons.theme.BaseActivity |
| 15 | +import fr.free.nrw.commons.utils.CommonsDateUtil.getIso8601DateFormatShort |
| 16 | +import fr.free.nrw.commons.utils.DateUtil.getExtraShortDateString |
| 17 | +import fr.free.nrw.commons.utils.SwipableCardView |
| 18 | +import fr.free.nrw.commons.utils.ViewUtil.showLongToast |
| 19 | +import timber.log.Timber |
| 20 | +import java.text.ParseException |
| 21 | + |
| 22 | +/** |
| 23 | + * A view which represents a single campaign |
| 24 | + */ |
| 25 | +class CampaignView : SwipableCardView { |
| 26 | + private var campaign: Campaign? = null |
| 27 | + private var binding: LayoutCampaginBinding? = null |
| 28 | + private var viewHolder: ViewHolder? = null |
| 29 | + private var campaignPreference = CAMPAIGNS_DEFAULT_PREFERENCE |
| 30 | + |
| 31 | + constructor(context: Context) : super(context) { |
| 32 | + init() |
| 33 | + } |
| 34 | + |
| 35 | + constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) { |
| 36 | + init() |
| 37 | + } |
| 38 | + |
| 39 | + constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super( |
| 40 | + context, attrs, defStyleAttr) { |
| 41 | + init() |
| 42 | + } |
| 43 | + |
| 44 | + fun setCampaign(campaign: Campaign?) { |
| 45 | + this.campaign = campaign |
| 46 | + if (campaign != null) { |
| 47 | + if (campaign.isWLMCampaign) { |
| 48 | + campaignPreference = WLM_CARD_PREFERENCE |
| 49 | + } |
| 50 | + visibility = VISIBLE |
| 51 | + viewHolder!!.init() |
| 52 | + } else { |
| 53 | + visibility = GONE |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + override fun onSwipe(view: View): Boolean { |
| 58 | + view.visibility = GONE |
| 59 | + (context as BaseActivity).defaultKvStore.putBoolean(CAMPAIGNS_DEFAULT_PREFERENCE, false) |
| 60 | + showLongToast( |
| 61 | + context, |
| 62 | + resources.getString(R.string.nearby_campaign_dismiss_message) |
| 63 | + ) |
| 64 | + return true |
| 65 | + } |
| 66 | + |
| 67 | + private fun init() { |
| 68 | + binding = LayoutCampaginBinding.inflate( |
| 69 | + LayoutInflater.from(context), this, true |
| 70 | + ) |
| 71 | + viewHolder = ViewHolder() |
| 72 | + setOnClickListener { |
| 73 | + campaign?.let { |
| 74 | + if (it.isWLMCampaign) { |
| 75 | + ((context) as MainActivity).showNearby() |
| 76 | + } else { |
| 77 | + Utils.handleWebUrl(context, Uri.parse(it.link)) |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + inner class ViewHolder { |
| 84 | + fun init() { |
| 85 | + if (campaign != null) { |
| 86 | + binding!!.ivCampaign.setImageDrawable( |
| 87 | + ContextCompat.getDrawable(binding!!.root.context, R.drawable.ic_campaign) |
| 88 | + ) |
| 89 | + binding!!.tvTitle.text = campaign!!.title |
| 90 | + binding!!.tvDescription.text = campaign!!.description |
| 91 | + try { |
| 92 | + if (campaign!!.isWLMCampaign) { |
| 93 | + binding!!.tvDates.text = String.format( |
| 94 | + "%1s - %2s", campaign!!.startDate, |
| 95 | + campaign!!.endDate |
| 96 | + ) |
| 97 | + } else { |
| 98 | + val startDate = getIso8601DateFormatShort().parse( |
| 99 | + campaign?.startDate |
| 100 | + ) |
| 101 | + val endDate = getIso8601DateFormatShort().parse( |
| 102 | + campaign?.endDate |
| 103 | + ) |
| 104 | + binding!!.tvDates.text = String.format( |
| 105 | + "%1s - %2s", getExtraShortDateString( |
| 106 | + startDate!! |
| 107 | + ), getExtraShortDateString(endDate!!) |
| 108 | + ) |
| 109 | + } |
| 110 | + } catch (e: ParseException) { |
| 111 | + Timber.e(e) |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + companion object { |
| 118 | + const val CAMPAIGNS_DEFAULT_PREFERENCE: String = "displayCampaignsCardView" |
| 119 | + const val WLM_CARD_PREFERENCE: String = "displayWLMCardView" |
| 120 | + } |
| 121 | +} |
0 commit comments