Commit 20013e44 by LouisWang

feat(项目):调整代码规范

parent 754147f8
......@@ -17,7 +17,7 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".demo.list.ListActivity" />
<activity android:name=".demo.list.PopularWebsitesActivity" />
</application>
</manifest>
\ No newline at end of file
......@@ -19,8 +19,8 @@ import retrofit2.Response
class ResponseListConverter<D>(private val clazz: Class<D>) : IConverter<ArrayList<D>> {
override fun convertResponse(
owner: LifecycleOwner?,
response: Response<ResponseBody>
owner: LifecycleOwner?,
response: Response<ResponseBody>
): ArrayList<D>? {
val json = response.body()?.string() ?: return null
val jsonObj = JSONObject(json)
......
......@@ -23,8 +23,6 @@ inline fun <reified D : Any> BaseRequest<*>.asResponse(): HttpObservable<D> {
/**
* 将Http响应数据转换成一个列表,去除最外层封装
*
* @param isAuth 是否需要添加认证参数,默认是true。
*/
inline fun <reified D : Any> BaseRequest<*>.asResponseList(): HttpObservable<ArrayList<D>> {
return HttpObservableImpl(this, ResponseListConverter(D::class.java))
......
......@@ -4,7 +4,7 @@ import com.autocareai.lib.net.HttpUtil
import com.autocareai.lib.net.observable.HttpObservable
import com.autocareai.mvvmdemo.common.http.asResponse
import com.autocareai.mvvmdemo.common.http.asResponseList
import com.autocareai.mvvmdemo.demo.enity.ListEntity
import com.autocareai.mvvmdemo.demo.enity.PopularWebsitesEntity
import com.autocareai.mvvmdemo.demo.enity.UserEntity
/**
......@@ -27,16 +27,16 @@ object DemoApi {
*/
fun login(phone: String, password: String): HttpObservable<UserEntity> {
return HttpUtil.postForm("user/login")
.param("username", phone)
.param("password", password)
.asResponse()
.param("username", phone)
.param("password", password)
.asResponse()
}
/**
* 获取常用网站
*/
fun getPopularWebsites(): HttpObservable<ArrayList<ListEntity>> {
fun getPopularWebsites(): HttpObservable<ArrayList<PopularWebsitesEntity>> {
return HttpUtil.get("friend/json")
.asResponseList()
.asResponseList()
}
}
\ No newline at end of file
......@@ -12,7 +12,7 @@ import kotlinx.android.parcel.Parcelize
* </pre>
*/
@Parcelize
class ListEntity : Parcelable {
class PopularWebsitesEntity : Parcelable {
//网站id
var id = 0
......
......@@ -7,35 +7,40 @@ import com.autocareai.lib.route.Route
import com.autocareai.mvvmdemo.R
import com.autocareai.mvvmdemo.common.view.BaseLifecycleActivity
import com.autocareai.mvvmdemo.demo.route.DemoRoute
import kotlinx.android.synthetic.main.list_activity.*
import kotlinx.android.synthetic.main.activity_popular_websites.*
/**
* <pre>
* author : louis wang
* time : 2020/06/28
* desc :
* desc : 常用网站界面
* version: 1.0
* </pre>
*/
@Route(path = DemoRoute.DEMO_LIST)
class ListActivity : BaseLifecycleActivity<ListViewModel>() {
@Route(path = DemoRoute.POPULAR_WEBSITES)
class PopularWebsitesActivity : BaseLifecycleActivity<PopularWebsitesViewModel>() {
private val adapter by lazy { ListAdapter() }
//常用网站适配器
private val mPopularWebsitesAdapter by lazy { PopularWebsitesAdapter() }
override fun getLayoutId(): Int = R.layout.list_activity
override fun getLayoutId(): Int = R.layout.activity_popular_websites
override fun getViewModelClass(): Class<ListViewModel> = ListViewModel::class.java
override fun getViewModelClass(): Class<PopularWebsitesViewModel> =
PopularWebsitesViewModel::class.java
override fun initView(savedInstanceState: Bundle?) {
swipeRefresh.setOnRefreshListener { loadDataOnCreate() }
recycleView.layoutManager = LinearLayoutManager(this)
recycleView.adapter = adapter
recycleView.adapter = mPopularWebsitesAdapter
}
override fun initListener() {
swipeRefresh.setOnRefreshListener { loadDataOnCreate() }
}
override fun initLifecycleObserver() {
mViewModel.listDataEvent.observe(this, Observer {
swipeRefresh.isRefreshing = false
adapter.setNewData(it)
mPopularWebsitesAdapter.setNewData(it)
})
}
......
......@@ -5,7 +5,7 @@ import com.autocareai.lib.extension.load
import com.autocareai.lib.widget.recyclerview.BaseViewHolder
import com.autocareai.mvvmdemo.R
import com.autocareai.mvvmdemo.common.view.BaseAdapter
import com.autocareai.mvvmdemo.demo.enity.ListEntity
import com.autocareai.mvvmdemo.demo.enity.PopularWebsitesEntity
/**
......@@ -16,13 +16,15 @@ import com.autocareai.mvvmdemo.demo.enity.ListEntity
* version: 1.0
* </pre>
*/
class ListAdapter : BaseAdapter<ListEntity>(R.layout.list_item) {
class PopularWebsitesAdapter :
BaseAdapter<PopularWebsitesEntity>(R.layout.recycle_item_popular_websites) {
override fun convert(helper: BaseViewHolder, item: ListEntity) {
override fun convert(helper: BaseViewHolder, item: PopularWebsitesEntity) {
super.convert(helper, item)
helper.setText(R.id.tvTitle, item.name)
helper.setText(R.id.tvLink, item.link)
helper.getView<ImageView>(R.id.image).load(item.link, R.mipmap.ic_launcher, R.mipmap.ic_launcher)
helper.getView<ImageView>(R.id.image)
.load(item.link, R.mipmap.ic_launcher, R.mipmap.ic_launcher)
}
}
\ No newline at end of file
......@@ -5,33 +5,33 @@ import com.autocareai.lib.lifecycle.extension.post
import com.autocareai.lib.lifecycle.livedata.SingleLiveEvent
import com.autocareai.mvvmdemo.common.view.BaseViewModel
import com.autocareai.mvvmdemo.demo.api.DemoApi
import com.autocareai.mvvmdemo.demo.enity.ListEntity
import com.autocareai.mvvmdemo.demo.enity.PopularWebsitesEntity
/**
* <pre>
* author : louis wang
* time : 2020/06/28
* desc :
* desc : 常用网站ViewModel
* version: 1.0
* </pre>
*/
class ListViewModel : BaseViewModel() {
class PopularWebsitesViewModel : BaseViewModel() {
/**
* 常用网站数据
*/
var listDataEvent = SingleLiveEvent<ArrayList<ListEntity>>()
var listDataEvent = SingleLiveEvent<ArrayList<PopularWebsitesEntity>>()
/**
* 获取常用网站
*/
fun getPopularWebsites(owner: LifecycleOwner) {
DemoApi.getPopularWebsites()
.attachLifecycle(owner)
.onSuccess {
listDataEvent.post(it)
}.onError { _, message ->
shortToast(message)
}.async()
.attachLifecycle(owner)
.onSuccess {
listDataEvent.post(it)
}.onError { _, message ->
shortToast(message)
}.async()
}
}
\ No newline at end of file
......@@ -38,7 +38,7 @@ class MainActivity : BaseLifecycleActivity<MainViewModel>() {
mViewModel.toListEvent.observe(this, Observer {
// 跳转至列表界面
DemoRoute.toList().navigation(this)
DemoRoute.toPopularWebsites().navigation(this)
})
}
}
......@@ -12,12 +12,17 @@ import com.autocareai.lib.route.RouteNavigation
*/
object DemoRoute {
//登录界面
const val LOGIN = "/demo/login"
const val DEMO_LIST = "/demo/list"
//常用网站界面
const val POPULAR_WEBSITES = "/demo/list"
fun toList(): RouteNavigation {
return RouteNavigation(DEMO_LIST)
/**
* 跳转常用网站界面
*/
fun toPopularWebsites(): RouteNavigation {
return RouteNavigation(POPULAR_WEBSITES)
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment