This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BaseApp extends Application { | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
AndroidNetworking.initialize(getApplicationContext()); | |
if (BuildConfig.DEBUG) { | |
//For logging api request and response | |
AndroidNetworking.enableLogging(HttpLoggingInterceptor.Level.BODY); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static final String URLEX = "https://jsonplaceholder.typicode.com/users/{user_id}/albums"; | |
AndroidNetworking.get(URLEx).addPathParameter("user_id","1").build().getAsJSONArray(new JSONArrayRequestListener() { | |
@Override | |
public void onResponse(JSONArray response) { | |
// Once data is received it will available for processing | |
} | |
@Override | |
public void onError(ANError anError) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static final String URLEx = "https://jsonplaceholder.typicode.com/users/{user_id}/albums"; | |
Rx2AndroidNetworking.get(URLEx).addPathParameter("user_id", "1").build().getJSONArraySingle().doOnSuccess(jsonArray -> { | |
//Handle once data is available | |
}).doOnError(throwable -> { | |
//Handle in case of Error | |
}).subscribeOn(Schedulers.io()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User { | |
private String title; | |
private int id, userId; | |
} | |
//Mention the model class to which the data should be parsed if it is list or single just change | |
// the getType and same can be done for normal api requests | |
Rx2AndroidNetworking.get(URLEx).addPathParameter("user_id", "1").build(). | |
getObjectListSingle(User.class). | |
doOnSuccess(jsonArray -> { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PostAdapter : | |
ListAdapter<Post, PostAdapter.ViewHolder>(TaskDiffCallBack()) { | |
//This check runs on background thread | |
class TaskDiffCallBack : DiffUtil.ItemCallback<Post>() { | |
override fun areItemsTheSame(oldItem: Post, newItem: Post): Boolean { | |
Log.d(TAG,Thread.currentThread().name) | |
return oldItem.id == newItem.id; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.carnot.recyclerviewlistadapter | |
import androidx.appcompat.app.AppCompatActivity | |
import android.os.Bundle | |
import kotlinx.android.synthetic.main.activity_main.* | |
class MainActivity : AppCompatActivity() { | |
lateinit var adapter: PostAdapter | |
override fun onCreate(savedInstanceState: Bundle?) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override fun onBindViewHolder(holder: ViewHolder, position: Int) { | |
//to access data from list we have getItem(position) and no need to implement | |
val task = getItem(position) | |
holder.tvDesc.text = task.body | |
holder.tvTitle.text = task.title | |
} | |
//Also no need to implement getItemCount() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Don't add validation in this function as the name of the function only suggest to add it to db | |
fun savePhoneNumber(phoneNumber: String) { | |
if (!phoneNumber.equals("") && phoneNumber.length == 10 && phoneNumber regex check and etc...) { | |
dao.saveNumber(phoneNumber) | |
} | |
} | |
//Do only pass numbers post validation | |
fun savePhoneNumber(phoneNumber: String) { | |
dao.saveNumber(phoneNumber) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Don't pass too many arguments | |
fun saveUserDetails( | |
firstName: String, | |
lastName: String, | |
phoneNumber: String, | |
gender: String, | |
age: Int | |
) {} | |
// Do create a type and pass it as function param |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Helper class which hides the library and exposes only required functionality | |
class ImageLoader { | |
fun setImageFromUrl(url: String, imageView: SomeViewHolder) { | |
// Do your Picasso or what ever library related stuff here | |
Picasso.get().load(url).into(imageView) | |
} | |
} | |
class SomeScreen { |
OlderNewer