]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/adapters/HistoryArrayAdapter.java
Combine the Yahoo search URLs. https://redmine.stoutner.com/issues/804
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / adapters / HistoryArrayAdapter.java
1 /*
2  * Copyright © 2016-2019,2021-2022 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
5  *
6  * Privacy Browser Android is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Privacy Browser Android is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Privacy Browser Android.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 package com.stoutner.privacybrowser.adapters;
21
22 import android.content.Context;
23 import android.graphics.Typeface;
24 import android.view.LayoutInflater;
25 import android.view.View;
26 import android.view.ViewGroup;
27 import android.widget.ArrayAdapter;
28 import android.widget.ImageView;
29 import android.widget.TextView;
30
31 import androidx.annotation.NonNull;
32
33 import com.stoutner.privacybrowser.R;
34 import com.stoutner.privacybrowser.dataclasses.History;
35
36 import java.util.ArrayList;
37
38 public class HistoryArrayAdapter extends ArrayAdapter<History> {
39
40     // `currentPage` is used in `HistoryArrayAdapter` and `getView()`.
41     private final int currentPage;
42
43     public HistoryArrayAdapter(Context context, ArrayList<History> historyArrayList, int currentPageId) {
44         // `super` must be called from the base `ArrayAdapter`.  `0` is the `textViewResourceId`, which is unused.
45         super(context, 0, historyArrayList);
46
47         // Store `currentPageId` in the class variable.
48         currentPage = currentPageId;
49     }
50
51     @Override
52     @NonNull
53     public View getView(int position, View convertView, @NonNull ViewGroup parent) {
54         // Inflate the view if it is null.
55         if (convertView == null) {
56             // `false` does not attach `url_history_item_linearlayout` to `parent`.
57             convertView = LayoutInflater.from(getContext()).inflate(R.layout.url_history_item_linearlayout, parent, false);
58         }
59
60         // Get handles for `favoriteIconImageView` and `urlTextView`.
61         ImageView favoriteIconImageView = convertView.findViewById(R.id.history_favorite_icon_imageview);
62         TextView urlTextView = convertView.findViewById(R.id.history_url_textview);
63
64         // Get the URL history for this position.
65         History history = getItem(position);
66
67         // Remove the lint warning below that `history` might be `null`.
68         assert history != null;
69
70         // Set `favoriteIconImageView` and `urlTextView`.
71         favoriteIconImageView.setImageBitmap(history.favoriteIcon);
72         urlTextView.setText(history.url);
73
74         // Set the URL text for `currentPage` to be bold.
75         if (position == currentPage) {
76             urlTextView.setTypeface(Typeface.DEFAULT_BOLD);
77         } else {  // Set the default typeface for all the other entries.
78             urlTextView.setTypeface(Typeface.DEFAULT);
79         }
80
81         // Return the modified `convertView`.
82         return convertView;
83     }
84 }