cleanup
This commit is contained in:
NudeDude 2018-07-27 10:51:42 +02:00
parent c71a811160
commit 1ecf3d4853
7 changed files with 72 additions and 48 deletions

View File

@ -284,7 +284,7 @@ public class MainActivity extends AppCompatActivity implements
* Login Handle
*/
private void login() {
homeId = TwitterEngine.getHomeId();
homeId = settings.getUserId();
timelineList = findViewById(R.id.tl_list);
trendList = findViewById(R.id.tr_list);
mentionList = findViewById(R.id.m_list);

View File

@ -16,10 +16,13 @@ public class GlobalSettings {
private int tweet_color;
private boolean loadImage;
private boolean loggedIn;
private int row;
private int woeId;
private String key1, key2;
private long userId;
private GlobalSettings(Context context) {
@ -31,6 +34,10 @@ public class GlobalSettings {
tweet_color = settings.getInt("tweet_color",0xff19aae8);
row = settings.getInt("preload",20);
loadImage = settings.getBoolean("image_load", true);
loggedIn = settings.getBoolean("login", false);
key1 = settings.getString("key1", "");
key2 = settings.getString("key2", "");
userId = settings.getLong("userID", -1L);
}
public int getBackgroundColor() {
@ -49,7 +56,6 @@ public class GlobalSettings {
return tweet_color;
}
public boolean loadImages() {
return loadImage;
}
@ -62,6 +68,19 @@ public class GlobalSettings {
return row;
}
public boolean getLogin() { return loggedIn;}
public String[] getKeys() {
String out[] = new String[2];
out[0] = key1;
out[1] = key2;
return out;
}
public long getUserId(){
return userId;
}
public void setBackgroundColor(int color) {
Editor edit = settings.edit();
@ -112,6 +131,26 @@ public class GlobalSettings {
edit.apply();
}
public void setLogin(boolean login) {
Editor edit = settings.edit();
edit.putBoolean("preload", login);
loggedIn = login;
edit.apply();
}
public void setConnection(String key1, String key2, Long userId) {
Editor e = settings.edit();
loggedIn = true;
this.key1 = key1;
this.key2 = key2;
this.userId = userId;
e.putBoolean("login", true);
e.putLong("userID", userId);
e.putString("key1", key1);
e.putString("key2", key2);
e.apply();
}
public static GlobalSettings getInstance(Context context) {
if(ourInstance == null) {

View File

@ -47,6 +47,7 @@ public class ProfileLoader extends AsyncTask<Long,Void,Long> {
private TwitterEngine mTwitter;
private String errMsg = "";
private int font, highlight;
private long homeId;
private boolean imgEnabled;
private boolean isHome = false;
private boolean isFollowing = false;
@ -66,7 +67,7 @@ public class ProfileLoader extends AsyncTask<Long,Void,Long> {
font = settings.getFontColor();
highlight = settings.getHighlightColor();
imgEnabled = settings.loadImages();
homeId = settings.getUserId();
RecyclerView profileTweets = ui.get().findViewById(R.id.ht_list);
RecyclerView profileFavorits = ui.get().findViewById(R.id.hf_list);
homeTl = (TimelineRecycler) profileTweets.getAdapter();
@ -89,7 +90,7 @@ public class ProfileLoader extends AsyncTask<Long,Void,Long> {
final long MODE = args[1];
long id = 1L;
try {
isHome = TwitterEngine.getHomeId() == userId;
isHome = homeId == userId;
if(!isHome && MODE != LOAD_DB)
{
boolean connection[] = mTwitter.getConnection(userId);

View File

@ -2,6 +2,8 @@ package org.nuclearfog.twidda.backend;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.widget.Toast;
@ -15,6 +17,7 @@ public class Registration extends AsyncTask<String, Void, Boolean> {
private WeakReference<LoginPage> ui;
private TwitterEngine mTwitter;
private String errorMessage;
private String redirectionURL = "";
public Registration(Context context) {
@ -28,7 +31,7 @@ public class Registration extends AsyncTask<String, Void, Boolean> {
String pin = twitterPin[0];
try {
if( pin.trim().isEmpty() ) {
mTwitter.request(ui.get());
redirectionURL = mTwitter.request();
} else {
mTwitter.initialize(pin);
return true;
@ -50,7 +53,12 @@ public class Registration extends AsyncTask<String, Void, Boolean> {
if(success) {
connect.setResult(Activity.RESULT_OK);
connect.finish();
} else if(errorMessage != null) {
} else {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(redirectionURL));
connect.startActivity(i);
}
if(errorMessage != null) {
Toast.makeText(connect,"Fehler: "+errorMessage,Toast.LENGTH_LONG).show();
}
}

View File

@ -1,9 +1,6 @@
package org.nuclearfog.twidda.backend;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.support.annotation.NonNull;
import org.nuclearfog.twidda.backend.listitems.Trend;
@ -41,9 +38,9 @@ public class TwitterEngine {
private final String TWITTER_CONSUMER_SECRET = "xxx";
private static TwitterEngine mTwitter;
private static long twitterID = -1L;
private long twitterID = -1L;
private Twitter twitter;
private SharedPreferences settings;
private GlobalSettings settings;
private RequestToken reqToken;
private boolean login;
private int load;
@ -55,8 +52,8 @@ public class TwitterEngine {
* @see #getInstance
*/
private TwitterEngine(Context context) {
settings = context.getSharedPreferences("settings", 0);
login = settings.getBoolean("login", false);
settings = GlobalSettings.getInstance(context);
login = settings.getLogin();
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);
@ -67,16 +64,14 @@ public class TwitterEngine {
/**
* get RequestToken and Open Twitter Registration Website
* @throws TwitterException if Connection is unavailable
* Request Registration Website
* @return Link to App Registration
* @throws TwitterException if internet connection is unavailable
*/
public void request(Context context) throws TwitterException {
public String request() throws TwitterException {
if(reqToken == null)
reqToken = twitter.getOAuthRequestToken();
String redirectURL = reqToken.getAuthenticationURL();
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(redirectURL));
context.startActivity(i);
return reqToken.getAuthenticationURL();
}
@ -121,13 +116,7 @@ public class TwitterEngine {
*/
private void saveCurrentUser(String key1, String key2) throws TwitterException {
twitterID = twitter.getId();
SharedPreferences.Editor e = settings.edit();
e.putBoolean("login", true);
e.putLong("userID",twitterID);
e.putString("username", twitter.getScreenName());
e.putString("key1", key1);
e.putString("key2", key2);
e.apply();
settings.setConnection(key1, key2, twitterID);
}
@ -136,13 +125,11 @@ public class TwitterEngine {
* & initialize Twitter
*/
private void init() {
String key1,key2;
if( login ) {
key1 = settings.getString("key1", " ");
key2 = settings.getString("key2", " ");
initKeys(key1,key2);
String keys[] = settings.getKeys();
initKeys(keys[0],keys[1]);
}
twitterID = settings.getLong("userID", -1L);
twitterID = settings.getUserId();
}
@ -150,7 +137,7 @@ public class TwitterEngine {
* set amount of tweets to be loaded
*/
private void setLoad() {
load = settings.getInt("preload", 20);
load = settings.getRowLimit();
}
@ -592,15 +579,6 @@ public class TwitterEngine {
}
/**
* Return User ID
* @return result
*/
public static long getHomeId() {
return twitterID;
}
/**
* Singleton
* @param context Main Thread Context

View File

@ -19,7 +19,6 @@ import android.widget.TextView;
import org.nuclearfog.twidda.R;
import org.nuclearfog.twidda.backend.GlobalSettings;
import org.nuclearfog.twidda.backend.StatusLoader;
import org.nuclearfog.twidda.backend.TwitterEngine;
import org.nuclearfog.twidda.backend.listitems.Tweet;
import org.nuclearfog.twidda.viewadapter.TimelineRecycler;
@ -37,6 +36,7 @@ public class TweetDetail extends AppCompatActivity implements View.OnClickListen
private long tweetID;
private long userID;
private StatusLoader mStat, mReply;
private GlobalSettings settings;
private String username = "";
@Override
@ -45,7 +45,8 @@ public class TweetDetail extends AppCompatActivity implements View.OnClickListen
setContentView(R.layout.tweetpage);
getExtras(getIntent().getExtras());
boolean home = userID == TwitterEngine.getHomeId();
settings = GlobalSettings.getInstance(this);
boolean home = userID == settings.getUserId();
answer_list = findViewById(R.id.answer_list);
Button retweet = findViewById(R.id.rt_button_detail);
@ -163,7 +164,6 @@ public class TweetDetail extends AppCompatActivity implements View.OnClickListen
}
private void setContent() {
GlobalSettings settings = GlobalSettings.getInstance(this);
int backgroundColor = settings.getBackgroundColor();
int fontColor = settings.getFontColor();
CollapsingToolbarLayout cLayout = findViewById(R.id.tweet_detail);

View File

@ -21,7 +21,6 @@ import android.widget.TextView;
import org.nuclearfog.twidda.R;
import org.nuclearfog.twidda.backend.GlobalSettings;
import org.nuclearfog.twidda.backend.ProfileLoader;
import org.nuclearfog.twidda.backend.TwitterEngine;
import org.nuclearfog.twidda.backend.listitems.Tweet;
import org.nuclearfog.twidda.viewadapter.TimelineRecycler;
@ -54,9 +53,8 @@ public class UserProfile extends AppCompatActivity implements View.OnClickListen
getSupportActionBar().setDisplayShowTitleEnabled(false);
getExtras(getIntent().getExtras());
home = userId == TwitterEngine.getHomeId();
GlobalSettings settings = GlobalSettings.getInstance(this);
home = userId == settings.getUserId();
background = settings.getBackgroundColor();
font_color = settings.getFontColor();
highlight = settings.getHighlightColor();