From d4617560fe22e116ff3b199775ea5b6cf1b75e48 Mon Sep 17 00:00:00 2001 From: NudeDude Date: Sat, 11 Aug 2018 20:05:12 +0200 Subject: [PATCH] bugfix, code cleanup --- .../org/nuclearfog/twidda/MainActivity.java | 101 +++++++++--------- .../twidda/backend/GlobalSettings.java | 10 ++ .../twidda/backend/ProfileLoader.java | 28 +++-- .../twidda/backend/StatusLoader.java | 3 +- .../twidda/viewadapter/LogAdapter.java | 7 +- app/src/main/res/layout/item_error.xml | 19 ---- 6 files changed, 75 insertions(+), 93 deletions(-) delete mode 100644 app/src/main/res/layout/item_error.xml diff --git a/app/src/main/java/org/nuclearfog/twidda/MainActivity.java b/app/src/main/java/org/nuclearfog/twidda/MainActivity.java index 6f7a5680..638352b3 100644 --- a/app/src/main/java/org/nuclearfog/twidda/MainActivity.java +++ b/app/src/main/java/org/nuclearfog/twidda/MainActivity.java @@ -3,6 +3,7 @@ package org.nuclearfog.twidda; import android.content.Intent; import android.os.Bundle; import android.support.v4.widget.SwipeRefreshLayout; +import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; @@ -15,6 +16,7 @@ import android.view.ViewGroup; import android.view.animation.Animation; import android.view.animation.TranslateAnimation; import android.widget.TabHost; +import android.widget.TabHost.OnTabChangeListener; import android.widget.TabHost.TabSpec; import org.nuclearfog.twidda.backend.GlobalSettings; @@ -30,8 +32,7 @@ import org.nuclearfog.twidda.window.TweetDetail; import org.nuclearfog.twidda.window.TweetPopup; import org.nuclearfog.twidda.window.UserProfile; -public class MainActivity extends AppCompatActivity implements - SwipeRefreshLayout.OnRefreshListener, TabHost.OnTabChangeListener, +public class MainActivity extends AppCompatActivity implements OnRefreshListener, OnTabChangeListener, TimelineRecycler.OnItemClicked, TrendRecycler.OnItemClicked { private SwipeRefreshLayout timelineReload,trendReload,mentionReload; @@ -55,21 +56,61 @@ public class MainActivity extends AppCompatActivity implements TwitterEngine mTwitter = TwitterEngine.getInstance(this); settings = GlobalSettings.getInstance(this); boolean login = mTwitter.loggedIn(); + if( !login ) { Intent i = new Intent(this, LoginPage.class); startActivityForResult(i,REQ_CODE); - } else { - login(); } + + homeId = settings.getUserId(); + timelineList = findViewById(R.id.tl_list); + trendList = findViewById(R.id.tr_list); + mentionList = findViewById(R.id.m_list); + timelineReload = findViewById(R.id.timeline); + trendReload = findViewById(R.id.trends); + mentionReload = findViewById(R.id.mention); + tabhost = findViewById(R.id.main_tabhost); + toolbar = findViewById(R.id.profile_toolbar); + + timelineList.setLayoutManager(new LinearLayoutManager(this)); + timelineList.setHasFixedSize(true); + + trendList.setLayoutManager(new LinearLayoutManager(this)); + trendList.setHasFixedSize(true); + + mentionList.setLayoutManager(new LinearLayoutManager(this)); + mentionList.setHasFixedSize(true); + + setSupportActionBar(toolbar); + if (getSupportActionBar() != null) + getSupportActionBar().setDisplayShowTitleEnabled(false); + + tabhost.setup(); + TabSpec tab1 = tabhost.newTabSpec("timeline"); + tab1.setContent(R.id.timeline); + tab1.setIndicator("", getDrawable(R.drawable.home)); + tabhost.addTab(tab1); + TabSpec tab2 = tabhost.newTabSpec("trends"); + tab2.setContent(R.id.trends); + tab2.setIndicator("", getDrawable(R.drawable.hash)); + tabhost.addTab(tab2); + TabSpec tab3 = tabhost.newTabSpec("mention"); + tab3.setContent(R.id.mention); + tab3.setIndicator("", getDrawable(R.drawable.mention)); + tabhost.addTab(tab3); + + tabhost.setOnTabChangedListener(this); + timelineReload.setOnRefreshListener(this); + trendReload.setOnRefreshListener(this); + mentionReload.setOnRefreshListener(this); + setTabContent(); } @Override protected void onActivityResult(int reqCode, int returnCode, Intent i) { super.onActivityResult(reqCode,returnCode,i); if(reqCode == REQ_CODE) { - if(returnCode == RESULT_OK) { - login(); - } else { + if (returnCode != RESULT_OK) { finish(); } } @@ -275,52 +316,6 @@ public class MainActivity extends AppCompatActivity implements } - private void login() { - homeId = settings.getUserId(); - timelineList = findViewById(R.id.tl_list); - trendList = findViewById(R.id.tr_list); - mentionList = findViewById(R.id.m_list); - timelineReload = findViewById(R.id.timeline); - trendReload = findViewById(R.id.trends); - mentionReload = findViewById(R.id.mention); - tabhost = findViewById(R.id.main_tabhost); - toolbar = findViewById(R.id.profile_toolbar); - - timelineList.setLayoutManager(new LinearLayoutManager(this)); - timelineList.setHasFixedSize(true); - - trendList.setLayoutManager(new LinearLayoutManager(this)); - trendList.setHasFixedSize(true); - - mentionList.setLayoutManager(new LinearLayoutManager(this)); - mentionList.setHasFixedSize(true); - - setSupportActionBar(toolbar); - if(getSupportActionBar() != null) - getSupportActionBar().setDisplayShowTitleEnabled(false); - - tabhost.setup(); - TabSpec tab1 = tabhost.newTabSpec("timeline"); - tab1.setContent(R.id.timeline); - tab1.setIndicator("",getDrawable(R.drawable.home)); - tabhost.addTab(tab1); - TabSpec tab2 = tabhost.newTabSpec("trends"); - tab2.setContent(R.id.trends); - tab2.setIndicator("",getDrawable(R.drawable.hash)); - tabhost.addTab(tab2); - TabSpec tab3 = tabhost.newTabSpec("mention"); - tab3.setContent(R.id.mention); - tab3.setIndicator("",getDrawable(R.drawable.mention)); - tabhost.addTab(tab3); - - tabhost.setOnTabChangedListener(this); - timelineReload.setOnRefreshListener(this); - trendReload.setOnRefreshListener(this); - mentionReload.setOnRefreshListener(this); - setTabContent(); - } - - private void setTabContent() { int background = settings.getBackgroundColor(); int fontColor = settings.getFontColor(); diff --git a/app/src/main/java/org/nuclearfog/twidda/backend/GlobalSettings.java b/app/src/main/java/org/nuclearfog/twidda/backend/GlobalSettings.java index 347f8ae3..c7a98ebf 100644 --- a/app/src/main/java/org/nuclearfog/twidda/backend/GlobalSettings.java +++ b/app/src/main/java/org/nuclearfog/twidda/backend/GlobalSettings.java @@ -4,12 +4,17 @@ import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; +import java.text.SimpleDateFormat; +import java.util.Locale; + public class GlobalSettings { private static GlobalSettings ourInstance; private SharedPreferences settings; + private SimpleDateFormat sdf; + private int background_color; private int font_color; private int highlight_color; @@ -40,6 +45,7 @@ public class GlobalSettings { key1 = settings.getString("key1",""); key2 = settings.getString("key2",""); userId = settings.getLong("userID",-1L); + sdf = new SimpleDateFormat("dd.MM.yyyy, HH:mm:ss", Locale.GERMANY); // TODO editable date format } public int getBackgroundColor() { @@ -89,6 +95,10 @@ public class GlobalSettings { return userId; } + public SimpleDateFormat getDateFormatter() { + return sdf; + } + public void setBackgroundColor(int color) { Editor edit = settings.edit(); diff --git a/app/src/main/java/org/nuclearfog/twidda/backend/ProfileLoader.java b/app/src/main/java/org/nuclearfog/twidda/backend/ProfileLoader.java index 1848eaa8..d49c1070 100644 --- a/app/src/main/java/org/nuclearfog/twidda/backend/ProfileLoader.java +++ b/app/src/main/java/org/nuclearfog/twidda/backend/ProfileLoader.java @@ -25,7 +25,6 @@ import java.lang.ref.WeakReference; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; -import java.util.Locale; import twitter4j.TwitterException; @@ -44,9 +43,9 @@ public class ProfileLoader extends AsyncTask { private String profileImage, link, dateString; private TimelineRecycler homeTl, homeFav; private WeakReference ui; + private SimpleDateFormat sdf; private TwitterEngine mTwitter; private ErrorLog errorLog; - private int font, highlight; private long homeId; private boolean imgEnabled; private boolean isHome = false; @@ -66,9 +65,10 @@ public class ProfileLoader extends AsyncTask { ui = new WeakReference<>((UserProfile)context); mTwitter = TwitterEngine.getInstance(context); GlobalSettings settings = GlobalSettings.getInstance(context); + sdf = settings.getDateFormatter(); errorLog = new ErrorLog(ui.get()); - font = settings.getFontColor(); - highlight = settings.getHighlightColor(); + int font = settings.getFontColor(); + int highlight = settings.getHighlightColor(); imgEnabled = settings.loadImages(); homeId = settings.getUserId(); RecyclerView profileTweets = ui.get().findViewById(R.id.ht_list); @@ -78,10 +78,14 @@ public class ProfileLoader extends AsyncTask { if(homeTl == null) { homeTl = new TimelineRecycler(ui.get()); + homeTl.setColor(highlight, font); + homeTl.toggleImage(imgEnabled); profileTweets.setAdapter(homeTl); } if(homeFav == null) { homeFav = new TimelineRecycler(ui.get()); + homeFav.setColor(highlight, font); + homeFav.toggleImage(imgEnabled); profileFavorits.setAdapter(homeFav); } } @@ -122,12 +126,11 @@ public class ProfileLoader extends AsyncTask { follower = Integer.toString(user.follower); following = Integer.toString(user.following); profileImage = user.profileImg; - Date d = new Date(user.created); - SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy, HH:mm:ss", Locale.GERMANY); - dateString = "seit "+ sdf.format(d); + Date time = new Date(user.created); + dateString = "seit " + sdf.format(time); description = description.replace('\n', ' '); - if(MODE == GET_TWEETS) + if (MODE == GET_TWEETS && !isLocked) { List tweets; @@ -144,18 +147,15 @@ public class ProfileLoader extends AsyncTask { } } homeTl.setData(tweets); - homeTl.setColor(highlight,font); - homeTl.toggleImage(imgEnabled); - } - else if(MODE == GET_FAVS) + } else if (MODE == GET_FAVS && !isLocked) { List favorits; - if(homeFav.getItemCount() > 0) { id = homeFav.getItemId(0); favorits = mTwitter.getUserFavs(userId,args[2],id); database.storeUserFavs(favorits,userId); favorits.addAll(homeFav.getData()); + } else { favorits = database.getUserFavs(userId); if(favorits.size() == 0 && !isLocked) { @@ -164,8 +164,6 @@ public class ProfileLoader extends AsyncTask { } } homeFav.setData(favorits); - homeFav.setColor(highlight,font); - homeFav.toggleImage(imgEnabled); } else if(MODE == ACTION_FOLLOW) { diff --git a/app/src/main/java/org/nuclearfog/twidda/backend/StatusLoader.java b/app/src/main/java/org/nuclearfog/twidda/backend/StatusLoader.java index e420452d..30dcbe8b 100644 --- a/app/src/main/java/org/nuclearfog/twidda/backend/StatusLoader.java +++ b/app/src/main/java/org/nuclearfog/twidda/backend/StatusLoader.java @@ -31,7 +31,6 @@ import org.nuclearfog.twidda.window.TweetDetail; import java.lang.ref.WeakReference; import java.text.SimpleDateFormat; import java.util.List; -import java.util.Locale; import twitter4j.TwitterException; @@ -65,7 +64,7 @@ public class StatusLoader extends AsyncTask { public StatusLoader(Context context) { mTwitter = TwitterEngine.getInstance(context); GlobalSettings settings = GlobalSettings.getInstance(context); - sdf = new SimpleDateFormat("dd.MM.yyyy, HH:mm:ss", Locale.GERMANY); + sdf = settings.getDateFormatter(); font = settings.getFontColor(); highlight = settings.getHighlightColor(); toggleImg = settings.loadImages(); diff --git a/app/src/main/java/org/nuclearfog/twidda/viewadapter/LogAdapter.java b/app/src/main/java/org/nuclearfog/twidda/viewadapter/LogAdapter.java index bd55f981..28a9c360 100644 --- a/app/src/main/java/org/nuclearfog/twidda/viewadapter/LogAdapter.java +++ b/app/src/main/java/org/nuclearfog/twidda/viewadapter/LogAdapter.java @@ -8,8 +8,6 @@ import android.view.View; import android.view.ViewGroup; import android.widget.TextView; -import org.nuclearfog.twidda.R; - import java.util.List; public class LogAdapter extends Adapter { @@ -28,7 +26,7 @@ public class LogAdapter extends Adapter { @NonNull @Override public LogAdapter.ItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { - View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_error, parent,false); + View v = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_1, parent, false); return new ItemHolder(v); } @@ -36,13 +34,14 @@ public class LogAdapter extends Adapter { public void onBindViewHolder(@NonNull ItemHolder vh, int index) { vh.message.setText(messages.get(index)); vh.message.setTextColor(0xffff0000); + vh.message.setTextSize(12.0f); } class ItemHolder extends RecyclerView.ViewHolder { public final TextView message; ItemHolder(View v) { super(v); - message = v.findViewById(R.id.errortext); + message = v.findViewById(android.R.id.text1); } } } \ No newline at end of file diff --git a/app/src/main/res/layout/item_error.xml b/app/src/main/res/layout/item_error.xml deleted file mode 100644 index a7abd077..00000000 --- a/app/src/main/res/layout/item_error.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - - - - \ No newline at end of file