Skip to content

Commit

Permalink
src moved from root
Browse files Browse the repository at this point in the history
  • Loading branch information
merlinJeyakumar committed Feb 4, 2022
1 parent 27c01d2 commit 557ddfa
Show file tree
Hide file tree
Showing 60 changed files with 2,966 additions and 0 deletions.
29 changes: 29 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
plugins {
id("com.android.library")
id("kotlin-android")
id("kotlin-parcelize")
id("dagger.hilt.android.plugin")
id("kotlin-kapt")
}

android {
setCompileSdkVersion(Configs.compileSdkVersion)
defaultConfig {
setMinSdkVersion(Configs.minSdkVersion)
setTargetSdkVersion(Configs.targetSdkVersion.toString())
vectorDrawables.useSupportLibrary = true
multiDexEnabled = true
}
buildFeatures {
viewBinding = true
dataBinding = true
}
}

dependencies {
requiredLibraries()
dateTimeLibraries()
roomLibraries()
supportLibraries()
imageLoaderLibraries()
}
1 change: 1 addition & 0 deletions src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<manifest package="nativedevps.support" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.nativedevps.support.base_class

import android.os.Bundle
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import nativedevps.support.R
import nativedevps.support.databinding.ActivityActionbarBinding

abstract class ActionBarActivity<VB : ViewDataBinding, VM : BaseViewModel>(
private val layout: Int,
viewModelClass: Class<VM>,
) : BaseActivity<ActivityActionbarBinding, VM>(R.layout.activity_actionbar, viewModelClass) {

protected lateinit var childBinding: VB

/*super call required*/
override fun onInit(savedInstanceState: Bundle?) {
viewBind()
initActionBar()
}

private fun viewBind() {
childBinding =
DataBindingUtil.inflate(layoutInflater, layout, binding.rootFrameLayout, false)
binding.rootFrameLayout.addView(childBinding.root)
}

private fun initActionBar() {
setSupportActionBar(binding.toolbar)
}

fun setTitle(title: String) {
supportActionBar?.title = title
}

fun setNavigateUpEnabled(boolean: Boolean) {
supportActionBar?.setDisplayHomeAsUpEnabled(boolean)
}

override fun onBackPressed() {
super.onBackPressed()
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
}
}
87 changes: 87 additions & 0 deletions src/main/java/com/nativedevps/support/base_class/BaseActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.nativedevps.support.base_class

import android.content.Context
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModelProvider
import androidx.viewbinding.ViewBinding
import com.nativedevps.support.custom_views.ProgressDialog
import com.nativedevps.support.inline.orElse
import com.nativedevps.support.utility.language.ContextWrapper
import com.nativedevps.support.utility.language.Utility.getDeviceLocale
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.*


abstract class BaseActivity<B : ViewDataBinding, VM : BaseViewModel>(
internal val bindingFactory: Int,
private val viewModelClass: Class<VM>,
) : AppCompatActivity() {
protected val viewModel: VM by lazy { ViewModelProvider(this).get(viewModelClass) }
private var progressDialog: ProgressDialog? = null

protected val binding: B by contentView(bindingFactory)

private lateinit var _binding: ViewBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
initBinding()
preInit()
setContentView(binding.root)

initObserver()
onInit(savedInstanceState)
viewModel.onCreate()
}

open fun preInit() {
}

private fun initObserver() {
viewModel.liveDataProgressBar.observe(this, { triple ->
if (triple.first) {
progressDialog?.setProgress(triple.second).orElse {
progressDialog = ProgressDialog(this)
}
progressDialog?.setMessage(triple.third)?.build()
} else {
progressDialog?.dismiss()
progressDialog = null
}
})
}

private fun initBinding() {
binding.lifecycleOwner = this
}

abstract fun onInit(savedInstanceState: Bundle?)

override fun onDestroy() {
super.onDestroy()
progressDialog?.dismiss()
}

fun runOnNewThread(callback: suspend CoroutineScope.() -> Unit) {
CoroutineScope(Dispatchers.IO).launch {
callback()
}
}

override fun attachBaseContext(context: Context) {
val lang = getLocale(context) ?: getDeviceLocale()?.language
val contextWrapper = Locale(lang).let {
Locale.setDefault(it)
ContextWrapper.wrap(context, it)
}
super.attachBaseContext(contextWrapper)
}

open fun getLocale(context: Context): String? {
return null
}
}
154 changes: 154 additions & 0 deletions src/main/java/com/nativedevps/support/base_class/BaseAdapter2.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package com.nativedevps.support.base_class

import android.util.Log
import androidx.recyclerview.widget.DiffUtil
import com.nativedevps.support.custom_views.ListAdapter
import com.nativedevps.support.inline.orElse

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.util.*

