84 lines
2.8 KiB
Kotlin
84 lines
2.8 KiB
Kotlin
package pl.edu.amu.wmi.socialaggregator.socialplatforms
|
|
|
|
import android.content.Context
|
|
import android.graphics.Bitmap
|
|
import android.util.Log
|
|
import io.reactivex.Single
|
|
import io.reactivex.subjects.PublishSubject
|
|
import pl.edu.amu.wmi.socialaggregator.R
|
|
import pl.edu.amu.wmi.socialaggregator.utils.InternalStorage
|
|
import java.io.ByteArrayOutputStream
|
|
import java.io.File
|
|
import java.text.DateFormat
|
|
import java.util.*
|
|
|
|
abstract class FacebookMock : SocialPlatform {
|
|
|
|
companion object {
|
|
val TAG = FacebookMock::class.java.canonicalName
|
|
}
|
|
|
|
override fun getName(): String = "Facebook Mock"
|
|
|
|
override fun getLogo(): Int = R.mipmap.ic_logo_facebook
|
|
|
|
override fun login(context: Context) {
|
|
val loginsDir = InternalStorage.getFileOrDir(context, "logins")
|
|
loginsDir?.mkdir()
|
|
if (loginsDir != null) {
|
|
val loginFile = File(loginsDir, "facebook")
|
|
loginFile.createNewFile()
|
|
} else {
|
|
Log.e(TAG, "Could not create logins directory")
|
|
}
|
|
}
|
|
|
|
override fun logout(context: Context) {
|
|
InternalStorage.getFileOrDir(context, "logins/facebook")?.delete()
|
|
}
|
|
|
|
override fun isLoggedIn(context: Context): Boolean {
|
|
return InternalStorage.getFileOrDir(context, "logins/facebook")?.exists() ?: false
|
|
}
|
|
|
|
override fun addPost(context: Context, text: String, images: List<Bitmap>): PublishSubject<Any> {
|
|
val postsDir = InternalStorage.getFileOrDir(context, "posts/facebook")
|
|
if (postsDir != null) {
|
|
val postDir = File(postsDir, System.currentTimeMillis().toString())
|
|
postDir.mkdirs()
|
|
|
|
val textFile = File(postDir, "content")
|
|
textFile.createNewFile()
|
|
textFile.writeText(text)
|
|
|
|
images.forEachIndexed { index, image ->
|
|
val imageFile = File(postDir, "image$index")
|
|
imageFile.createNewFile()
|
|
|
|
ByteArrayOutputStream().use { stream ->
|
|
image.compress(Bitmap.CompressFormat.JPEG, 100, stream)
|
|
imageFile.writeBytes(stream.toByteArray())
|
|
}
|
|
}
|
|
|
|
} else {
|
|
Log.e(TAG, "Could not create posts directory")
|
|
}
|
|
return PublishSubject.create()
|
|
}
|
|
|
|
override fun getPosts(context: Context): Single<List<Post>> {
|
|
val postsDir = InternalStorage.getFileOrDir(context, "posts/facebook")
|
|
return Single.just(postsDir?.listFiles()?.flatMap {
|
|
it.listFiles().map {
|
|
val dt = Date(it.lastModified())
|
|
Post(
|
|
this,
|
|
it.readText(),
|
|
DateFormat.getDateTimeInstance().format(dt),
|
|
emptyList()
|
|
)
|
|
}
|
|
}?.toList() ?: emptyList())
|
|
}
|
|
} |