Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #545 - Add support for campaigns #4423

Merged
merged 21 commits into from
Aug 18, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
4b1722f
Integrate WLM
ashishkumar468 Jul 8, 2021
2f212e4
BugFix in Monuments
ashishkumar468 Jul 18, 2021
eaa7d12
More bug fixes
ashishkumar468 Jul 24, 2021
7aff95b
Use lowercase country code in WLM uploads
ashishkumar468 Aug 1, 2021
cee1006
Bug-Fix, WLM Campaign Icon
ashishkumar468 Aug 3, 2021
6171f5f
1. Updated monuments query to use any of the following properties for…
ashishkumar468 Aug 9, 2021
1a7c760
Updated WLM Banner String, Handle NPE in contributions callback
ashishkumar468 Aug 14, 2021
bf753ca
Added nearby-monuments query log lines
ashishkumar468 Aug 14, 2021
b3dea30
Handle WLM Query exception : - if an exception is thrown in WLM query…
ashishkumar468 Aug 15, 2021
2821d80
Fix BookmarkLocationDaoTest
ashishkumar468 Aug 15, 2021
2428d0f
Added Column Address in BookmarkLocationDaoTest
ashishkumar468 Aug 15, 2021
84561dc
Use fallback description as usual nearby pins even for WLM pins, inst…
ashishkumar468 Aug 17, 2021
e919590
Test fix in BookmarkLocationDao
ashishkumar468 Aug 17, 2021
35af80b
Updated template for WLM, removed redundant feilds
ashishkumar468 Aug 17, 2021
49e8e12
Fixed WLM template
ashishkumar468 Aug 17, 2021
bf603f7
Removed categories from WLM template
ashishkumar468 Aug 17, 2021
530bd46
Fixed BookmarkControllerTest
ashishkumar468 Aug 17, 2021
ed82456
Fixed BookmarkLocationFragmentUnitTest
ashishkumar468 Aug 17, 2021
c98d746
fix ModelFunctions
ashishkumar468 Aug 17, 2021
6dfbfef
Fixed BookmarksDaoLocationTest
ashishkumar468 Aug 17, 2021
f200539
Fixed WLM template
ashishkumar468 Aug 18, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions app/src/main/java/fr/free/nrw/commons/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import androidx.browser.customtabs.CustomTabsIntent;
import androidx.core.content.ContextCompat;

import fr.free.nrw.commons.kvstore.JsonKvStore;
import java.util.Date;
import org.wikipedia.dataclient.WikiSite;
import org.wikipedia.page.PageTitle;

Expand All @@ -29,6 +31,7 @@
import timber.log.Timber;

import static android.widget.Toast.LENGTH_SHORT;
import static fr.free.nrw.commons.campaigns.CampaignView.CAMPAIGNS_DEFAULT_PREFERENCE;

