Skip to content

Commit

Permalink
Added more presenters in dependency injection modules (fossasia#2484)
Browse files Browse the repository at this point in the history
  • Loading branch information
pallav12 authored Feb 6, 2020
1 parent 3799b9d commit 388d20b
Show file tree
Hide file tree
Showing 18 changed files with 48 additions and 94 deletions.
7 changes: 3 additions & 4 deletions app/src/main/java/org/fossasia/susi/ai/chat/ChatActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ import org.fossasia.susi.ai.helper.Constant
import org.fossasia.susi.ai.helper.PrefManager
import org.fossasia.susi.ai.helper.Utils.hideSoftKeyboard
import org.fossasia.susi.ai.skills.SkillsActivity
import org.koin.android.ext.android.inject
import org.koin.core.parameter.parametersOf
import timber.log.Timber

/**
Expand All @@ -65,7 +67,7 @@ import timber.log.Timber
@Suppress("UNUSED_PARAMETER", "DEPRECATION")
class ChatActivity : AppCompatActivity(), IChatView {

lateinit var chatPresenter: IChatPresenter
private val chatPresenter: IChatPresenter by inject { parametersOf(this) }
lateinit var youtubeVid: IYoutubeVid
private val PERM_REQ_CODE = 1
private lateinit var recyclerAdapter: ChatFeedRecyclerAdapter
Expand Down Expand Up @@ -96,9 +98,6 @@ class ChatActivity : AppCompatActivity(), IChatView {
val firstRun = intent.getBooleanExtra(Constant.FIRST_TIME, false)
gestureDetectorCompat = GestureDetectorCompat(this, CustomGestureListener())

chatPresenter = ChatPresenter(this)
chatPresenter.onAttach(this)

youtubeVid = YoutubeVid(this)
setUpUI()
initializationMethod(firstRun)
Expand Down
8 changes: 1 addition & 7 deletions app/src/main/java/org/fossasia/susi/ai/chat/ChatPresenter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,11 @@ import timber.log.Timber
* The P in MVP
* Created by chiragw15 on 9/7/17.
*/
class ChatPresenter(context: Context) :
class ChatPresenter(context: Context, private val chatView: IChatView?) :
IChatPresenter, IChatModel.OnRetrievingMessagesFinishedListener,
IChatModel.OnLocationFromIPReceivedListener, IChatModel.OnMessageFromSusiReceivedListener,
IDatabaseRepository.OnDatabaseUpdateListener {

private var chatView: IChatView? = null
var chatModel: IChatModel = ChatModel()
private var utilModel: UtilModel = UtilModel(context)
private var databaseRepository: IDatabaseRepository = DatabaseRepository()
Expand All @@ -77,10 +76,6 @@ class ChatPresenter(context: Context) :
@Volatile
var queueExecuting = AtomicBoolean(false)

override fun onAttach(chatView: IChatView) {
this.chatView = chatView
}

override fun setUp() {

// find total number of messages and find new message index
Expand Down Expand Up @@ -619,6 +614,5 @@ class ChatPresenter(context: Context) :
override fun onDetach() {
locationHelper.removeListener()
databaseRepository.closeDatabase()
chatView = null
}
}
11 changes: 3 additions & 8 deletions app/src/main/java/org/fossasia/susi/ai/chat/STTfragment.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.fossasia.susi.ai.chat

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Handler
Expand All @@ -23,24 +22,21 @@ import org.fossasia.susi.ai.R
import org.fossasia.susi.ai.chat.adapters.recycleradapters.VoiceCommandsAdapter
import org.fossasia.susi.ai.chat.contract.IChatPresenter
import org.fossasia.susi.ai.helper.PrefManager
import org.koin.android.ext.android.inject
import org.koin.core.parameter.parametersOf
import timber.log.Timber

/**
* Created by meeera on 17/8/17.
*/
class STTFragment : Fragment() {
lateinit var recognizer: SpeechRecognizer
lateinit var chatPresenter: IChatPresenter
private val chatPresenter: IChatPresenter by inject { parametersOf(this) }
private val thisActivity = activity
private var textToSpeech: TextToSpeech? = null
private val mainHandler: Handler = Handler()
private val subHandler: Handler = Handler()

override fun onAttach(context: Context?) {
super.onAttach(context)
chatPresenter = ChatPresenter(requireContext())
}

@NonNull
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val rootView = inflater.inflate(R.layout.fragment_sttframe, container, false)
Expand Down Expand Up @@ -140,7 +136,6 @@ class STTFragment : Fragment() {
activity?.searchChat?.show()
activity?.voiceSearchChat?.show()
activity?.btnSpeak?.isEnabled = true
chatPresenter.startHotwordDetection()
activity?.supportFragmentManager?.popBackStackImmediate()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class VoiceCommandsAdapter(val items: ArrayList<String>, val context: Context?)
lateinit var chatPresenter: IChatPresenter

override fun onCreateViewHolder(parent: ViewGroup, p1: Int): ViewHolder {
chatPresenter = ChatPresenter(context as Context)
chatPresenter = ChatPresenter(context as Context, null)
return ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_voice_commands, parent, false))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ import org.fossasia.susi.ai.rest.responses.susi.SusiResponse
*/
interface IChatPresenter {

// At the start of Activity
fun onAttach(chatView: IChatView)

fun retrieveOldMessages(firstRun: Boolean)
fun getUndeliveredMessages()
fun setUp()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ class ConnectedDevicePresenter(private val connectedDeviceView: IConnectedDevice

private val roomModel: RoomModel = RoomModel()

override fun onAttach(connectedDeviceView: IConnectedDeviceView) {
}

override fun getDevices() {
roomModel.getConnectedDevices(this)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package org.fossasia.susi.ai.device.connecteddevices.contract

interface IConnectedDevicePresenter {

fun onAttach(connectedDeviceView: IConnectedDeviceView)

fun getDevices()

fun openViewDevice(position: Int)
Expand Down
21 changes: 21 additions & 0 deletions app/src/main/java/org/fossasia/susi/ai/di/Modules.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
package org.fossasia.susi.ai.di

import org.fossasia.susi.ai.chat.ChatPresenter
import org.fossasia.susi.ai.chat.contract.IChatPresenter
import org.fossasia.susi.ai.chat.contract.IChatView
import org.fossasia.susi.ai.device.connecteddevices.ConnectedDevicePresenter
import org.fossasia.susi.ai.device.connecteddevices.contract.IConnectedDevicePresenter
import org.fossasia.susi.ai.device.connecteddevices.contract.IConnectedDeviceView
import org.fossasia.susi.ai.device.viewdevice.ViewDevicePresenter
import org.fossasia.susi.ai.device.viewdevice.contract.IViewDevicePresenter
import org.fossasia.susi.ai.device.viewdevice.contract.IViewDeviceView
import org.fossasia.susi.ai.login.LoginPresenter
import org.fossasia.susi.ai.login.contract.ILoginPresenter
import org.fossasia.susi.ai.login.contract.ILoginView
import org.fossasia.susi.ai.signup.SignUpPresenter
import org.fossasia.susi.ai.signup.contract.ISignUpPresenter
import org.fossasia.susi.ai.signup.contract.ISignUpView
import org.fossasia.susi.ai.skills.groupwiseskills.GroupWiseSkillsPresenter
import org.fossasia.susi.ai.skills.groupwiseskills.contract.IGroupWiseSkillsPresenter
import org.fossasia.susi.ai.skills.groupwiseskills.contract.IGroupWiseSkillsView
import org.fossasia.susi.ai.skills.settings.SettingsPresenter
import org.fossasia.susi.ai.skills.settings.contract.ISettingsPresenter
import org.fossasia.susi.ai.skills.settings.contract.ISettingsView
import org.koin.android.ext.koin.androidContext
import org.koin.core.module.Module
import org.koin.dsl.module

Expand All @@ -19,4 +32,12 @@ val modules: Module = module(override = true) {
factory<IConnectedDevicePresenter> { (view: IConnectedDeviceView) -> ConnectedDevicePresenter(view) }

factory<IViewDevicePresenter> { (view: IViewDeviceView) -> ViewDevicePresenter(view) }

factory<IChatPresenter> { (view: IChatView) -> ChatPresenter(androidContext(), view) }

factory<ILoginPresenter> { (view: ILoginView) -> LoginPresenter(androidContext(), view) }

factory<ISignUpPresenter> { (view: ISignUpView) -> SignUpPresenter(androidContext(), view) }

factory<ISettingsPresenter> { (view: ISettingsView) -> SettingsPresenter(androidContext(), view) }
}
11 changes: 4 additions & 7 deletions app/src/main/java/org/fossasia/susi/ai/login/LoginActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import org.fossasia.susi.ai.helper.Utils.hideSoftKeyboard
import org.fossasia.susi.ai.login.contract.ILoginPresenter
import org.fossasia.susi.ai.login.contract.ILoginView
import org.fossasia.susi.ai.signup.SignUpActivity
import org.koin.android.ext.android.inject
import org.koin.core.parameter.parametersOf

/**
* <h1>The Login activity.</h1>
Expand All @@ -34,7 +36,7 @@ class LoginActivity : AppCompatActivity(), ILoginView {

lateinit var forgotPasswordProgressDialog: AlertDialog
lateinit var builder: AlertDialog.Builder
private lateinit var loginPresenter: ILoginPresenter
private val loginPresenter: ILoginPresenter by inject { parametersOf(this) }
private lateinit var progressDialog: ProgressDialog

@SuppressLint("InflateParams")
Expand Down Expand Up @@ -67,8 +69,8 @@ class LoginActivity : AppCompatActivity(), ILoginView {
cancelRequestPassword()
requestPassword()

loginPresenter = LoginPresenter(this)
loginPresenter.onAttach(this)

val bundle = intent.extras
val string = bundle?.getString("email")
if (string != null)
Expand Down Expand Up @@ -208,11 +210,6 @@ class LoginActivity : AppCompatActivity(), ILoginView {
outState.putBoolean(Constant.SERVER, customServer.isChecked)
}

override fun onDestroy() {
loginPresenter.onDetach()
super.onDestroy()
}

override fun resetPasswordSuccess() {
startActivity(Intent(this@LoginActivity, ForgotPass::class.java))
}
Expand Down
10 changes: 2 additions & 8 deletions app/src/main/java/org/fossasia/susi/ai/login/LoginPresenter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,14 @@ import timber.log.Timber
* The P in MVP
* Created by chiragw15 on 4/7/17.
*/
class LoginPresenter(loginActivity: LoginActivity) :
class LoginPresenter(context: Context, private val loginView: ILoginView) :
ILoginPresenter,
ILoginModel.OnLoginFinishedListener,
IForgotPasswordModel.OnFinishListener {

private var loginModel: LoginModel = LoginModel()
private var utilModel: UtilModel = UtilModel(loginActivity)
private var utilModel: UtilModel = UtilModel(context)
private var databaseRepository: IDatabaseRepository = DatabaseRepository()
private var loginView: ILoginView? = null
var forgotPasswordModel: ForgotPasswordModel = ForgotPasswordModel()
lateinit var email: String
lateinit var message: String
Expand All @@ -52,7 +51,6 @@ class LoginPresenter(loginActivity: LoginActivity) :
private lateinit var credentialRequest: CredentialRequest

override fun onAttach(loginView: ILoginView) {
this.loginView = loginView

if (utilModel.getAnonymity()) {
loginView.skipLogin()
Expand Down Expand Up @@ -195,10 +193,6 @@ class LoginPresenter(loginActivity: LoginActivity) :
loginView?.onLoginSuccess(message)
}

override fun onDetach() {
loginView = null
}

override fun requestPassword(email: String, url: String, isPersonalServerChecked: Boolean) {
if (email.isEmpty()) {
loginView?.invalidCredentials(true, Constant.EMAIL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ interface ILoginPresenter {

fun cancelLogin()

fun onDetach()

fun requestPassword(email: String, url: String, isPersonalServerChecked: Boolean)

fun cancelSignup()
Expand Down
12 changes: 3 additions & 9 deletions app/src/main/java/org/fossasia/susi/ai/signup/SignUpActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import org.fossasia.susi.ai.login.LoginActivity
import org.fossasia.susi.ai.signup.contract.ISignUpPresenter
import org.fossasia.susi.ai.signup.contract.ISignUpView
import org.fossasia.susi.ai.skills.SkillsActivity
import org.koin.android.ext.android.inject
import org.koin.core.parameter.parametersOf

/**
* <h1>The SignUp activity.</h1>
Expand All @@ -32,7 +34,7 @@ import org.fossasia.susi.ai.skills.SkillsActivity

class SignUpActivity : AppCompatActivity(), ISignUpView {

private lateinit var signUpPresenter: ISignUpPresenter
private val signUpPresenter: ISignUpPresenter by inject { parametersOf(this) }
private lateinit var progressDialog: ProgressDialog
private lateinit var forgotPasswordProgressDialog: Dialog
private lateinit var builder: AlertDialog.Builder
Expand Down Expand Up @@ -75,9 +77,6 @@ class SignUpActivity : AppCompatActivity(), ISignUpView {
addListeners()

cancelRequestPassword()

signUpPresenter = SignUpPresenter(this)
signUpPresenter.onAttach(this)
}

private fun addListeners() {
Expand Down Expand Up @@ -267,11 +266,6 @@ class SignUpActivity : AppCompatActivity(), ISignUpView {
}
}

override fun onDestroy() {
signUpPresenter.onDetach()
super.onDestroy()
}

override fun showForgotPasswordProgress(boolean: Boolean) {
if (boolean) forgotPasswordProgressDialog.show() else forgotPasswordProgressDialog.dismiss()
}
Expand Down
14 changes: 3 additions & 11 deletions app/src/main/java/org/fossasia/susi/ai/signup/SignUpPresenter.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.fossasia.susi.ai.signup

import android.content.Context
import android.graphics.Color
import java.net.UnknownHostException
import org.fossasia.susi.ai.R
Expand Down Expand Up @@ -27,20 +28,15 @@ import retrofit2.Response
* Created by mayanktripathi on 05/07/17.
*/

class SignUpPresenter(signUpActivity: SignUpActivity) : ISignUpPresenter, ISignUpModel.OnSignUpFinishedListener, IForgotPasswordModel.OnFinishListener {
class SignUpPresenter(context: Context, private val signUpView: ISignUpView) : ISignUpPresenter, ISignUpModel.OnSignUpFinishedListener, IForgotPasswordModel.OnFinishListener {

private var signUpView: ISignUpView? = null
private var signUpModel: SignUpModel = SignUpModel()
private var utilModel: UtilModel = UtilModel(signUpActivity)
private var utilModel: UtilModel = UtilModel(context)
private var settingView: ISettingsView? = null
lateinit var email: String
var forgotPasswordModel: ForgotPasswordModel = ForgotPasswordModel()
private var databaseRepository: IDatabaseRepository = DatabaseRepository()

override fun onAttach(signUpView: ISignUpView) {
this.signUpView = signUpView
}

override fun signUp(email: String, password: String, conpass: String, isSusiServerSelected: Boolean, url: String, isTermsAndConditionSelected: Boolean) {

if (email.isEmpty()) {
Expand Down Expand Up @@ -133,10 +129,6 @@ class SignUpPresenter(signUpActivity: SignUpActivity) : ISignUpPresenter, ISignU
signUpModel.cancelSignUp()
}

override fun onDetach() {
signUpView = null
}

override fun loginLogout() {
utilModel.clearToken()
utilModel.clearPrefs()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@ package org.fossasia.susi.ai.signup.contract

interface ISignUpPresenter {

fun onAttach(signUpView: ISignUpView)

fun signUp(email: String, password: String, conpass: String, isSusiServerSelected: Boolean, url: String, isTermsAndConditionSelected: Boolean)

fun onDetach()

fun cancelSignUp()

fun loginLogout()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class SkillsActivity : AppCompatActivity(), SkillFragmentCallback {
overridePendingTransition(R.anim.trans_left_in, R.anim.trans_left_out)
setContentView(R.layout.activity_skills)

settingsPresenter = SettingsPresenter(this)
settingsPresenter = SettingsPresenter(this, null)
loginLogoutModulePresenter = LoginLogoutModulePresenter(this)
val skillFragment = SkillListingFragment()
val privacyFragment = PrivacyFragment()
Expand Down
Loading

0 comments on commit 388d20b

Please sign in to comment.