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() {
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() {

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.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<Item>,
@ -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 {

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.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 {

View File

@ -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<String, CachingAuthenticator>()
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<SuccessResponse> {
return service.loginToSelfoss(config.userLogin, config.userPassword)
}
fun login(): Call<SuccessResponse> =
service.loginToSelfoss(config.userLogin, config.userPassword)
fun readItems(tag: String?, sourceId: Long?, search: String?): Call<List<Item>> =
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)
fun starredItems(tag: String?, sourceId: Long?, search: String?): Call<List<Item>> =
getItems("starred", tag, sourceId, search)
private fun getItems(type: String, tag: String?, sourceId: Long?, search: String?): Call<List<Item>> {
return service.getItems(type, tag, sourceId, search, userName, password)
}
private fun getItems(type: String, tag: String?, sourceId: Long?, search: String?): Call<List<Item>> =
service.getItems(type, tag, sourceId, search, userName, password)
fun markItem(itemId: String): Call<SuccessResponse> {
return service.markAsRead(itemId, userName, password)
}
fun markItem(itemId: String): Call<SuccessResponse> =
service.markAsRead(itemId, userName, password)
fun unmarkItem(itemId: String): Call<SuccessResponse> {
return service.unmarkAsRead(itemId, userName, password)
}
fun unmarkItem(itemId: String): Call<SuccessResponse> =
service.unmarkAsRead(itemId, userName, password)
fun readAll(ids: List<String>): Call<SuccessResponse> {
return service.markAllAsRead(ids, userName, password)
}
fun readAll(ids: List<String>): Call<SuccessResponse> =
service.markAllAsRead(ids, userName, password)
fun starrItem(itemId: String): Call<SuccessResponse> {
return service.starr(itemId, userName, password)
}
fun starrItem(itemId: String): Call<SuccessResponse> =
service.starr(itemId, userName, password)
fun unstarrItem(itemId: String): Call<SuccessResponse> {
return service.unstarr(itemId, userName, password)
}
fun unstarrItem(itemId: String): Call<SuccessResponse> =
service.unstarr(itemId, userName, password)
val stats: Call<Stats>
get() = service.stats(userName, password)
@ -119,23 +103,20 @@ class SelfossApi(c: Context) {
val tags: Call<List<Tag>>
get() = service.tags(userName, password)
fun update(): Call<String> {
return service.update(userName, password)
}
fun update(): Call<String> =
service.update(userName, password)
val sources: Call<List<Sources>>
get() = service.sources(userName, password)
fun deleteSource(id: String): Call<SuccessResponse> {
return service.deleteSource(id, userName, password)
}
fun deleteSource(id: String): Call<SuccessResponse> =
service.deleteSource(id, userName, password)
fun spouts(): Call<Map<String, Spout>> {
return service.spouts(userName, password)
}
fun spouts(): Call<Map<String, Spout>> =
service.spouts(userName, password)
fun createSource(title: String, url: String, spout: String, tags: String, filter: String): Call<SuccessResponse> {
return service.createSource(title, url, spout, tags, filter, userName, password)
}
fun createSource(title: String, url: String, spout: String, tags: String, filter: String): Call<SuccessResponse> =
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 {
@GET("login")
fun loginToSelfoss(@Query("username") username: String, @Query("password") password: String): Call<SuccessResponse>
@ -91,3 +92,4 @@ internal interface SelfossService {
@Query("username") username: String,
@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)
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;
}
});

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