Skip to content

Commit

Permalink
组件封装
Browse files Browse the repository at this point in the history
  • Loading branch information
linxiangcheer committed Mar 19, 2021
1 parent 89052dc commit c3e4305
Show file tree
Hide file tree
Showing 21 changed files with 389 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .idea/codeStyles/Project.xml

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

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

import android.app.Application
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
import org.koin.core.context.startKoin
import org.koin.core.logger.Level

/*
*
* 抽象的公用BaseApplication*/
class BaseApplication: Application() {
override fun onCreate() {
super.onCreate()

startKoin {
androidLogger(Level.ERROR) //log level error方法,保证这句话不出错不然不写

androidContext(this@BaseApplication)
}
}
}
24 changes: 24 additions & 0 deletions common/src/main/java/com/cniao5/common/base/BaseActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.cniao5.common.base

import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer

/*
* BaseActivity的抽象基类
* */
abstract class BaseActivity: AppCompatActivity(){



/*
* 扩展liveData的observer函数
* */
protected fun <T: Any> LiveData<T>.observerKt(block:(T) -> Unit) {
this.observe(this@BaseActivity, Observer { data ->
// block.invoke(data)
block(data)
})
}

}
27 changes: 27 additions & 0 deletions common/src/main/java/com/cniao5/common/base/BaseFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.cniao5.common.base

import androidx.annotation.LayoutRes
import androidx.fragment.app.Fragment
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer

/*
* Fragment的抽象基类
* */
abstract class BaseFragment: Fragment {

constructor(): super()

constructor(@LayoutRes layout: Int) : super(layout)

/*
* 扩展liveData的observer函数
* */
protected fun <T: Any> LiveData<T>.observerKt(block:(T) -> Unit) {
this.observe(viewLifecycleOwner, Observer { data ->
// block.invoke(data)
block(data)
})
}

}
87 changes: 87 additions & 0 deletions common/src/main/java/com/cniao5/common/ktx/ActivityKtx.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.cniao5.common.ktx

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.os.Build
import android.view.View
import android.view.WindowInsets
import android.view.WindowInsetsController
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import androidx.activity.ComponentActivity
import androidx.annotation.LayoutRes
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.LifecycleOwner

/*
* Activity相关的ktx,扩展函数或扩展属性
* */

/*
* Activity中使用DataBinding时setContentView的简化
* layout 布局文件
* return 返回一个Binding的对象实例
* @LayoutRes 可以检测是否是资源布局
* */
fun <T : ViewDataBinding> Activity.bindView(@LayoutRes layout: Int): T {
return DataBindingUtil.setContentView(this, layout)
}

/*
* Activity中使用DataBinding时setContentView的简化
* layout 布局文件
* return 返回一个Binding的对象实例 T类型的 可null的
* */
fun <T : ViewDataBinding> Activity.bindView(view: View): T? {
return DataBindingUtil.bind<T>(view)
}

/*
* 界面Activity的沉浸式状态栏,使得可以在状态栏里面显示部分需要的图片
* 注意:需要在setContentView之前调用该函数才生效
* */
fun Activity.immediateStatusBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
window.setDecorFitsSystemWindows(false)
window.insetsController?.let {
it.hide(WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars())
it.systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
}
} else {
@Suppress("DEPRECATION")
// window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_FULLSCREEN
// or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
// or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
// or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
// or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
// or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION)
window.decorView.systemUiVisibility =
(View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
window.statusBarColor = Color.TRANSPARENT
}
}

/*
* 软键盘的隐藏
* view 事件控件view
* */
fun Activity.dismissKeyBoard(view: View){
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager?
imm?.hideSoftInputFromWindow(view.windowToken, 0)
}

//region 扩展属性
/*
* 扩展lifeCycleOwner属性,便于和Fragment之间使用lifecycleOwner的时候保持参数一致性
* */
val ComponentActivity.viewLifeCycleOwner: LifecycleOwner
get() = this

/*
* Activity的扩展字段,便于Fragment中使用livedata之类的时候,参数一致性
* */
val Activity.context: Context
get() = this
//endregion
2 changes: 2 additions & 0 deletions common/src/main/java/com/cniao5/common/ktx/FragmentKtx.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package com.cniao5.common.ktx

21 changes: 21 additions & 0 deletions common/src/main/java/com/cniao5/common/model/AbsentLifeData.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.cniao5.common.model

import androidx.lifecycle.LiveData

/*
* 创建一个空的liveData的对象类
* 常用于创建一个空的liveData对象来转接数据
* */
class AbsentLifeData<T: Any?> private constructor(): LiveData<T>() {

init {
postValue(null)
}

companion object {
fun <T: Any?> create(): LiveData<T> {
return AbsentLifeData()
}
}

}
54 changes: 54 additions & 0 deletions common/src/main/java/com/cniao5/common/model/SingleLiveData.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.cniao5.common.model

import android.util.Log
import androidx.annotation.MainThread
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import java.util.concurrent.atomic.AtomicBoolean

