added loader for trend locations

This commit is contained in:
NudeDude 2019-06-22 14:37:34 +02:00
parent 50b49bec57
commit 084b75a15f
17 changed files with 191 additions and 265 deletions

View File

@ -5,7 +5,7 @@
<GradleProjectSettings>
<option name="distributionType" value="LOCAL" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleHome" value="/opt/Gradle/gradle-5.4" />
<option name="gradleHome" value="/opt/Gradle/gradle-5.4.1" />
<option name="modules">
<set>
<option value="$PROJECT_DIR$" />

View File

@ -6,13 +6,13 @@ import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.Adapter;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
import java.lang.ref.WeakReference;
public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageHolder> {
public class ImageAdapter extends Adapter<ImageAdapter.ImageHolder> {
private WeakReference<OnImageClickListener> itemClickListener;
private Bitmap[] images;

View File

@ -11,6 +11,7 @@ import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.Adapter;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
import com.squareup.picasso.Picasso;
@ -27,7 +28,7 @@ import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MessageHolder> {
public class MessageAdapter extends Adapter<MessageAdapter.MessageHolder> {
private WeakReference<OnItemSelected> itemClickListener;
private Message[] messages;

View File

@ -84,10 +84,10 @@ public class TrendAdapter extends Adapter<TrendAdapter.ItemHolder> {
@Override
public void onBindViewHolder(@NonNull ItemHolder vh, int index) {
Trend trend = trends[index];
String posStr = Integer.toString(trend.getPosition()) + '.';
String posStr = trend.getPosition();
vh.trends.setText(trend.getName());
vh.pos.setText(posStr);
vh.trends.setTextColor(font_color);
vh.pos.setText(posStr);
vh.pos.setTextColor(font_color);
}

View File

@ -1,62 +0,0 @@
package org.nuclearfog.twidda.adapter;
import android.content.Context;
import android.content.res.Resources;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import org.nuclearfog.twidda.R;
public class WorldIdAdapter extends ArrayAdapter {
private LayoutInflater mInflater;
private String[] location;
private int[] worldId;
public WorldIdAdapter(Context context) {
super(context, android.R.layout.simple_spinner_dropdown_item);
mInflater = LayoutInflater.from(context);
Resources res = context.getResources();
location = res.getStringArray(R.array.location);
worldId = res.getIntArray(R.array.woeId);
}
@Override
public int getCount() {
return location.length;
}
@Override
public String getItem(int position) {
return location[position];
}
@Override
public long getItemId(int position) {
return worldId[position];
}
@NonNull
@Override
public View getView(int position, @Nullable View view, @NonNull ViewGroup parent) {
if (view == null) {
view = mInflater.inflate(android.R.layout.simple_spinner_item, parent, false);
}
TextView country = view.findViewById(android.R.id.text1);
country.setText(getItem(position));
country.setGravity(Gravity.CENTER_HORIZONTAL);
return view;
}
}

View File

@ -0,0 +1,62 @@
package org.nuclearfog.twidda.backend;
import android.os.AsyncTask;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import org.nuclearfog.twidda.R;
import org.nuclearfog.twidda.backend.helper.ErrorHandler;
import org.nuclearfog.twidda.backend.items.TrendLocation;
import org.nuclearfog.twidda.database.GlobalSettings;
import org.nuclearfog.twidda.window.AppSettings;
import java.lang.ref.WeakReference;
import java.util.List;
import twitter4j.TwitterException;
public class LocationLoader extends AsyncTask<Void, Void, List<TrendLocation>> {
private WeakReference<AppSettings> ui;
private GlobalSettings settings;
private TwitterEngine mTwitter;
private TwitterException err;
public LocationLoader(AppSettings context) {
ui = new WeakReference<>(context);
settings = GlobalSettings.getInstance(context);
mTwitter = TwitterEngine.getInstance(context);
}
@Override
protected List<TrendLocation> doInBackground(Void[] v) {
try {
return mTwitter.getLocations();
} catch (TwitterException err) {
this.err = err;
} catch (Exception err) {
err.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(List<TrendLocation> locations) {
if (ui.get() != null) {
if (locations != null && !locations.isEmpty()) {
Spinner woeId = ui.get().findViewById(R.id.woeid);
ArrayAdapter adapter = (ArrayAdapter) woeId.getAdapter();
adapter.clear();
adapter.addAll(locations);
adapter.notifyDataSetChanged();
int position = adapter.getPosition(settings.getTrendLocation());
woeId.setSelection(position);
} else if (err != null) {
ErrorHandler.printError(ui.get(), err);
}
}
}
}

View File

@ -7,6 +7,7 @@ import androidx.annotation.Nullable;
import org.nuclearfog.twidda.BuildConfig;
import org.nuclearfog.twidda.backend.items.Message;
import org.nuclearfog.twidda.backend.items.Trend;
import org.nuclearfog.twidda.backend.items.TrendLocation;
import org.nuclearfog.twidda.backend.items.Tweet;
import org.nuclearfog.twidda.backend.items.TweetHolder;
import org.nuclearfog.twidda.backend.items.TwitterUser;
@ -20,6 +21,7 @@ import java.util.List;
import twitter4j.DirectMessage;
import twitter4j.IDs;
import twitter4j.Location;
import twitter4j.Paging;
import twitter4j.Query;
import twitter4j.QueryResult;
@ -205,11 +207,23 @@ public class TwitterEngine {
public List<Trend> getTrends(int woeId) throws TwitterException {
List<Trend> result = new LinkedList<>();
twitter4j.Trend[] trends = twitter.getPlaceTrends(woeId).getTrends();
for (int i = 0; i < trends.length; i++)
result.add(new Trend(i, trends[i].getName()));
return result;
}
for (int i = 0; i < trends.length; i++) {
Trend item = new Trend(i + 1, trends[i].getName());
result.add(item);
}
/**
* get available locations
*
* @return list of locations
* @throws TwitterException if access is unavailable
*/
public List<TrendLocation> getLocations() throws TwitterException {
List<TrendLocation> result = new LinkedList<>();
List<Location> locations = twitter.getAvailableTrends();
for (Location location : locations)
result.add(new TrendLocation(location));
return result;
}

View File

@ -2,12 +2,13 @@ package org.nuclearfog.twidda.backend.items;
public class Trend {
private final int position;
private final String trend;
private final String position;
private final String name;
public Trend(int position, String trend) {
this.position = position;
this.trend = trend;
public Trend(int pos, String name) {
this.position = pos + 1 + ".";
this.name = name;
}
/**
@ -16,7 +17,7 @@ public class Trend {
* @return trend name
*/
public String getName() {
return trend;
return name;
}
/**
@ -24,7 +25,7 @@ public class Trend {
*
* @return trend rank
*/
public int getPosition() {
public String getPosition() {
return position;
}

View File

@ -0,0 +1,53 @@
package org.nuclearfog.twidda.backend.items;
import androidx.annotation.NonNull;
import twitter4j.Location;
public class TrendLocation {
private final String placeName;
private final int id;
public TrendLocation(Location location) {
String country = location.getCountryName();
String placeName = location.getName();
if (country == null || country.trim().isEmpty() || country.equals(placeName))
this.placeName = placeName;
else
this.placeName = country + ", " + placeName;
this.id = location.getWoeid();
}
public TrendLocation(String placeName, int id) {
this.placeName = placeName;
this.id = id;
}
public String getName() {
return placeName;
}
public int getWoeId() {
return id;
}
@Override
@NonNull
public String toString() {
return placeName != null ? placeName : "";
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof TrendLocation))
return false;
return ((TrendLocation) obj).getWoeId() == id;
}
}

View File

@ -4,6 +4,8 @@ import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import org.nuclearfog.twidda.backend.items.TrendLocation;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.text.NumberFormat;
@ -17,18 +19,16 @@ public class GlobalSettings {
private SharedPreferences settings;
private NumberFormat formatter;
private TrendLocation location;
private String key1, key2;
private boolean loadImage;
private boolean loadAnswer;
private boolean loggedIn;
private boolean customWorldId;
private int background_color;
private int font_color;
private int highlight_color;
private int tweet_color;
private int row;
private int woeId;
private int woeIdPos;
private long userId;
private String proxyHost, proxyPort;
@ -36,9 +36,6 @@ public class GlobalSettings {
private GlobalSettings(Context context) {
settings = context.getSharedPreferences(NAME, MODE_PRIVATE);
woeId = settings.getInt("world_id", 1);
customWorldId = settings.getBoolean("custom_woeId", false);
woeIdPos = settings.getInt("world_id_pos", 0);
background_color = settings.getInt("background_color", 0xff0f114a);
highlight_color = settings.getInt("highlight_color", 0xffff00ff);
font_color = settings.getInt("font_color", 0xffffffff);
@ -54,6 +51,9 @@ public class GlobalSettings {
proxyPort = settings.getString("proxy_port", "");
proxyUser = settings.getString("proxy_user", "");
proxyPass = settings.getString("proxy_pass", "");
String place = settings.getString("location", "");
int woeId = settings.getInt("world_id", 1);
location = new TrendLocation(place, woeId);
formatter = NumberFormat.getIntegerInstance();
configureProxy();
}
@ -197,68 +197,20 @@ public class GlobalSettings {
edit.apply();
}
/**
* get World ID for trends
*
* @return World ID
*/
public int getWoeId() {
return woeId;
public TrendLocation getTrendLocation() {
return location;
}
/**
* set World ID for trends
*
* @param id World ID
*/
public void setWoeId(long id) {
woeId = (int) id;
public void setTrendLocation(TrendLocation location) {
this.location = location;
Editor edit = settings.edit();
edit.putInt("world_id", (int) id);
edit.putInt("world_id", location.getWoeId());
edit.putString("location", location.getName());
edit.apply();
}
/**
* return position of the world id dropdown list
*
* @return position
*/
public int getWoeIdSelection() {
return woeIdPos;
}
/**
* set last position of the dropdown list
*
* @param pos position of the last selection
*/
public void setWoeIdSelection(int pos) {
woeIdPos = pos;
Editor edit = settings.edit();
edit.putInt("world_id_pos", pos);
edit.apply();
}
/**
* Check if custom World ID is set
*
* @return if custom world ID is set
*/
public boolean getCustomWidSet() {
return customWorldId;
}
/**
* Set custom World ID
*
* @param customWoeId true if Custom world ID is set
*/
public void setCustomWidSet(boolean customWoeId) {
customWorldId = customWoeId;
Editor edit = settings.edit();
edit.putBoolean("custom_woeId", customWoeId);
edit.apply();
}
/**
* get loading limit of tweets/users

View File

@ -39,7 +39,7 @@ public class TrendLoader extends AsyncTask<Void, Void, List<Trend>> {
GlobalSettings settings = GlobalSettings.getInstance(root.getContext());
RecyclerView list = root.findViewById(R.id.fragment_list);
adapter = (TrendAdapter) list.getAdapter();
woeId = settings.getWoeId();
woeId = settings.getTrendLocation().getWoeId();
}

View File

@ -13,8 +13,7 @@ import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
@ -33,21 +32,21 @@ import com.flask.colorpicker.OnColorChangedListener;
import com.flask.colorpicker.builder.ColorPickerDialogBuilder;
import org.nuclearfog.twidda.R;
import org.nuclearfog.twidda.adapter.WorldIdAdapter;
import org.nuclearfog.twidda.backend.LocationLoader;
import org.nuclearfog.twidda.backend.TwitterEngine;
import org.nuclearfog.twidda.backend.items.TrendLocation;
import org.nuclearfog.twidda.database.Database;
import org.nuclearfog.twidda.database.GlobalSettings;
import static android.os.AsyncTask.Status.RUNNING;
import static android.view.View.GONE;
import static android.view.View.INVISIBLE;
import static android.view.View.VISIBLE;
import static android.widget.Toast.LENGTH_SHORT;
import static org.nuclearfog.twidda.MainActivity.APP_LOGOUT;
import static org.nuclearfog.twidda.MainActivity.DB_CLEARED;
public class AppSettings extends AppCompatActivity implements OnClickListener,
OnDismissListener, OnItemSelectedListener, OnCheckedChangeListener {
OnDismissListener, OnCheckedChangeListener {
private static final int BACKGROUND = 0;
private static final int FONTCOLOR = 1;
@ -56,16 +55,16 @@ public class AppSettings extends AppCompatActivity implements OnClickListener,
private GlobalSettings settings;
private ConnectivityManager mConnect;
private LocationLoader locationAsync;
private Button colorButton1, colorButton2, colorButton3, colorButton4;
private CheckBox toggleImg, toggleAns;
private EditText proxyAddr, proxyPort, proxyUser, proxyPass;
private EditText woeIdText;
private CheckBox toggleImg, toggleAns;
private ArrayAdapter adapter;
private Spinner woeId;
private View root;
private int color = 0;
private int mode = 0;
private boolean customWoeId = false;
@Override
protected void onCreate(Bundle b) {
@ -85,7 +84,6 @@ public class AppSettings extends AppCompatActivity implements OnClickListener,
proxyPass = findViewById(R.id.edit_proxypass);
toggleImg = findViewById(R.id.toggleImg);
toggleAns = findViewById(R.id.toggleAns);
woeIdText = findViewById(R.id.woe_id);
woeId = findViewById(R.id.woeid);
root = findViewById(R.id.settings_layout);
Toolbar toolbar = findViewById(R.id.toolbar_setting);
@ -99,6 +97,10 @@ public class AppSettings extends AppCompatActivity implements OnClickListener,
if (!settings.getLogin())
login_layout.setVisibility(GONE);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item);
adapter.add(settings.getTrendLocation());
woeId.setAdapter(adapter);
logout.setOnClickListener(this);
load_popup.setOnClickListener(this);
delButton.setOnClickListener(this);
@ -106,7 +108,6 @@ public class AppSettings extends AppCompatActivity implements OnClickListener,
colorButton2.setOnClickListener(this);
colorButton3.setOnClickListener(this);
colorButton4.setOnClickListener(this);
woeId.setOnItemSelectedListener(this);
toggleImg.setOnCheckedChangeListener(this);
toggleAns.setOnCheckedChangeListener(this);
}
@ -117,8 +118,6 @@ public class AppSettings extends AppCompatActivity implements OnClickListener,
super.onStart();
toggleImg.setChecked(settings.getImageLoad());
toggleAns.setChecked(settings.getAnswerLoad());
woeId.setAdapter(new WorldIdAdapter(this));
woeId.setSelection(settings.getWoeIdSelection());
root.setBackgroundColor(settings.getBackgroundColor());
colorButton1.setBackgroundColor(settings.getBackgroundColor());
colorButton2.setBackgroundColor(settings.getFontColor());
@ -132,11 +131,10 @@ public class AppSettings extends AppCompatActivity implements OnClickListener,
proxyPort.setText(settings.getProxyPort());
proxyUser.setText(settings.getProxyUser());
proxyPass.setText(settings.getProxyPass());
customWoeId = settings.getCustomWidSet();
if (customWoeId) {
String text = Long.toString(settings.getWoeId());
woeIdText.setVisibility(VISIBLE);
woeIdText.setText(text);
if (adapter.getCount() <= 1) {
locationAsync = new LocationLoader(this);
locationAsync.execute();
}
}
@ -147,15 +145,21 @@ public class AppSettings extends AppCompatActivity implements OnClickListener,
settings.setProxyServer(proxyAddr.getText().toString(), proxyPort.getText().toString());
settings.setProxyLogin(proxyUser.getText().toString(), proxyPass.getText().toString());
settings.configureProxy();
if (customWoeId) {
String woeText = woeIdText.getText().toString();
settings.setWoeId(Long.parseLong(woeText));
}
TrendLocation location = (TrendLocation) woeId.getSelectedItem();
settings.setTrendLocation(location);
super.onBackPressed();
}
}
@Override
protected void onStop() {
super.onStop();
if (locationAsync != null && locationAsync.getStatus() == RUNNING)
locationAsync.cancel(true);
}
@Override
public boolean onCreateOptionsMenu(Menu m) {
getMenuInflater().inflate(R.menu.settings, m);
@ -290,29 +294,6 @@ public class AppSettings extends AppCompatActivity implements OnClickListener,
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == parent.getCount() - 1) {
woeIdText.setVisibility(VISIBLE);
settings.setCustomWidSet(true);
customWoeId = true;
} else {
woeIdText.setVisibility(INVISIBLE);
woeIdText.setText("");
settings.setCustomWidSet(false);
settings.setWoeId(id);
customWoeId = false;
}
settings.setWoeIdSelection(position);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
woeId.setSelection(settings.getWoeIdSelection());
}
@Override
public void onCheckedChanged(CompoundButton c, boolean checked) {
switch (c.getId()) {
@ -347,14 +328,6 @@ public class AppSettings extends AppCompatActivity implements OnClickListener,
Editable editAddr = proxyAddr.getText();
Editable editUser = proxyUser.getText();
if (customWoeId) {
Editable woeText = woeIdText.getText();
if (woeText == null || woeText.toString().isEmpty()) {
String errMsg = getString(R.string.error_woeid_empty);
woeIdText.setError(errMsg);
success = false;
}
}
if (editAddr != null && !editAddr.toString().isEmpty()) {
Editable editPort = proxyPort.getText();
if (editPort == null || editPort.toString().isEmpty()) {

View File

@ -157,22 +157,8 @@
<Spinner
android:id="@+id/woeid"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3" />
<EditText
android:id="@+id/woe_id"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginStart="@dimen/padding_side"
android:layout_marginLeft="@dimen/padding_side"
android:layout_weight="2"
android:background="@android:color/transparent"
android:hint="@string/woeid"
android:inputType="number"
android:singleLine="true"
android:visibility="invisible" />
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

View File

@ -1,25 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="location">
<item>Weltweit</item>
<item>Deutschland</item>
<item>Frankreich</item>
<item>USA</item>
<item>Mexiko</item>
<item>Algerien</item>
<!--Last Element should stay here-->
<item>Benutzerdefiniert</item>
</string-array>
<integer-array name="woeId">
<item>1</item>
<item>23424829</item>
<item>23424819</item>
<item>23424977</item>
<item>23424900</item>
<item>23424740</item>
<item>0</item>
</integer-array>
</resources>

View File

@ -103,7 +103,6 @@
<string name="proxy_port">Port</string>
<string name="proxy_settings">Proxy Einstellung</string>
<string name="verifierbutton">Login</string>
<string name="woeid">World ID</string>
<string name="popup">Popup</string>
<string name="pin">PIN</string>
<string name="following">Following</string>
@ -116,6 +115,5 @@
<string name="proxy_address_text">HTTPS Proxy</string>
<string name="proxy_authentication">Authentifierung</string>
<string name="error_empty_port">Proxy port muss gesetzt werden!</string>
<string name="error_woeid_empty">WOEID musst gesetzt werden!</string>
<string name="error_empty_pass">Proxy Passort darf nicht leer sein!</string>
</resources>

View File

@ -1,25 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="location">
<item>World</item>
<item>Germany</item>
<item>France</item>
<item>USA</item>
<item>Mexico</item>
<item>Algeria</item>
<!--Last Element should stay here-->
<item>custom</item>
</string-array>
<integer-array name="woeId">
<item>1</item>
<item>23424829</item>
<item>23424819</item>
<item>23424977</item>
<item>23424900</item>
<item>23424740</item>
<item>0</item>
</integer-array>
</resources>

View File

@ -48,7 +48,6 @@
<string name="answering">answering </string>
<string name="sent_from">sent from: </string>
<string name="progress_kill">stop loading</string>
<string name="woeid">World ID</string>
<string name="mute">mute</string>
<string name="unmute">un-mute</string>
<string name="muted">muted</string>
@ -115,7 +114,6 @@
<string name="proxy_password">password</string>
<string name="proxy_address_text">HTTPS Proxy</string>
<string name="proxy_authentication">Authentication</string>
<string name="error_woeid_empty">WOEID must be set!</string>
<string name="error_empty_port">Port must be set!</string>
<string name="error_empty_pass">Proxy password should not be empty!</string>
</resources>