Merge branch 'master' into downloads

This commit is contained in:
Bnyro 2022-06-06 16:39:32 +02:00
commit aa139247cb
14 changed files with 151 additions and 40 deletions

View File

@ -9,6 +9,7 @@ import android.widget.ProgressBar
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
@ -40,6 +41,11 @@ class Home : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val searchButton = view.findViewById<com.google.android.material.floatingactionbutton
.FloatingActionButton>(R.id.search_fab)
searchButton.setOnClickListener {
findNavController().navigate(R.id.searchFragment)
}
val recyclerView = view.findViewById<RecyclerView>(R.id.recview)
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
val grid = sharedPreferences.getString(

View File

@ -282,8 +282,13 @@ class MainActivity : AppCompatActivity() {
}
}
} catch (e: Exception) {
navController.popBackStack()
moveTaskToBack(true)
// try catch to prevent nointernet activity to crash
try {
navController.popBackStack()
moveTaskToBack(true)
} catch (e: Exception) {
super.onBackPressed()
}
}
}

View File

@ -30,6 +30,7 @@ import androidx.preference.PreferenceManager
import com.github.libretube.dialogs.LoginDialog
import com.github.libretube.util.RetrofitInstance
import com.github.libretube.util.changeIcon
import com.github.libretube.util.checkUpdate
import com.github.libretube.util.restartMainActivity
import com.github.libretube.util.updateTheme
import com.google.android.material.color.DynamicColors
@ -154,6 +155,11 @@ class SettingsActivity :
}
}
super.onCreate(savedInstanceState)
try {
checkUpdate(childFragmentManager)
} catch (e: Exception) {
e.printStackTrace()
}
}
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {

View File

@ -0,0 +1,32 @@
package com.github.libretube.dialogs
import android.app.Dialog
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.fragment.app.DialogFragment
import com.github.libretube.R
import com.google.android.material.dialog.MaterialAlertDialogBuilder
class UpdateAvailableDialog(
private val versionTag: String,
private val updateLink: String
) : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let {
MaterialAlertDialogBuilder(requireContext())
.setTitle(context?.getString(R.string.update_available, versionTag))
.setMessage(context?.getString(R.string.update_available_text))
.setNegativeButton(context?.getString(R.string.cancel)) { _, _ ->
dismiss()
}
.setPositiveButton(context?.getString(R.string.okay)) { _, _ ->
val uri = Uri.parse(updateLink)
val intent = Intent(Intent.ACTION_VIEW).setData(uri)
startActivity(intent)
}
.show()
} ?: throw IllegalStateException("Activity cannot be null")
}
}

View File

@ -0,0 +1,74 @@
package com.github.libretube.util
import android.util.Log
import androidx.fragment.app.FragmentManager
import com.github.libretube.BuildConfig
import com.github.libretube.dialogs.UpdateAvailableDialog
import java.io.BufferedReader
import java.io.InputStreamReader
import java.net.URL
import javax.net.ssl.HttpsURLConnection
import org.json.JSONArray
import org.json.JSONObject
fun checkUpdate(childFragmentManager: FragmentManager) {
var updateInfo: UpdateInfo? = UpdateInfo("", "")
// run http request as thread to make it async
val thread = Thread {
// otherwise crashes without internet
try {
updateInfo = getUpdateInfo()
} catch (e: Exception) {
}
}
thread.start()
// wait for the thread to finish
thread.join()
// show the UpdateAvailableDialog if there's an update available
if (updateInfo?.tagName != "" && BuildConfig.VERSION_NAME != updateInfo?.tagName) {
val updateAvailableDialog = UpdateAvailableDialog(
updateInfo?.tagName!!,
updateInfo?.updateUrl!!
)
updateAvailableDialog.show(childFragmentManager, "UpdateAvailableDialog")
}
}
fun getUpdateInfo(): UpdateInfo? {
val latest = URL("https://api.github.com/repos/libre-tube/LibreTube/releases/latest")
val json = StringBuilder()
val urlConnection: HttpsURLConnection?
urlConnection = latest.openConnection() as HttpsURLConnection
val br = BufferedReader(InputStreamReader(urlConnection.inputStream))
var line: String?
while (br.readLine().also { line = it } != null) json.append(line)
// Parse and return json data
val jsonRoot = JSONObject(json.toString())
if (jsonRoot.has("tag_name") &&
jsonRoot.has("html_url") &&
jsonRoot.has("assets")
) {
val updateUrl = jsonRoot.getString("html_url")
val jsonAssets: JSONArray = jsonRoot.getJSONArray("assets")
for (i in 0 until jsonAssets.length()) {
val jsonAsset = jsonAssets.getJSONObject(i)
if (jsonAsset.has("name")) {
val name = jsonAsset.getString("name")
if (name.endsWith(".apk")) {
val tagName = jsonRoot.getString("name")
Log.i("", "Lastest version: $tagName")
return UpdateInfo(updateUrl, tagName)
}
}
}
}
return null
}
// data class for the update info, required to return the data
data class UpdateInfo(
val updateUrl: String,
val tagName: String
)

