Some code cleaning.

This commit is contained in:
Amine 2017-06-13 22:03:41 +02:00
parent 246ec2c3ac
commit 5320f88230
7 changed files with 83 additions and 95 deletions

View File

@ -537,7 +537,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
private fun getUnRead() { private fun getUnRead() {
elementsShown = UNREAD_SHOWN 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() { private fun getRead() {

View File

@ -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.Item
import apps.amine.bou.readerforselfoss.api.selfoss.SelfossApi import apps.amine.bou.readerforselfoss.api.selfoss.SelfossApi
import apps.amine.bou.readerforselfoss.api.selfoss.SuccessResponse 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.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, class ItemCardAdapter(private val app: Activity,
private val items: ArrayList<Item>, private val items: ArrayList<Item>,
@ -93,14 +90,12 @@ class ItemCardAdapter(private val app: Activity,
val fHolder = holder val fHolder = holder
if (itm.getIcon(c).isEmpty()) { if (itm.getIcon(c).isEmpty()) {
val color = generator.getColor(itm.sourcetitle) 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 =
TextDrawable
val drawable = builder.build(textDrawable.toString(), color) .builder()
.round()
.build(texDrawableFromSource(itm.sourcetitle), color)
holder.sourceImage.setImageDrawable(drawable) holder.sourceImage.setImageDrawable(drawable)
} else { } else {

View File

@ -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.SelfossApi
import apps.amine.bou.readerforselfoss.api.selfoss.Sources import apps.amine.bou.readerforselfoss.api.selfoss.Sources
import apps.amine.bou.readerforselfoss.api.selfoss.SuccessResponse import apps.amine.bou.readerforselfoss.api.selfoss.SuccessResponse
import apps.amine.bou.readerforselfoss.utils.texDrawableFromSource
class SourcesListAdapter(private val app: Activity, class SourcesListAdapter(private val app: Activity,
@ -45,15 +45,13 @@ class SourcesListAdapter(private val app: Activity,
val fHolder = holder val fHolder = holder
if (itm.getIcon(c).isEmpty()) { if (itm.getIcon(c).isEmpty()) {
val color = generator.getColor(itm.title) 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 =
TextDrawable
val drawable = builder.build(textDrawable.toString(), color) .builder()
holder.sourceImage!!.setImageDrawable(drawable) .round()
.build(texDrawableFromSource(itm.title), color)
holder.sourceImage.setImageDrawable(drawable)
} else { } else {
Glide Glide
.with(c) .with(c)
@ -64,12 +62,12 @@ class SourcesListAdapter(private val app: Activity,
override fun setResource(resource: Bitmap) { override fun setResource(resource: Bitmap) {
val circularBitmapDrawable = RoundedBitmapDrawableFactory.create(c.resources, resource) val circularBitmapDrawable = RoundedBitmapDrawableFactory.create(c.resources, resource)
circularBitmapDrawable.isCircular = true 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 { override fun getItemCount(): Int {
@ -77,8 +75,8 @@ class SourcesListAdapter(private val app: Activity,
} }
inner class ViewHolder(internal val mView: ConstraintLayout) : RecyclerView.ViewHolder(mView) { inner class ViewHolder(internal val mView: ConstraintLayout) : RecyclerView.ViewHolder(mView) {
var sourceImage: ImageView? = null lateinit var sourceImage: ImageView
var sourceTitle: TextView? = null lateinit var sourceTitle: TextView
init { init {

View File

@ -12,7 +12,6 @@ import com.burgstaller.okhttp.digest.Credentials
import com.burgstaller.okhttp.digest.DigestAuthenticator import com.burgstaller.okhttp.digest.DigestAuthenticator
import com.google.gson.GsonBuilder import com.google.gson.GsonBuilder
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Call import retrofit2.Call
import retrofit2.Retrofit import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory 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) { class SelfossApi(c: Context) {
private val service: SelfossService private val service: SelfossService
@ -30,41 +30,33 @@ class SelfossApi(c: Context) {
init { init {
val interceptor = HttpLoggingInterceptor()
interceptor.level = HttpLoggingInterceptor.Level.BODY
val httpBuilder = OkHttpClient.Builder()
val authCache = ConcurrentHashMap<String, CachingAuthenticator>() val authCache = ConcurrentHashMap<String, CachingAuthenticator>()
val httpUserName = config.httpUserLogin val httpUserName = config.httpUserLogin
val httpPassword = config.httpUserPassword val httpPassword = config.httpUserPassword
val credentials = Credentials(httpUserName, httpPassword) val credentials = Credentials(httpUserName, httpPassword)
val basicAuthenticator = BasicAuthenticator(credentials) userName = config.userLogin
val digestAuthenticator = DigestAuthenticator(credentials) password = config.userPassword
// note that all auth schemes should be registered as lowercase!
val authenticator = DispatchingAuthenticator.Builder() val authenticator = DispatchingAuthenticator.Builder()
.with("digest", digestAuthenticator) .with("digest", DigestAuthenticator(credentials))
.with("basic", basicAuthenticator) .with("basic", BasicAuthenticator(credentials))
.build() .build()
val client = httpBuilder val client =
OkHttpClient
.Builder()
.authenticator(CachingAuthenticatorDecorator(authenticator, authCache)) .authenticator(CachingAuthenticatorDecorator(authenticator, authCache))
.addInterceptor(AuthenticationCacheInterceptor(authCache)) .addInterceptor(AuthenticationCacheInterceptor(authCache))
.addInterceptor(interceptor)
.build() .build()
val gson =
val builder = GsonBuilder() GsonBuilder()
builder.registerTypeAdapter(Boolean::class.javaPrimitiveType, BooleanTypeAdapter()) .registerTypeAdapter(Boolean::class.javaPrimitiveType, BooleanTypeAdapter())
val gson = builder
.setLenient() .setLenient()
.create() .create()
userName = config.userLogin
password = config.userPassword
val retrofit = val retrofit =
Retrofit Retrofit
.Builder() .Builder()
@ -75,43 +67,35 @@ class SelfossApi(c: Context) {
service = retrofit.create(SelfossService::class.java) service = retrofit.create(SelfossService::class.java)
} }
fun login(): Call<SuccessResponse> { fun login(): Call<SuccessResponse> =
return service.loginToSelfoss(config.userLogin, config.userPassword) service.loginToSelfoss(config.userLogin, config.userPassword)
}
fun readItems(tag: String?, sourceId: Long?, search: String?): Call<List<Item>> = fun readItems(tag: String?, sourceId: Long?, search: String?): Call<List<Item>> =
getItems("read", tag, sourceId, search) getItems("read", tag, sourceId, search)
fun unreadItems(tag: String?, sourceId: Long?, search: String?): Call<List<Item>> = fun newItems(tag: String?, sourceId: Long?, search: String?): Call<List<Item>> =
getItems("unread", tag, sourceId, search) getItems("unread", tag, sourceId, search)
fun starredItems(tag: String?, sourceId: Long?, search: String?): Call<List<Item>> = fun starredItems(tag: String?, sourceId: Long?, search: String?): Call<List<Item>> =
getItems("starred", tag, sourceId, search) getItems("starred", tag, sourceId, search)
private fun getItems(type: String, tag: String?, sourceId: Long?, search: String?): Call<List<Item>> { private fun getItems(type: String, tag: String?, sourceId: Long?, search: String?): Call<List<Item>> =
return service.getItems(type, tag, sourceId, search, userName, password) service.getItems(type, tag, sourceId, search, userName, password)
}
fun markItem(itemId: String): Call<SuccessResponse> { fun markItem(itemId: String): Call<SuccessResponse> =
return service.markAsRead(itemId, userName, password) service.markAsRead(itemId, userName, password)
}
fun unmarkItem(itemId: String): Call<SuccessResponse> { fun unmarkItem(itemId: String): Call<SuccessResponse> =
return service.unmarkAsRead(itemId, userName, password) service.unmarkAsRead(itemId, userName, password)
}
fun readAll(ids: List<String>): Call<SuccessResponse> { fun readAll(ids: List<String>): Call<SuccessResponse> =
return service.markAllAsRead(ids, userName, password) service.markAllAsRead(ids, userName, password)
}
fun starrItem(itemId: String): Call<SuccessResponse> { fun starrItem(itemId: String): Call<SuccessResponse> =
return service.starr(itemId, userName, password) service.starr(itemId, userName, password)
}
fun unstarrItem(itemId: String): Call<SuccessResponse> =
fun unstarrItem(itemId: String): Call<SuccessResponse> { service.unstarr(itemId, userName, password)
return service.unstarr(itemId, userName, password)
}
val stats: Call<Stats> val stats: Call<Stats>
get() = service.stats(userName, password) get() = service.stats(userName, password)
@ -119,23 +103,20 @@ class SelfossApi(c: Context) {
val tags: Call<List<Tag>> val tags: Call<List<Tag>>
get() = service.tags(userName, password) get() = service.tags(userName, password)
fun update(): Call<String> { fun update(): Call<String> =
return service.update(userName, password) service.update(userName, password)
}
val sources: Call<List<Sources>> val sources: Call<List<Sources>>
get() = service.sources(userName, password) get() = service.sources(userName, password)
fun deleteSource(id: String): Call<SuccessResponse> { fun deleteSource(id: String): Call<SuccessResponse> =
return service.deleteSource(id, userName, password) service.deleteSource(id, userName, password)
}
fun spouts(): Call<Map<String, Spout>> { fun spouts(): Call<Map<String, Spout>> =
return service.spouts(userName, password) service.spouts(userName, password)
}
fun createSource(title: String, url: String, spout: String, tags: String, filter: String): Call<SuccessResponse> { fun createSource(title: String, url: String, spout: String, tags: String, filter: String): Call<SuccessResponse> =
return service.createSource(title, url, spout, tags, filter, userName, password) service.createSource(title, url, spout, tags, filter, userName, password)
}
} }
// codebeat:enable[ARITY,TOO_MANY_FUNCTIONS]

View File

@ -11,6 +11,7 @@ import retrofit2.http.Query
// codebeat:disable[ARITY]
internal interface SelfossService { internal interface SelfossService {
@GET("login") @GET("login")
fun loginToSelfoss(@Query("username") username: String, @Query("password") password: String): Call<SuccessResponse> fun loginToSelfoss(@Query("username") username: String, @Query("password") password: String): Call<SuccessResponse>
@ -91,3 +92,4 @@ internal interface SelfossService {
@Query("username") username: String, @Query("username") username: String,
@Query("password") password: String): Call<SuccessResponse> @Query("password") password: String): Call<SuccessResponse>
} }
// codebeat:disable[ARITY]

View File

@ -162,18 +162,21 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
*/ */
@TargetApi(Build.VERSION_CODES.HONEYCOMB) @TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class LinksPreferenceFragment extends PreferenceFragment { public static class LinksPreferenceFragment extends PreferenceFragment {
public void openUrl(Uri uri) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(browserIntent);
}
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_links); addPreferencesFromResource(R.xml.pref_links);
setHasOptionsMenu(true); setHasOptionsMenu(true);
Preference tracker = findPreference( "trackerLink" ); findPreference( "trackerLink" ).setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
tracker.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override @Override
public boolean onPreferenceClick(Preference preference) { public boolean onPreferenceClick(Preference preference) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.tracker_url))); openUrl(Uri.parse(getString(R.string.tracker_url)));
startActivity(browserIntent);
return true; return true;
} }
}); });
@ -181,8 +184,7 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
findPreference("sourceLink").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { findPreference("sourceLink").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override @Override
public boolean onPreferenceClick(Preference preference) { public boolean onPreferenceClick(Preference preference) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.source_url))); openUrl(Uri.parse(getString(R.string.source_url)));
startActivity(browserIntent);
return false; return false;
} }
}); });

View File

@ -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()
}