abstract class BaseAdapter2<ITEM_TYPE, SELECTION_TYPE>() :
ListAdapter<ITEM_TYPE, BaseViewHolder<ITEM_TYPE, SELECTION_TYPE>>() {

private val TAG: String = "BaseAdapter"
private val selectionList: MutableList<SELECTION_TYPE> = ArrayList()

init {
val diffItemCallback = object : DiffUtil.ItemCallback<ITEM_TYPE>() {
override fun areItemsTheSame(
oldItem: ITEM_TYPE,
newItem: ITEM_TYPE,
): Boolean {
return isSameItem(oldItem, newItem)
}

override fun areContentsTheSame(
oldItem: ITEM_TYPE,
newItem: ITEM_TYPE,
): Boolean {
return isSameContent(oldItem, newItem)
}

override fun getChangePayload(oldItem: ITEM_TYPE, newItem: ITEM_TYPE): Any? {
return getChangePayload()?.let { it }
.orElse { super.getChangePayload(oldItem, newItem) }
}
}
init(diffItemCallback)
}

companion object {
const val PAYLOAD_SELECTION_MODE: String = "is_selection_mode"
}

fun setSelected(key: SELECTION_TYPE) {
if (isSelectable(key)) {
if (selectionList.contains(key)) {
Log.e(TAG, "error: item already selected")
return
}
selectionList.add(key)
notifyItemChanged(
getIndex(key),
PAYLOAD_SELECTION_MODE
)
}
}

fun clearSelection(topic: SELECTION_TYPE) {
if (!selectionList.contains(topic)) {
Log.e(TAG, "error: item already cleared")
return
}
selectionList.remove(topic)
notifyItemChanged(
getIndex(topic),
PAYLOAD_SELECTION_MODE
)
}

fun clearSelection(notifyAll: Boolean = false) {
if (!notifyAll) {
for (topicId in selectionList) {
val itemId = getIndex(topicId)
if (itemId != -1) {
notifyItemChanged(itemId, PAYLOAD_SELECTION_MODE)
} else {
Log.e(TAG, "error: item not found $topicId")
}
}
} else {
notifyDataSetChanged()
}
selectionList.clear()
}

fun selectAll(notifyAll: Boolean = true) {
val selectableKeys = mutableListOf<SELECTION_TYPE>()
for (key in getAllKeys()) {
if (isSelectable(key)) {
selectableKeys.add(key)
}
}
selectionList.clear()
selectionList.addAll(selectableKeys)
if (notifyAll) {
notifyDataSetChanged()
} else {
for (selectableKey in selectableKeys) {
val itemId = getIndex(selectableKey)
if (itemId != -1) {
notifyItemChanged(itemId, PAYLOAD_SELECTION_MODE)
}
}
}

}

fun isSelected(topic: SELECTION_TYPE): Boolean {
return selectionList.contains(topic)
}

fun isSelectionMode(): Boolean {
return selectionList.isNotEmpty()
}

fun getSelections(): MutableList<SELECTION_TYPE> {
return selectionList
}

abstract fun getList(): List<ITEM_TYPE>

open fun getIndex(itemKey: SELECTION_TYPE): Int {
return -1
}

open fun getAllKeys(): List<SELECTION_TYPE> {
return listOf()
}

open fun isSelectable(key: SELECTION_TYPE): Boolean {
return true
}

open fun isSameItem(oldItem: ITEM_TYPE, newItem: ITEM_TYPE): Boolean {
return false
}

open fun isSameContent(oldItem: ITEM_TYPE, newItem: ITEM_TYPE): Boolean {
return true
}

open fun getChangePayload(): Any? {
return null
}

suspend fun runOnUiThread(callback: suspend CoroutineScope.() -> Unit) {
withContext(Dispatchers.Main) {
callback()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.nativedevps.support.base_class

open class BaseRepository
50 changes: 50 additions & 0 deletions src/main/java/com/nativedevps/support/base_class/BaseViewHolder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.nativedevps.support.base_class

import android.content.Context
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import org.jetbrains.anko.toast

abstract class BaseViewHolder<M, SELECTION_TYPE>(
private val selectedList: List<SELECTION_TYPE>,
itemView: View
) : RecyclerView.ViewHolder(itemView) {

val context: Context get() = itemView.context

fun toast(resId: Int) {
itemView.context.toast(resId)
}

fun toast(text: String) {
itemView.context.toast(text)
}

abstract fun bind(position: Int, item: M)

open fun bind(
position: Int,
item: M,
payload: List<Any>
) {
}

open fun setOptions(
position: Int,
item: M,
option: Map<String, Any> = mutableMapOf()
) {
}

fun getSelectionList(): List<SELECTION_TYPE> {
return selectedList
}

fun isSelected(text: SELECTION_TYPE): Boolean {
return getSelectionList().contains(text)
}

fun isSelectionMode(): Boolean {
return getSelectionList().isNotEmpty()
}
}
Loading

0 comments on commit 557ddfa

Please sign in to comment.