Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Html games #4846

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.net.Uri
import android.os.Bundle
import android.provider.Settings
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
Expand Down Expand Up @@ -70,15 +71,15 @@ abstract class BaseContainerFragment : BaseResourceFragment() {
AdapterCourses.showRating(`object`, rating, timesRated, ratingBar)
}
}
fun getUrlsAndStartDownload(
lib: List<RealmMyLibrary?>, urls: ArrayList<String>
) {

fun getUrlsAndStartDownload(lib: List<RealmMyLibrary?>, urls: ArrayList<String>) {
for (library in lib) {
val url = Utilities.getUrl(library)
if (!FileUtils.checkFileExist(url) && !TextUtils.isEmpty(url)) urls.add(url)
}
if (urls.isNotEmpty()) startDownload(urls)
}

fun initRatingView(type: String?, id: String?, title: String?, listener: OnRatingChangeListener?) {
timesRated = requireView().findViewById(R.id.times_rated)
rating = requireView().findViewById(R.id.tv_rating)
Expand All @@ -98,12 +99,14 @@ abstract class BaseContainerFragment : BaseResourceFragment() {
}
}
}

override fun onAttach(context: Context) {
super.onAttach(context)
if (context is OnHomeItemClickListener) {
homeItemClickListener = context
}
}

private fun openIntent(items: RealmMyLibrary, typeClass: Class<*>?) {
val fileOpenIntent = Intent(activity, typeClass)
if (items.resourceLocalAddress?.contains("ole/audio") == true || items.resourceLocalAddress?.contains("ole/video") == true) {
Expand All @@ -115,6 +118,7 @@ abstract class BaseContainerFragment : BaseResourceFragment() {
}
startActivity(fileOpenIntent)
}

private fun openPdf(item: RealmMyLibrary) {
val fileOpenIntent = Intent(activity, PDFReaderActivity::class.java)
fileOpenIntent.putExtra("TOUCHED_FILE", item.id + "/" + item.resourceLocalAddress)
Expand All @@ -123,31 +127,37 @@ abstract class BaseContainerFragment : BaseResourceFragment() {
}

fun openResource(items: RealmMyLibrary) {
val matchingItems = mRealm.where(RealmMyLibrary::class.java)
.equalTo("resourceLocalAddress", items.resourceLocalAddress)
.findAll()
val anyOffline = matchingItems.any { it.isResourceOffline() }
if (anyOffline) {
val offlineItem = matchingItems.first { it.isResourceOffline()}
openFileType(offlineItem, "offline")
} else {
if (items.isResourceOffline()) {
openFileType(items, "offline")
} else if (FileUtils.getFileExtension(items.resourceLocalAddress) == "mp4") {
openFileType(items, "online")
} else {
val arrayList = ArrayList<String>()
arrayList.add(Utilities.getUrl(items))
startDownload(arrayList)
profileDbHandler.setResourceOpenCount(items, KEY_RESOURCE_DOWNLOAD)
}
val intent = Intent(context, WebViewActivity::class.java).apply {
putExtra("isLocalFile", true)
putExtra("RESOURCE_ID", "6f7b1d7a7c9c73e736b38657bc013556") // The _id from the library item
}
startActivity(intent)
// val matchingItems = mRealm.where(RealmMyLibrary::class.java)
// .equalTo("resourceLocalAddress", items.resourceLocalAddress)
// .findAll()
// val anyOffline = matchingItems.any { it.isResourceOffline() }
// if (anyOffline) {
// val offlineItem = matchingItems.first { it.isResourceOffline()}
// openFileType(offlineItem, "offline")
// } else {
// if (items.isResourceOffline()) {
// openFileType(items, "offline")
// } else if (FileUtils.getFileExtension(items.resourceLocalAddress) == "mp4") {
// openFileType(items, "online")
// } else {
// val arrayList = ArrayList<String>()
// arrayList.add(Utilities.getUrl(items))
// startDownload(arrayList)
// profileDbHandler.setResourceOpenCount(items, KEY_RESOURCE_DOWNLOAD)
// }
// }
}

private fun checkFileExtension(items: RealmMyLibrary) {
val filenameArray = items.resourceLocalAddress?.split("\\.".toRegex())?.toTypedArray()
val extension = filenameArray?.get(filenameArray.size - 1)
val mimetype = Utilities.getMimeType(items.resourceLocalAddress)
val extension = items.resourceLocalAddress
?.substringAfterLast('.', "")
?.lowercase()

if (mimetype != null) {
if (mimetype.contains("image")) {
Expand All @@ -167,6 +177,9 @@ abstract class BaseContainerFragment : BaseResourceFragment() {
"txt" -> {
openIntent(items, TextFileViewerActivity::class.java)
}
"html", "htm" -> {
openHtmlResource(items)
}
"md" -> {
openIntent(items, MarkdownViewerActivity::class.java)
}
Expand Down Expand Up @@ -233,6 +246,7 @@ abstract class BaseContainerFragment : BaseResourceFragment() {
checkFileExtension(items)
}
}

open fun playVideo(videoType: String, items: RealmMyLibrary) {
val intent = Intent(activity, VideoPlayerActivity::class.java)
val bundle = Bundle()
Expand Down Expand Up @@ -296,6 +310,7 @@ abstract class BaseContainerFragment : BaseResourceFragment() {
}
}
}

fun setResourceButton(resources: List<RealmMyLibrary>?, btnResources: Button) {
if (resources.isNullOrEmpty()) {
btnResources.visibility = View.GONE
Expand All @@ -310,6 +325,15 @@ abstract class BaseContainerFragment : BaseResourceFragment() {
}
}

private fun openHtmlResource(items: RealmMyLibrary) {
val intent = Intent(activity, WebViewActivity::class.java).apply {
putExtra("TOUCHED_FILE", "${items.id}/${items.resourceLocalAddress}")
putExtra("title", items.title)
putExtra("isLocalFile", true)
}
startActivity(intent)
}

open fun handleBackPressed() {
val fragmentManager = parentFragmentManager
fragmentManager.popBackStack()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.net.Uri
import android.os.Bundle
//import android.provider.Settings
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
Expand Down Expand Up @@ -70,15 +71,15 @@ abstract class BaseContainerFragment : BaseResourceFragment() {
AdapterCourses.showRating(`object`, rating, timesRated, ratingBar)
}
}
fun getUrlsAndStartDownload(
lib: List<RealmMyLibrary?>, urls: ArrayList<String>
) {

fun getUrlsAndStartDownload(lib: List<RealmMyLibrary?>, urls: ArrayList<String>) {
for (library in lib) {
val url = Utilities.getUrl(library)
if (!FileUtils.checkFileExist(url) && !TextUtils.isEmpty(url)) urls.add(url)
}
if (urls.isNotEmpty()) startDownload(urls)
}

fun initRatingView(type: String?, id: String?, title: String?, listener: OnRatingChangeListener?) {
timesRated = requireView().findViewById(R.id.times_rated)
rating = requireView().findViewById(R.id.tv_rating)
Expand All @@ -98,12 +99,14 @@ abstract class BaseContainerFragment : BaseResourceFragment() {
}
}
}

override fun onAttach(context: Context) {
super.onAttach(context)
if (context is OnHomeItemClickListener) {
homeItemClickListener = context
}
}

private fun openIntent(items: RealmMyLibrary, typeClass: Class<*>?) {
val fileOpenIntent = Intent(activity, typeClass)
if (items.resourceLocalAddress?.contains("ole/audio") == true || items.resourceLocalAddress?.contains("ole/video") == true) {
Expand All @@ -115,6 +118,7 @@ abstract class BaseContainerFragment : BaseResourceFragment() {
}
startActivity(fileOpenIntent)
}

private fun openPdf(item: RealmMyLibrary) {
val fileOpenIntent = Intent(activity, PDFReaderActivity::class.java)
fileOpenIntent.putExtra("TOUCHED_FILE", item.id + "/" + item.resourceLocalAddress)
Expand Down Expand Up @@ -145,9 +149,10 @@ abstract class BaseContainerFragment : BaseResourceFragment() {
}

private fun checkFileExtension(items: RealmMyLibrary) {
val filenameArray = items.resourceLocalAddress?.split("\\.".toRegex())?.toTypedArray()
val extension = filenameArray?.get(filenameArray.size - 1)
val mimetype = Utilities.getMimeType(items.resourceLocalAddress)
val extension = items.resourceLocalAddress
?.substringAfterLast('.', "")
?.lowercase()

if (mimetype != null) {
if (mimetype.contains("image")) {
Expand All @@ -167,6 +172,9 @@ abstract class BaseContainerFragment : BaseResourceFragment() {
"txt" -> {
openIntent(items, TextFileViewerActivity::class.java)
}
"html", "htm" -> {
openHtmlResource(items)
}
"md" -> {
openIntent(items, MarkdownViewerActivity::class.java)
}
Expand Down Expand Up @@ -233,6 +241,7 @@ abstract class BaseContainerFragment : BaseResourceFragment() {
checkFileExtension(items)
}
}

open fun playVideo(videoType: String, items: RealmMyLibrary) {
val intent = Intent(activity, VideoPlayerActivity::class.java)
val bundle = Bundle()
Expand Down Expand Up @@ -296,6 +305,7 @@ abstract class BaseContainerFragment : BaseResourceFragment() {
}
}
}

fun setResourceButton(resources: List<RealmMyLibrary>?, btnResources: Button) {
if (resources.isNullOrEmpty()) {
btnResources.visibility = View.GONE
Expand All @@ -310,6 +320,15 @@ abstract class BaseContainerFragment : BaseResourceFragment() {
}
}

private fun openHtmlResource(items: RealmMyLibrary) {
val intent = Intent(activity, WebViewActivity::class.java).apply {
putExtra("TOUCHED_FILE", "${items.id}/${items.resourceLocalAddress}")
putExtra("title", items.title)
putExtra("isLocalFile", true)
}
startActivity(intent)
}

// open fun handleBackPressed() {
// val fragmentManager = parentFragmentManager
// fragmentManager.popBackStack()
Expand Down
50 changes: 48 additions & 2 deletions app/src/main/java/org/ole/planet/myplanet/model/RealmMyLibrary.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.ole.planet.myplanet.model

import android.content.Context
import android.content.SharedPreferences
import android.util.Log
import com.google.gson.JsonArray
import com.google.gson.JsonNull
import com.google.gson.JsonObject
Expand All @@ -20,6 +21,7 @@ import java.io.FileWriter
import java.io.IOException
import java.util.Calendar
import java.util.Date
import java.util.UUID

open class RealmMyLibrary : RealmObject() {
@PrimaryKey
Expand Down Expand Up @@ -63,6 +65,8 @@ open class RealmMyLibrary : RealmObject() {
var courseId: String? = null
var stepId: String? = null
var isPrivate: Boolean = false
var attachments: RealmList<RealmAttachment>? = null

fun serializeResource(): JsonObject {
return JsonObject().apply {
addProperty("_id", _id)
Expand Down Expand Up @@ -258,13 +262,15 @@ open class RealmMyLibrary : RealmObject() {
if (!mRealm.isInTransaction) {
mRealm.beginTransaction()
}
logLargeString("insertMyLibrary", doc.toString())
val resourceId = JsonUtils.getString("_id", doc)
val settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
var resource = mRealm.where(RealmMyLibrary::class.java).equalTo("id", resourceId).findFirst()
if (resource == null) {
resource = mRealm.createObject(RealmMyLibrary::class.java, resourceId)
}
resource?.apply {

setUserId(userId)
_id = resourceId
if (!stepId.isNullOrBlank()) {
Expand All @@ -279,7 +285,25 @@ open class RealmMyLibrary : RealmObject() {
description = JsonUtils.getString("description", doc)
if (doc.has("_attachments")) {
val attachments = doc["_attachments"].asJsonObject
attachments.entrySet().forEach { (key, _) ->
if (this.attachments == null) {
this.attachments = RealmList()
}

attachments.entrySet().forEach { (key, attachmentValue) ->
val attachmentObj = attachmentValue.asJsonObject

val realmAttachment = mRealm.createObject(RealmAttachment::class.java, UUID.randomUUID().toString())
realmAttachment.apply {
name = key
contentType = attachmentObj.get("content_type")?.asString
length = attachmentObj.get("length")?.asLong ?: 0
digest = attachmentObj.get("digest")?.asString
isStub = attachmentObj.get("stub")?.asBoolean == true
revpos = attachmentObj.get("revpos")?.asInt ?: 0
}

this.attachments?.add(realmAttachment)

if (key.indexOf("/") < 0) {
resourceRemoteAddress = "${settings.getString("couchdbURL", "http://")}/resources/$resourceId/$key"
resourceLocalAddress = key
Expand Down Expand Up @@ -311,6 +335,8 @@ open class RealmMyLibrary : RealmObject() {
isPrivate = JsonUtils.getBoolean("private", doc)
setLanguages(JsonUtils.getJsonArray("languages", doc), this)
}

logLargeString("insertMyLibrary resource", resource.id.toString() + " " + resource.resourceRemoteAddress)
mRealm.commitTransaction()

val csvRow = arrayOf(
Expand Down Expand Up @@ -402,12 +428,32 @@ open class RealmMyLibrary : RealmObject() {

@JvmStatic
fun getArrayList(libraries: List<RealmMyLibrary>, type: String): Set<String?> {
return libraries.mapNotNull { if (type == "mediums") it.mediaType else it.language }.filterNot { it.isNullOrBlank() }.toSet()
return libraries.mapNotNull { if (type == "mediums") it.mediaType else it.language }.filterNot { it.isBlank() }.toSet()
}

@JvmStatic
fun getSubjects(libraries: List<RealmMyLibrary>): Set<String> {
return libraries.flatMap { it.subject ?: emptyList() }.toSet()
}

fun logLargeString(tag: String, content: String) {
if (content.length > 3000) {
Log.d(tag, content.substring(0, 3000))
logLargeString(tag, content.substring(3000))
} else {
Log.d(tag, content)
}
}
}
}

open class RealmAttachment : RealmObject() {
@PrimaryKey
var id: String? = null
var name: String? = null
var contentType: String? = null
var length: Long = 0
var digest: String? = null
var isStub: Boolean = false
var revpos: Int = 0
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class UploadToShelfService(context: Context) {
for (model in userModels) {
try {
val password = sharedPreferences.getString("loginUserPassword", "")
val header = "Basic " + Base64.encodeToString(("${model.name}:${password}").toByteArray(), Base64.NO_WRAP)
val header = "Basic ${Base64.encodeToString(("${ model.name }:${ password }").toByteArray(), Base64.NO_WRAP)}"
val res = apiInterface?.getJsonObject(header, "${replacedUrl(model)}/_users/org.couchdb.user:${model.name}")?.execute()
if (res?.body() == null) {
val obj = model.serialize()
Expand Down
Loading
Loading