View File

@ -1,10 +0,0 @@
<vector android:height="24dp"
android:tint="#FFFFFF"
android:viewportHeight="24"
android:viewportWidth="24"
android:width="24dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<path
android:fillColor="@android:color/white"
android:pathData="M7.41,8.59L12,13.17l4.59,-4.58L18,10l-6,6 -6,-6 1.41,-1.41z" />
</vector>

View File

@ -1,11 +0,0 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="?attr/colorControlNormal"
android:viewportWidth="24"
android:viewportHeight="24">
<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>

View File

@ -1,10 +0,0 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="35dp"
android:height="35dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M15.5,14h-0.79l-0.28,-0.27C15.41,12.59 16,11.11 16,9.5 16,5.91 13.09,3 9.5,3S3,5.91 3,9.5 5.91,16 9.5,16c1.61,0 3.09,-0.59 4.23,-1.57l0.27,0.28v0.79l5,4.99L20.49,19l-4.99,-5zM9.5,14C7.01,14 5,11.99 5,9.5S7.01,5 9.5,5 14,7.01 14,9.5 11.99,14 9.5,14z" />
</vector>

View File

@ -22,7 +22,7 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_wifi_off" />
app:srcCompat="@drawable/ic_no_wifi" />
<TextView
android:id="@+id/noInternet_textView"
@ -39,6 +39,7 @@
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/retry"
android:textColor="?android:attr/colorBackground"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/noInternet_textView" />

View File

@ -12,7 +12,8 @@
See the License for the specific language governing permissions and
limitations under the License.
-->
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<merge xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 0dp dimensions are used to prevent this view from influencing the size of
the parent view if it uses "wrap_content". It is expanded to occupy the
@ -48,9 +49,10 @@
android:id="@+id/close_imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_close2"
android:src="@drawable/ic_close"
android:padding="@dimen/exo_icon_padding"
android:background="#00FFFFFF" />
android:background="#00FFFFFF"
app:tint="@color/white" />
</LinearLayout>
<LinearLayout
@ -85,9 +87,10 @@
android:id="@+id/quality_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_arrow_down2"
android:src="@drawable/ic_arrow_down"
android:padding="@dimen/exo_icon_padding"
android:background="#00FFFFFF" />
android:background="#00FFFFFF"
app:tint="@color/white" />
</LinearLayout>
</FrameLayout>

View File

@ -29,5 +29,18 @@
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/progressBar"
app:layout_constraintTop_toTopOf="parent" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/search_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_margin="16dp"
app:tint="?android:attr/colorBackground"
app:backgroundTint="?attr/colorSecondary"
app:srcCompat="@drawable/ic_search" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -35,7 +35,7 @@
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:padding="5dp"
android:src="@drawable/ic_close2"
android:src="@drawable/ic_close"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:shapeAppearanceOverlay="@style/roundedImageViewRounded" />

View File

@ -116,4 +116,6 @@
<string name="piped">Piped</string>
<string name="youtube">YouTube</string>
<string name="playOnBackground">Play on background</string>
<string name="update_available">Version %1$s is available</string>
<string name="update_available_text">There is a new update available. Click okay to become redirected to the update page on GitHub.</string>
</resources>