Created
December 5, 2018 08:19
-
-
Save AlexGladkov/8a570e140647f1b4ca258f3626d2a8bf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import android.graphics.Bitmap | |
| import android.os.Bundle | |
| import android.view.LayoutInflater | |
| import android.view.View | |
| import android.view.ViewGroup | |
| import android.widget.Toast | |
| import com.arellomobile.mvp.presenter.InjectPresenter | |
| import com.arellomobile.mvp.presenter.ProvidePresenter | |
| import com.sddhelp.R | |
| import kotlinx.android.synthetic.main.fragment_profile.* | |
| import org.joda.time.DateTime | |
| import ru.sddhelp.app.di.App | |
| import ru.sddhelp.app.interfaces.BioContainer | |
| import ru.sddhelp.app.interfaces.ConfigurationProvider | |
| import ru.sddhelp.helpers.KeyboardHandler | |
| import ru.sddhelp.app.interfaces.RouterProvider | |
| import ru.sddhelp.app.presenters.ProfilePresenter | |
| import ru.sddhelp.app.views.ProfileView | |
| import ru.sddhelp.base.BaseChildFragment | |
| import ru.sddhelp.data.implementations.AuthRepositoryImpl | |
| import ru.sddhelp.data.implementations.OtherRepositoryImpl | |
| import ru.sddhelp.data.implementations.ServiceRepositoryImpl | |
| import ru.sddhelp.domain.models.Configuration | |
| import ru.sddhelp.domain.models.Service | |
| import ru.sddhelp.domain.repositories.AuthRepository | |
| import ru.sddhelp.domain.repositories.OtherRepository | |
| import ru.sddhelp.domain.repositories.ServiceRepository | |
| import ru.sddhelp.helpers.ImageLoader | |
| import ru.sddhelp.helpers.PermissionHandler | |
| import ru.sddhelp.helpers.ScreenKeys | |
| import java.io.ByteArrayOutputStream | |
| import java.io.File | |
| import java.io.FileOutputStream | |
| import javax.inject.Inject | |
| /** | |
| * Created by agladkov on 27.12.17. | |
| */ | |
| class ProfileFragment : BaseChildFragment(), ProfileView { | |
| private val TAG: String = ProfileFragment::class.java.simpleName | |
| @Inject | |
| lateinit var authRepository: AuthRepository | |
| @Inject | |
| lateinit var otherRepository: OtherRepository | |
| @Inject | |
| lateinit var serviceRepository: ServiceRepository | |
| @InjectPresenter | |
| lateinit var presenter: ProfilePresenter | |
| @ProvidePresenter | |
| fun providePresenter(): ProfilePresenter { | |
| return ProfilePresenter(router = (parentFragment as RouterProvider).getRouter(), | |
| config = (activity as ConfigurationProvider).getConfiguration()) | |
| } | |
| companion object { | |
| fun getNewInstance(): ProfileFragment { | |
| val fragment = ProfileFragment() | |
| val args = Bundle() | |
| fragment.arguments = args | |
| return fragment | |
| } | |
| } | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| App.appComponent.inject(fragment = this@ProfileFragment) | |
| super.onCreate(savedInstanceState) | |
| } | |
| override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { | |
| return inflater.inflate(R.layout.fragment_profile, container, false) | |
| } | |
| override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
| super.onViewCreated(view, savedInstanceState) | |
| presenter.loadView() | |
| nsvProfileContainer.setOnClickListener { | |
| llProfile.requestFocus() | |
| (activity as? KeyboardHandler)?.hideKeyboard() | |
| } | |
| nsvProfileContainer.setOnTouchListener { _, _ -> | |
| llProfile.requestFocus() | |
| (activity as? KeyboardHandler)?.hideKeyboard() | |
| false | |
| } | |
| civProfileAvatar.setOnClickListener { | |
| (activity as? PermissionHandler)?.askPhotoPermission(source = ScreenKeys.Profile.value) | |
| } | |
| btnProfileBack.setOnClickListener { | |
| presenter.onBackClick() | |
| } | |
| btnProfileSave.setOnClickListener { | |
| (activity as? KeyboardHandler)?.hideKeyboard() | |
| presenter.onSaveClick(name = txtProfileName.text.toString(), surname = txtProfileSurname.text.toString(), | |
| email = txtProfileEmail.text.toString()) | |
| } | |
| } | |
| fun updateProfilePhoto(selectedImage: Bitmap?) { | |
| selectedImage.let { image -> | |
| val f = File(context?.cacheDir, "profileAvatar${DateTime().toString("dd.MM.yyyyHH:mm")}") | |
| f.createNewFile() | |
| val bos = ByteArrayOutputStream() | |
| image?.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos) | |
| val bitmapdata = bos.toByteArray() | |
| val fos = FileOutputStream(f) | |
| fos.write(bitmapdata) | |
| fos.flush() | |
| fos.close() | |
| presenter.uploadImage(file = f) | |
| (activity as? BioContainer)?.updateAvatar(path = f.absolutePath) | |
| } | |
| civProfileAvatar.setImageBitmap(selectedImage) | |
| } | |
| // View implementation | |
| override fun startUpdate() { | |
| btnProfileSave.visibility = View.GONE | |
| cpvProfile.visibility = View.VISIBLE | |
| } | |
| override fun endUpdate() { | |
| btnProfileSave.visibility = View.VISIBLE | |
| cpvProfile.visibility = View.GONE | |
| } | |
| override fun showError(message: String) { | |
| Toast.makeText(context, message, Toast.LENGTH_SHORT).show() | |
| } | |
| override fun showError(message: Int) { | |
| Toast.makeText(context, getString(message), Toast.LENGTH_SHORT).show() | |
| } | |
| override fun setupView(name: String, surname: String, email: String, avatar: String) { | |
| txtProfileName.setText(name) | |
| txtProfileSurname.setText(surname) | |
| txtProfileEmail.setText(email) | |
| ImageLoader().loadImage(File(avatar), civProfileAvatar) | |
| } | |
| override fun updateConfig(config: Configuration) { | |
| Toast.makeText(context, getString(R.string.profile_updated), Toast.LENGTH_SHORT).show() | |
| (activity as? BioContainer)?.updateName(config.name) | |
| (activity as? BioContainer)?.updateSurname(config.surname) | |
| (activity as? BioContainer)?.updateEmail(config.email) | |
| presenter.onBackClick() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment