|
| 1 | +package fr.free.nrw.commons.campaigns; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.content.Intent; |
| 5 | +import android.net.Uri; |
| 6 | +import android.support.annotation.NonNull; |
| 7 | +import android.support.annotation.Nullable; |
| 8 | +import android.util.AttributeSet; |
| 9 | +import android.view.View; |
| 10 | +import android.widget.TextView; |
| 11 | +import butterknife.BindView; |
| 12 | +import butterknife.ButterKnife; |
| 13 | +import fr.free.nrw.commons.R; |
| 14 | +import fr.free.nrw.commons.contributions.MainActivity; |
| 15 | +import fr.free.nrw.commons.utils.SwipableCardView; |
| 16 | +import fr.free.nrw.commons.utils.ViewUtil; |
| 17 | +import java.text.ParseException; |
| 18 | +import java.text.SimpleDateFormat; |
| 19 | +import java.util.Date; |
| 20 | + |
| 21 | +/** |
| 22 | + * A view which represents a single campaign |
| 23 | + */ |
| 24 | +public class CampaignView extends SwipableCardView { |
| 25 | + Campaign campaign = null; |
| 26 | + private ViewHolder viewHolder; |
| 27 | + |
| 28 | + public CampaignView(@NonNull Context context) { |
| 29 | + super(context); |
| 30 | + init(); |
| 31 | + } |
| 32 | + |
| 33 | + public CampaignView(@NonNull Context context, @Nullable AttributeSet attrs) { |
| 34 | + super(context, attrs); |
| 35 | + init(); |
| 36 | + } |
| 37 | + |
| 38 | + public CampaignView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { |
| 39 | + super(context, attrs, defStyleAttr); |
| 40 | + init(); |
| 41 | + } |
| 42 | + |
| 43 | + public void setCampaign(Campaign campaign) { |
| 44 | + this.campaign = campaign; |
| 45 | + if (campaign != null) { |
| 46 | + this.setVisibility(View.VISIBLE); |
| 47 | + viewHolder.init(); |
| 48 | + } else { |
| 49 | + this.setVisibility(View.GONE); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + @Override public boolean onSwipe(View view) { |
| 54 | + view.setVisibility(View.GONE); |
| 55 | + ((MainActivity) getContext()).prefs.edit() |
| 56 | + .putBoolean("displayCampaignsCardView", false) |
| 57 | + .apply(); |
| 58 | + ViewUtil.showLongToast(getContext(), |
| 59 | + getResources().getString(R.string.nearby_campaign_dismiss_message)); |
| 60 | + return true; |
| 61 | + } |
| 62 | + |
| 63 | + private void init() { |
| 64 | + View rootView = inflate(getContext(), R.layout.layout_campagin, this); |
| 65 | + viewHolder = new ViewHolder(rootView); |
| 66 | + setOnClickListener(view -> { |
| 67 | + if (campaign != null) { |
| 68 | + showCampaignInBrowser(campaign.getLink()); |
| 69 | + } |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * open the url associated with the campaign in the system's default browser |
| 75 | + */ |
| 76 | + private void showCampaignInBrowser(String link) { |
| 77 | + Intent view = new Intent(); |
| 78 | + view.setAction(Intent.ACTION_VIEW); |
| 79 | + view.setData(Uri.parse(link)); |
| 80 | + getContext().startActivity(view); |
| 81 | + } |
| 82 | + |
| 83 | + public class ViewHolder { |
| 84 | + |
| 85 | + @BindView(R.id.tv_title) TextView tvTitle; |
| 86 | + @BindView(R.id.tv_description) TextView tvDescription; |
| 87 | + @BindView(R.id.tv_dates) TextView tvDates; |
| 88 | + |
| 89 | + public ViewHolder(View itemView) { |
| 90 | + ButterKnife.bind(this, itemView); |
| 91 | + } |
| 92 | + |
| 93 | + public void init() { |
| 94 | + if (campaign != null) { |
| 95 | + tvTitle.setText(campaign.getTitle()); |
| 96 | + tvDescription.setText(campaign.getDescription()); |
| 97 | + SimpleDateFormat inputDateFormat = new SimpleDateFormat("yyyy-MM-dd"); |
| 98 | + SimpleDateFormat outputDateFormat = new SimpleDateFormat("dd MMM"); |
| 99 | + try { |
| 100 | + Date startDate = inputDateFormat.parse(campaign.getStartDate()); |
| 101 | + Date endDate = inputDateFormat.parse(campaign.getEndDate()); |
| 102 | + tvDates.setText(String.format("%1s - %2s", outputDateFormat.format(startDate), |
| 103 | + outputDateFormat.format(endDate))); |
| 104 | + } catch (ParseException e) { |
| 105 | + e.printStackTrace(); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments