Skip to content
This repository has been archived by the owner on Jan 29, 2023. It is now read-only.

Commit

Permalink
Add name history on header click
Browse files Browse the repository at this point in the history
  • Loading branch information
cyb3rko committed May 30, 2021
1 parent 4ba764d commit 547a8a0
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 3 deletions.
71 changes: 68 additions & 3 deletions app/src/main/java/com/cyb3rko/cavedroid/HomeFragment.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package com.cyb3rko.cavedroid

import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.app.ProgressDialog
import android.content.*
import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Bundle
import android.text.Html
import android.util.Log
import android.view.*
import android.view.inputmethod.InputMethodManager
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.EditText
import android.widget.SearchView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.forEach
import androidx.fragment.app.Fragment
Expand All @@ -21,6 +23,7 @@ import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.callbacks.onPreShow
import com.afollestad.materialdialogs.customview.customView
import com.afollestad.materialdialogs.customview.getCustomView
import com.afollestad.materialdialogs.list.listItems
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.DiskCacheStrategy
Expand Down Expand Up @@ -113,8 +116,70 @@ class HomeFragment : Fragment() {
val action = HomeFragmentDirections.openProfileCategory(3, currentName, "Current offers")
findNavController().navigate(action)
}

binding.header.setOnClickListener {
loadNameHistory()
}
}

private fun loadNameHistory() {
val progressDialog = ProgressDialog(myContext)
progressDialog.setMessage("Fetching Data...")
progressDialog.show()
var viewType = 0
val webView = WebView(myContext)
webView.settings.javaScriptEnabled = true
val webInterface = JavascriptInterface()
webView.addJavascriptInterface(webInterface, "HtmlViewer")
webView.webViewClient = object: WebViewClient() {
override fun onPageFinished(view: WebView?, url: String?) {
webView.loadUrl("javascript:window.HtmlViewer.showHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');")

if (viewType == 0) {
GlobalScope.launch {
while (webInterface.html == null) {
Thread.sleep(50)
}

val uuid = webInterface.html!!.split("\"id\":\"")[1].removeSuffix("\"}</pre></body></head>")
webInterface.html = null
activity?.runOnUiThread {
webView.loadUrl("https://api.mojang.com/user/profiles/$uuid/names")
}
viewType = 1
}
} else {
GlobalScope.launch {
while (webInterface.html == null) {
Thread.sleep(50)
}

val nameParts = webInterface.html!!.split("name\":\"").drop(1).toMutableList()
nameParts.forEachIndexed { index, s ->
nameParts[index] = s.split("\"")[0]
}
nameParts.reverse()
activity?.runOnUiThread {
progressDialog.dismiss()
MaterialDialog(myContext).show {
noAutoDismiss()
title(text = "Name History")
listItems(items = nameParts) { _, _, name ->
val clip = ClipData.newPlainText(name, name)
(myContext.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager).setPrimaryClip(clip)
showClipboardToast()
}
}
}
}
}
}
}
webView.loadUrl("https://api.mojang.com/users/profiles/minecraft/$currentName")
}

private fun showClipboardToast() = Toast.makeText(myContext, "Copied name to clipboard", Toast.LENGTH_SHORT).show()

private fun loadProfile(name: String) {
val formattedName = if (!name.contains(" ")) name else name.replace(" ", "%20")
val avatarName = if (name != "The Bank") name else "God"
Expand Down
Binary file added app/src/main/res/drawable/_ic_history.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions app/src/main/res/layout/fragment_home.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@
tools:textSize="22sp"
tools:text="Cyb3rKo" />

<ImageView
android:id="@+id/history_icon"
android:layout_width="15dp"
android:layout_height="15dp"
android:src="@drawable/_ic_history"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
app:tint="@color/foreground_color"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

</androidx.cardview.widget.CardView>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-night/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
<color name="refreshLayoutBackground">@color/cardview_dark_background</color>
<color name="refreshLayoutArrow">#FFFFFF</color>
<color name="click_hint">#FFFFFF</color>
<color name="foreground_color">@android:color/primary_text_dark</color>
</resources>
6 changes: 6 additions & 0 deletions app/src/main/res/values/about_icons.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
<item>https://www.flaticon.com/de/kostenloses-icon/ausgaben_2273267</item>
</string-array>

<string-array name="ic_history">
<item>Freepik</item>
<item>flaticon.com</item>
<item>https://www.flaticon.com/free-icon/history_2277956?term=history</item>
</string-array>

<string-array name="ic_offers">
<item>Freepik</item>
<item>flaticon.com</item>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
<color name="refreshLayoutBackground">#FFFFFF</color>
<color name="refreshLayoutArrow">#393939</color>
<color name="click_hint">#000000</color>
<color name="foreground_color">@android:color/primary_text_light</color>
</resources>

0 comments on commit 547a8a0

Please sign in to comment.