public class Utils {

Expand Down Expand Up @@ -206,4 +209,35 @@ public static void setUnderlinedText(TextView textView, int stringResourceName,
textView.setText(content);
}

/**
* For now we are enabling the monuments only when the date lies between 1 Sept & 31 OCt
* @param date
* @return
*/
public static boolean isMonumentsEnabled(final Date date, final JsonKvStore store){
if(date.getDay()>=1 && date.getMonth()>=9 && date.getDay()<=31 && date.getMonth()<=10 ){
return true;
}

return store.getBoolean(CAMPAIGNS_DEFAULT_PREFERENCE) || true ;
}

/**
* Util function to get the start date of wlm monument
* For this release we are hardcoding it to be 1st September
* @return
*/
public static String getWLMStartDate() {
return "1 Sep";
}

/***
* Util function to get the end date of wlm monument
* For this release we are hardcoding it to be 31st October
* @return
*/
public static String getWLMEndDate() {
return "31 Oct";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -147,25 +147,26 @@ public boolean findBookmarkLocation(Place bookmarkLocation) {
}

@NonNull
Place fromCursor(Cursor cursor) {
LatLng location = new LatLng(cursor.getDouble(cursor.getColumnIndex(Table.COLUMN_LAT)),
Place fromCursor(final Cursor cursor) {
final LatLng location = new LatLng(cursor.getDouble(cursor.getColumnIndex(Table.COLUMN_LAT)),
cursor.getDouble(cursor.getColumnIndex(Table.COLUMN_LONG)), 1F);

Sitelinks.Builder builder = new Sitelinks.Builder();
final Sitelinks.Builder builder = new Sitelinks.Builder();
builder.setWikipediaLink(cursor.getString(cursor.getColumnIndex(Table.COLUMN_WIKIPEDIA_LINK)));
builder.setWikidataLink(cursor.getString(cursor.getColumnIndex(Table.COLUMN_WIKIDATA_LINK)));
builder.setCommonsLink(cursor.getString(cursor.getColumnIndex(Table.COLUMN_COMMONS_LINK)));

return new Place(
cursor.getString(cursor.getColumnIndex(Table.COLUMN_LANGUAGE)),
cursor.getString(cursor.getColumnIndex(Table.COLUMN_NAME)),
Label.fromText((cursor.getString(cursor.getColumnIndex(Table.COLUMN_LABEL_TEXT)))),
cursor.getString(cursor.getColumnIndex(Table.COLUMN_DESCRIPTION)),
location,
cursor.getString(cursor.getColumnIndex(Table.COLUMN_CATEGORY)),
builder.build(),
cursor.getString(cursor.getColumnIndex(Table.COLUMN_PIC)),
Boolean.parseBoolean(cursor.getString(cursor.getColumnIndex(Table.COLUMN_EXISTS)))
cursor.getString(cursor.getColumnIndex(Table.COLUMN_LANGUAGE)),
cursor.getString(cursor.getColumnIndex(Table.COLUMN_NAME)),
Label.fromText((cursor.getString(cursor.getColumnIndex(Table.COLUMN_LABEL_TEXT)))),
cursor.getString(cursor.getColumnIndex(Table.COLUMN_DESCRIPTION)),
location,
cursor.getString(cursor.getColumnIndex(Table.COLUMN_CATEGORY)),
builder.build(),
cursor.getString(cursor.getColumnIndex(Table.COLUMN_PIC)),
Boolean.parseBoolean(cursor.getString(cursor.getColumnIndex(Table.COLUMN_EXISTS))),
cursor.getString(cursor.getColumnIndex(Table.COLUMN_ADDRESS))
);
}

Expand All @@ -184,6 +185,7 @@ private ContentValues toContentValues(Place bookmarkLocation) {
cv.put(BookmarkLocationsDao.Table.COLUMN_LONG, bookmarkLocation.location.getLongitude());
cv.put(BookmarkLocationsDao.Table.COLUMN_PIC, bookmarkLocation.pic);
cv.put(BookmarkLocationsDao.Table.COLUMN_EXISTS, bookmarkLocation.exists.toString());
cv.put(BookmarkLocationsDao.Table.COLUMN_ADDRESS, bookmarkLocation.getAddress());
return cv;
}

Expand All @@ -204,6 +206,7 @@ public static class Table {
static final String COLUMN_COMMONS_LINK = "location_commons_link";
static final String COLUMN_PIC = "location_pic";
static final String COLUMN_EXISTS = "location_exists";
static final String COLUMN_ADDRESS = "location_address";

// NOTE! KEEP IN SAME ORDER AS THEY ARE DEFINED UP THERE. HELPS HARD CODE COLUMN INDICES.
public static final String[] ALL_FIELDS = {
Expand All @@ -220,7 +223,8 @@ public static class Table {
COLUMN_WIKIDATA_LINK,
COLUMN_COMMONS_LINK,
COLUMN_PIC,
COLUMN_EXISTS
COLUMN_EXISTS,
COLUMN_ADDRESS
};

static final String DROP_TABLE_STATEMENT = "DROP TABLE IF EXISTS " + TABLE_NAME;
Expand All @@ -239,7 +243,8 @@ public static class Table {
+ COLUMN_WIKIDATA_LINK + " STRING,"
+ COLUMN_COMMONS_LINK + " STRING,"
+ COLUMN_PIC + " STRING,"
+ COLUMN_EXISTS + " STRING"
+ COLUMN_EXISTS + " STRING,"
+ COLUMN_ADDRESS + " STRING"
+ ");";

public static void onCreate(SQLiteDatabase db) {
Expand All @@ -251,7 +256,7 @@ public static void onDelete(SQLiteDatabase db) {
onCreate(db);
}

public static void onUpdate(SQLiteDatabase db, int from, int to) {
public static void onUpdate(final SQLiteDatabase db, int from, final int to) {
Timber.d("bookmarksLocations db is updated from:"+from+", to:"+to);
if (from == to) {
return;
Expand Down Expand Up @@ -306,6 +311,14 @@ public static void onUpdate(SQLiteDatabase db, int from, int to) {
Timber.e(exception);
}
}

if(from>=15){
try {
db.execSQL("ALTER TABLE bookmarksLocations ADD COLUMN location_address STRING;");
} catch (SQLiteException exception){
Timber.e(exception);
}
}
}
}
}
3 changes: 2 additions & 1 deletion app/src/main/java/fr/free/nrw/commons/campaigns/Campaign.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ data class Campaign(var title: String? = null,
var description: String? = null,
var startDate: String? = null,
var endDate: String? = null,
var link: String? = null)
var link: String? = null,
var isWLMCampaign: Boolean = false)
54 changes: 41 additions & 13 deletions app/src/main/java/fr/free/nrw/commons/campaigns/CampaignView.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import android.net.Uri;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import fr.free.nrw.commons.theme.BaseActivity;
import org.wikipedia.util.DateUtil;

import java.text.ParseException;
Expand All @@ -27,9 +29,14 @@
* A view which represents a single campaign
*/
public class CampaignView extends SwipableCardView {
Campaign campaign = null;
Campaign campaign;
private ViewHolder viewHolder;

public static final String CAMPAIGNS_DEFAULT_PREFERENCE = "displayCampaignsCardView";
public static final String WLM_CARD_PREFERENCE = "displayWLMCardView";

private String campaignPreference = CAMPAIGNS_DEFAULT_PREFERENCE;

public CampaignView(@NonNull Context context) {
super(context);
init();
Expand All @@ -45,37 +52,46 @@ public CampaignView(@NonNull Context context, @Nullable AttributeSet attrs, int
init();
}

public void setCampaign(Campaign campaign) {
public void setCampaign(final Campaign campaign) {
this.campaign = campaign;
if (campaign != null) {
this.setVisibility(View.VISIBLE);
if (campaign.isWLMCampaign()) {
campaignPreference = WLM_CARD_PREFERENCE;
}
setVisibility(View.VISIBLE);
viewHolder.init();
} else {
this.setVisibility(View.GONE);
}
}

@Override public boolean onSwipe(View view) {
@Override public boolean onSwipe(final View view) {
view.setVisibility(View.GONE);
((MainActivity) getContext()).defaultKvStore
.putBoolean("displayCampaignsCardView", false);
((BaseActivity) getContext()).defaultKvStore
.putBoolean(campaignPreference, false);
ViewUtil.showLongToast(getContext(),
getResources().getString(R.string.nearby_campaign_dismiss_message));
return true;
}

private void init() {
View rootView = inflate(getContext(), R.layout.layout_campagin, this);
final View rootView = inflate(getContext(), R.layout.layout_campagin, this);
viewHolder = new ViewHolder(rootView);
setOnClickListener(view -> {
if (campaign != null) {
Utils.handleWebUrl(getContext(), Uri.parse(campaign.getLink()));
if (campaign.isWLMCampaign()) {
((MainActivity)(getContext())).showNearby();
} else {
Utils.handleWebUrl(getContext(), Uri.parse(campaign.getLink()));
}
}
});
}

public class ViewHolder {

@BindView(R.id.iv_campaign)
ImageView ivCampaign;
@BindView(R.id.tv_title) TextView tvTitle;
@BindView(R.id.tv_description) TextView tvDescription;
@BindView(R.id.tv_dates) TextView tvDates;
Expand All @@ -86,14 +102,26 @@ public ViewHolder(View itemView) {

public void init() {
if (campaign != null) {
ivCampaign.setImageDrawable(
getResources().getDrawable(R.drawable.ic_campaign));

tvTitle.setText(campaign.getTitle());
tvDescription.setText(campaign.getDescription());
try {
Date startDate = CommonsDateUtil.getIso8601DateFormatShort().parse(campaign.getStartDate());
Date endDate = CommonsDateUtil.getIso8601DateFormatShort().parse(campaign.getEndDate());
tvDates.setText(String.format("%1s - %2s", DateUtil.getExtraShortDateString(startDate),
DateUtil.getExtraShortDateString(endDate)));
} catch (ParseException e) {
if (campaign.isWLMCampaign()) {
tvDates.setText(
String.format("%1s - %2s", campaign.getStartDate(),
campaign.getEndDate()));
} else {
final Date startDate = CommonsDateUtil.getIso8601DateFormatShort()
.parse(campaign.getStartDate());
final Date endDate = CommonsDateUtil.getIso8601DateFormatShort()
.parse(campaign.getEndDate());
tvDates.setText(
String.format("%1s - %2s", startDate,
endDate));
}
} catch (final ParseException e) {
e.printStackTrace();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

import static fr.free.nrw.commons.contributions.Contribution.STATE_FAILED;
import static fr.free.nrw.commons.contributions.Contribution.STATE_PAUSED;
import static fr.free.nrw.commons.nearby.fragments.NearbyParentFragment.WLM_URL;
import static fr.free.nrw.commons.utils.LengthUtils.formatDistanceBetween;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
Expand All @@ -27,21 +26,21 @@
import androidx.fragment.app.FragmentManager.OnBackStackChangedListener;
import androidx.fragment.app.FragmentTransaction;
import fr.free.nrw.commons.CommonsApplication;
import fr.free.nrw.commons.Utils;
import fr.free.nrw.commons.auth.SessionManager;
import fr.free.nrw.commons.notification.Notification;
import fr.free.nrw.commons.notification.NotificationController;
import fr.free.nrw.commons.theme.BaseActivity;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Named;
import androidx.work.WorkInfo;
import androidx.work.WorkManager;
import butterknife.BindView;
import butterknife.ButterKnife;
import fr.free.nrw.commons.CommonsApplication;
import fr.free.nrw.commons.Media;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.auth.SessionManager;
import fr.free.nrw.commons.campaigns.Campaign;
import fr.free.nrw.commons.campaigns.CampaignView;
import fr.free.nrw.commons.campaigns.CampaignsPresenter;
Expand All @@ -59,10 +58,7 @@
import fr.free.nrw.commons.nearby.NearbyController;
import fr.free.nrw.commons.nearby.NearbyNotificationCardView;
import fr.free.nrw.commons.nearby.Place;
import fr.free.nrw.commons.notification.Notification;
import fr.free.nrw.commons.notification.NotificationActivity;
import fr.free.nrw.commons.notification.NotificationController;
import fr.free.nrw.commons.theme.BaseActivity;
import fr.free.nrw.commons.upload.worker.UploadWorker;
import fr.free.nrw.commons.utils.ConfigUtils;
import fr.free.nrw.commons.utils.DialogUtil;
Expand All @@ -73,9 +69,6 @@
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Named;
import timber.log.Timber;

public class ContributionsFragment
Expand Down Expand Up @@ -118,6 +111,8 @@ public class ContributionsFragment

public TextView notificationCount;

private Campaign wlmCampaign;

@NonNull
public static ContributionsFragment newInstance() {
ContributionsFragment fragment = new ContributionsFragment();
Expand All @@ -137,6 +132,7 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_contributions, container, false);
ButterKnife.bind(this, view);
initWLMCampaign();
presenter.onAttachView(this);
contributionsPresenter.onAttachView(this);
campaignView.setVisibility(View.GONE);
Expand Down Expand Up @@ -177,6 +173,15 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
return view;
}

/**
* Initialise the campaign object for WML
*/
private void initWLMCampaign() {
wlmCampaign = new Campaign(getString(R.string.wlm_campaign_title),
getString(R.string.wlm_campaign_description), Utils.getWLMStartDate().toString(),
Utils.getWLMEndDate().toString(), WLM_URL, true);
}

@Override
public void onCreateOptionsMenu(@NonNull final Menu menu, @NonNull final MenuInflater inflater) {
inflater.inflate(R.menu.contribution_activity_notification_menu, menu);
Expand Down Expand Up @@ -518,13 +523,17 @@ public void onLocationChangedMedium(LatLng latLng) {
}

/**
* ask the presenter to fetch the campaigns only if user has not manually disabled it
* As the home screen has limited space, we have choosen to show either campaigns or WLM card.
* The WLM Card gets the priority over monuments, so if the WLM is going on we show that instead
* of campaigns on the campaigns card
*/
private void fetchCampaigns() {
if (store.getBoolean("displayCampaignsCardView", true)) {
if (Utils.isMonumentsEnabled(new Date(), store)) {
campaignView.setCampaign(wlmCampaign);
campaignView.setVisibility(View.VISIBLE);
} else if (store.getBoolean(CampaignView.CAMPAIGNS_DEFAULT_PREFERENCE, true)) {
presenter.getCampaigns();
}
else{
} else {
campaignView.setVisibility(View.GONE);
}
}
Expand Down
Loading