Created
February 17, 2023 23:03
-
-
Save m07med176/c030a6f0581330d4029c6eddf4c84f94 to your computer and use it in GitHub Desktop.
worker
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
object Constants { | |
const val DATA_SUCCESS = "DATA_SUCCESS" | |
const val DATA_ERROR = "DATA_ERROR" | |
const val WORKER_TAG = "WORKER_TAG" | |
} |
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
// GLIDE | |
implementation 'com.github.bumptech.glide:glide:4.14.2' | |
annotationProcessor 'com.github.bumptech.glide:compiler:4.14.2' | |
// region Retrofit | |
implementation "com.squareup.retrofit2:retrofit:2.9.0" | |
implementation "com.squareup.retrofit2:converter-gson:2.9.0" | |
implementation 'com.google.code.gson:gson:2.10.1' | |
// endregion | |
//Work Manager | |
def work_version = "2.7.1" | |
implementation "androidx.work:work-runtime-ktx:$work_version" |
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
private const val TAG = "MainActivity" | |
class MainActivity : AppCompatActivity() { | |
lateinit var recyclerView: RecyclerView | |
lateinit var myAdapter: ProductsAdapter | |
lateinit var progressDialog: ProgressDialog | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
recyclerView = findViewById(R.id.recyclerView) | |
progressDialog = waitingMessageDialog() | |
progressDialog.show() | |
val workerManager = WorkManager.getInstance(this) | |
val request = OneTimeWorkRequestBuilder<MyWorker>() | |
.addTag(Constants.WORKER_TAG) | |
.build() | |
workerManager.enqueue(request) | |
workerManager.getWorkInfosByTagLiveData(Constants.WORKER_TAG).observe(this, Observer {workInfo-> | |
val info = workInfo?.find { it.id == request.id } | |
when(info?.state){ | |
WorkInfo.State.SUCCEEDED ->{ | |
progressDialog.cancel() | |
val myResponse = info.outputData.getString(Constants.DATA_SUCCESS) | |
val myData= Gson().fromJson(myResponse,ProductHolder::class.java) | |
sendToRecyclerView(myData) | |
} | |
WorkInfo.State.FAILED ->{ | |
progressDialog.cancel() | |
val errorResponse = info.outputData.getString(Constants.DATA_ERROR) | |
Log.d(TAG, "Error: $errorResponse")} | |
else ->{ | |
progressDialog.cancel() | |
} | |
} | |
}) | |
// RetrofiteAPI.getApi().getProducts().enqueue(object : Callback<Root>{ | |
// override fun onResponse(call: Call<Root>, response: Response<Root>) { | |
// if (response.isSuccessful) { | |
// myAdapter = ProductsAdapter(this@MainActivity, response.body()?.products ?: ArrayList()) | |
// recyclerView.setHasFixedSize(true) | |
// recyclerView.adapter = myAdapter | |
// } | |
// } | |
// | |
// override fun onFailure(call: Call<Root>, t: Throwable) { | |
// Log.d(TAG, "onFailure: ") | |
// } | |
// }) | |
} | |
private fun sendToRecyclerView(productHolder: ProductHolder) { | |
myAdapter = ProductsAdapter(this@MainActivity, productHolder.products ?: ArrayList()) | |
recyclerView.setHasFixedSize(true) | |
recyclerView.adapter = myAdapter | |
} | |
fun waitingMessageDialog(): ProgressDialog { | |
val progress: ProgressDialog = ProgressDialog(this) | |
progress.setTitle("Loading") | |
progress.setMessage("Please wait until downloading ..") | |
return progress | |
} | |
} |
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
data class Product( | |
var id:Int = 0, | |
var title: String? = null, | |
var description: String? = null, | |
var price:Int = 0, | |
var discountPercentage:Double = 0.0, | |
var rating:Double = 0.0, | |
var stock:Int = 0, | |
var brand: String? = null, | |
var category: String? = null, | |
var thumbnail: String? = null | |
) | |
data class ProductHolder( | |
var products: ArrayList<Product>? = null, | |
var total:Int = 0, | |
var skip:Int = 0, | |
var limit:Int = 0 | |
) |
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 MyWorker(context: Context,paras: WorkerParameters):Worker(context,paras) { | |
override fun doWork(): Result { | |
val api = RetrofiteAPI.getApi().getProducts() | |
val productData = api.execute() | |
try{ | |
if (productData.isSuccessful) { | |
/// Pair("productData",productData.body().products) | |
// "productData" to productData.body().products | |
val data = Gson().toJson(productData.body()).toString() | |
return Result.success(workDataOf(Constants.DATA_SUCCESS to data)) | |
} else { | |
return Result.failure(workDataOf(Constants.DATA_ERROR to productData.errorBody().toString())) | |
} | |
}catch (e:java.lang.Exception){ | |
return Result.failure(workDataOf(Constants.DATA_ERROR to e.message)) | |
} | |
} | |
} |
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
interface NetworkAPI{ | |
@GET("products") | |
fun getProducts(): Call<ProductHolder> | |
} | |
class RetrofiteAPI { | |
companion object{ | |
fun getApi():NetworkAPI{ | |
val retrofit = Retrofit.Builder() | |
.baseUrl("https://dummyjson.com/") | |
.addConverterFactory(GsonConverterFactory.create()) | |
.build() | |
return retrofit.create(NetworkAPI::class.java) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment