zerrium
4 years ago
10 changed files with 294 additions and 6 deletions
-
51app/src/main/java/awais/instagrabber/adapters/KeywordsFilterAdapter.java
-
30app/src/main/java/awais/instagrabber/adapters/viewholder/dialogs/KeywordsFilterDialogViewHolder.java
-
61app/src/main/java/awais/instagrabber/dialogs/KeywordsFilterDialog.java
-
26app/src/main/java/awais/instagrabber/fragments/settings/PostPreferencesFragment.java
-
4app/src/main/java/awais/instagrabber/utils/Constants.java
-
47app/src/main/java/awais/instagrabber/utils/KeywordsFilterUtils.java
-
3app/src/main/java/awais/instagrabber/utils/SettingsHelper.java
-
52app/src/main/res/layout/dialog_keywords_filter.xml
-
23app/src/main/res/layout/item_keyword.xml
-
3app/src/main/res/values/strings.xml
@ -0,0 +1,51 @@ |
|||||
|
package awais.instagrabber.adapters; |
||||
|
|
||||
|
import android.content.Context; |
||||
|
import android.view.LayoutInflater; |
||||
|
import android.view.View; |
||||
|
import android.view.ViewGroup; |
||||
|
|
||||
|
import androidx.annotation.NonNull; |
||||
|
import androidx.recyclerview.widget.RecyclerView; |
||||
|
|
||||
|
import java.util.ArrayList; |
||||
|
import java.util.HashSet; |
||||
|
|
||||
|
import awais.instagrabber.R; |
||||
|
import awais.instagrabber.adapters.viewholder.dialogs.KeywordsFilterDialogViewHolder; |
||||
|
import awais.instagrabber.utils.Constants; |
||||
|
import awais.instagrabber.utils.SettingsHelper; |
||||
|
|
||||
|
public class KeywordsFilterAdapter extends RecyclerView.Adapter<KeywordsFilterDialogViewHolder> { |
||||
|
|
||||
|
private final Context context; |
||||
|
private final ArrayList<String> items; |
||||
|
|
||||
|
public KeywordsFilterAdapter(Context context, ArrayList<String> items){ |
||||
|
this.context = context; |
||||
|
this.items = items; |
||||
|
} |
||||
|
|
||||
|
@NonNull |
||||
|
@Override |
||||
|
public KeywordsFilterDialogViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { |
||||
|
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_keyword, parent, false); |
||||
|
return new KeywordsFilterDialogViewHolder(v); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void onBindViewHolder(@NonNull KeywordsFilterDialogViewHolder holder, int position) { |
||||
|
holder.getTextView().setText(items.get(position)); |
||||
|
holder.getDeleteButton().setOnClickListener(view -> { |
||||
|
SettingsHelper settingsHelper = new SettingsHelper(context); |
||||
|
items.remove(items.get(position)); |
||||
|
settingsHelper.putStringSet(Constants.KEYWORD_FILTERS, new HashSet<>(items)); |
||||
|
notifyDataSetChanged(); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public int getItemCount() { |
||||
|
return 0; |
||||
|
} |
||||
|
} |
@ -0,0 +1,30 @@ |
|||||
|
package awais.instagrabber.adapters.viewholder.dialogs; |
||||
|
|
||||
|
import android.view.View; |
||||
|
import android.widget.ImageView; |
||||
|
import android.widget.TextView; |
||||
|
|
||||
|
import androidx.annotation.NonNull; |
||||
|
import androidx.recyclerview.widget.RecyclerView; |
||||
|
|
||||
|
import awais.instagrabber.R; |
||||
|
|
||||
|
public class KeywordsFilterDialogViewHolder extends RecyclerView.ViewHolder { |
||||
|
|
||||
|
private final ImageView deleteButton; |
||||
|
private final TextView item; |
||||
|
|
||||
|
public KeywordsFilterDialogViewHolder(@NonNull View itemView) { |
||||
|
super(itemView); |
||||
|
deleteButton = itemView.findViewById(R.id.keyword_delete); |
||||
|
item = itemView.findViewById(R.id.keyword_text); |
||||
|
} |
||||
|
|
||||
|
public ImageView getDeleteButton(){ |
||||
|
return deleteButton; |
||||
|
} |
||||
|
|
||||
|
public TextView getTextView(){ |
||||
|
return item; |
||||
|
} |
||||
|
} |
@ -0,0 +1,61 @@ |
|||||
|
package awais.instagrabber.dialogs; |
||||
|
|
||||
|
import android.content.Context; |
||||
|
import android.os.Bundle; |
||||
|
import android.view.LayoutInflater; |
||||
|
import android.view.View; |
||||
|
import android.view.ViewGroup; |
||||
|
import android.widget.EditText; |
||||
|
|
||||
|
import androidx.annotation.NonNull; |
||||
|
import androidx.annotation.Nullable; |
||||
|
import androidx.fragment.app.DialogFragment; |
||||
|
import androidx.recyclerview.widget.LinearLayoutManager; |
||||
|
import androidx.recyclerview.widget.RecyclerView; |
||||
|
|
||||
|
import java.util.ArrayList; |
||||
|
import java.util.HashSet; |
||||
|
|
||||
|
import awais.instagrabber.adapters.KeywordsFilterAdapter; |
||||
|
import awais.instagrabber.databinding.DialogKeywordsFilterBinding; |
||||
|
import awais.instagrabber.utils.Constants; |
||||
|
import awais.instagrabber.utils.SettingsHelper; |
||||
|
|
||||
|
public final class KeywordsFilterDialog extends DialogFragment { |
||||
|
|
||||
|
@Nullable |
||||
|
@Override |
||||
|
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { |
||||
|
DialogKeywordsFilterBinding dialogKeywordsFilterBinding = DialogKeywordsFilterBinding.inflate(inflater, container, false); |
||||
|
|
||||
|
final Context context = getContext(); |
||||
|
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context); |
||||
|
final RecyclerView recyclerView = dialogKeywordsFilterBinding.recycler; |
||||
|
recyclerView.setLayoutManager(linearLayoutManager); |
||||
|
|
||||
|
SettingsHelper settingsHelper = new SettingsHelper(context); |
||||
|
ArrayList<String> items = new ArrayList<>(settingsHelper.getStringSet(Constants.KEYWORD_FILTERS)); |
||||
|
KeywordsFilterAdapter adapter = new KeywordsFilterAdapter(context, items); |
||||
|
recyclerView.setAdapter(adapter); |
||||
|
|
||||
|
final EditText editText = dialogKeywordsFilterBinding.editText; |
||||
|
|
||||
|
dialogKeywordsFilterBinding.addIcon.setOnClickListener(view ->{ |
||||
|
final String s = editText.getText().toString(); |
||||
|
if(s.isEmpty()) return; |
||||
|
if(items.contains(s)) { |
||||
|
editText.setText(""); |
||||
|
return; |
||||
|
} |
||||
|
items.add(s); |
||||
|
settingsHelper.putStringSet(Constants.KEYWORD_FILTERS, new HashSet<>(items)); |
||||
|
adapter.notifyDataSetChanged(); |
||||
|
}); |
||||
|
|
||||
|
dialogKeywordsFilterBinding.btnOK.setOnClickListener(view ->{ |
||||
|
this.dismiss(); |
||||
|
}); |
||||
|
|
||||
|
return super.onCreateView(inflater, container, savedInstanceState); |
||||
|
} |
||||
|
} |
@ -0,0 +1,47 @@ |
|||||
|
package awais.instagrabber.utils; |
||||
|
|
||||
|
import java.util.ArrayList; |
||||
|
import java.util.List; |
||||
|
import java.util.Locale; |
||||
|
|
||||
|
import awais.instagrabber.repositories.responses.Caption; |
||||
|
import awais.instagrabber.repositories.responses.Media; |
||||
|
|
||||
|
public final class KeywordsFilterUtils { |
||||
|
|
||||
|
private final ArrayList<String> keywords; |
||||
|
|
||||
|
public KeywordsFilterUtils(ArrayList<String> keywords){ |
||||
|
this.keywords = keywords; |
||||
|
} |
||||
|
|
||||
|
public boolean filter(final String caption){ |
||||
|
if(caption == null) return false; |
||||
|
final String temp = caption.toLowerCase(Locale.getDefault()); |
||||
|
for(final String s:keywords){ |
||||
|
if(temp.contains(s)) return true; |
||||
|
} |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
public boolean filter(final Media media){ |
||||
|
if(media == null) return false; |
||||
|
final Caption c = media.getCaption(); |
||||
|
if(c == null) return false; |
||||
|
final String temp = c.getText().toLowerCase(LocaleUtils.getCurrentLocale()); |
||||
|
for(final String s:keywords){ |
||||
|
if(temp.contains(s)) return true; |
||||
|
} |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
public List<Media> filter(final List<Media> media){ |
||||
|
if(media == null) return new ArrayList<>(); |
||||
|
|
||||
|
final List<Media> result= new ArrayList<>(); |
||||
|
for(final Media m:media){ |
||||
|
if(filter(m)) result.add(m); |
||||
|
} |
||||
|
return result; |
||||
|
} |
||||
|
} |
@ -0,0 +1,52 @@ |
|||||
|
<?xml version="1.0" encoding="utf-8"?> |
||||
|
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" |
||||
|
android:layout_width="match_parent" |
||||
|
android:layout_height="wrap_content" |
||||
|
android:padding="16dp"> |
||||
|
<LinearLayout |
||||
|
android:layout_width="match_parent" |
||||
|
android:layout_height="wrap_content" |
||||
|
android:orientation="vertical"> |
||||
|
|
||||
|
<LinearLayout |
||||
|
android:layout_width="match_parent" |
||||
|
android:layout_height="wrap_content" |
||||
|
android:orientation="horizontal" |
||||
|
android:layout_marginTop="8dp" |
||||
|
android:layout_marginBottom="8dp"> |
||||
|
|
||||
|
<androidx.appcompat.widget.AppCompatEditText |
||||
|
android:id="@+id/edit_text" |
||||
|
android:layout_width="320dp" |
||||
|
android:layout_height="48dp" |
||||
|
android:hint="@string/hint_keyword" /> |
||||
|
|
||||
|
<ImageView |
||||
|
android:id="@+id/add_icon" |
||||
|
android:layout_width="48dp" |
||||
|
android:layout_height="48dp" |
||||
|
android:background="?android:attr/selectableItemBackgroundBorderless" |
||||
|
android:scaleType="center" |
||||
|
android:layout_marginStart="8dp" /> |
||||
|
</LinearLayout> |
||||
|
|
||||
|
<androidx.recyclerview.widget.RecyclerView |
||||
|
android:id="@+id/recycler" |
||||
|
android:layout_width="match_parent" |
||||
|
android:layout_height="wrap_content" /> |
||||
|
|
||||
|
<FrameLayout |
||||
|
android:layout_width="match_parent" |
||||
|
android:layout_height="wrap_content"> |
||||
|
|
||||
|
<androidx.appcompat.widget.AppCompatButton |
||||
|
android:id="@+id/btnOK" |
||||
|
style="@style/Widget.AppCompat.Button.ButtonBar.AlertDialog" |
||||
|
android:layout_width="wrap_content" |
||||
|
android:layout_height="wrap_content" |
||||
|
android:layout_gravity="end" |
||||
|
android:text="@string/ok" /> |
||||
|
</FrameLayout> |
||||
|
|
||||
|
</LinearLayout> |
||||
|
</ScrollView> |
@ -0,0 +1,23 @@ |
|||||
|
<?xml version="1.0" encoding="utf-8"?> |
||||
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
|
android:layout_width="match_parent" |
||||
|
android:layout_height="wrap_content" |
||||
|
android:gravity="center_vertical" |
||||
|
android:orientation="horizontal" |
||||
|
android:paddingBottom="8dp" |
||||
|
android:paddingTop="8dp"> |
||||
|
|
||||
|
<androidx.appcompat.widget.AppCompatTextView |
||||
|
android:id="@+id/keyword_text" |
||||
|
android:layout_width="0dp" |
||||
|
android:layout_height="wrap_content" |
||||
|
android:layout_weight="1" /> |
||||
|
|
||||
|
<ImageView |
||||
|
android:id="@+id/keyword_delete" |
||||
|
android:layout_width="48dp" |
||||
|
android:layout_height="48dp" |
||||
|
android:background="?android:attr/selectableItemBackground" |
||||
|
android:scaleType="center" /> |
||||
|
|
||||
|
</LinearLayout> |
Write
Preview
Loading…
Cancel
Save
Reference in new issue