mirror of https://github.com/FongMi/TV.git
parent
d4e42ef5ba
commit
a7521e59df
@ -0,0 +1,145 @@ |
||||
package com.fongmi.android.tv.ui.activity; |
||||
|
||||
import android.app.Activity; |
||||
import android.content.Intent; |
||||
import android.text.TextUtils; |
||||
import android.view.View; |
||||
import android.view.inputmethod.EditorInfo; |
||||
|
||||
import androidx.leanback.widget.ArrayObjectAdapter; |
||||
import androidx.leanback.widget.ItemBridgeAdapter; |
||||
import androidx.leanback.widget.ListRow; |
||||
import androidx.lifecycle.ViewModelProvider; |
||||
import androidx.viewbinding.ViewBinding; |
||||
|
||||
import com.fongmi.android.tv.api.ApiConfig; |
||||
import com.fongmi.android.tv.bean.Result; |
||||
import com.fongmi.android.tv.bean.Site; |
||||
import com.fongmi.android.tv.bean.Vod; |
||||
import com.fongmi.android.tv.databinding.ActivitySearchBinding; |
||||
import com.fongmi.android.tv.model.SiteViewModel; |
||||
import com.fongmi.android.tv.ui.custom.CustomRowPresenter; |
||||
import com.fongmi.android.tv.ui.custom.CustomSelector; |
||||
import com.fongmi.android.tv.ui.presenter.TitlePresenter; |
||||
import com.fongmi.android.tv.ui.presenter.VodPresenter; |
||||
import com.fongmi.android.tv.utils.ResUtil; |
||||
import com.google.common.collect.Lists; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import java.util.concurrent.ExecutorService; |
||||
import java.util.concurrent.Executors; |
||||
|
||||
public class SearchActivity extends BaseActivity implements VodPresenter.OnClickListener { |
||||
|
||||
private ActivitySearchBinding mBinding; |
||||
private SiteViewModel mSiteViewModel; |
||||
private ArrayObjectAdapter mAdapter; |
||||
private ExecutorService mService; |
||||
|
||||
public static void start(Activity activity) { |
||||
start(activity, ""); |
||||
} |
||||
|
||||
public static void start(Activity activity, String keyword) { |
||||
Intent intent = new Intent(activity, SearchActivity.class); |
||||
intent.putExtra("keyword", keyword); |
||||
//activity.startActivity(intent);
|
||||
} |
||||
|
||||
@Override |
||||
protected ViewBinding getBinding() { |
||||
return mBinding = ActivitySearchBinding.inflate(getLayoutInflater()); |
||||
} |
||||
|
||||
@Override |
||||
protected void initView() { |
||||
setRecyclerView(); |
||||
setViewModel(); |
||||
} |
||||
|
||||
@Override |
||||
protected void initEvent() { |
||||
mBinding.search.setOnClickListener(view -> startSearch()); |
||||
mBinding.keyword.setOnEditorActionListener((textView, actionId, event) -> { |
||||
if (actionId == EditorInfo.IME_ACTION_DONE) mBinding.search.performClick(); |
||||
return true; |
||||
}); |
||||
} |
||||
|
||||
private void setRecyclerView() { |
||||
CustomSelector selector = new CustomSelector(); |
||||
selector.addPresenter(String.class, new TitlePresenter()); |
||||
selector.addPresenter(ListRow.class, new CustomRowPresenter(16), VodPresenter.class); |
||||
mBinding.recycler.setVerticalSpacing(ResUtil.dp2px(16)); |
||||
mBinding.recycler.setAdapter(new ItemBridgeAdapter(mAdapter = new ArrayObjectAdapter(selector))); |
||||
} |
||||
|
||||
private void setViewModel() { |
||||
mSiteViewModel = new ViewModelProvider(this).get(SiteViewModel.class); |
||||
mSiteViewModel.result.observe(this, this::addVideo); |
||||
} |
||||
|
||||
private void addVideo(Result result) { |
||||
List<ListRow> rows = new ArrayList<>(); |
||||
for (List<Vod> items : Lists.partition(result.getList(), 5)) { |
||||
ArrayObjectAdapter adapter = new ArrayObjectAdapter(new VodPresenter(this, 5)); |
||||
adapter.addAll(0, items); |
||||
rows.add(new ListRow(adapter)); |
||||
} |
||||
mAdapter.add(result.getList().get(0).getSite().getName()); |
||||
mAdapter.addAll(mAdapter.size(), rows); |
||||
} |
||||
|
||||
private void startSearch() { |
||||
String keyword = mBinding.keyword.getText().toString().trim(); |
||||
if (TextUtils.isEmpty(keyword)) return; |
||||
hideLayout(); |
||||
mService = Executors.newFixedThreadPool(5); |
||||
for (Site item : ApiConfig.get().getSites()) { |
||||
if (item.isSearchable()) { |
||||
mService.execute(() -> mSiteViewModel.searchContent(item.getKey(), keyword)); |
||||
} |
||||
} |
||||
} |
||||
|
||||
private void stopSearch() { |
||||
if (mService != null) { |
||||
mService.shutdownNow(); |
||||
mService = null; |
||||
} |
||||
} |
||||
|
||||
private void showLayout() { |
||||
mAdapter.clear(); |
||||
mBinding.layout.setVisibility(View.VISIBLE); |
||||
mBinding.recycler.setVisibility(View.INVISIBLE); |
||||
} |
||||
|
||||
private void hideLayout() { |
||||
mBinding.keyword.setText(""); |
||||
mBinding.layout.setVisibility(View.GONE); |
||||
mBinding.recycler.setVisibility(View.VISIBLE); |
||||
} |
||||
|
||||
@Override |
||||
public void onItemClick(Vod item) { |
||||
DetailActivity.start(this, item.getSite().getKey(), item.getVodId()); |
||||
} |
||||
|
||||
@Override |
||||
public void onBackPressed() { |
||||
if (mBinding.recycler.getVisibility() == View.VISIBLE) { |
||||
stopSearch(); |
||||
showLayout(); |
||||
} else { |
||||
super.onBackPressed(); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
protected void onDestroy() { |
||||
super.onDestroy(); |
||||
stopSearch(); |
||||
} |
||||
} |
||||
@ -0,0 +1,58 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
xmlns:app="http://schemas.android.com/apk/res-auto" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
android:keepScreenOn="true"> |
||||
|
||||
<androidx.leanback.widget.VerticalGridView |
||||
android:id="@+id/recycler" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
android:clipChildren="false" |
||||
android:clipToPadding="false" |
||||
android:paddingStart="24dp" |
||||
android:paddingTop="24dp" |
||||
android:paddingEnd="24dp" |
||||
android:paddingBottom="24dp" |
||||
android:visibility="gone" |
||||
app:focusOutEnd="true" |
||||
app:focusOutFront="true" /> |
||||
|
||||
<LinearLayout |
||||
android:id="@+id/layout" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:layout_centerInParent="true" |
||||
android:layout_marginStart="24dp" |
||||
android:layout_marginEnd="24dp" |
||||
android:gravity="center" |
||||
android:orientation="horizontal"> |
||||
|
||||
<EditText |
||||
android:id="@+id/keyword" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:layout_marginEnd="24dp" |
||||
android:background="@drawable/selector_item" |
||||
android:hint="輸入關鍵字..." |
||||
android:importantForAutofill="no" |
||||
android:inputType="textCapCharacters|textAutoCorrect|textAutoComplete" |
||||
android:maxLength="20" |
||||
android:singleLine="true" |
||||
android:textColor="@color/white" |
||||
android:textColorHint="@color/white" |
||||
android:textSize="24sp" /> |
||||
|
||||
<ImageView |
||||
android:id="@+id/search" |
||||
android:layout_width="48dp" |
||||
android:layout_height="48dp" |
||||
android:background="@drawable/selector_item" |
||||
android:focusable="true" |
||||
android:focusableInTouchMode="true" |
||||
android:scaleType="fitCenter" |
||||
android:src="@drawable/ic_search" /> |
||||
|
||||
</LinearLayout> |
||||
</RelativeLayout> |
||||
Loading…
Reference in new issue