fixed image loading, status edit bug fix

This commit is contained in:
nuclearfog 2023-04-20 02:26:03 +02:00
parent e77babd536
commit e74a59aa71
No known key found for this signature in database
GPG Key ID: 03488A185C476379
15 changed files with 131 additions and 71 deletions

View File

@ -82,14 +82,8 @@ public class MastodonPoll implements Poll {
@Override
public int getLimit() {
if (voted || expired) {
return 0;
}
if (multipleChoice) {
return options.length;
}
return 1;
public boolean multipleChoiceEnabled() {
return multipleChoice;
}

View File

@ -75,8 +75,8 @@ public class PollV2 implements Poll {
@Override
public int getLimit() {
return 0;
public boolean multipleChoiceEnabled() {
return false;
}

View File

@ -1,9 +1,10 @@
package org.nuclearfog.twidda.backend.helper;
import android.location.Location;
import androidx.annotation.NonNull;
import org.nuclearfog.twidda.model.Location;
/**
* This class contains location information used for {@link StatusUpdate}
*
@ -11,14 +12,30 @@ import androidx.annotation.NonNull;
*/
public class LocationUpdate {
private double[] coordinates = new double[2];
private double longitude, latitude;
/**
* @param location Android location information
*
*/
public LocationUpdate(double longitude, double latitude) {
this.longitude = longitude;
this.latitude = latitude;
}
/**
* create locationupdate from status location
*
* @param location location information
*/
public LocationUpdate(Location location) {
coordinates[0] = location.getLongitude();
coordinates[1] = location.getLatitude();
String[] locationStr = location.getCoordinates().split(",");
try {
this.longitude = Double.parseDouble(locationStr[0]);
this.latitude = Double.parseDouble(locationStr[1]);
} catch (NumberFormatException e) {
longitude = 0.0;
latitude = 0.0;
}
}
/**
@ -27,7 +44,7 @@ public class LocationUpdate {
* @return longitute value
*/
public double getLongitude() {
return coordinates[0];
return longitude;
}
/**
@ -36,13 +53,13 @@ public class LocationUpdate {
* @return latitude value
*/
public double getLatitude() {
return coordinates[1];
return latitude;
}
@NonNull
@Override
public String toString() {
return "longitude=" + coordinates[0] + " latitude=" + coordinates[1];
return "longitude=" + longitude + " latitude=" + latitude;
}
}

View File

@ -2,6 +2,8 @@ package org.nuclearfog.twidda.backend.helper;
import androidx.annotation.NonNull;
import org.nuclearfog.twidda.model.Poll;
import java.util.LinkedList;
import java.util.List;
@ -18,15 +20,30 @@ public class PollUpdate {
private boolean hideTotals;
private List<String> options;
/**
*
*/
public PollUpdate() {
options = new LinkedList<>();
}
/**
* get validity in seconds
* create poll using existing poll
*
* @return time until the poll is finnished
* @param poll existing poll to update
*/
public PollUpdate(Poll poll) {
options = new LinkedList<>();
multipleChoice = poll.multipleChoiceEnabled();
for (Poll.Option option : poll.getOptions()) {
options.add(option.getTitle());
}
}
/**
* set poll duration
*
* @return duration time in seconds
*/
public int getDuration() {
return duration;

View File

@ -98,6 +98,12 @@ public class StatusUpdate implements Serializable {
sensitive = status.isSensitive();
spoiler = status.isSpoiler();
visibility = status.getVisibility();
if (status.getPoll() != null) {
poll = new PollUpdate(status.getPoll());
}
if (status.getLocation() != null) {
location = new LocationUpdate(status.getLocation());
}
}
/**
@ -207,7 +213,7 @@ public class StatusUpdate implements Serializable {
* @param location location information
*/
public void addLocation(@NonNull Location location) {
this.location = new LocationUpdate(location);
this.location = new LocationUpdate(location.getLongitude(), location.getLatitude());
}
/**

View File

@ -1739,7 +1739,6 @@ public class AppDatabase {
buf.deleteCharAt(buf.length() - 1);
}
column.put(PollTable.ID, poll.getId());
column.put(PollTable.LIMIT, poll.getLimit());
column.put(PollTable.EXPIRATION, poll.getEndTime());
column.put(PollTable.OPTIONS, buf.toString());
db.insertWithOnConflict(PollTable.NAME, "", column, SQLiteDatabase.CONFLICT_REPLACE);

View File

@ -158,7 +158,6 @@ public class DatabaseAdapter {
+ PollTable.NAME + "("
+ PollTable.ID + " INTEGER PRIMARY KEY,"
+ PollTable.NAME + " TEXT,"
+ PollTable.LIMIT + " INTEGER,"
+ PollTable.EXPIRATION + " INTEGER,"
+ PollTable.OPTIONS + " TEXT);";
@ -1049,11 +1048,6 @@ public class DatabaseAdapter {
*/
String EXPIRATION = "expires_at";
/**
* maximum selection count
*/
String LIMIT = "select_limit";
/**
* poll options titles separated by ';'
*/

View File

@ -21,11 +21,10 @@ public class DatabasePoll implements Poll, PollTable {
private static final Pattern SEPARATOR = Pattern.compile(";");
public static final String[] PROJECTION = {ID, LIMIT, EXPIRATION, OPTIONS};
public static final String[] PROJECTION = {ID, EXPIRATION, OPTIONS};
private long id;
private long expired;
private int limit;
private long endTime;
private DatabasePollOption[] options = {};
/**
@ -33,9 +32,8 @@ public class DatabasePoll implements Poll, PollTable {
*/
public DatabasePoll(Cursor cursor) {
id = cursor.getLong(0);
limit = cursor.getInt(1);
expired = cursor.getLong(2);
String optionStr = cursor.getString(3);
endTime = cursor.getLong(1);
String optionStr = cursor.getString(2);
if (optionStr != null && !optionStr.isEmpty()) {
String[] optArray = SEPARATOR.split(optionStr);
options = new DatabasePollOption[optArray.length];
@ -65,14 +63,14 @@ public class DatabasePoll implements Poll, PollTable {
@Override
public int getLimit() {
return limit;
public boolean multipleChoiceEnabled() {
return false;
}
@Override
public long getEndTime() {
return expired;
return endTime;
}

View File

@ -25,9 +25,9 @@ public interface Poll extends Serializable {
boolean closed();
/**
* @return vote selection limit
* @return true if multiple choice is enabled
*/
int getLimit();
boolean multipleChoiceEnabled();
/**
* @return time where the poll expires
@ -40,7 +40,7 @@ public interface Poll extends Serializable {
int voteCount();
/**
* @return option
* @return array of vote options
*/
Option[] getOptions();

View File

@ -58,6 +58,11 @@ public class EditOptionsAdapter extends Adapter<EditOptionsHolder> implements On
} else {
holder.setState(position, EditOptionsHolder.STATE_DISABLED);
}
if (options.get(position) != null) {
holder.setDescription(options.get(position));
} else {
holder.setDescription("");
}
}

View File

@ -20,14 +20,13 @@ import java.util.TreeSet;
*/
public class OptionsAdapter extends Adapter<Optionholder> implements OnHolderClickListener {
private GlobalSettings settings;
private int totalVotes, limitVotes;
private Poll.Option[] options = {};
private Set<Integer> selection;
private int totalVotes = 1;
private int limitVotes = 1;
private GlobalSettings settings;
/**
*
*/
@ -88,8 +87,15 @@ public class OptionsAdapter extends Adapter<Optionholder> implements OnHolderCli
selection.add(i);
}
}
if (poll.voted() || poll.closed()) {
limitVotes = 0;
} else if (poll.multipleChoiceEnabled()) {
limitVotes = poll.getOptions().length;
} else {
limitVotes = 1;
}
totalVotes = poll.voteCount();
limitVotes = poll.getLimit();
notifyDataSetChanged();
}

View File

@ -3,7 +3,8 @@ package org.nuclearfog.twidda.ui.adapter.holder;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.text.SpannableString;
import android.graphics.drawable.Drawable;
import android.text.SpannableStringBuilder;
import android.text.style.StyleSpan;
import android.view.LayoutInflater;
import android.view.View;
@ -92,23 +93,32 @@ public class CardHolder extends ViewHolder implements OnClickListener {
* @param card card content
*/
public void setContent(Card card) {
String textStr;
String title = card.getTitle();
if (title.length() > TITLE_MAX_LEN) {
textStr = title.substring(0, TITLE_MAX_LEN - 3) + "...";
} else {
textStr = title;
}
if (!card.getDescription().isEmpty())
textStr += '\n' + card.getDescription();
SpannableString textSpan = new SpannableString(textStr);
if (!title.isEmpty())
textSpan.setSpan(new StyleSpan(Typeface.BOLD), 0, Math.min(title.length(), TITLE_MAX_LEN), 0);
linkText.setText(textSpan);
SpannableStringBuilder urlDescription = new SpannableStringBuilder();
Drawable placeholder = new ColorDrawable(EMPTY_COLOR);
// set url preview image
if (settings.imagesEnabled() && !card.getImageUrl().isEmpty()) {
picasso.load(card.getImageUrl()).networkPolicy(NetworkPolicy.NO_STORE).memoryPolicy(MemoryPolicy.NO_STORE).into(preview);
picasso.load(card.getImageUrl()).networkPolicy(NetworkPolicy.NO_STORE).memoryPolicy(MemoryPolicy.NO_STORE).placeholder(placeholder).into(preview);
} else {
preview.setImageDrawable(new ColorDrawable(EMPTY_COLOR));
preview.setImageDrawable(placeholder);
}
// set url title and truncate if needed
if (!card.getTitle().trim().isEmpty()) {
// truncate title
if (card.getTitle().length() > TITLE_MAX_LEN) {
urlDescription.append(card.getTitle().substring(0, TITLE_MAX_LEN - 3));
urlDescription.append("...");
urlDescription.setSpan(new StyleSpan(Typeface.BOLD), 0, TITLE_MAX_LEN, 0);
} else {
urlDescription.append(card.getTitle());
urlDescription.setSpan(new StyleSpan(Typeface.BOLD), 0, card.getTitle().length(), 0);
}
}
// set url description
if (!card.getDescription().isEmpty()) {
urlDescription.append('\n');
urlDescription.append(card.getDescription());
}
// apply description
linkText.setText(urlDescription);
}
}

View File

@ -102,11 +102,20 @@ public class EditOptionsHolder extends ViewHolder implements OnClickListener, Te
@Override
public void afterTextChanged(Editable s) {
int position = getLayoutPosition();
if (position != NO_POSITION) {
if (position != NO_POSITION && option_name.hasFocus()) {
listener.OnOptionChange(position, s.toString());
}
}
/**
* set option description
*
* @param description descrition of the option
*/
public void setDescription(String description) {
option_name.setText(description);
}
/**
* set option state and option hint
*

View File

@ -83,9 +83,9 @@ public class PollHolder extends ViewHolder implements OnClickListener {
voteButton.setVisibility(View.GONE);
} else {
expiration.setText(StringUtils.formatExpirationTime(expiration.getResources(), poll.getEndTime()));
if (poll.voted()) {
if (poll.voted() || poll.closed()) {
voteButton.setVisibility(View.GONE);
} else if (poll.getLimit() > 0) {
} else {
voteButton.setVisibility(View.VISIBLE);
}
}

View File

@ -1,6 +1,7 @@
package org.nuclearfog.twidda.ui.adapter.holder;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
@ -17,6 +18,7 @@ import com.squareup.picasso.RequestCreator;
import org.nuclearfog.twidda.R;
import org.nuclearfog.twidda.config.GlobalSettings;
import org.nuclearfog.twidda.database.impl.DatabaseMedia;
import org.nuclearfog.twidda.model.Media;
import jp.wasabeef.picasso.transformations.BlurTransformation;
@ -69,17 +71,20 @@ public class PreviewHolder extends ViewHolder implements OnClickListener {
* @param media media content
*/
public void setContent(Media media, boolean blurImage) {
if (settings.imagesEnabled() && !media.getPreviewUrl().isEmpty()) {
RequestCreator picassoBuilder = picasso.load(media.getPreviewUrl()).error(R.drawable.no_image);
picassoBuilder.networkPolicy(NetworkPolicy.NO_STORE).memoryPolicy(MemoryPolicy.NO_STORE);
RequestCreator picassoBuilder = picasso.load(media.getPreviewUrl());
Drawable placeholder = new ColorDrawable(EMPTY_COLOR);
// check if online media and image loading is enabled
if (!(media instanceof DatabaseMedia) && settings.imagesEnabled() && !media.getPreviewUrl().isEmpty()) {
// set image blur if enabled
if (blurImage) {
BlurTransformation blurTransformation = new BlurTransformation(previewImage.getContext(), 30);
picassoBuilder.transform(blurTransformation);
}
picassoBuilder.into(previewImage);
picassoBuilder.networkPolicy(NetworkPolicy.NO_STORE).memoryPolicy(MemoryPolicy.NO_STORE).placeholder(placeholder).into(previewImage);
} else {
previewImage.setImageDrawable(new ColorDrawable(EMPTY_COLOR));
previewImage.setImageDrawable(placeholder);
}
// set 'play video' button
if (media.getMediaType() == Media.VIDEO || media.getMediaType() == Media.GIF) {
playIcon.setVisibility(View.VISIBLE);
} else {