Changed primary key from location to entity id

This commit is contained in:
Kanahia 2024-07-08 06:56:09 +05:30
parent 438aa3b41b
commit a28117397d
7 changed files with 38 additions and 14 deletions

View file

@ -165,7 +165,8 @@ public class BookmarkLocationsDao {
cursor.getString(cursor.getColumnIndex(Table.COLUMN_CATEGORY)), cursor.getString(cursor.getColumnIndex(Table.COLUMN_CATEGORY)),
builder.build(), builder.build(),
cursor.getString(cursor.getColumnIndex(Table.COLUMN_PIC)), cursor.getString(cursor.getColumnIndex(Table.COLUMN_PIC)),
Boolean.parseBoolean(cursor.getString(cursor.getColumnIndex(Table.COLUMN_EXISTS))) Boolean.parseBoolean(cursor.getString(cursor.getColumnIndex(Table.COLUMN_EXISTS))),
cursor.getString(cursor.getColumnIndex(Table.COLUMN_ENTITY_ID))
); );
} }
@ -184,6 +185,7 @@ public class BookmarkLocationsDao {
cv.put(BookmarkLocationsDao.Table.COLUMN_LONG, bookmarkLocation.location.getLongitude()); cv.put(BookmarkLocationsDao.Table.COLUMN_LONG, bookmarkLocation.location.getLongitude());
cv.put(BookmarkLocationsDao.Table.COLUMN_PIC, bookmarkLocation.pic); cv.put(BookmarkLocationsDao.Table.COLUMN_PIC, bookmarkLocation.pic);
cv.put(BookmarkLocationsDao.Table.COLUMN_EXISTS, bookmarkLocation.exists.toString()); cv.put(BookmarkLocationsDao.Table.COLUMN_EXISTS, bookmarkLocation.exists.toString());
cv.put(BookmarkLocationsDao.Table.COLUMN_ENTITY_ID, bookmarkLocation.entityID);
return cv; return cv;
} }
@ -204,6 +206,7 @@ public class BookmarkLocationsDao {
static final String COLUMN_COMMONS_LINK = "location_commons_link"; static final String COLUMN_COMMONS_LINK = "location_commons_link";
static final String COLUMN_PIC = "location_pic"; static final String COLUMN_PIC = "location_pic";
static final String COLUMN_EXISTS = "location_exists"; static final String COLUMN_EXISTS = "location_exists";
static final String COLUMN_ENTITY_ID = "location_entity_id";
// NOTE! KEEP IN SAME ORDER AS THEY ARE DEFINED UP THERE. HELPS HARD CODE COLUMN INDICES. // NOTE! KEEP IN SAME ORDER AS THEY ARE DEFINED UP THERE. HELPS HARD CODE COLUMN INDICES.
public static final String[] ALL_FIELDS = { public static final String[] ALL_FIELDS = {
@ -221,6 +224,7 @@ public class BookmarkLocationsDao {
COLUMN_COMMONS_LINK, COLUMN_COMMONS_LINK,
COLUMN_PIC, COLUMN_PIC,
COLUMN_EXISTS, COLUMN_EXISTS,
COLUMN_ENTITY_ID
}; };
static final String DROP_TABLE_STATEMENT = "DROP TABLE IF EXISTS " + TABLE_NAME; static final String DROP_TABLE_STATEMENT = "DROP TABLE IF EXISTS " + TABLE_NAME;
@ -239,7 +243,8 @@ public class BookmarkLocationsDao {
+ COLUMN_WIKIDATA_LINK + " STRING," + COLUMN_WIKIDATA_LINK + " STRING,"
+ COLUMN_COMMONS_LINK + " STRING," + COLUMN_COMMONS_LINK + " STRING,"
+ COLUMN_PIC + " STRING," + COLUMN_PIC + " STRING,"
+ COLUMN_EXISTS + " STRING" + COLUMN_EXISTS + " STRING,"
+ COLUMN_ENTITY_ID + " STRING"
+ ");"; + ");";
public static void onCreate(SQLiteDatabase db) { public static void onCreate(SQLiteDatabase db) {
@ -306,6 +311,13 @@ public class BookmarkLocationsDao {
Timber.e(exception); Timber.e(exception);
} }
} }
if (from >= 15){
try {
db.execSQL("ALTER TABLE bookmarksLocations ADD COLUMN location_entity_id STRING;");
} catch (SQLiteException exception){
Timber.e(exception);
}
}
} }
} }
} }

View file

@ -24,8 +24,9 @@ public class Place implements Parcelable {
public String name; public String name;
private Label label; private Label label;
private String longDescription; private String longDescription;
@PrimaryKey @NonNull
public LatLng location; public LatLng location;
@PrimaryKey @NonNull
public String entityID;
private String category; private String category;
public String pic; public String pic;
// exists boolean will tell whether the place exists or not, // exists boolean will tell whether the place exists or not,
@ -47,10 +48,11 @@ public class Place implements Parcelable {
pic = null; pic = null;
exists = null; exists = null;
siteLinks = null; siteLinks = null;
entityID = null;
} }
public Place(String language, String name, Label label, String longDescription, LatLng location, public Place(String language, String name, Label label, String longDescription, LatLng location,
String category, Sitelinks siteLinks, String pic, Boolean exists) { String category, Sitelinks siteLinks, String pic, Boolean exists, String entityID) {
this.language = language; this.language = language;
this.name = name; this.name = name;
this.label = label; this.label = label;
@ -60,10 +62,11 @@ public class Place implements Parcelable {
this.siteLinks = siteLinks; this.siteLinks = siteLinks;
this.pic = (pic == null) ? "" : pic; this.pic = (pic == null) ? "" : pic;
this.exists = exists; this.exists = exists;
this.entityID = entityID;
} }
public Place(String name, String longDescription, LatLng location, String category, public Place(String name, String longDescription, LatLng location, String category,
Sitelinks siteLinks, String pic, String thumb) { Sitelinks siteLinks, String pic, String thumb, String entityID) {
this.name = name; this.name = name;
this.longDescription = longDescription; this.longDescription = longDescription;
this.location = location; this.location = location;
@ -74,6 +77,7 @@ public class Place implements Parcelable {
this.language = null; this.language = null;
this.label = null; this.label = null;
this.exists = true; this.exists = true;
this.entityID = entityID;
} }
public Place(Parcel in) { public Place(Parcel in) {
@ -89,6 +93,7 @@ public class Place implements Parcelable {
String existString = in.readString(); String existString = in.readString();
this.exists = Boolean.parseBoolean(existString); this.exists = Boolean.parseBoolean(existString);
this.isMonument = in.readInt() == 1; this.isMonument = in.readInt() == 1;
this.entityID = in.readString();
} }
public static Place from(NearbyResultItem item) { public static Place from(NearbyResultItem item) {
@ -97,6 +102,10 @@ public class Place implements Parcelable {
if (!StringUtils.isBlank(itemClass)) { if (!StringUtils.isBlank(itemClass)) {
classEntityId = itemClass.replace("http://www.wikidata.org/entity/", ""); classEntityId = itemClass.replace("http://www.wikidata.org/entity/", "");
} }
String entityId = "";
if (!StringUtils.isBlank(item.getItem().getValue())){
entityId = item.getItem().getValue().replace("http://www.wikidata.org/entity/", "");
}
// Set description when not null and not empty // Set description when not null and not empty
String description = String description =
(item.getDescription().getValue() != null && !item.getDescription().getValue() (item.getDescription().getValue() != null && !item.getDescription().getValue()
@ -129,7 +138,7 @@ public class Place implements Parcelable {
.build(), .build(),
item.getPic().getValue(), item.getPic().getValue(),
// Checking if the place exists or not // Checking if the place exists or not
(item.getDestroyed().getValue() == "") && (item.getEndTime().getValue() == "")); (item.getDestroyed().getValue() == "") && (item.getEndTime().getValue() == ""), entityId);
} }
/** /**
@ -296,6 +305,7 @@ public class Place implements Parcelable {
", siteLinks='" + siteLinks.toString() + '\'' + ", siteLinks='" + siteLinks.toString() + '\'' +
", pic='" + pic + '\'' + ", pic='" + pic + '\'' +
", exists='" + exists.toString() + '\'' + ", exists='" + exists.toString() + '\'' +
", entityID='" + entityID + '\'' +
'}'; '}';
} }
@ -314,6 +324,7 @@ public class Place implements Parcelable {
dest.writeString(category); dest.writeString(category);
dest.writeParcelable(siteLinks, 0); dest.writeParcelable(siteLinks, 0);
dest.writeString(pic); dest.writeString(pic);
dest.writeString(entityID);
dest.writeString(exists.toString()); dest.writeString(exists.toString());
dest.writeInt(isMonument ? 1 : 0); dest.writeInt(isMonument ? 1 : 0);
} }

View file

@ -12,8 +12,8 @@ public abstract class PlaceDao {
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
public abstract void saveSynchronous(Place place); public abstract void saveSynchronous(Place place);
@Query("SELECT * from place WHERE location=:l") @Query("SELECT * from place WHERE entityID=:entity")
public abstract Place getPlace(LatLng l); public abstract Place getPlace(String entity);
public Completable save(final Place place) { public Completable save(final Place place) {
return Completable return Completable

View file

@ -14,8 +14,8 @@ public class PlacesLocalDataSource {
this.placeDao = placeDao; this.placeDao = placeDao;
} }
public Place fetchPlace(LatLng latLng){ public Place fetchPlace(String entityID){
return placeDao.getPlace(latLng); return placeDao.getPlace(entityID);
} }
public Completable savePlace(Place place) { public Completable savePlace(Place place) {

View file

@ -18,8 +18,8 @@ public class PlacesRepository {
return localDataSource.savePlace(place); return localDataSource.savePlace(place);
} }
public Place fetchPlace(LatLng latLng){ public Place fetchPlace(String entityID){
return localDataSource.fetchPlace(latLng); return localDataSource.fetchPlace(entityID);
} }
} }

View file

@ -1409,7 +1409,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
for (Place place : batch) { for (Place place : batch) {
Observable<Place> placeObservable = Observable Observable<Place> placeObservable = Observable
.fromCallable(() -> { .fromCallable(() -> {
Place fetchedPlace = placesRepository.fetchPlace(place.location); Place fetchedPlace = placesRepository.fetchPlace(place.entityID);
return fetchedPlace != null ? fetchedPlace : place; return fetchedPlace != null ? fetchedPlace : place;
}) })
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())

View file

@ -47,7 +47,8 @@ public class PlaceUtils {
.setWikidataLink("") // we don't necessarily have them, can be fetched later .setWikidataLink("") // we don't necessarily have them, can be fetched later
.build(), .build(),
media.getImageUrl(), media.getImageUrl(),
media.getThumbUrl())); media.getThumbUrl(),
""));
} }
return explorePlaceList; return explorePlaceList;
} }