Browse Source

-OkHttpClient initializes only once (small optimization)

pull/24/head
Ezerous 8 years ago
parent
commit
2e6f29c60c
  1. 16
      app/src/main/java/gr/thmmy/mthmmy/activities/BaseActivity.java
  2. 2
      app/src/main/java/gr/thmmy/mthmmy/activities/TopicActivity.java
  3. 4
      app/src/main/java/gr/thmmy/mthmmy/sections/recent/RecentFragment.java
  4. 11
      app/src/main/java/gr/thmmy/mthmmy/utils/Thmmy.java

16
app/src/main/java/gr/thmmy/mthmmy/activities/BaseActivity.java

@ -8,10 +8,13 @@ import com.franmontiel.persistentcookiejar.cache.SetCookieCache;
import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor; import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor;
import okhttp3.CookieJar; import okhttp3.CookieJar;
import okhttp3.OkHttpClient;
public class BaseActivity extends AppCompatActivity { public class BaseActivity extends AppCompatActivity {
private static boolean cookiesInit=false; //To initialize cookie stuff only once per app start private static boolean init =false; //To initialize stuff only once per app start
private static OkHttpClient client;
protected static CookieJar cookieJar; protected static CookieJar cookieJar;
protected static SharedPrefsCookiePersistor sharedPrefsCookiePersistor; protected static SharedPrefsCookiePersistor sharedPrefsCookiePersistor;
@ -19,11 +22,14 @@ public class BaseActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
if(!cookiesInit) if(!init)
{ {
sharedPrefsCookiePersistor = new SharedPrefsCookiePersistor(BaseActivity.this); sharedPrefsCookiePersistor = new SharedPrefsCookiePersistor(BaseActivity.this);
cookieJar = new PersistentCookieJar(new SetCookieCache(), sharedPrefsCookiePersistor); cookieJar = new PersistentCookieJar(new SetCookieCache(), sharedPrefsCookiePersistor);
cookiesInit=true; client = new OkHttpClient.Builder()
.cookieJar(cookieJar)
.build();
init =true;
} }
} }
@ -36,4 +42,8 @@ public class BaseActivity extends AppCompatActivity {
public static SharedPrefsCookiePersistor getSharedPrefsCookiePersistor() { public static SharedPrefsCookiePersistor getSharedPrefsCookiePersistor() {
return sharedPrefsCookiePersistor; return sharedPrefsCookiePersistor;
} }
public static OkHttpClient getClient() {
return client;
}
} }

2
app/src/main/java/gr/thmmy/mthmmy/activities/TopicActivity.java

@ -60,7 +60,7 @@ public class TopicActivity extends BaseActivity {
recyclerView.setLayoutManager(new LinearLayoutManager(findViewById(R.id.list).getContext())); recyclerView.setLayoutManager(new LinearLayoutManager(findViewById(R.id.list).getContext()));
recyclerView.setAdapter(topicAdapter); recyclerView.setAdapter(topicAdapter);
client = new OkHttpClient.Builder().build(); client = BaseActivity.getClient();
new TopicTask().execute(extras.getString("TOPIC_URL")); new TopicTask().execute(extras.getString("TOPIC_URL"));
} }

4
app/src/main/java/gr/thmmy/mthmmy/sections/recent/RecentFragment.java

@ -15,6 +15,7 @@ import android.widget.RelativeLayout;
import android.widget.Toast; import android.widget.Toast;
import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.R;
import gr.thmmy.mthmmy.activities.BaseActivity;
import gr.thmmy.mthmmy.data.TopicSummary; import gr.thmmy.mthmmy.data.TopicSummary;
import gr.thmmy.mthmmy.utils.CustomRecyclerView; import gr.thmmy.mthmmy.utils.CustomRecyclerView;
@ -88,8 +89,7 @@ public class RecentFragment extends Fragment
sectionNumber = getArguments().getInt(ARG_SECTION_NUMBER); sectionNumber = getArguments().getInt(ARG_SECTION_NUMBER);
client = new OkHttpClient.Builder().build(); client = BaseActivity.getClient();
topicSummaries = new ArrayList<>(); topicSummaries = new ArrayList<>();

11
app/src/main/java/gr/thmmy/mthmmy/utils/Thmmy.java

@ -91,10 +91,7 @@ public class Thmmy
.build(); .build();
} }
OkHttpClient client = new OkHttpClient.Builder() OkHttpClient client = BaseActivity.getClient();
.cookieJar(BaseActivity.getCookieJar())
.build();
try try
{ {
@ -151,6 +148,7 @@ public class Thmmy
} }
//To maintain data between activities/ between activity state change (possibly temporary solution)
public static class LoginData implements Parcelable public static class LoginData implements Parcelable
{ {
private int status; private int status;
@ -240,12 +238,9 @@ public class Thmmy
//--------------------------------------LOGOUT-------------------------------------------------- //--------------------------------------LOGOUT--------------------------------------------------
//Two options: (username, password, duration) or nothing - cookies
public static int logout(HttpUrl logoutLink) public static int logout(HttpUrl logoutLink)
{ {
OkHttpClient client = new OkHttpClient.Builder() OkHttpClient client = BaseActivity.getClient();
.cookieJar(BaseActivity.getCookieJar()) //cookies will be deleted
.build();
Request request = new Request.Builder() Request request = new Request.Builder()
.url(logoutLink) .url(logoutLink)
.build(); .build();

Loading…
Cancel
Save