a
This commit is contained in:
+24
@@ -0,0 +1,24 @@
|
||||
package com.bbgo.module_collect
|
||||
|
||||
import androidx.test.platform.app.InstrumentationRegistry
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
|
||||
import org.junit.Assert.*
|
||||
|
||||
/**
|
||||
* Instrumented test, which will execute on an Android device.
|
||||
*
|
||||
* See [testing documentation](http://d.android.com/tools/testing).
|
||||
*/
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class ExampleInstrumentedTest {
|
||||
@Test
|
||||
fun useAppContext() {
|
||||
// Context of the app under test.
|
||||
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
|
||||
assertEquals("com.bbgo.module_collect.test", appContext.packageName)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.bbgo.module_collect">
|
||||
|
||||
</manifest>
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.bbgo.module_collect.bean
|
||||
|
||||
import androidx.annotation.Keep
|
||||
|
||||
/**
|
||||
* author: wangyb
|
||||
* date: 2021/5/27 7:10 下午
|
||||
* description: todo
|
||||
*/
|
||||
@Keep
|
||||
data class CollectBean(val data: Any, var positon: Int, var type: String)
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.bbgo.module_collect.net.api
|
||||
|
||||
import com.bbgo.common_base.bean.HttpResult
|
||||
import com.bbgo.module_collect.bean.CollectBean
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import retrofit2.http.POST
|
||||
import retrofit2.http.Path
|
||||
|
||||
/**
|
||||
* author: wangyb
|
||||
* date: 4/7/21 9:24 PM
|
||||
* description: http api
|
||||
*/
|
||||
interface HttpApiService {
|
||||
|
||||
/**
|
||||
* 收藏站内文章
|
||||
* http://www.wanandroid.com/lg/collect/1165/json
|
||||
* @param id article id
|
||||
*/
|
||||
@POST("lg/collect/{id}/json")
|
||||
fun addCollectArticle(@Path("id") id: Int): Flow<HttpResult<CollectBean>>
|
||||
|
||||
/**
|
||||
* 文章列表中取消收藏文章
|
||||
* http://www.wanandroid.com/lg/uncollect_originId/2333/json
|
||||
* @param id
|
||||
*/
|
||||
@POST("lg/uncollect_originId/{id}/json")
|
||||
fun cancelCollectArticle(@Path("id") id: Int): Flow<HttpResult<CollectBean>>
|
||||
|
||||
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package com.bbgo.module_collect.repository
|
||||
|
||||
import com.bbgo.common_base.bean.HttpResult
|
||||
import com.bbgo.common_base.net.ServiceCreators
|
||||
import com.bbgo.module_collect.bean.CollectBean
|
||||
import com.bbgo.module_collect.net.api.HttpApiService
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
/**
|
||||
* author: wangyb
|
||||
* date: 3/29/21 9:32 PM
|
||||
* description: todo
|
||||
*/
|
||||
class CollectRepository private constructor() {
|
||||
|
||||
companion object {
|
||||
@Volatile
|
||||
private var repository: CollectRepository? = null
|
||||
|
||||
fun getInstance(): CollectRepository {
|
||||
if (repository == null) {
|
||||
synchronized(CollectRepository::class.java) {
|
||||
if (repository == null) {
|
||||
repository = CollectRepository()
|
||||
}
|
||||
}
|
||||
}
|
||||
return repository!!
|
||||
}
|
||||
}
|
||||
|
||||
private val service = ServiceCreators.create(HttpApiService::class.java)
|
||||
|
||||
fun collectArticle(id: Int) : Flow<HttpResult<CollectBean>> {
|
||||
return service.addCollectArticle(id)
|
||||
}
|
||||
|
||||
fun unCollectArticle(id: Int) : Flow<HttpResult<CollectBean>> {
|
||||
return service.cancelCollectArticle(id)
|
||||
}
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package com.bbgo.module_collect.service
|
||||
|
||||
import android.content.Context
|
||||
import com.alibaba.android.arouter.facade.annotation.Route
|
||||
import com.bbgo.common_base.constants.RouterPath
|
||||
import com.bbgo.common_service.collect.CollectService
|
||||
import com.bbgo.module_collect.repository.CollectRepository
|
||||
import com.bbgo.module_collect.viewmodel.CollectViewModel
|
||||
|
||||
/**
|
||||
* author: wangyb
|
||||
* date: 2021/5/27 7:31 下午
|
||||
* description: todo
|
||||
*/
|
||||
@Route(path = RouterPath.Collect.SERVICE_COLLECT)
|
||||
class CollectServiceImpl : CollectService{
|
||||
|
||||
private val viewModel = CollectViewModel(CollectRepository.getInstance())
|
||||
|
||||
override fun collect(indexPage: Int, position: Int,pageId: Int) {
|
||||
viewModel.collectArticle(indexPage, position, pageId)
|
||||
}
|
||||
|
||||
override fun unCollect(indexPage: Int, position: Int,pageId: Int) {
|
||||
viewModel.unCollectArticle(indexPage, position, pageId)
|
||||
// viewModelScope.launch {
|
||||
// CollectRepository.getInstance().unCollectArticle(pageId)
|
||||
// .catch {
|
||||
//
|
||||
// }
|
||||
// .collectLatest {
|
||||
// if (it.errorCode != 0) {
|
||||
// context?.showToast("取消收藏失败")
|
||||
// return@collectLatest
|
||||
// }
|
||||
// context?.getString(R.string.cancel_collect_success)?.let { context?.showToast(it) }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
override fun init(context: Context?) {
|
||||
}
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
package com.bbgo.module_collect.viewmodel
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.bbgo.common_base.bus.BusKey
|
||||
import com.bbgo.common_base.bus.LiveDataBus
|
||||
import com.bbgo.common_base.constants.Constants.CollectType.COLLECT
|
||||
import com.bbgo.common_base.constants.Constants.CollectType.UNCOLLECT
|
||||
import com.bbgo.common_base.constants.Constants.CollectType.UNKNOWN
|
||||
import com.bbgo.common_base.event.MessageEvent
|
||||
import com.bbgo.common_base.ext.USER_NOT_LOGIN
|
||||
import com.bbgo.common_base.util.log.Logs
|
||||
import com.bbgo.module_collect.repository.CollectRepository
|
||||
import kotlinx.coroutines.flow.catch
|
||||
import kotlinx.coroutines.flow.collectLatest
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
/**
|
||||
* author: wangyb
|
||||
* date: 3/29/21 9:31 PM
|
||||
* description: todo
|
||||
*/
|
||||
class CollectViewModel(private val repository: CollectRepository) : ViewModel() {
|
||||
|
||||
fun collectArticle(indexPage: Int, position: Int, id: Int) = viewModelScope.launch {
|
||||
repository.collectArticle(id)
|
||||
.catch {
|
||||
Logs.e(TAG, it.message, it)
|
||||
}
|
||||
.collectLatest {
|
||||
val event = if (it.errorCode == USER_NOT_LOGIN) {
|
||||
MessageEvent(indexPage, UNKNOWN, position, id)
|
||||
} else {
|
||||
MessageEvent(indexPage, COLLECT, position, id)
|
||||
}
|
||||
LiveDataBus.get().with(BusKey.COLLECT).value = event
|
||||
}
|
||||
}
|
||||
|
||||
fun unCollectArticle(indexPage: Int, position: Int, id: Int) = viewModelScope.launch {
|
||||
repository.unCollectArticle(id)
|
||||
.catch {
|
||||
Logs.e(TAG, it.message, it)
|
||||
}
|
||||
.collectLatest {
|
||||
val event = if (it.errorCode == USER_NOT_LOGIN) {
|
||||
MessageEvent(indexPage, UNKNOWN, position, id)
|
||||
} else {
|
||||
MessageEvent(indexPage, UNCOLLECT, position, id)
|
||||
}
|
||||
LiveDataBus.get().with(BusKey.COLLECT).value = event
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val TAG = "CollectViewModel"
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.bbgo.module_collect
|
||||
|
||||
import org.junit.Test
|
||||
|
||||
import org.junit.Assert.*
|
||||
|
||||
/**
|
||||
* Example local unit test, which will execute on the development machine (host).
|
||||
*
|
||||
* See [testing documentation](http://d.android.com/tools/testing).
|
||||
*/
|
||||
class ExampleUnitTest {
|
||||
@Test
|
||||
fun addition_isCorrect() {
|
||||
assertEquals(4, 2 + 2)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user