Skip to content

Commit

Permalink
首页banner
Browse files Browse the repository at this point in the history
  • Loading branch information
linxiangcheer committed Mar 19, 2021
1 parent 98c839b commit 1bf9bf1
Show file tree
Hide file tree
Showing 34 changed files with 494 additions and 88 deletions.
3 changes: 2 additions & 1 deletion app/src/main/java/com/cniao5/cainiaowo/CnApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.alibaba.android.arouter.launcher.ARouter
import com.cniao5.common.BaseApplication
import com.cniao5.common.ktx.application
import com.cniao5.course.moduleCourse
import com.cniao5.home.moduleHome
import com.cniao5.mine.moduleMine
import com.cniao5.study.moduleStudy
import com.test.service.assistant.AssistantApp
Expand All @@ -15,7 +16,7 @@ import org.koin.core.module.Module
class CnApplication : BaseApplication() {

private val modules = arrayListOf<Module>(
moduleService,/*moduleHome,*/ moduleLogin, moduleMine, moduleStudy, moduleCourse
moduleService, moduleHome, moduleLogin, moduleMine, moduleStudy, moduleCourse
)

override fun initConfig() {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/cniao5/cainiaowo/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.cniao5.cainiaowo.databinding.ActivityMainBinding
import com.cniao5.common.base.BaseActivity
import com.cniao5.common.widget.BnvVp2Mediator
import com.cniao5.course.ui.CourseFragment
import com.cniao5.home.HomeFragment
import com.cniao5.home.ui.HomeFragment
import com.cniao5.mine.MineContainerFragment
import com.cniao5.study.ui.StudyFragment

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.cniao5.course.ui
package com.cniao5.course

import androidx.lifecycle.LiveData
import androidx.lifecycle.viewModelScope
Expand All @@ -7,6 +7,8 @@ import com.cniao5.common.base.BaseViewModel
import com.cniao5.course.net.CourseListRsp
import com.cniao5.course.net.CourseTypes
import com.cniao5.course.repo.ICourseResource
import com.cniao5.course.ui.CoursePagingAdapter
import com.cniao5.course.ui.CourseRecycAdapter

class CourseViewModel(val repo: ICourseResource) : BaseViewModel() {

Expand Down
1 change: 0 additions & 1 deletion course/src/main/java/com/cniao5/course/libCourse.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import com.cniao5.common.utils.getBaseHost
import com.cniao5.course.net.CourseService
import com.cniao5.course.repo.CourseResource
import com.cniao5.course.repo.ICourseResource
import com.cniao5.course.ui.CourseViewModel
import org.koin.androidx.viewmodel.dsl.viewModel
import org.koin.core.parameter.parametersOf
import org.koin.dsl.bind
Expand Down
48 changes: 24 additions & 24 deletions course/src/main/java/com/cniao5/course/ui/CourseFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.cniao5.course.ui
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand All @@ -14,9 +13,9 @@ import androidx.lifecycle.lifecycleScope
import androidx.paging.LoadState
import com.blankj.utilcode.util.ToastUtils
import com.cniao5.common.base.BaseFragment
import com.cniao5.course.CourseViewModel
import com.cniao5.course.R
import com.cniao5.course.databinding.*
import com.cniao5.course.net.CourseListRsp
import com.google.android.material.tabs.TabLayout
import kotlinx.coroutines.flow.collectLatest
import org.koin.androidx.viewmodel.ext.android.viewModel
Expand Down Expand Up @@ -51,11 +50,33 @@ class CourseFragment: BaseFragment() {
mBinding = FragmentCourseBinding.bind(view)

return mBinding.apply {

vm = viewModel

val adapter = viewModel.coursePagingAdapter
//下方的load Header头部load
rvCourse.adapter = adapter.withLoadStateFooter(CourseLoadAdapter(adapter))

vm = viewModel
//Load上滑进度条监听
adapter.addLoadStateListener { loadState ->
if (loadState.refresh is LoadState.Loading) {
pbFragmentCourse.visibility = View.VISIBLE
} else {
pbFragmentCourse.visibility = View.GONE

//获取加载错误事件
val error = when {
loadState.prepend is LoadState.Error -> loadState.prepend as LoadState.Error
loadState.append is LoadState.Error -> loadState.append as LoadState.Error
loadState.refresh is LoadState.Error -> loadState.refresh as LoadState.Error
else -> null
}

error?.let {
ToastUtils.showShort(it.error.message)
}
}
}

viewModel.apply {

Expand Down Expand Up @@ -118,27 +139,6 @@ class CourseFragment: BaseFragment() {
courseRecycAdapter.submitClear(it?.datas ?: emptyList())
}

//Load上滑进度条监听
adapter.addLoadStateListener { loadState ->
if (loadState.refresh is LoadState.Loading) {
pbFragmentCourse.visibility = View.VISIBLE
} else {
pbFragmentCourse.visibility = View.GONE

//获取加载错误事件
val error = when {
loadState.prepend is LoadState.Error -> loadState.prepend as LoadState.Error
loadState.append is LoadState.Error -> loadState.append as LoadState.Error
loadState.refresh is LoadState.Error -> loadState.refresh as LoadState.Error
else -> null
}

error?.let {
ToastUtils.showShort(it.error.message)
}
}
}

//全部类型按钮点击事件
spinnerTypeCourse.setOnClickListener {
popupWindow_type.showAsDropDown(it)
Expand Down
2 changes: 1 addition & 1 deletion course/src/main/res/layout/fragment_course.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<data>
<variable
name="vm"
type="com.cniao5.course.ui.CourseViewModel" />
type="com.cniao5.course.CourseViewModel" />

</data>

Expand Down
2 changes: 1 addition & 1 deletion course/src/main/res/layout/item_course_recyc.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
android:gravity="center"
android:paddingVertical="2dp"
android:text="免费学"
android:textColor="@color/colorAccent"
android:textColor="@color/lightseagreen"
android:textSize="12sp"
android:visibility="@{(info.isFree()==1)?View.VISIBLE:View.GONE}"
app:layout_constraintBottom_toBottomOf="parent"
Expand Down
4 changes: 4 additions & 0 deletions home/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@ android {
}
dependencies {
implementation project(path:':service')
//第三方banner
implementation 'com.youth.banner:banner:2.1.0'

//from dependencies.gradle

}
3 changes: 3 additions & 0 deletions home/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cniao5.home">

<!--网络权限-->
<uses-permission android:name="android.permission.INTERNET"/>

</manifest>
23 changes: 0 additions & 23 deletions home/src/main/java/com/cniao5/home/HomeFragment.kt

This file was deleted.

36 changes: 36 additions & 0 deletions home/src/main/java/com/cniao5/home/LibHome.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.cniao5.home

import com.cniao5.common.network.KtRetrofit
import com.cniao5.common.utils.getBaseHost
import com.cniao5.home.net.HomeService
import com.cniao5.home.repo.HomeResource
import com.cniao5.home.repo.IHomeResource
import com.cniao5.home.ui.HomeViewModel
import org.koin.core.parameter.parametersOf
import org.koin.androidx.viewmodel.dsl.viewModel
import org.koin.dsl.bind
import org.koin.dsl.module


/*
* 依赖注入管理 Home的module
* */
val moduleHome = module {

//service retrofit
//single声明单例对象
// single {
// KtRetrofit.initConfig("http://yapi.54yct.com/mock/24/") //baseurl
// .getService(MineService::class.java)
// }

single {
get<KtRetrofit> { parametersOf(getBaseHost()) }.getService(HomeService::class.java)
}

//repo IMineResource
single { HomeResource(get()) } bind IHomeResource::class

viewModel { HomeViewModel(get()) }

}
30 changes: 30 additions & 0 deletions home/src/main/java/com/cniao5/home/net/HomeRsp.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.cniao5.home.net
import androidx.annotation.Keep

import com.google.gson.annotations.SerializedName


/*
* 首页上方banner图数据
* */
class BannerList : ArrayList<BannerList.BannerListItem>(){
@Keep
data class BannerListItem(
@SerializedName("client_url")
val clientUrl: Any?,
@SerializedName("created_time")
val createdTime: String?,
val id: Int,
@SerializedName("img_url")
val imgUrl: String?,
val name: Any?,
@SerializedName("order_num")
val orderNum: Int,
@SerializedName("page_show")
val pageShow: Int,
@SerializedName("redirect_url")
val redirectUrl: String?,
val state: Int,
val type: String?
)
}
23 changes: 23 additions & 0 deletions home/src/main/java/com/cniao5/home/net/HomeService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.cniao5.home.net

import com.test.service.net.BaseResponse
import retrofit2.Call
import retrofit2.http.GET
import retrofit2.http.Query


/*
* 主页模块的接口
* */
interface HomeService {

/*
* 首页上方banner
* */
@GET("ad/new/banner/list")
fun getBanner(
@Query("type")type: Int = 2, //类型 1:小程序 2:web 3:h5 4:ios 5:android 如: 2表示web 默认2
@Query("page_show") pageshow:Int = 1 //页面显示 1 首页 2 课程 3 大数据学院 4 机器人学院 5 人工智能学院 6 推广员 默认1
) : Call<BaseResponse>

}
41 changes: 41 additions & 0 deletions home/src/main/java/com/cniao5/home/repo/HomeResource.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.cniao5.home.repo

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.LogUtils
import com.cniao5.common.network.support.serverData
import com.cniao5.home.net.BannerList
import com.cniao5.home.net.HomeService
import com.test.service.net.onBizError
import com.test.service.net.onBizOK
import com.test.service.net.onFailure
import com.test.service.net.onSuccess

class HomeResource (val service: HomeService) : IHomeResource {

private val _liveBanner = MutableLiveData<BannerList>()

override val liveBanner: LiveData<BannerList>
get() = _liveBanner

/*
* 获取首页上方banner图数据
* */
override suspend fun getBanner() {
service.getBanner()
.serverData()
.onSuccess {
onBizError { code, message ->
LogUtils.w("获取banner信息 onBizzError $code$message")
}
onBizOK<BannerList> { code, data, message ->
_liveBanner.value = data
LogUtils.i("获取banner信息 onBizzOK $data")
}
}
.onFailure {
LogUtils.e("获取banner信息 接口异常 ${it.message}")
}
}

}
15 changes: 15 additions & 0 deletions home/src/main/java/com/cniao5/home/repo/IHomeResource.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.cniao5.home.repo

import androidx.lifecycle.LiveData
import com.cniao5.home.net.BannerList
/*
* 抽象接口
* */
interface IHomeResource {

//首页上方banner图
val liveBanner: LiveData<BannerList>

suspend fun getBanner()

}
Loading

0 comments on commit 1bf9bf1

Please sign in to comment.