Skip to content

Commit

Permalink
接入WorkManager实现版本更新功能
Browse files Browse the repository at this point in the history
  • Loading branch information
fmtcias committed May 4, 2020
1 parent ba7f09e commit 8ee571a
Show file tree
Hide file tree
Showing 55 changed files with 4,340 additions and 291 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Jetpack_GitHub
[![Platform](https://img.shields.io/badge/platform-Android-green.svg)](https://github.com/fmtjava/OpenGitHub)
[![API](https://img.shields.io/badge/API-17%2B-brightgreen.svg?style=flat)](https://android-arsenal.com/api?level=16)
[![API](https://img.shields.io/badge/API-21%2B-brightgreen.svg?style=flat)](https://android-arsenal.com/api?level=16)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Release Version](https://img.shields.io/badge/version-2.0-red.svg)](https://fir.im/8jw7)
[![](https://img.shields.io/badge/Author-fmtjava-blue.svg)](https://github.com/fmtjava)
Expand Down Expand Up @@ -84,6 +84,9 @@ Model-View-ViewModel,View 指绿色的 Activity/Fragment,主要负责界面
Glide相比起Fresco要轻量很多,api调用起来也很简洁,对图片加载要求不是很高的话建议使用Glide。

# 更新日志
### v2.1
* 接入WorkManager实现版本更新功能
* 新增smallestWidth限定符屏幕适配方案
### v2.0
* 接入Paging改写动态分页列表页面,并封装Paging版分页模版(BasePagingVMFragment、BaseLPagingModel)
* 提供Paging版分页列表模版以及普通版分页列表模版,方便对比学习
Expand Down
19 changes: 12 additions & 7 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ kapt {
}

android {
compileSdkVersion 28
compileSdkVersion 29
defaultConfig {
applicationId "com.fmt.github"
minSdkVersion 17
targetSdkVersion 28
versionCode 9
versionName "2.0"
minSdkVersion 21
targetSdkVersion 29
versionCode 10
versionName "2.1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
signingConfigs {
Expand All @@ -33,6 +33,7 @@ android {
signingConfig signingConfigs.release
}
}

dataBinding {
enabled = true
}
Expand Down Expand Up @@ -81,6 +82,9 @@ dependencies {
//paging
implementation "androidx.paging:paging-runtime-ktx:2.1.2"

//WorkManager
implementation "androidx.work:work-runtime-ktx:2.3.4"

//koin(依赖注入)
implementation "org.koin:koin-androidx-scope:2.0.1"
implementation "org.koin:koin-androidx-viewmodel:2.0.1"
Expand All @@ -93,10 +97,11 @@ dependencies {
implementation 'de.hdodenhof:circleimageview:3.0.0'
implementation 'com.just.agentweb:agentweb:4.0.3-beta'
implementation project(path: ':simplebehavior')
implementation 'com.github.bumptech.glide:glide:4.10.0'
implementation 'com.github.bumptech.glide:glide:4.11.0'
kapt "com.github.bumptech.glide:compiler:4.11.0"
implementation 'com.github.GrenderG:Toasty:1.4.2'
implementation 'com.github.jd-alexander:LikeButton:0.2.3'
implementation 'com.github.Kennyc1012:MultiStateView:2.1'
implementation 'com.github.Kennyc1012:MultiStateView:2.1.2'
implementation 'com.jaredrummler:animated-svg-view:1.0.6'
implementation 'com.jeremyliao:live-event-bus-x:1.4.4'
implementation 'com.github.nitrico.lastadapter:lastadapter:2.3.0'
Expand Down
70 changes: 44 additions & 26 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,56 +1,74 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:dist="http://schemas.android.com/apk/distribution"
package="com.fmt.github">
xmlns:dist="http://schemas.android.com/apk/distribution"
xmlns:tools="http://schemas.android.com/tools"
package="com.fmt.github">

<dist:module dist:instant="true" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.READ_LOGS" />

<application
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/icon_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".WelcomeActivity" android:theme="@style/SplashTheme">
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/icon_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:networkSecurityConfig="@xml/network_security_config"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity
android:name=".WelcomeActivity"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".user.activity.LoginActivity"
android:screenOrientation="portrait" />
android:name=".user.activity.LoginActivity"
android:screenOrientation="portrait" />
<activity
android:name=".home.activity.HomeActivity"
android:screenOrientation="portrait"
android:theme="@style/ToolBarTheme" />
android:name=".home.activity.HomeActivity"
android:screenOrientation="portrait"
android:theme="@style/ToolBarTheme" />

<activity
android:name=".home.activity.CommonSearchActivity"
android:screenOrientation="portrait" />
android:name=".home.activity.CommonSearchActivity"
android:screenOrientation="portrait" />

<activity
android:name=".repos.activity.ReposDetailActivity"
android:screenOrientation="portrait" />
android:name=".repos.activity.ReposDetailActivity"
android:screenOrientation="portrait" />
<activity
android:name=".user.activity.UserInfoActivity"
android:screenOrientation="portrait" />
android:name=".user.activity.UserInfoActivity"
android:screenOrientation="portrait" />
<activity
android:name=".user.activity.AboutActivity"
android:screenOrientation="portrait" />
android:name=".user.activity.AboutActivity"
android:screenOrientation="portrait" />
<activity
android:name=".user.activity.PhotoPreviewActivity"
android:screenOrientation="portrait"
android:theme="@style/FullScreenTheme" />
android:name=".user.activity.PhotoPreviewActivity"
android:screenOrientation="portrait"
android:theme="@style/FullScreenTheme" />

<!-- Android 7.0以上 照片、APK等下载保存路径-->
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>

</manifest>
3 changes: 2 additions & 1 deletion app/src/main/java/com/fmt/github/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class App : Application() {
super.onCreate()
mApplication = this

//启动器进行异步初始化
TaskDispatcher.init(this)
TaskDispatcher.createInstance()
.addTask(InitBuGlyTask())
Expand All @@ -26,4 +27,4 @@ class App : Application() {
}
}

object AppContext : ContextWrapper(mApplication)//ContextWrapper对Context上下文进行包装(装饰者模式)
object AppContext : ContextWrapper(mApplication)//ContextWrapper对Context上下文进行包装(装饰者模式)
8 changes: 8 additions & 0 deletions app/src/main/java/com/fmt/github/GlideModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.fmt.github

import com.bumptech.glide.annotation.GlideModule
import com.bumptech.glide.module.AppGlideModule

@GlideModule
class GlideModule : AppGlideModule() {
}
19 changes: 10 additions & 9 deletions app/src/main/java/com/fmt/github/WelcomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,16 @@ class WelcomeActivity : BaseDataBindActivity<ActivityWelcomeBinding>() {
}

private fun askForPermission() {
runWithPermissions(Permission.READ_PHONE_STATE, granted = object : Callback {
override fun invoke(result: AssentResult) {
checkIsLogin()
}
}, denied = object : Callback {
override fun invoke(result: AssentResult) {
finish()
}
})
runWithPermissions(Permission.READ_PHONE_STATE, Permission.READ_EXTERNAL_STORAGE,
Permission.WRITE_EXTERNAL_STORAGE, granted = object : Callback {
override fun invoke(result: AssentResult) {
checkIsLogin()
}
}, denied = object : Callback {
override fun invoke(result: AssentResult) {
finish()
}
})
}

private fun checkIsLogin() {
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/com/fmt/github/config/Configs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ object Configs {
val SCOPE = listOf("user", "repo", "notifications", "gist")
val FINGERPRINT: String by lazy { Build.FINGERPRINT + UUID.randomUUID().toString() }

//仓库信息
const val OWNER = "fmtjava"
const val OWNER_REPO = "Jetpack_GitHub"

//bugly
const val BUGLY_APP_ID = "8a37e3b7c7"

Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/com/fmt/github/constant/Constant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ object Constant {
const val USER_PASSWORD = "password"
const val AUTHORIZATIONS = "authorizations"
const val AUTHORIZATION = "Authorization"
const val AUTHOR_NAME = "fmtjava"
const val AUTHOR_AVATAR_URL = "https://avatars3.githubusercontent.com/u/16126424?v=4"
const val DOWNLOAD = "download"
const val STAR_EVENT_KEY= "star_event"
}
2 changes: 1 addition & 1 deletion app/src/main/java/com/fmt/github/data/db/AppDataBase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import androidx.room.RoomDatabase
import com.fmt.github.user.model.db.User
import com.fmt.github.user.dao.UserDao

@Database(entities = [User::class], version = 1)
@Database(entities = [User::class], version = 1,exportSchema = false)
abstract class AppDataBase : RoomDatabase(){

abstract fun getUserDao(): UserDao
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/com/fmt/github/data/db/Room.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.fmt.github.data.db

import androidx.room.Room
import com.fmt.github.AppContext

private const val DB_NAME = "open_github_db"

val room = Room.databaseBuilder(AppContext, AppDataBase::class.java, DB_NAME).build()

val userDao = room.getUserDao()
49 changes: 49 additions & 0 deletions app/src/main/java/com/fmt/github/data/http/Retrofit.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.fmt.github.data.http

import android.util.Log
import com.fmt.github.BuildConfig
import com.fmt.github.data.http.interceptor.AuthorizationInterceptor
import com.fmt.github.home.api.DownloadApi
import com.fmt.github.repos.api.ReposApi
import com.fmt.github.user.api.UserApi
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

private const val BASE_URL = "https://api.github.com/"
private const val TIME_OUT = 60L
private const val TAG = "fmt"

val httpLoggingInterceptor = HttpLoggingInterceptor(object : HttpLoggingInterceptor.Logger {
override fun log(message: String) {
Log.e(TAG, message)
}
}).also {
it.level = HttpLoggingInterceptor.Level.BODY
}

val okHttpClient = OkHttpClient.Builder()
.addInterceptor(AuthorizationInterceptor())
.connectTimeout(TIME_OUT, TimeUnit.SECONDS)
.writeTimeout(TIME_OUT, TimeUnit.SECONDS)
.readTimeout(TIME_OUT, TimeUnit.SECONDS).also {
if (BuildConfig.DEBUG) {
it.addInterceptor(httpLoggingInterceptor)
}
}.build()

val retrofit: Retrofit = Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.baseUrl(BASE_URL)
.build()

object ReposService : ReposApi by retrofit.create(ReposApi::class.java)//接口代理实现

object UserService : UserApi by retrofit.create(UserApi::class.java)

object DownloadService : DownloadApi by retrofit.create(DownloadApi::class.java)


Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import android.util.Base64
import com.fmt.github.config.Settings
import com.fmt.github.constant.Constant
import com.fmt.github.ext.isLogin
import com.fmt.github.ext.otherwise
import com.fmt.github.ext.yes
import okhttp3.Interceptor
import okhttp3.Response

Expand All @@ -13,20 +15,27 @@ class AuthorizationInterceptor : Interceptor {

val request = chain.request()

return chain.proceed(request.newBuilder().apply {
when {
request.url.pathSegments.contains(Constant.AUTHORIZATIONS) -> {//登陆授权/退出接口
val userCredentials = "${Settings.Account.userName}:${Settings.Account.password}"
val auth = "basic ${Base64.encodeToString(userCredentials.toByteArray(), Base64.NO_WRAP)}"
addHeader(Constant.AUTHORIZATION, auth)
return request.url.pathSegments.contains(Constant.DOWNLOAD).yes {//下载Apk,不需要添加请求头
chain.proceed(request)
}.otherwise {
chain.proceed(request.newBuilder().apply {
when {
request.url.pathSegments.contains(Constant.AUTHORIZATIONS) -> {//登陆授权/退出接口
val userCredentials =
"${Settings.Account.userName}:${Settings.Account.password}"
val auth = "basic ${Base64.encodeToString(
userCredentials.toByteArray(),
Base64.NO_WRAP
)}"
addHeader(Constant.AUTHORIZATION, auth)
}
isLogin() -> {//权限接口
val auth = "Token ${Settings.Account.token}"
addHeader(Constant.AUTHORIZATION, auth)
}
}
isLogin() -> {//权限接口
val auth = "Token ${Settings.Account.token}"
addHeader(Constant.AUTHORIZATION, auth)
}
}

}.build())

}.build())
}
}
}
Loading

0 comments on commit 8ee571a

Please sign in to comment.