This repository was archived by the owner on Oct 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProfilePresenterImpl.kt
More file actions
249 lines (212 loc) · 7.02 KB
/
ProfilePresenterImpl.kt
File metadata and controls
249 lines (212 loc) · 7.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package io.blockv.sample.feature.profile
import android.app.Activity
import android.content.ContentResolver
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri
import android.view.MenuItem
import android.view.View
import io.blockv.common.builder.UpdateUserBuilder
import io.blockv.sample.BasePresenter
import io.blockv.sample.R
import io.reactivex.Single
import android.graphics.Matrix
import android.support.media.ExifInterface
import android.util.Log
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
class ProfilePresenterImpl(
private val profileScreen: ProfileScreen,
private val contentResolver: ContentResolver
) : BasePresenter(), ProfilePresenter {
private val PHOTO_REQUEST_CODE = 1000
override fun onCreate() {
reload()
}
private fun reload() {
collect(
userManager.getCurrentUser()
.doOnSubscribe { profileScreen.showDialog(getString(R.string.feature_profile_loading)) }
.doFinally { profileScreen.hideDialog() }
.observeOn(AndroidSchedulers.mainThread())
.map { user ->
profileScreen.setUserId(user.id)
profileScreen.setFirstName(user.firstName)
profileScreen.setLastName(user.lastName)
profileScreen.setBirthday(user.birthday)
profileScreen.setAvatar(user.avatarUri)
}
.observeOn(Schedulers.io())
.flatMap {
userManager
.getCurrentUserTokens()
}
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ tokens ->
profileScreen.setTokens(tokens)
}, { throwable ->
profileScreen.showToast(throwable.message!!)
})
)
}
override fun onOptionsItemSelected(menuItem: MenuItem) {
if (menuItem.itemId == android.R.id.home) {
profileScreen.finish()
}
}
override fun onSaveDetailsClick(firstName: String, lastName: String, birthday: String) {
collect(
userManager.updateCurrentUser(
UpdateUserBuilder()
.setFirstName(firstName)
.setLastName(lastName)
.setBirthday(birthday)
.build()
)
.subscribeOn(Schedulers.io())
.doOnSubscribe { profileScreen.showDialog(getString(R.string.feature_profile_saving)) }
.doFinally { profileScreen.hideDialog() }
.observeOn(AndroidSchedulers.mainThread())
.map { user ->
profileScreen.setUserId(user.id)
profileScreen.setFirstName(user.firstName)
profileScreen.setLastName(user.lastName)
profileScreen.setBirthday(user.birthday)
}
.subscribe({
}, { throwable ->
profileScreen.showToast(throwable.message!!)
})
)
}
override fun onSavePasswordClick(password: String) {
if (password.isEmpty()) {
profileScreen.showToast(getString(R.string.feature_profile_enter_new_password))
return
}
collect(
userManager.updateCurrentUser(
UpdateUserBuilder()
.setPassword(password)
.build()
)
.subscribeOn(Schedulers.io())
.doOnSubscribe { profileScreen.showDialog(getString(R.string.feature_profile_saving)) }
.doFinally { profileScreen.hideDialog() }
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
}, { throwable ->
profileScreen.showToast(throwable.message!!)
})
)
}
override fun onLogOutClick(view: View) {
onDestroy()
val logout = userManager
.logout()
.doOnSubscribe { profileScreen.showDialog(getString(R.string.feature_profile_logging_out)) }
.doFinally {
profileScreen.hideDialog()
profileScreen.restartApp()
}
.subscribe({
}, { throwable ->
profileScreen.showToast(throwable.message!!)
})
}
override fun onAvatarClick(view: View) {
profileScreen.startSelectPhotoActivity(PHOTO_REQUEST_CODE)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == PHOTO_REQUEST_CODE) {
val selectedImage: Uri = data.data!!
val loadAvatar = loadAvatar(selectedImage)
.flatMapCompletable { userManager.uploadAvatar(it) }
.doFinally {
profileScreen.hideDialog()
reload()
}
.doOnSubscribe { profileScreen.showDialog(getString(R.string.feature_profile_uploading)) }
.subscribe({
}, { throwable ->
profileScreen.showToast(throwable.message!!)
})
}
}
}
private fun loadAvatar(uri: Uri): Single<Bitmap> {
return Single.fromCallable {
val matrix = Matrix()
lateinit var out: Bitmap
try {
val inputStream = contentResolver.openInputStream(uri)
val exif = ExifInterface(inputStream!!)
val rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL)
val rotationInDegrees = when (rotation) {
ExifInterface.ORIENTATION_ROTATE_90 -> 90
ExifInterface.ORIENTATION_ROTATE_180 -> 180
ExifInterface.ORIENTATION_ROTATE_270 -> 270
else -> 0
}
if (rotation.toFloat() != 0f) matrix.preRotate(rotationInDegrees.toFloat())
} catch (e: Exception) {
Log.e("loadAvatar", e.message)
}
var input = contentResolver.openInputStream(uri)
val options = BitmapFactory.Options()
options.inJustDecodeBounds = true
BitmapFactory.decodeStream(input, null, options)
input?.close()
options.inJustDecodeBounds = false
options.inSampleSize = calculateInSampleSize(options, 500, 500)
input = contentResolver.openInputStream(uri)
val orignal = BitmapFactory.decodeStream(input, null, options)
input?.close()
if (orignal != null) {
out = if (orignal.width >= orignal.height) {
Bitmap.createBitmap(
orignal,
orignal.width / 2 - orignal.height / 2,
0,
orignal.height,
orignal.height,
matrix,
true
)
} else {
Bitmap.createBitmap(
orignal,
0,
orignal.height / 2 - orignal.width / 2,
orignal.width,
orignal.width,
matrix,
true
)
}
}
out
}
.subscribeOn(Schedulers.computation())
.observeOn(AndroidSchedulers.mainThread())
}
private fun calculateInSampleSize(options: BitmapFactory.Options, reqWidth: Int, reqHeight: Int): Int {
val height = options.outHeight
val width = options.outWidth
var inSampleSize = 1
if (height > reqHeight || width > reqWidth) {
val halfHeight = height / 2
val halfWidth = width / 2
while (halfHeight / inSampleSize >= reqHeight && halfWidth / inSampleSize >= reqWidth) {
inSampleSize *= 2
}
}
return inSampleSize
}
override fun onDestroy() {
profileScreen.hideDialog()
dispose()
}
}