Skip to content

Commit

Permalink
feat: WorkManager Process Not Perfect MovieDetail
Browse files Browse the repository at this point in the history
  • Loading branch information
biezhihua committed Sep 15, 2018
1 parent b8fc358 commit 80f0e48
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 10 deletions.
9 changes: 9 additions & 0 deletions app/src/main/java/com/bzh/dytt/api/NetworkService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.bzh.dytt.api
import android.arch.lifecycle.LiveData
import com.bzh.dytt.vo.MovieDetail
import com.bzh.dytt.vo.MovieDetailResponse
import retrofit2.Call
import retrofit2.http.Header
import retrofit2.http.POST
import retrofit2.http.Query
Expand All @@ -27,6 +28,14 @@ interface NetworkService {
@Query("categoryId") categoryId: Int,
@Query("movieDetailId") movieDetailId: Int): LiveData<ApiResponse<MovieDetail>>

@POST("/adminapi/api/movieDetail.json")
fun movieDetailNormal(
@Header("x-header-request-timestamp") headerTimestamp: String = "",
@Header("x-header-request-key") headerKey: String = "",
@Header("x-header-request-imei") headerImei: String = "",
@Query("categoryId") categoryId: Int,
@Query("movieDetailId") movieDetailId: Int): Call<MovieDetail>

@POST("/adminapi/api/movieList.json")
fun search(
@Header("x-header-request-timestamp") headerTimestamp: String = "",
Expand Down
7 changes: 1 addition & 6 deletions app/src/main/java/com/bzh/dytt/db/DyttDB.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ import android.content.Context
import android.os.Build
import android.support.annotation.VisibleForTesting
import android.util.Log
import androidx.work.Constraints
import androidx.work.NetworkType
import androidx.work.OneTimeWorkRequest
import androidx.work.WorkManager
import androidx.work.*
import com.bzh.dytt.vo.MovieDetail
import com.bzh.dytt.workers.FetchVideoDetailWorker

Expand Down Expand Up @@ -56,15 +53,13 @@ abstract class DyttDB : RoomDatabase() {
Constraints.Builder()
.setRequiresCharging(true)
.setRequiresDeviceIdle(true)
.setRequiresCharging(false)
.setRequiredNetworkType(NetworkType.CONNECTED)
.setRequiresBatteryNotLow(true)
.setRequiresStorageNotLow(true)
.build()
} else {
Constraints.Builder()
.setRequiresCharging(true)
.setRequiresCharging(false)
.setRequiredNetworkType(NetworkType.CONNECTED)
.setRequiresBatteryNotLow(true)
.setRequiresStorageNotLow(true)
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/bzh/dytt/db/MovieDetailDAO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import com.bzh.dytt.vo.MovieDetail
interface MovieDetailDAO {

@Query("SELECT * FROM movie_detail WHERE isPrefect = :isPrefect")
fun movieListNotPrefect(isPrefect: Boolean): List<MovieDetail>
fun getMovieListByPrefect(isPrefect: Boolean): List<MovieDetail>

@Query("SELECT * FROM movie_detail WHERE categoryId=:type ORDER BY id DESC LIMIT :limit")
fun movieList(type: Int?, limit: Int = 30): LiveData<List<MovieDetail>>
Expand Down
87 changes: 84 additions & 3 deletions app/src/main/java/com/bzh/dytt/workers/FetchVideoDetailWorker.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,100 @@
package com.bzh.dytt.workers

import android.content.Context
import android.os.Looper
import android.util.Log
import androidx.work.Worker
import com.bzh.dytt.api.NetworkService
import com.bzh.dytt.db.DyttDB
import com.bzh.dytt.key.KeyUtils
import com.bzh.dytt.vo.MovieDetail
import okhttp3.Cache
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.converter.scalars.ScalarsConverterFactory
import java.util.concurrent.TimeUnit

class FetchVideoDetailWorker : Worker() {

private fun provideCacheInterceptor(): Interceptor {
return Interceptor { chain ->
val request = chain.request()
val response = chain.proceed(request)
response.newBuilder()
.header("Cache-Control", "public, max-age=1")
.removeHeader("Pragma")
.build()
}
}

private fun provideOkHttpCache(application: Context): Cache {
val cacheSize = 10 * 1024 * 1024
return Cache(application.cacheDir, cacheSize.toLong())
}

private fun provideOkHttpClient(okhttpCache: Cache, okHttpCacheInterceptor: Interceptor): OkHttpClient {
return OkHttpClient.Builder()
.cache(okhttpCache)
.connectTimeout(30, TimeUnit.SECONDS)
.addNetworkInterceptor(okHttpCacheInterceptor)
.build()
}

private fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit {
return Retrofit.Builder()
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.baseUrl("http://m.dydytt.net:8080")
.client(okHttpClient)
.build()
}

private fun provideNetworkService(retrofit: Retrofit): NetworkService {
return retrofit.create(NetworkService::class.java)
}

override fun doWork(): Result {

val networkService = provideNetworkService(provideRetrofit(provideOkHttpClient(provideOkHttpCache(applicationContext), provideCacheInterceptor())))

val movieDetailDao = DyttDB.getInstance(applicationContext).movieDetailDao()

val liveData = movieDetailDao.movieListNotPrefect(false)
Log.d(TAG, "doWork() called isMainThread ${Looper.myLooper() == Looper.getMainLooper()} networkService $networkService movieDetailDao $movieDetailDao ")

val listByPrefect = movieDetailDao.getMovieListByPrefect(false)

Log.d(TAG, "doWork() called listByPrefect ${listByPrefect}")

for (movieDetail in listByPrefect) {

val timeStamp = System.currentTimeMillis() / 1000L
val imei = ""
val key = KeyUtils.getHeaderKey(timeStamp)

Log.d(TAG, "doWork() called before ${movieDetail.name} $key $imei $timeStamp ${movieDetail.categoryId} ${movieDetail.id} ")

val response: Response<MovieDetail> = networkService.movieDetailNormal(headerKey = key,
headerTimestamp = "$timeStamp",
headerImei = imei,
categoryId = movieDetail.categoryId,
movieDetailId = movieDetail.id).execute()

if (response.isSuccessful) {
if (response.body() != null) {
val movie = response.body()!!
movieDetailDao.updateMovie(movie)
Log.d(TAG, "doWork() called response success ${movie.name}")
} else {
Log.d(TAG, "doWork() called response body is null")
}
} else {
Log.d(TAG, "doWork() called response not success")
}

for (data in liveData) {
Log.d(TAG, "FetchVideoDetailWorker doWork $data")
Thread.sleep(1000L)
}

return Result.SUCCESS
Expand Down

0 comments on commit 80f0e48

Please sign in to comment.