Skip to content

Instantly share code, notes, and snippets.

View NsAveek's full-sized avatar
🏠
Working from home

Aveek NsAveek

🏠
Working from home
View GitHub Profile
@NsAveek
NsAveek / UploadFile.kt
Created October 20, 2018 05:37
Upload file using Retrofit
private void uploadFile(File file){
RequestBody videoBody = RequestBody.create(MediaType.parse("video/"), file);
MultipartBody.Part vFile = MultipartBody.Part.createFormData("upload-file", file.getName(), videoBody);
UploadVideoService videoService = RetrofitClientInstance.Companion.getRetrofitInstance();
Call<ResponseBody> serverCom=videoService.uploadVideo(vFile);
serverCom.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
ResponseBody body = response.body();
try {
// having "with"
with(viewModel){
val balanceText = this.balanceText
val category = this.category.run {
value = false
}
}
// without using "with"
val balanceText = viewmodel.balanceText
// Without run
fun printPersonName(person : Person){
print(person.name)
}
// With run
fun printPersonName(person : Person) = person.run{
print(name)
}
// Without Let
if(person!=null){
doSomething()
}
// With Let
person?.let{
doSomething()
}
val person = Person().apply{
name = "aveek"
tel = "+98234552344"
}.also{
Log.d("test",it.name)
}
activity.setResult(Activity.RESULT_OK, Intent().apply { // this:intent
putExtra("response", true)
putExtra("message", "success")
putExtra("result", "ok")
})
activity.setResult(Activity.RESULT_OK, Intent().also { // it:intent
it.putExtra("response", true)
it.putExtra("message", "success")
it.putExtra("result", "ok")
const val addFragment = 1
const val replaceFragment = 2
private inline fun generateFragmentTransaction(fragmentToReplaceOrAdd : Fragment, addOrReplace: Int) : FragmentTransaction {
return when(addOrReplace) {
addFragment -> supportFragmentManager.beginTransaction().add(R.id.container_frame, fragmentToReplaceOrAdd)
else -> supportFragmentManager.beginTransaction().replace(R.id.container_frame, fragmentToReplaceOrAdd)
}
}
private fun fragmentAddOrReplacer(name : String, transaction : ()-> FragmentTransaction){
transaction().apply {
/** Represents Module for Test Management
* @author Aveek
* @author www.myproject.com
* @version 1
* @since 4.4.0 - Version
*/
@Module
internal class TestModule (val context : TestManagement) {
/**
* provides viewModel of the XML
@NsAveek
NsAveek / BaseStrategy.kt
Last active March 6, 2019 15:01
Interface for Strategy Design Pattern
interface IBaseStrategy {
fun isEnable(enable : Boolean)
}
interface IPendingStrategy : IBaseStrategy{
}
interface IErrorStrategy : IBaseStrategy{
var OnReUpload : (result : Result)-> Unit
}
interface IManageStrategy : IBaseStrategy{
@NsAveek
NsAveek / StateViewHolder.kt
Last active March 4, 2019 16:01
A view holder class with State design pattern implemented
class StateViewHolder(binding: ViewDataBinding) : RecyclerView.ViewHolder(binding.root) {
lateinit var viewModel: BaseViewModel
fun bind(info: Pair<Pair<Boolean, String>, Int>, remaining: Int) {
@DrawableRes val icon: Int
val stateContext: BaseStateContext
if (AppFlavor.country == Country.Bangladesh) {
icon=R.drawable.ico_state_1