Skip to content

Commit

Permalink
开眼App模块构建:分类列表及详情接口调试、多个BaseUrl的场景处理、分类列表完成
Browse files Browse the repository at this point in the history
  • Loading branch information
GraceJoJo committed Jan 18, 2019
1 parent 7f4056d commit c9a37ef
Show file tree
Hide file tree
Showing 49 changed files with 856 additions and 31 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ dependencies {
if (!Boolean.valueOf(rootProject.ext.isBuildModule)) {
implementation project(":module-core")
implementation project(":module-mall")
implementation project(":module-discover")
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class ARouterConfig {
const val ACT_SEARCH = "/mall/act_search"
const val ACT_GoodsFilter= "/mall/act_goodsfilter"
const val ACT_GoodsDetail= "/mall/act_goodsdetail"
const val ACT_Category= "/found/act_category"
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ package com.jojo.design.common_base.net
*/
class API {
companion object {
val BASE_SERVER_IP = "http://api.xiangqu.com/"
//想去app
val BASE_XIANGQU_SERVER_IP = "http://api.xiangqu.com/"
//开眼app
val BASE_KAIYAN_SERVER_IP = "http://baobab.kaiyanapp.com/api/v4/"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ import retrofit2.converter.gson.GsonConverterFactory
import java.io.File
import java.io.IOException
import java.util.concurrent.TimeUnit
import android.R.attr.port
import android.R.attr.host
import android.R.attr.scheme
import com.alibaba.android.arouter.facade.model.RouteMeta.build
import okhttp3.HttpUrl


/**
* author : JOJO
Expand Down Expand Up @@ -56,12 +62,13 @@ object RetrofitManager {
.addInterceptor(CommonQueryParamsInterceptor())
.addInterceptor(HeaderInterceptor())
.addInterceptor(LoggingInterceptor())//添加请求拦截(可以在此处打印请求信息和响应信息)
.addInterceptor(MutiBaseUrlInterceptor()) //多个BaseUrl的处理
.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
.writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
.readTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
.build()
retrofit = Retrofit.Builder()
.baseUrl(API.BASE_SERVER_IP)//基础URL 建议以 / 结尾
.baseUrl(API.BASE_XIANGQU_SERVER_IP)//默认的BaseUrl 建议以/结尾
.addConverterFactory(GsonConverterFactory.create())//设置 Json 转换器
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())//RxJava 适配器
.client(mClient)
Expand Down Expand Up @@ -185,6 +192,55 @@ object RetrofitManager {
}
}

/**
* 处理多个BaseUrl的情形
*/
class MutiBaseUrlInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
//获取request
val request = chain.request()
//从request中获取原有的HttpUrl实例oldHttpUrl
var oldHttpUrl = request.url()
//获取request的创建者builder
val builder = request.newBuilder()
//从request中获取headers,通过给定的键url_name
val headerValues = request.headers("base_url")
if (headerValues != null && headerValues!!.size > 0) {
//如果有这个header,先将配置的header删除,因此header仅用作app和okhttp之间使用
builder.removeHeader("base_url")
//匹配获得新的BaseUrl
val headerValue = headerValues!!.get(0)
var newBaseUrl: HttpUrl? = null
if ("kaiyan" == headerValue) {
newBaseUrl = HttpUrl.parse(API.BASE_KAIYAN_SERVER_IP)
} else {
newBaseUrl = oldHttpUrl
return chain.proceed(builder.url(newBaseUrl).build())
}
//在oldHttpUrl的基础上重建新的HttpUrl,修改需要修改的url部分
Log.e("Url", "intercept-oldHttpUrl: " + oldHttpUrl.toString())
//oldHttpUrl:http://api.xiangqu.com/categories?key=0db6ffd00372064035ef33763dd1c61e&t=1547700576328
val newUrl = oldHttpUrl
.newBuilder()
.scheme("http")//更换网络协议,根据实际情况更换成https或者http
.host(newBaseUrl!!.host())//更换主机名
.port(newBaseUrl.port())//更换端口
// .addPathSegment("api")//添加第一个参数 (已经包含Segment的HttpUrl,addPathSegment时,无法直接跟在host后面,只能在最后一个参数后面接)
// .addPathSegment("v4")//添加第二个参数
.build()
//重建这个request,通过builder.url(finalRequestUrl).build()
Log.e("Url", "intercept: " + newUrl.toString())
//newUrl: http://baobab.kaiyanapp.com/categories?key=0db6ffd00372064035ef33763dd1c61e&t=1547700576328
//期望的finalUrl: http://baobab.kaiyanapp.com/api/v4/categories?key=0db6ffd00372064035ef33763dd1c61e&t=1547700576328
var spltUrls = newUrl.toString().split(".com")
var finalUrl = spltUrls[0] + ".com" + "/api/v4" + spltUrls[1]
Log.e("Url", "finalUrl: " + finalUrl)
return chain.proceed(builder.url(finalUrl).build())
}
return chain.proceed(request)
}
}


/**
* Retrofit上传文件
Expand Down Expand Up @@ -265,4 +321,28 @@ object RetrofitManager {
})

}

/**
* 建立请求(未封装Json数据的情形)
*/
fun <T> doRequest(observable: Observable<T>, observerListener: BaseObserverListener<T>): DisposableObserver<T> {

return observable
.compose(RxSchedulers.io_main())
.subscribeWith(object : DisposableObserver<T>() {

override fun onNext(result: T) {
observerListener.onSuccess(result)
}

override fun onError(e: Throwable) {
observerListener.onError(e)
}

override fun onComplete() {
observerListener.onComplete()
}
})

}
}
2 changes: 2 additions & 0 deletions common-ui/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ dependencies {
api rootProject.ext.dependencies["recyclerview-v7"]
api rootProject.ext.dependencies["banner"]
api rootProject.ext.dependencies["cardview-v7"]
//ios弹性效果
api rootProject.ext.dependencies["ios-scroll"]
}
repositories {
mavenCentral()
Expand Down
8 changes: 5 additions & 3 deletions config.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ext {
* isBuildModule=true
* 修改之后需要Sync方可生效
*/
isBuildModule = true
isBuildModule = false

android = [
compileSdkVersion: 26,
Expand Down Expand Up @@ -78,7 +78,9 @@ ext {
"FlycoTabLayout" : "com.flyco.tablayout:FlycoTabLayout_Lib:2.0.8@aar",
"Smarttablayout" : "com.ogaclejapan.smarttablayout:library:1.6.1@aar",
"Smarttablayout-utils" : "com.ogaclejapan.smarttablayout:utils-v4:1.6.1@aar",
"banner" : "com.bigkoo:convenientbanner:2.0.5",
"banner" : "com.bigkoo:convenientbanner:2.0.5",
//ios 列表弹性效果
"ios-scroll" : "me.everything:overscroll-decor-android:1.0.4",

//tools
"multidex" : "com.android.support:multidex:1.0.2",
Expand All @@ -92,7 +94,7 @@ ext {
"room" : "android.arch.persistence.room:runtime:1.0.0",
"room-compiler" : "android.arch.persistence.room:compiler:1.0.0",
//添加Room的RxJava支持(必须在使用的module中添加上)
"room-support-rxjava2" : "android.arch.persistence.room:rxjava2:1.0.0",
"room-support-rxjava2" : "android.arch.persistence.room:rxjava2:1.0.0",
]


Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package com.jojo.design.module_core.ui.home

import android.os.Bundle
import com.alibaba.android.arouter.launcher.ARouter
import com.jojo.design.common_base.config.arouter.ARouterConfig
import com.jojo.design.common_base.config.arouter.ARouterConstants
import com.jojo.design.common_base.dagger.mvp.BaseContract
import com.jojo.design.common_base.dagger.mvp.BaseFragment
import com.jojo.design.common_ui.view.MultipleStatusView
import com.jojo.design.module_core.R
import com.will.weiyuekotlin.component.ApplicationComponent
import kotlinx.android.synthetic.main.fra_discorvery.*

/**
* author : JOJO
* e-mail : [email protected]
* date : 2018/12/7 11:34 AM
* desc : 我的(shoping)
* desc : 发现
*/
class DiscoveryFragment : BaseFragment<BaseContract.BasePresenter, BaseContract.BaseModel>() {
private var mTitle: String? = null
Expand Down Expand Up @@ -46,6 +50,9 @@ class DiscoveryFragment : BaseFragment<BaseContract.BasePresenter, BaseContract.
}

override fun startFragmentEvents() {

tv.setOnClickListener {
ARouter.getInstance().build(ARouterConfig.ACT_Category)
.navigation()
}
}
}
42 changes: 19 additions & 23 deletions module-core/src/main/res/layout/fra_discorvery.xml
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<data>
<include
android:id="@+id/title"
layout="@layout/common_title"></include>

<variable
name="user"
type="com.jojo.design.common_base.bean.ErrorBean"></variable>
</data>
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发现"
android:textColor="@color/color_212121"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title" />

<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<include
android:id="@+id/title"
layout="@layout/common_title"></include>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发现"
app:layout_constraintTop_toBottomOf="@+id/title" />

</android.support.constraint.ConstraintLayout>
</layout>
</android.support.constraint.ConstraintLayout>
1 change: 1 addition & 0 deletions module-discover/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
74 changes: 74 additions & 0 deletions module-discover/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
if (Boolean.valueOf(rootProject.ext.isBuildModule)) {
apply plugin: 'com.android.application'
} else {
apply plugin: 'com.android.library'
}
apply plugin: 'kotlin-android'
//kotlin扩展插件,无需findViewbyId
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
//ARouter
kapt {
arguments {
arg("moduleName", project.getName())
}
generateStubs = true
}
android {
compileSdkVersion rootProject.ext.android["compileSdkVersion"]
buildToolsVersion rootProject.ext.android["buildToolsVersion"]
defaultConfig {
minSdkVersion rootProject.ext.android["minSdkVersion"]
targetSdkVersion rootProject.ext.android["targetSdkVersion"]
versionCode rootProject.ext.android["versionCode"]
versionName rootProject.ext.android["versionName"]
testInstrumentationRunner rootProject.ext.dependencies["androidJUnitRunner"]
//MultiDex分包方法
multiDexEnabled true
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
//支持DataBinding
dataBinding {
enabled = true
}

//java插件引入了一个概念叫做SourceSets,通过修改SourceSets中的属性,可以指定哪些源文件
//( 或文件夹下的源文件 ) 要被编译 , 哪些源文件要被排除 。
sourceSets {
main {
if (Boolean.valueOf(rootProject.ext.isBuildModule)) {
manifest.srcFile 'src/main/module/AndroidManifest.xml'
} else {
manifest.srcFile 'src/main/AndroidManifest.xml'
java {
//排除java/debug文件夹下的所有文件
exclude '*module'
}
}
}
}
}


dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(":common-base")
implementation project(":common-ui")
//ARouter
kapt rootProject.ext.dependencies["arouter-compiler"]
//解决Kotlin和DataBinding冲突
kapt "com.android.databinding:compiler:$android_version"
//butterknife-compiler
kapt rootProject.ext.dependencies["butterknife-compiler"]
kapt rootProject.ext.dependencies["dagger-compiler"]
}

repositories {
mavenCentral()
}
21 changes: 21 additions & 0 deletions module-discover/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Loading

0 comments on commit c9a37ef

Please sign in to comment.