Commit 0a5a3f63 authored by Nicolas Pomepuy's avatar Nicolas Pomepuy Committed by Geoffrey Métais

Migrate chapter dialog to BottomSheetDialog

parent 62375ff9
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@color/orange300" />
<item android:state_selected="true" android:drawable="@color/orange500" />
<item android:state_activated="true" android:drawable="@color/orange500" />
<item android:state_focused="true" android:drawable="@color/orange300" />
<item android:state_pressed="true" android:drawable="@color/orange500" />
......
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="selected"
type="Boolean" />
<variable
name="chapter"
type="org.videolan.vlc.gui.dialogs.SelectChapterDialog.Chapter" />
<variable
name="holder"
type="org.videolan.vlc.gui.dialogs.SelectChapterDialog.ChapterViewHolder" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background_chapter_item"
android:onClick="@{holder::onClick}"
android:selected="@{selected}"
android:orientation="vertical">
<TextView
android:id="@+id/chapterTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="@{chapter.name}"
app:selected="@{selected}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/chapter_time"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/chapter_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="@{chapter.time}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="@dimen/default_margin">
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/half_default_margin"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_chapter_normal_w" />
<!--FIXME
It should be android:src="?attr/ic_chapter_normal_style" but a weird bug, probably in the
styles, makes VLC crash -->
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:id="@+id/textView6"
style="@style/Theme.VLC.BottomSheetTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/half_default_margin"
android:layout_marginStart="@dimen/half_default_margin"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@string/go_to_chapter"
android:textSize="20sp"
android:textStyle="bold" />
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
<ListView
android:id="@+id/chapter_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:choiceMode="singleChoice"
android:layout_gravity="center_horizontal" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/chapter_list"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:choiceMode="singleChoice"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView6" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/chapter_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/chapter_time"
android:background="@drawable/background_chapter_item"
android:ellipsize="marquee"
android:padding="@dimen/half_default_margin"
android:singleLine="true"
android:maxLines="1"
android:text="Chapter_name" />
<TextView
android:id="@+id/chapter_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@drawable/background_chapter_item"
android:padding="@dimen/half_default_margin"
android:text="00:00" />
</RelativeLayout>
\ No newline at end of file
/**
* **************************************************************************
* PickTimeFragment.java
* ****************************************************************************
* Copyright © 2015 VLC authors and VideoLAN
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
* ***************************************************************************
*/
package org.videolan.vlc.gui.dialogs;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import org.videolan.libvlc.MediaPlayer;
import org.videolan.medialibrary.Tools;
import org.videolan.vlc.PlaybackService;
import org.videolan.vlc.R;
import org.videolan.vlc.gui.helpers.UiTools;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
public class SelectChapterDialog extends DismissDialogFragment implements Observer<PlaybackService> {
public final static String TAG = "VLC/SelectChapterDialog";
private ListView mChapterList;
protected PlaybackService mService;
public static SelectChapterDialog newInstance() {
return new SelectChapterDialog();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_select_chapter, container);
mChapterList = view.findViewById(R.id.chapter_list);
getDialog().setCancelable(true);
getDialog().setCanceledOnTouchOutside(true);
Window window = getDialog().getWindow();
window.setBackgroundDrawableResource(UiTools.getResourceFromAttribute(getActivity(), R.attr.rounded_bg));
window.setLayout(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
PlaybackService.Companion.getService().observe(this, this);
}
private void initChapterList() {
final MediaPlayer.Chapter[] chapters = mService.getChapters(-1);
int chaptersCount = chapters != null ? chapters.length : 0;
if (chaptersCount <= 1) return;
final List<Map<String, String>> chapterList = new ArrayList<Map<String, String>>();
for (int i = 0; i < chaptersCount; i++) {
String name;
if (chapters[i].name == null || chapters[i].name.equals(""))
name = getResources().getString(R.string.chapter) + " " + i;
else
name = chapters[i].name;
chapterList.add(putData(name, Tools.millisToString(chapters[i].timeOffset)));
}
String[] from = { "name", "time" };
int[] to = { R.id.chapter_name, R.id.chapter_time };
SimpleAdapter adapter = new SimpleAdapter(getActivity(), chapterList,
R.layout.dialog_select_chapter_item, from, to);
mChapterList.setAdapter(adapter);
mChapterList.setSelection(mService.getChapterIdx());
mChapterList.setItemChecked(mService.getChapterIdx(), true);
mChapterList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mService.setChapterIdx(position);
dismiss();
}
});
}
private Map<String, String> putData(String name, String time) {
Map<String, String> item = new HashMap<String, String>();
item.put("name", name);
item.put("time", time);
return item;
}
@Override
public void onChanged(PlaybackService service) {
if (service != null) {
mService = service;
initChapterList();
} else mService = null;
}
}
/**
* **************************************************************************
* PickTimeFragment.java
* ****************************************************************************
* Copyright © 2015 VLC authors and VideoLAN
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
* ***************************************************************************
*/
package org.videolan.vlc.gui.dialogs
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.ObsoleteCoroutinesApi
import org.videolan.medialibrary.Tools
import org.videolan.vlc.PlaybackService
import org.videolan.vlc.R
import org.videolan.vlc.databinding.ChapterListItemBinding
import org.videolan.vlc.gui.helpers.BottomSheetBehavior
import java.util.*
@ObsoleteCoroutinesApi
@ExperimentalCoroutinesApi
class SelectChapterDialog : VLCBottomSheetDialogFragment(), Observer<PlaybackService>, IOnChapterSelectedListener {
companion object {
fun newInstance(): SelectChapterDialog {
return SelectChapterDialog()
}
}
private lateinit var chapterList: RecyclerView
private var service: PlaybackService? = null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.dialog_select_chapter, container)
chapterList = view.findViewById(R.id.chapter_list)
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
PlaybackService.service.observe(this, this)
}
private fun initChapterList() {
val chapters = service!!.getChapters(-1)
val chaptersCount = chapters?.size ?: 0
if (chaptersCount <= 1) return
val chapterData = ArrayList<Chapter>()
for (i in 0 until chaptersCount) {
val name: String = if (chapters!![i].name == null || chapters[i].name == "")
resources.getString(R.string.chapter) + " " + i
else
chapters[i].name
chapterData.add(Chapter(name, Tools.millisToString(chapters[i].timeOffset)))
}
val adapter = ChapterAdapter(chapterData, service?.chapterIdx, this)
chapterList.layoutManager = LinearLayoutManager(requireActivity())
chapterList.adapter = adapter
}
override fun onChapterSelected(position: Int) {
service?.chapterIdx = position
dismiss()
}
override fun onChanged(service: PlaybackService?) {
if (service != null) {
this.service = service
initChapterList()
} else
this.service = null
}
override fun getDefaultState(): Int {
return BottomSheetBehavior.STATE_EXPANDED
}
override fun needToManageOrientation(): Boolean {
return true
}
data class Chapter(val name: String, val time: String)
inner class ChapterAdapter(private val chapters: List<Chapter>, private val selectedIndex: Int?, private val listener: IOnChapterSelectedListener) : RecyclerView.Adapter<ChapterViewHolder>() {
private lateinit var binding: ChapterListItemBinding
override fun onBindViewHolder(holder: ChapterViewHolder, position: Int) {
binding.chapter = chapters[position]
binding.selected = selectedIndex == position
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ChapterViewHolder {
binding = DataBindingUtil.inflate(LayoutInflater.from(parent.context), R.layout.chapter_list_item, parent, false) as ChapterListItemBinding
return ChapterViewHolder(binding, listener)
}
override fun getItemCount(): Int {
return chapters.size
}
}
inner class ChapterViewHolder(var binding: ChapterListItemBinding, private val listener: IOnChapterSelectedListener) : RecyclerView.ViewHolder(binding.root) {
init {
binding.holder = this
}
fun onClick(v: View) {
listener.onChapterSelected(layoutPosition)
}
}
}
interface IOnChapterSelectedListener {
fun onChapterSelected(position: Int)
}
......@@ -587,4 +587,9 @@ public class UiTools {
final Display presentationDisplay = route != null ? route.getPresentationDisplay() : null;
return presentationDisplay != null;
}
@BindingAdapter("selected")
public static void isSelected(View v, Boolean isSelected) {
v.setSelected(isSelected);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment