一款基于google官方推荐架构体系封装和Jetpack的MVVM框架。致力于写出最精简,易读性又高的代码,又保证项目的健壮性。 项目正在搭建中,如果喜欢的话不妨点个star~
-
Kotlin+ Coroutines+KTX
-
JetPack
- Dagger-Hilt-用于依赖注入(ViewModel,Repository,ApiService)
- LiveData
- Lifecycle
- ViewModel
-
TheRouter -跨模块通信框架
-
Retrofit2 & OkHttp4 -Retrofit2.11.0搭配协程
-
Scarlet & Rxjava -Retrofit风格的WebSocket client
-
Logger - Simple, pretty and powerful logger for android
-
Kotson- 更简易的Gson使用 val userInfo: User="user json".toObject()
@AndroidEntryPoint //注解的入口
class TopArticleActivity : BaseActivity() {
private val topArticleViewModel: TopArticleViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_top_article)
initView()
topArticleViewModel.fetchTopArticle()
}
}
class TopArticleViewModel @ViewModelInject constructor(private val articleRepository: ArticleRepository) :
ViewModel() {
val articleList = MutableLiveData<MutableList<TopArticle>>()
fun fetchTopArticle() {
launchOnUI {
val response = articleRepository.fetchTopArticle()
response.whenSuccess {
articleList.value = it.toMutableList()
}
}
}
}
@Singleton
class ArticleRepository @Inject constructor(private val wanApiService: WanApiService) :
BaseRepository() {
suspend fun fetchTopArticle(): NetResult<List<TopArticle>> {
return fetchApi { wanApiService.topArticle() }
}
}
@InstallIn(SingletonComponent::class)
@Module
object WanNetModule {
@Provides
@Singleton
fun provideService(): WanApiService = RetrofitManager.getApiService(
WanApiService::class.java) //提供WanApiService单例,只需要写一次
}
interface DemoWebsocketService {
@Send
fun send(protocol: Protocol)
@Receive
fun observeCustomInfo(): Flowable<Result>
@Receive
fun observeWebSocketEvent(): Flowable<WebSocket.Event>
@Send
fun send(msg: String)
}
val wsManager: WSManager<DemoWebsocketService> = WSManager()
wsManager.service.send(message)
wsManager.service.observeCustomInfo().subscribe {
receivedMessages.postValue(it.message)
}