From 5320f88230d867181b8e0f4d60a6d1221820467d Mon Sep 17 00:00:00 2001 From: Amine Date: Tue, 13 Jun 2017 22:03:41 +0200 Subject: [PATCH] Some code cleaning. --- .../bou/readerforselfoss/HomeActivity.kt | 2 +- .../adapters/ItemCardAdapter.kt | 17 +-- .../adapters/SourcesListAdapter.kt | 24 ++-- .../api/selfoss/SelfossApi.kt | 109 ++++++++---------- .../api/selfoss/SelfossService.kt | 2 + .../settings/SettingsActivity.java | 14 ++- .../bou/readerforselfoss/utils/ItemsUtils.kt | 10 ++ 7 files changed, 83 insertions(+), 95 deletions(-) create mode 100644 app/src/main/java/apps/amine/bou/readerforselfoss/utils/ItemsUtils.kt diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt index e2fee6d..e701665 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt @@ -537,7 +537,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { private fun getUnRead() { elementsShown = UNREAD_SHOWN - doCallTo(R.string.cant_get_new_elements){t, id, f -> api.unreadItems(t, id, f)} + doCallTo(R.string.cant_get_new_elements){t, id, f -> api.newItems(t, id, f)} } private fun getRead() { diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemCardAdapter.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemCardAdapter.kt index c09e19f..adfecc2 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemCardAdapter.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemCardAdapter.kt @@ -35,11 +35,8 @@ import apps.amine.bou.readerforselfoss.R import apps.amine.bou.readerforselfoss.api.selfoss.Item import apps.amine.bou.readerforselfoss.api.selfoss.SelfossApi import apps.amine.bou.readerforselfoss.api.selfoss.SuccessResponse -import apps.amine.bou.readerforselfoss.utils.buildCustomTabsIntent +import apps.amine.bou.readerforselfoss.utils.* import apps.amine.bou.readerforselfoss.utils.customtabs.CustomTabActivityHelper -import apps.amine.bou.readerforselfoss.utils.openInBrowser -import apps.amine.bou.readerforselfoss.utils.openItemUrl -import apps.amine.bou.readerforselfoss.utils.shareLink class ItemCardAdapter(private val app: Activity, private val items: ArrayList, @@ -93,14 +90,12 @@ class ItemCardAdapter(private val app: Activity, val fHolder = holder if (itm.getIcon(c).isEmpty()) { val color = generator.getColor(itm.sourcetitle) - val textDrawable = StringBuilder() - for (s in itm.sourcetitle.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()) { - textDrawable.append(s[0]) - } - val builder = TextDrawable.builder().round() - - val drawable = builder.build(textDrawable.toString(), color) + val drawable = + TextDrawable + .builder() + .round() + .build(texDrawableFromSource(itm.sourcetitle), color) holder.sourceImage.setImageDrawable(drawable) } else { diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/SourcesListAdapter.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/SourcesListAdapter.kt index 1ded8ef..9c10893 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/SourcesListAdapter.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/SourcesListAdapter.kt @@ -25,7 +25,7 @@ import apps.amine.bou.readerforselfoss.R import apps.amine.bou.readerforselfoss.api.selfoss.SelfossApi import apps.amine.bou.readerforselfoss.api.selfoss.Sources import apps.amine.bou.readerforselfoss.api.selfoss.SuccessResponse - +import apps.amine.bou.readerforselfoss.utils.texDrawableFromSource class SourcesListAdapter(private val app: Activity, @@ -45,15 +45,13 @@ class SourcesListAdapter(private val app: Activity, val fHolder = holder if (itm.getIcon(c).isEmpty()) { val color = generator.getColor(itm.title) - val textDrawable = StringBuilder() - for (s in itm.title.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()) { - textDrawable.append(s[0]) - } - val builder = TextDrawable.builder().round() - - val drawable = builder.build(textDrawable.toString(), color) - holder.sourceImage!!.setImageDrawable(drawable) + val drawable = + TextDrawable + .builder() + .round() + .build(texDrawableFromSource(itm.title), color) + holder.sourceImage.setImageDrawable(drawable) } else { Glide .with(c) @@ -64,12 +62,12 @@ class SourcesListAdapter(private val app: Activity, override fun setResource(resource: Bitmap) { val circularBitmapDrawable = RoundedBitmapDrawableFactory.create(c.resources, resource) circularBitmapDrawable.isCircular = true - fHolder.sourceImage!!.setImageDrawable(circularBitmapDrawable) + fHolder.sourceImage.setImageDrawable(circularBitmapDrawable) } }) } - holder.sourceTitle!!.text = itm.title + holder.sourceTitle.text = itm.title } override fun getItemCount(): Int { @@ -77,8 +75,8 @@ class SourcesListAdapter(private val app: Activity, } inner class ViewHolder(internal val mView: ConstraintLayout) : RecyclerView.ViewHolder(mView) { - var sourceImage: ImageView? = null - var sourceTitle: TextView? = null + lateinit var sourceImage: ImageView + lateinit var sourceTitle: TextView init { diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossApi.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossApi.kt index 256146c..035accc 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossApi.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossApi.kt @@ -12,7 +12,6 @@ import com.burgstaller.okhttp.digest.Credentials import com.burgstaller.okhttp.digest.DigestAuthenticator import com.google.gson.GsonBuilder import okhttp3.OkHttpClient -import okhttp3.logging.HttpLoggingInterceptor import retrofit2.Call import retrofit2.Retrofit import retrofit2.converter.gson.GsonConverterFactory @@ -21,6 +20,7 @@ import apps.amine.bou.readerforselfoss.utils.Config +// codebeat:disable[ARITY,TOO_MANY_FUNCTIONS] class SelfossApi(c: Context) { private val service: SelfossService @@ -30,41 +30,33 @@ class SelfossApi(c: Context) { init { - val interceptor = HttpLoggingInterceptor() - interceptor.level = HttpLoggingInterceptor.Level.BODY - - val httpBuilder = OkHttpClient.Builder() val authCache = ConcurrentHashMap() val httpUserName = config.httpUserLogin val httpPassword = config.httpUserPassword - val credentials = Credentials(httpUserName, httpPassword) - val basicAuthenticator = BasicAuthenticator(credentials) - val digestAuthenticator = DigestAuthenticator(credentials) - - // note that all auth schemes should be registered as lowercase! - val authenticator = DispatchingAuthenticator.Builder() - .with("digest", digestAuthenticator) - .with("basic", basicAuthenticator) - .build() - - val client = httpBuilder - .authenticator(CachingAuthenticatorDecorator(authenticator, authCache)) - .addInterceptor(AuthenticationCacheInterceptor(authCache)) - .addInterceptor(interceptor) - .build() - - - val builder = GsonBuilder() - builder.registerTypeAdapter(Boolean::class.javaPrimitiveType, BooleanTypeAdapter()) - - val gson = builder - .setLenient() - .create() - userName = config.userLogin password = config.userPassword + + val authenticator = DispatchingAuthenticator.Builder() + .with("digest", DigestAuthenticator(credentials)) + .with("basic", BasicAuthenticator(credentials)) + .build() + + val client = + OkHttpClient + .Builder() + .authenticator(CachingAuthenticatorDecorator(authenticator, authCache)) + .addInterceptor(AuthenticationCacheInterceptor(authCache)) + .build() + + val gson = + GsonBuilder() + .registerTypeAdapter(Boolean::class.javaPrimitiveType, BooleanTypeAdapter()) + .setLenient() + .create() + + val retrofit = Retrofit .Builder() @@ -75,43 +67,35 @@ class SelfossApi(c: Context) { service = retrofit.create(SelfossService::class.java) } - fun login(): Call { - return service.loginToSelfoss(config.userLogin, config.userPassword) - } + fun login(): Call = + service.loginToSelfoss(config.userLogin, config.userPassword) fun readItems(tag: String?, sourceId: Long?, search: String?): Call> = getItems("read", tag, sourceId, search) - fun unreadItems(tag: String?, sourceId: Long?, search: String?): Call> = + fun newItems(tag: String?, sourceId: Long?, search: String?): Call> = getItems("unread", tag, sourceId, search) fun starredItems(tag: String?, sourceId: Long?, search: String?): Call> = getItems("starred", tag, sourceId, search) - private fun getItems(type: String, tag: String?, sourceId: Long?, search: String?): Call> { - return service.getItems(type, tag, sourceId, search, userName, password) - } + private fun getItems(type: String, tag: String?, sourceId: Long?, search: String?): Call> = + service.getItems(type, tag, sourceId, search, userName, password) - fun markItem(itemId: String): Call { - return service.markAsRead(itemId, userName, password) - } + fun markItem(itemId: String): Call = + service.markAsRead(itemId, userName, password) - fun unmarkItem(itemId: String): Call { - return service.unmarkAsRead(itemId, userName, password) - } + fun unmarkItem(itemId: String): Call = + service.unmarkAsRead(itemId, userName, password) - fun readAll(ids: List): Call { - return service.markAllAsRead(ids, userName, password) - } + fun readAll(ids: List): Call = + service.markAllAsRead(ids, userName, password) - fun starrItem(itemId: String): Call { - return service.starr(itemId, userName, password) - } + fun starrItem(itemId: String): Call = + service.starr(itemId, userName, password) - - fun unstarrItem(itemId: String): Call { - return service.unstarr(itemId, userName, password) - } + fun unstarrItem(itemId: String): Call = + service.unstarr(itemId, userName, password) val stats: Call get() = service.stats(userName, password) @@ -119,23 +103,20 @@ class SelfossApi(c: Context) { val tags: Call> get() = service.tags(userName, password) - fun update(): Call { - return service.update(userName, password) - } + fun update(): Call = + service.update(userName, password) val sources: Call> get() = service.sources(userName, password) - fun deleteSource(id: String): Call { - return service.deleteSource(id, userName, password) - } + fun deleteSource(id: String): Call = + service.deleteSource(id, userName, password) - fun spouts(): Call> { - return service.spouts(userName, password) - } + fun spouts(): Call> = + service.spouts(userName, password) - fun createSource(title: String, url: String, spout: String, tags: String, filter: String): Call { - return service.createSource(title, url, spout, tags, filter, userName, password) - } + fun createSource(title: String, url: String, spout: String, tags: String, filter: String): Call = + service.createSource(title, url, spout, tags, filter, userName, password) } +// codebeat:enable[ARITY,TOO_MANY_FUNCTIONS] \ No newline at end of file diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossService.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossService.kt index b0f0779..ae69d90 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossService.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossService.kt @@ -11,6 +11,7 @@ import retrofit2.http.Query +// codebeat:disable[ARITY] internal interface SelfossService { @GET("login") fun loginToSelfoss(@Query("username") username: String, @Query("password") password: String): Call @@ -91,3 +92,4 @@ internal interface SelfossService { @Query("username") username: String, @Query("password") password: String): Call } +// codebeat:disable[ARITY] \ No newline at end of file diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/settings/SettingsActivity.java b/app/src/main/java/apps/amine/bou/readerforselfoss/settings/SettingsActivity.java index 36d0fb5..2801eaa 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/settings/SettingsActivity.java +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/settings/SettingsActivity.java @@ -162,18 +162,21 @@ public class SettingsActivity extends AppCompatPreferenceActivity { */ @TargetApi(Build.VERSION_CODES.HONEYCOMB) public static class LinksPreferenceFragment extends PreferenceFragment { + public void openUrl(Uri uri) { + Intent browserIntent = new Intent(Intent.ACTION_VIEW, uri); + startActivity(browserIntent); + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.pref_links); setHasOptionsMenu(true); - Preference tracker = findPreference( "trackerLink" ); - tracker.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + findPreference( "trackerLink" ).setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { - Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.tracker_url))); - startActivity(browserIntent); + openUrl(Uri.parse(getString(R.string.tracker_url))); return true; } }); @@ -181,8 +184,7 @@ public class SettingsActivity extends AppCompatPreferenceActivity { findPreference("sourceLink").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { - Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.source_url))); - startActivity(browserIntent); + openUrl(Uri.parse(getString(R.string.source_url))); return false; } }); diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/ItemsUtils.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/ItemsUtils.kt new file mode 100644 index 0000000..259f17e --- /dev/null +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/ItemsUtils.kt @@ -0,0 +1,10 @@ +package apps.amine.bou.readerforselfoss.utils + + +fun texDrawableFromSource(str: String): String { + val textDrawable = StringBuilder() + for (s in str.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()) { + textDrawable.append(s[0]) + } + return textDrawable.toString() +} \ No newline at end of file