Skip to content

Commit

Permalink
Filter teilweise hinzugefügt
Browse files Browse the repository at this point in the history
  • Loading branch information
DonMarv00 committed Feb 17, 2024
1 parent 7673949 commit 0ef46bd
Show file tree
Hide file tree
Showing 16 changed files with 359 additions and 95 deletions.
4 changes: 3 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'com.mikepenz.aboutlibraries.plugin'

android {

Expand All @@ -24,7 +25,6 @@ android {
viewBinding true
}


namespace 'de.msdevs.einschlafhilfe'

compileOptions {
Expand All @@ -50,6 +50,8 @@ dependencies {
implementation("ru.gildor.coroutines:kotlin-coroutines-okhttp:1.0")
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.9.10"
implementation 'com.github.AppIntro:AppIntro:6.3.1'
implementation "com.mikepenz:aboutlibraries-core:10.9.1"
implementation "com.mikepenz:aboutlibraries:10.9.1"
}
repositories {
mavenCentral()
Expand Down
7 changes: 6 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@
<activity android:name=".AboutActivity"
android:label="@string/about" />


<activity
android:theme="@style/AppThemeNoToolbar"
android:screenOrientation="portrait"
tools:ignore="DiscouragedApi,LockedOrientationActivity"
android:name=".AboutLibrariesActivity"
android:exported="false" />
</application>
<queries>
<package android:name="com.spotify.music" />
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/de/msdevs/einschlafhilfe/AboutActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class AboutActivity : BaseActivity() {
opennUrl("https://github.com/MarvinStelter/DieDreiFragezeichenEinschlafhilfe")
}
btnLicenses.setOnClickListener{
startActivity(Intent(this@AboutActivity, AboutLibrariesActivity::class.java))

if(expandableLayout.isExpanded){
expandableLayout.collapse()
}else{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package de.msdevs.einschlafhilfe
import android.os.Bundle
import com.mikepenz.aboutlibraries.LibsBuilder
import com.mikepenz.aboutlibraries.ui.LibsActivity


class AboutLibrariesActivity : LibsActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
intent = LibsBuilder()
.withEdgeToEdge(true)
.withAboutMinimalDesign(true)
.withActivityTitle(getString(R.string.about_libs))
.withSearchEnabled(true)
.intent(this)
super.onCreate(savedInstanceState)
}

}
116 changes: 96 additions & 20 deletions app/src/main/java/de/msdevs/einschlafhilfe/FilterActivity.kt
Original file line number Diff line number Diff line change
@@ -1,57 +1,79 @@
package de.msdevs.einschlafhilfe


import android.database.sqlite.SQLiteDatabase
import android.os.Bundle
import android.util.Log
import android.view.ScaleGestureDetector
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.AutoCompleteTextView
import android.widget.FrameLayout
import android.widget.HorizontalScrollView
import android.widget.ScrollView
import android.widget.TextView
import android.widget.Toast
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.textfield.MaterialAutoCompleteTextView
import de.msdevs.einschlafhilfe.adapter.FilterListeAdapter
import de.msdevs.einschlafhilfe.database.DatabaseHelper
import de.msdevs.einschlafhilfe.databinding.ActivityFilterBinding
import de.msdevs.einschlafhilfe.models.JsonResponse
import de.msdevs.einschlafhilfe.utils.NetworkUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.OkHttpClient
import okhttp3.Request
import org.json.JSONObject
import ru.gildor.coroutines.okhttp.await


class FilterActivity : BaseActivity(){
import java.io.BufferedReader

class FilterActivity : BaseActivity(), FilterListeAdapter.OnDeleteItemListener{
private lateinit var rvFilterListe : RecyclerView
private lateinit var tvNothingAdded : TextView
private lateinit var binding : ActivityFilterBinding
private lateinit var folgen_database: SQLiteDatabase
private lateinit var databaseHelper: DatabaseHelper
private lateinit var tvInput : MaterialAutoCompleteTextView
private val episodeList = ArrayList<JsonResponse>()
private var filterList = ArrayList<JsonResponse>()
private var jsonIndexSeperator = HashMap<String, Int>()
private var apiCallCounter = 0
private lateinit var networkUtils: NetworkUtils
private lateinit var folgenListe : String

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityFilterBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
iniViews()
refreshList()
supportActionBar?.setDisplayHomeAsUpEnabled(true)

lifecycleScope.launch {
withContext(Dispatchers.IO) {
apiCall()
apiCall("ddf")
apiCall("d3")
apiCall("kids")
apiCall("sonderfolgen")
}
}


}
private suspend fun apiCall() {
val urlExtraParameter = "folgen.json"
episodeList.clear()
private suspend fun apiCall(extraParameter : String) {
try {
if(networkUtils.isConnected(this)){
val client = OkHttpClient.Builder().build()
val request =
Request.Builder().url(getString(R.string.base_url) + urlExtraParameter)
.build()

val folgenListe = client.newCall(request).await().body()?.string().toString()
if(extraParameter == "ddf"){
folgenListe = assets.open("offline_list.txt").bufferedReader().use(BufferedReader::readText)
}else if (extraParameter == "d3"){
folgenListe = assets.open("offline_list_dd.txt").bufferedReader().use(BufferedReader::readText)
}else if(extraParameter == "kids"){
folgenListe = assets.open("offline_list_kids.txt").bufferedReader().use(BufferedReader::readText)
}else{
folgenListe = assets.open("offline_list_sonderfolgen_ddf.txt").bufferedReader().use(BufferedReader::readText)
}
val jsonObject = JSONObject(folgenListe)
val jsonArray = jsonObject.optJSONArray("folgen")
if (jsonArray != null) {
Expand All @@ -61,12 +83,18 @@ class FilterActivity : BaseActivity(){
JsonResponse(
name = jsonObject.optString("name"),
beschreibung = jsonObject.optString("beschreibung"),
spotify = jsonObject.optString("spotify")
spotify = jsonObject.optString("spotify"),
nummer = jsonObject.optInt("nummer"),
type = extraParameter
)
)
}
runOnUiThread {
iniAutoComplete()
jsonIndexSeperator[extraParameter] = jsonArray.length()
apiCallCounter++
if(apiCallCounter == 4){
runOnUiThread {
iniAutoComplete()
}
}
}

Expand All @@ -85,11 +113,59 @@ class FilterActivity : BaseActivity(){

textView.setOnItemClickListener { parent, view, position, id ->
val selectedName = parent.getItemAtPosition(position) as String
databaseHelper.insertFilterFolge(folgen_database,selectedName,getEpisodeNumber(selectedName),getEpisodeType(selectedName))
textView.text = null
refreshList()
}
}
private fun refreshList(){
filterList = databaseHelper.getFilterList(folgen_database)
val adapter = FilterListeAdapter(filterList,this)
val layoutManger = LinearLayoutManager(this)

if(filterList.isNotEmpty()){
tvNothingAdded.visibility = View.GONE
}else{
tvNothingAdded.visibility = View.VISIBLE
}
rvFilterListe.layoutManager = layoutManger
rvFilterListe.adapter = adapter

}
private fun iniViews(){
folgen_database = openOrCreateDatabase("app_list",MODE_PRIVATE,null)
databaseHelper = DatabaseHelper(this)
databaseHelper.createTables(folgen_database)

networkUtils = NetworkUtils()
tvInput = binding.tvFolge
rvFilterListe = binding.rvFilterListe
tvNothingAdded = binding.tvNoEpisodesAdded
}

private fun getEpisodeNumber(nameToFind: String): String? {
for (jsonResponse in episodeList) {
if (jsonResponse.name == nameToFind) {
return jsonResponse.nummer.toString()
}
}
return null
}
private fun getEpisodeType(nameToFind: String): String? {
for (jsonResponse in episodeList) {
if (jsonResponse.name == nameToFind) {
return jsonResponse.type
}
}
return null
}
override fun onSupportNavigateUp(): Boolean {
onBackPressed()
return true
}

override fun onDeleteItem(name: String) {
databaseHelper.removeFromList(folgen_database,name)
refreshList()
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/de/msdevs/einschlafhilfe/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ class MainActivity : BaseActivity() {
if (jsonArray != null) {
for (i in 0 until jsonArray.length()) {
val jsonObject = jsonArray.getJSONObject(i)
episodeList.add(JsonResponse(name = jsonObject.optString("name"), beschreibung = jsonObject.optString("beschreibung"), spotify = jsonObject.optString("spotify")))
episodeList.add(JsonResponse(name = jsonObject.optString("name"), beschreibung = jsonObject.optString("beschreibung"), spotify = jsonObject.optString("spotify"), nummer = jsonObject.optInt("nummer"), type = ""))
}
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package de.msdevs.einschlafhilfe.adapter

import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import de.msdevs.einschlafhilfe.databinding.ItemFolgeBinding
import de.msdevs.einschlafhilfe.models.JsonResponse
import de.msdevs.einschlafhilfe.utils.NetworkUtils
import de.msdevs.einschlafhilfe.R

class FilterListeAdapter(private val folgeList: List<JsonResponse>, private val onDeleteItemListener: OnDeleteItemListener? = null) :
RecyclerView.Adapter<FilterListeAdapter.FolgeViewHolder>() {
private var networkUtils = NetworkUtils()

inner class FolgeViewHolder(private val binding: ItemFolgeBinding) :
RecyclerView.ViewHolder(binding.root) {

fun bind(folgeTitle: String, nummer : String, type : String) {
binding.tvFolgenName.text = folgeTitle
binding.tvFolgenNummer.text = binding.tvFolgenNummer.context.getString(R.string.nummer, nummer)
loadCover(nummer,type, binding.ivCover)

binding.ivDelete.setOnClickListener{
onDeleteItemListener?.onDeleteItem(folgeTitle)
}
}
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FolgeViewHolder {
val binding =
ItemFolgeBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return FolgeViewHolder(binding)
}

override fun onBindViewHolder(holder: FolgeViewHolder, position: Int) {
val item = folgeList[position]
holder.bind(item.name,item.nummer.toString(), item.type)
}

override fun getItemCount(): Int {
return folgeList.size
}

private fun loadCover(nummer : String, type : String, iv : ImageView){
val context : Context = iv.context
var prefix = ""
if(type == "ddf"){
prefix = ""
}else if(type == "d3"){
prefix = "dd"
}else if(type == "kids"){
prefix = "k"
}else if(type == "sonderfolgen"){
prefix = "x"
}else{
prefix = "s"
}
val url : String = if(prefix == "dd"){
context.getString(R.string.cover_citroncode_dd_url) + (nummer) + ".png"
}else{
context.getString(R.string.cover_citroncode_url) + prefix + (nummer) + ".png"
}
Log.e("FilterListeAdapter", "Cover URL: $url")
if(networkUtils.isConnected(context)){
Glide.with(context)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(iv)
}
}
interface OnDeleteItemListener {
fun onDeleteItem(name: String)
}
}
Loading

0 comments on commit 0ef46bd

Please sign in to comment.