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

Sanpablo challenge #4592

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ proguard
# Log files
*.log

# Raw folder
assets

# Android Studio Navigation editor temp folder
.navigation

Expand Down
Binary file added app/src/main/assets/images/october-challenge.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import io.realm.Case
import io.realm.RealmChangeListener
import io.realm.RealmObject
import io.realm.RealmResults
import org.ole.planet.myplanet.BuildConfig
import org.ole.planet.myplanet.MainApplication.Companion.context
import org.ole.planet.myplanet.R
import org.ole.planet.myplanet.base.BaseContainerFragment
Expand Down Expand Up @@ -72,9 +73,13 @@ import org.ole.planet.myplanet.utilities.DialogUtils.guestDialog
import org.ole.planet.myplanet.utilities.FileUtils.totalAvailableMemoryRatio
import org.ole.planet.myplanet.utilities.KeyboardUtils.setupUI
import org.ole.planet.myplanet.utilities.LocaleHelper
import org.ole.planet.myplanet.utilities.MarkdownDialog
import org.ole.planet.myplanet.utilities.TimeUtils.formatDate
import org.ole.planet.myplanet.utilities.Utilities
import org.ole.planet.myplanet.utilities.Utilities.toast
import java.time.LocalDate
import java.time.temporal.ChronoUnit
import java.util.Calendar
import java.util.Date
import java.util.UUID
import kotlin.math.ceil
Expand Down Expand Up @@ -230,6 +235,31 @@ class DashboardActivity : DashboardElementActivity(), OnHomeItemClickListener, N
}
}
})

val calendar = Calendar.getInstance()
val currentMonth = calendar.get(Calendar.MONTH)

if (currentMonth == Calendar.OCTOBER) {
if (settings.getString("serverURL", "") == "https://${BuildConfig.PLANET_GUATEMALA_URL}") {
val today = LocalDate.now()
val endOfMonth = today.withDayOfMonth(today.lengthOfMonth())
val remainingDays = ChronoUnit.DAYS.between(today, endOfMonth).toInt()

challengeDialog(remainingDays)
}
}
}

private fun challengeDialog(remainingDays: Int) {
val markdownContent = """
## october challenge: $remainingDays days remaining

<img src="file:///android_asset/images/october-challenge.png" width="100%">

""".trimIndent()

MarkdownDialog.newInstance(markdownContent)
.show(supportFragmentManager, "markdown_dialog")
}

private fun setupRealmListeners() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import io.realm.Case
import io.realm.RealmChangeListener
import io.realm.RealmObject
import io.realm.RealmResults
import org.ole.planet.myplanet.BuildConfig
import org.ole.planet.myplanet.MainApplication.Companion.context
import org.ole.planet.myplanet.R
//import org.ole.planet.myplanet.base.BaseContainerFragment
Expand Down Expand Up @@ -72,9 +73,13 @@ import org.ole.planet.myplanet.utilities.DialogUtils.guestDialog
import org.ole.planet.myplanet.utilities.FileUtils.totalAvailableMemoryRatio
import org.ole.planet.myplanet.utilities.KeyboardUtils.setupUI
import org.ole.planet.myplanet.utilities.LocaleHelper
import org.ole.planet.myplanet.utilities.MarkdownDialog
import org.ole.planet.myplanet.utilities.TimeUtils.formatDate
import org.ole.planet.myplanet.utilities.Utilities
import org.ole.planet.myplanet.utilities.Utilities.toast
import java.time.LocalDate
import java.time.temporal.ChronoUnit
import java.util.Calendar
import java.util.Date
import java.util.UUID
import kotlin.math.ceil
Expand Down Expand Up @@ -230,6 +235,31 @@ class DashboardActivity : DashboardElementActivity(), OnHomeItemClickListener, N
}
}
})

val calendar = Calendar.getInstance()
val currentMonth = calendar.get(Calendar.MONTH)

if (currentMonth == Calendar.OCTOBER) {
if (settings.getString("serverURL", "") == "https://${BuildConfig.PLANET_GUATEMALA_URL}") {
val today = LocalDate.now()
val endOfMonth = today.withDayOfMonth(today.lengthOfMonth())
val remainingDays = ChronoUnit.DAYS.between(today, endOfMonth).toInt()

challengeDialog(remainingDays)
}
}
}

private fun challengeDialog(remainingDays: Int) {
val markdownContent = """
## october challenge: $remainingDays days remaining

<img src="file:///android_asset/images/october-challenge.png" width="100%">

""".trimIndent()

MarkdownDialog.newInstance(markdownContent)
.show(supportFragmentManager, "markdown_dialog")
}

private fun setupRealmListeners() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ object Markdown {
.usePlugin(object : AbstractMarkwonPlugin() {
override fun configure(registry: MarkwonPlugin.Registry) {
registry.require(ImagesPlugin::class.java) { imagesPlugin ->
imagesPlugin.addSchemeHandler(FileSchemeHandler.create())
imagesPlugin.addSchemeHandler(FileSchemeHandler.createWithAssets(context.assets))
imagesPlugin.addSchemeHandler(NetworkSchemeHandler.create())
imagesPlugin.addSchemeHandler(OkHttpNetworkSchemeHandler.create())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package org.ole.planet.myplanet.utilities

import android.os.Bundle
import android.text.Spannable
import android.text.method.LinkMovementMethod
import android.text.style.URLSpan
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import org.ole.planet.myplanet.databinding.DialogCampaignChallengeBinding
import org.ole.planet.myplanet.utilities.Markdown.setMarkdownText

class MarkdownDialog : DialogFragment() {
private lateinit var dialogCampaignChallengeBinding: DialogCampaignChallengeBinding
private var markdownContent: String = ""

companion object {
private const val ARG_MARKDOWN_CONTENT = "markdown_content"

fun newInstance(markdownContent: String): MarkdownDialog {
val fragment = MarkdownDialog()
val args = Bundle().apply {
putString(ARG_MARKDOWN_CONTENT, markdownContent)
}
fragment.arguments = args
return fragment
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
markdownContent = arguments?.getString(ARG_MARKDOWN_CONTENT) ?: ""
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
dialogCampaignChallengeBinding = DialogCampaignChallengeBinding.inflate(inflater, container, false)
return dialogCampaignChallengeBinding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

setupMarkdown()
dialogCampaignChallengeBinding.markdownTextView.movementMethod = LinkMovementMethod.getInstance()
val textWithSpans = dialogCampaignChallengeBinding.markdownTextView.text
if (textWithSpans is Spannable) {
val urlSpans = textWithSpans.getSpans(0, textWithSpans.length, URLSpan::class.java)
for (urlSpan in urlSpans) {
val start = textWithSpans.getSpanStart(urlSpan)
val end = textWithSpans.getSpanEnd(urlSpan)
val dynamicTitle = textWithSpans.subSequence(start, end).toString()
textWithSpans.setSpan(CustomClickableSpan(urlSpan.url, dynamicTitle, requireActivity()), start, end, textWithSpans.getSpanFlags(urlSpan))
textWithSpans.removeSpan(urlSpan)
}
}
setupCloseButton()
}

override fun onStart() {
super.onStart()
val dialog = dialog ?: return

dialog.window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
}

private fun setupMarkdown() {
setMarkdownText(dialogCampaignChallengeBinding.markdownTextView, markdownContent)
}

private fun setupCloseButton() {
dialogCampaignChallengeBinding.closeButton.setOnClickListener {
dismiss()
}
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/baseline_close_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#000000" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">

<path android:fillColor="@android:color/white" android:pathData="M19,6.41L17.59,5 12,10.59 6.41,5 5,6.41 10.59,12 5,17.59 6.41,19 12,13.41 17.59,19 19,17.59 13.41,12z"/>

</vector>
28 changes: 28 additions & 0 deletions app/src/main/res/layout/dialog_campaign_challenge.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">

<ImageButton
android:id="@+id/closeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackgroundBorderless"
android:contentDescription="close dialog"
android:src="@drawable/baseline_close_24"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/markdownTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/closeButton" />

</androidx.constraintlayout.widget.ConstraintLayout>