/*
* 但事件响应的liveData,只有一个接收者能接受到信息,可以避免不必要的业务场景中的事件消费通知
* 只有调用call的时候,observer才能收到通知
* */
class SingleLiveData <T>: MutableLiveData<T>() {

// 原子变量
// 在多线程环境下,当有多个线程同时执行这些类的实例包含的方法时,具有排他性
// 即当某个线程进入方法,执行其中的指令时,不会被其他线程打断
private val mPending = AtomicBoolean(false)

@MainThread //主线程注解
override fun observe(owner: LifecycleOwner, observer: Observer<in T>) {

if (hasActiveObservers()){
Log.w(TAG,"多个观察者存在的时候,只有一个会被通知到数据更新")
}

super.observe(owner, Observer { t ->
// 比较AtomicBoolean和expect这两个值,如果一致,执行方法内的语句
// 执行完之后把AtomicBoolean的值设为update的值
// 任何内部或者外部的语句都不可能在两个动作之间运行
if (mPending.compareAndSet(true, false))
observer.onChanged(t)
})
}

override fun setValue(value: T?) { //value可以为空不然会报错
mPending.set(true)
super.setValue(value)
}

/*
* 传一些空值
* */
@MainThread //主线程注解
fun call() {
value = null
}

companion object{
private const val TAG = "SingleLiveData"
}

}
Binary file added common/src/main/res/mipmap-hdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added common/src/main/res/mipmap-mdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added common/src/main/res/mipmap-xhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added common/src/main/res/mipmap-xxhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
132 changes: 132 additions & 0 deletions common/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="abc_decor_view_status_guard">#ff000000</color>
<color name="abc_decor_view_status_guard_light">#ffffffff</color>
<color name="abc_search_url_text_normal">#ff7fa87f</color>
<color name="abc_search_url_text_pressed">@android:color/black</color>
<color name="abc_search_url_text_selected">@android:color/black</color>
<color name="accent_material_dark">@color/material_deep_teal_200</color>
<color name="accent_material_light">@color/material_deep_teal_500</color>
<color name="androidx_core_ripple_material_light">#1f000000</color>
<color name="androidx_core_secondary_text_default_material_light">#8a000000</color>
<color name="background_floating_material_dark">@color/material_grey_800</color>
<color name="background_floating_material_light">@android:color/white</color>
<color name="background_material_dark">@color/material_grey_850</color>
<color name="background_material_light">@color/material_grey_50</color>
<color name="black">#ff000000</color>
<color name="bright_foreground_disabled_material_dark">#80ffffff</color>
<color name="bright_foreground_disabled_material_light">#80000000</color>
<color name="bright_foreground_inverse_material_dark">@color/bright_foreground_material_light</color>
<color name="bright_foreground_inverse_material_light">@color/bright_foreground_material_dark</color>
<color name="bright_foreground_material_dark">@android:color/white</color>
<color name="bright_foreground_material_light">@android:color/black</color>
<color name="button_material_dark">#ff5a595b</color>
<color name="button_material_light">#ffd6d7d7</color>
<color name="cardview_dark_background">#ff424242</color>
<color name="cardview_light_background">#ffffffff</color>
<color name="cardview_shadow_end_color">#03000000</color>
<color name="cardview_shadow_start_color">#37000000</color>
<color name="colorAccent">#ffffa000</color>
<color name="colorBlack">#ff000000</color>
<color name="colorDivider">#ffbdbdbd</color>
<color name="colorIcons">#ff212121</color>
<color name="colorPrice">#fff56c6c</color>
<color name="colorPrimary">#ffffc107</color>
<color name="colorPrimaryDark">#ffffa000</color>
<color name="colorPrimaryLight">#ffffecb3</color>
<color name="colorPrimaryText">#ff212121</color>
<color name="colorSecondaryText">#ff757575</color>
<color name="colorWhite">#ffffffff</color>
<color name="colorWindowBackGround">#fff8fafc</color>
<color name="design_bottom_navigation_shadow_color">#14000000</color>
<color name="design_dark_default_color_background">#ff121212</color>
<color name="design_dark_default_color_error">#ffcf6679</color>
<color name="design_dark_default_color_on_background">#ffffffff</color>
<color name="design_dark_default_color_on_error">#ff000000</color>
<color name="design_dark_default_color_on_primary">#ff000000</color>
<color name="design_dark_default_color_on_secondary">#ff000000</color>
<color name="design_dark_default_color_on_surface">#ffffffff</color>
<color name="design_dark_default_color_primary">#ffba86fc</color>
<color name="design_dark_default_color_primary_dark">#ff000000</color>
<color name="design_dark_default_color_primary_variant">#ff3700b3</color>
<color name="design_dark_default_color_secondary">#ff03dac6</color>
<color name="design_dark_default_color_secondary_variant">#ff03dac6</color>
<color name="design_dark_default_color_surface">#ff121212</color>
<color name="design_default_color_background">#ffffffff</color>
<color name="design_default_color_error">#ffb00020</color>
<color name="design_default_color_on_background">#ff000000</color>
<color name="design_default_color_on_error">#ffffffff</color>
<color name="design_default_color_on_primary">#ffffffff</color>
<color name="design_default_color_on_secondary">#ff000000</color>
<color name="design_default_color_on_surface">#ff000000</color>
<color name="design_default_color_primary">#ff6200ee</color>
<color name="design_default_color_primary_dark">#ff3700b3</color>
<color name="design_default_color_primary_variant">#ff3700b3</color>
<color name="design_default_color_secondary">#ff03dac6</color>
<color name="design_default_color_secondary_variant">#ff018786</color>
<color name="design_default_color_surface">#ffffffff</color>
<color name="design_fab_shadow_end_color">@android:color/transparent</color>
<color name="design_fab_shadow_mid_color">#14000000</color>
<color name="design_fab_shadow_start_color">#44000000</color>
<color name="design_fab_stroke_end_inner_color">#0a000000</color>
<color name="design_fab_stroke_end_outer_color">#0f000000</color>
<color name="design_fab_stroke_top_inner_color">#1affffff</color>
<color name="design_fab_stroke_top_outer_color">#2effffff</color>
<color name="design_snackbar_background_color">#ff323232</color>
<color name="dim_foreground_disabled_material_dark">#80bebebe</color>
<color name="dim_foreground_disabled_material_light">#80323232</color>
<color name="dim_foreground_material_dark">#ffbebebe</color>
<color name="dim_foreground_material_light">#ff323232</color>
<color name="error_color_material_dark">#ffff7043</color>
<color name="error_color_material_light">#ffff5722</color>
<color name="foreground_material_dark">@android:color/white</color>
<color name="foreground_material_light">@android:color/black</color>
<color name="highlighted_text_material_dark">#6680cbc4</color>
<color name="highlighted_text_material_light">#66009688</color>
<color name="ic_launcher_background">#ffffffff</color>
<color name="material_blue_grey_800">#ff37474f</color>
<color name="material_blue_grey_900">#ff263238</color>
<color name="material_blue_grey_950">#ff21272b</color>
<color name="material_deep_teal_200">#ff80cbc4</color>
<color name="material_deep_teal_500">#ff008577</color>
<color name="material_grey_100">#fff5f5f5</color>
<color name="material_grey_300">#ffe0e0e0</color>
<color name="material_grey_50">#fffafafa</color>
<color name="material_grey_600">#ff757575</color>
<color name="material_grey_800">#ff424242</color>
<color name="material_grey_850">#ff303030</color>
<color name="material_grey_900">#ff212121</color>
<color name="mtrl_btn_text_color_disabled">#61000000</color>
<color name="mtrl_btn_transparent_bg_color">#00ffffff</color>
<color name="mtrl_scrim_color">#52000000</color>
<color name="mtrl_textinput_default_box_stroke_color">#6b000000</color>
<color name="mtrl_textinput_disabled_color">#1f000000</color>
<color name="mtrl_textinput_filled_box_default_background_color">#0a000000</color>
<color name="mtrl_textinput_focused_box_stroke_color">#00000000</color>
<color name="mtrl_textinput_hovered_box_stroke_color">#de000000</color>
<color name="notification_action_color_filter">@color/androidx_core_secondary_text_default_material_light</color>
<color name="notification_icon_bg_color">#ff9e9e9e</color>
<color name="notification_material_background_media_default_color">#ff424242</color>
<color name="primary_dark_material_dark">@android:color/black</color>
<color name="primary_dark_material_light">@color/material_grey_600</color>
<color name="primary_material_dark">@color/material_grey_900</color>
<color name="primary_material_light">@color/material_grey_100</color>
<color name="primary_text_default_material_dark">#ffffffff</color>
<color name="primary_text_default_material_light">#de000000</color>
<color name="primary_text_disabled_material_dark">#4dffffff</color>
<color name="primary_text_disabled_material_light">#39000000</color>
<color name="ripple_material_dark">#33ffffff</color>
<color name="ripple_material_light">#1f000000</color>
<color name="secondary_text_default_material_dark">#b3ffffff</color>
<color name="secondary_text_default_material_light">#8a000000</color>
<color name="secondary_text_disabled_material_dark">#36ffffff</color>
<color name="secondary_text_disabled_material_light">#24000000</color>
<color name="select_color">#ff2e2e32</color>
<color name="switch_thumb_disabled_material_dark">#ff616161</color>
<color name="switch_thumb_disabled_material_light">#ffbdbdbd</color>
<color name="switch_thumb_normal_material_dark">#ffbdbdbd</color>
<color name="switch_thumb_normal_material_light">#fff1f1f1</color>
<color name="tooltip_background_dark">#e6616161</color>
<color name="tooltip_background_light">#e6ffffff</color>
<color name="white">#ffffffff</color>
</resources>
Loading

0 comments on commit c3e4305

Please sign in to comment.