Skip to content

Commit

Permalink
Code clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
kanch231004 committed Jan 23, 2020
1 parent b0ffff7 commit 5814060
Show file tree
Hide file tree
Showing 15 changed files with 124 additions and 151 deletions.
7 changes: 7 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ android {
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

/** Replace with your api_token
* eg: Your api token is 6nt5d1nJHkqbkphf
* Following line should be replaced by
* buildConfigField 'String', 'API_DEVELOPER_TOKEN', "\"6nt5d1nJHkqbkphf\""*/

buildConfigField 'String', 'API_DEVELOPER_TOKEN', "\"${api_developer_token}\""
}
buildTypes {
release {
Expand Down
57 changes: 32 additions & 25 deletions app/src/main/java/com/cnx/pingme/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,19 @@ import javax.inject.Inject
import kotlin.collections.ArrayList




class MainActivity : AppCompatActivity(), HasSupportFragmentInjector {
class MainActivity : AppCompatActivity(), HasSupportFragmentInjector {

@Inject
lateinit var dispatchingAndroidInjector: DispatchingAndroidInjector<Fragment>

override fun supportFragmentInjector(): AndroidInjector<Fragment> = dispatchingAndroidInjector
override fun supportFragmentInjector(): AndroidInjector<Fragment> = dispatchingAndroidInjector

@Inject lateinit var viewModelFactory: ViewModelProvider.Factory
private lateinit var drawerLayout : DrawerLayout
@Inject
lateinit var viewModelFactory: ViewModelProvider.Factory
private lateinit var drawerLayout: DrawerLayout
private lateinit var chatViewModel: ChatViewModel
private var userSession : String = SESSION_BOB
private lateinit var chatRVAdapter : ChatAdapter
private var userSession: String = SESSION_BOB
private lateinit var chatRVAdapter: ChatAdapter

private var messages = ArrayList<MessageModel>()

Expand All @@ -49,7 +48,6 @@ class MainActivity : AppCompatActivity(), HasSupportFragmentInjector {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)


chatViewModel = injectViewModel(viewModelFactory)
chatViewModel.userSessionLd.postValue(userSession)
getChats()
Expand All @@ -67,12 +65,14 @@ class MainActivity : AppCompatActivity(), HasSupportFragmentInjector {

fabSend.setOnClickListener {

if(!TextUtils.isEmpty(etMessage.editableText)) {
if (!TextUtils.isEmpty(etMessage.editableText)) {


val message = MessageModel(UUID.randomUUID().toString(),userSession,
CHATBOT_ID, USER_NAME,"",
etMessage.editableText.toString().trim(),true)
val message = MessageModel(
UUID.randomUUID().toString(), userSession,
CHATBOT_ID, USER_NAME, "",
etMessage.editableText.toString().trim(), true
)

etMessage.setText("")
chatViewModel.sendAndReceiveChat(message)
Expand All @@ -87,8 +87,10 @@ class MainActivity : AppCompatActivity(), HasSupportFragmentInjector {

drawerLayout = findViewById<DrawerLayout>(R.id.drawer_layout)

var actionBarToggle = object : ActionBarDrawerToggle(this
,drawerLayout , toolbar, R.string.opened, R.string.closed) {
var actionBarToggle = object : ActionBarDrawerToggle(
this
, drawerLayout, toolbar, R.string.opened, R.string.closed
) {

override fun onDrawerOpened(drawerView: View) {
super.onDrawerOpened(drawerView)
Expand All @@ -107,43 +109,49 @@ class MainActivity : AppCompatActivity(), HasSupportFragmentInjector {

when (it.itemId) {

R.id.tom -> { userSession = SESSION_TOM ; ivProfile.setImageResource(R.drawable.pic_tom) }
R.id.tom -> {
userSession = SESSION_TOM; ivProfile.setImageResource(R.drawable.pic_tom)
}

R.id.bob -> { userSession = SESSION_BOB ; ivProfile.setImageResource(R.drawable.pic_bob)}
R.id.bob -> {
userSession = SESSION_BOB; ivProfile.setImageResource(R.drawable.pic_bob)
}

R.id.jennifer -> { userSession = SESSION_JENNI ; ivProfile.setImageResource(R.drawable.pic_jennifer)}
R.id.jennifer -> {
userSession = SESSION_JENNI; ivProfile.setImageResource(R.drawable.pic_jennifer)
}

R.id.mark -> { userSession = SESSION_MARK ; ivProfile.setImageResource(R.drawable.pic_mark)}
R.id.mark -> {
userSession = SESSION_MARK; ivProfile.setImageResource(R.drawable.pic_mark)
}

}

chatViewModel.userSessionLd.postValue(userSession)
toolbar.title = userSession
tvSelectedName.text = userSession

return@setNavigationItemSelectedListener true
return@setNavigationItemSelectedListener true
}

}


private fun getChats() {

chatViewModel.chatList.observe(this, Observer {

chatRVAdapter.submitList(it)

if (it.size > 0)
rvMsg.smoothScrollToPosition(it.size -1)
rvMsg.smoothScrollToPosition(it.size - 1)

})

chatViewModel.userSessionLd.observe(this, Observer {
chatViewModel.userSessionLd.observe(this, Observer {
userSession = it
})
}


private fun hideKeyboard() {

val view = this.currentFocus
Expand All @@ -152,5 +160,4 @@ class MainActivity : AppCompatActivity(), HasSupportFragmentInjector {
imm?.hideSoftInputFromWindow(view.windowToken, 0)
}
}

}
8 changes: 3 additions & 5 deletions app/src/main/java/com/cnx/pingme/PingMeApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import dagger.android.HasActivityInjector
import javax.inject.Inject


class PingMeApp : Application() , HasActivityInjector {
class PingMeApp : Application(), HasActivityInjector {

@Inject
public lateinit var dispatchingAndroidInjector: DispatchingAndroidInjector<Activity>
Expand All @@ -20,10 +20,9 @@ class PingMeApp : Application() , HasActivityInjector {
lateinit var workManager: WorkManager

companion object {
var appInstance : PingMeApp? = null
var appInstance: PingMeApp? = null
}


override fun onCreate() {
super.onCreate()

Expand All @@ -36,5 +35,4 @@ class PingMeApp : Application() , HasActivityInjector {

override fun activityInjector() = dispatchingAndroidInjector


}
}
1 change: 0 additions & 1 deletion app/src/main/java/com/cnx/pingme/api/BaseDataSource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,5 @@ abstract class BaseDataSource {
Timber.e(message)
return Result.error("Network call has failed for a following reason: $message")
}

}

1 change: 0 additions & 1 deletion app/src/main/java/com/cnx/pingme/api/ChatService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@ interface ChatService {
suspend fun getChats(@Query("externalID") externalId : String, @Query("message") message : String,
@Query("chatBotID") chatBotID : Int = CHATBOT_ID,@Query("apiKey") apiKey : String = apiKeyValue
) : Response<ChatResponse>

}
14 changes: 7 additions & 7 deletions app/src/main/java/com/cnx/pingme/api/OfflineChatService.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.cnx.pingme.api

import com.cnx.pingme.BuildConfig
import com.cnx.pingme.utils.APIKEY
import com.cnx.pingme.utils.CHATBOT_ID
import retrofit2.Call
Expand All @@ -10,20 +11,19 @@ import retrofit2.http.Query
* previous versions as work manager is itself executing
* this call in background so need of couroutine calling this function anymore */


/** This apikey value can be changed to show error */

const val apiKeyValue = "6nt5d1nJHkqbkphe"
const val apiKeyValue = BuildConfig.API_DEVELOPER_TOKEN

interface OfflineChatService {

companion object {
const val ENDPOINT = "https://www.personalityforge.com/"
}


@GET("api/chat/")
fun getChats(@Query("externalID") externalId : String, @Query("message") message : String,
@Query("chatBotID") chatBotID : Int = CHATBOT_ID, @Query(APIKEY) apiKey : String = apiKeyValue
) : Call<ChatResponse>
}
fun getChats(
@Query("externalID") externalId: String, @Query("message") message: String,
@Query("chatBotID") chatBotID: Int = CHATBOT_ID, @Query(APIKEY) apiKey: String = apiKeyValue
): Call<ChatResponse>
}
2 changes: 1 addition & 1 deletion app/src/main/java/com/cnx/pingme/api/ResponseModels.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ data class MessageModel(
var message: String?,
var isSent : Boolean = false,
var isSuccess : Boolean = true
)
)
14 changes: 5 additions & 9 deletions app/src/main/java/com/cnx/pingme/chat/ChatVH.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,21 @@ import kotlinx.android.synthetic.main.rv_sent_message.view.*
/** Could be inside adapter class if it is not gonna be used elsewhere */


class ChatVH(itemView: View) : RecyclerView.ViewHolder(itemView ) {
class ChatVH(itemView: View) : RecyclerView.ViewHolder(itemView) {

fun bind(messageModel : MessageModel) {
fun bind(messageModel: MessageModel) {

with(itemView) {

if(messageModel.isSent) {

if (messageModel.isSent) {
tvMessage.text = messageModel.message
tvName.text = messageModel.chatBotName
tvFailed.visibility = if (messageModel.isSuccess) View.GONE else View.VISIBLE

} else {

tvSenderMessage.text = messageModel.message
tvSenderName.text = messageModel.userSession
}

}

}

}
}
16 changes: 7 additions & 9 deletions app/src/main/java/com/cnx/pingme/chat/ChatViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,20 @@ import com.cnx.pingme.room.ChatRepository
import kotlinx.coroutines.CoroutineScope
import javax.inject.Inject

class ChatViewModel @Inject constructor(private val chatRepository: ChatRepository,
@CoroutineScopeIO private val ioCoroutineScope: CoroutineScope

): ViewModel() {
class ChatViewModel @Inject constructor(
private val chatRepository: ChatRepository,
@CoroutineScopeIO private val ioCoroutineScope: CoroutineScope

) : ViewModel() {

fun sendAndReceiveChat(messageModel: MessageModel) {

chatRepository.sendAndReceiveChat(messageModel,ioCoroutineScope)
chatRepository.sendAndReceiveChat(messageModel, ioCoroutineScope)
}


var userSessionLd : MutableLiveData<String> = MutableLiveData()
var userSessionLd: MutableLiveData<String> = MutableLiveData()

val chatList = chatRepository.getChatList(userSessionLd)



}
}
10 changes: 1 addition & 9 deletions app/src/main/java/com/cnx/pingme/chat/FeedAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ import com.cnx.pingme.utils.SENT_MESSAGE

class ChatAdapter : PagedListAdapter<MessageModel, ChatVH>(diffCallback) {


override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ChatVH {


val view = LayoutInflater.from(parent.context)
.inflate( viewType,parent,false)

Expand All @@ -24,21 +22,18 @@ class ChatAdapter : PagedListAdapter<MessageModel, ChatVH>(diffCallback) {

val chat = getItem(position)
chat?.let { holder.bind(messageModel = chat) }

}


override fun getItemViewType(position: Int): Int {
return if(currentList!![position]?.isSent == true) SENT_MESSAGE else RECEIVED_MESSAGE
}


companion object {
/**
* This diff callback informs the PagedListAdapter how to compute list differences when new
* PagedLists arrive.
* <p>
* When you add a Chat with the 'Add' button, the PagedListAdapter uses diffCallback to
* When you add a Chat, the PagedListAdapter uses diffCallback to
* detect there's only a single item difference from before, so it only needs to animate and
* rebind a single view.
*
Expand All @@ -48,12 +43,9 @@ class ChatAdapter : PagedListAdapter<MessageModel, ChatVH>(diffCallback) {
override fun areItemsTheSame(oldItem: MessageModel, newItem: MessageModel): Boolean =
oldItem.id == newItem.id


override fun areContentsTheSame(oldItem: MessageModel, newItem: MessageModel): Boolean =
oldItem == newItem
}
}


}

2 changes: 0 additions & 2 deletions app/src/main/java/com/cnx/pingme/room/AppDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ abstract class AppDatabase : RoomDatabase() {

abstract fun getChatDao(): ChatDao


companion object {

@Volatile
private var instance: AppDatabase? = null

Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/com/cnx/pingme/room/ChatDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ interface ChatDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertChat(chat : MessageModel)


@Query("SELECT * From MessageModel Where userSession = :userSess")
fun getMessageForId(userSess : String) : DataSource.Factory<Int,MessageModel>

@Query("Update MessageModel SET isSuccess = :isSuccess Where id = :msgId")
fun updateChat(isSuccess : Boolean, msgId: Int)
}
}
Loading

0 comments on commit 5814060

Please sign in to comment.