Skip to content

Commit

Permalink
Android: Pushing new fragments in MobileLibraryBrowser tabs now work …
Browse files Browse the repository at this point in the history
…properly.

Related to issue #406.
  • Loading branch information
ycastonguay committed Jul 1, 2013
1 parent cd63e0d commit 5ecda3d
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 125 deletions.
26 changes: 19 additions & 7 deletions MPfm/MPfm.Android/Classes/Activities/MainActivity.cs
Expand Up @@ -45,11 +45,9 @@ public class MainActivity : BaseActivity, IMobileOptionsMenuView
private ITinyMessengerHub _messengerHub;
private AndroidNavigationManager _navigationManager;
private SplashFragment _splashFragment;
//private MainFragment _mainFragment;
private LinearLayout _miniPlayer;
private List<KeyValuePair<MobileOptionsMenuType, string>> _options;
private ViewPager _viewPager;
private List<KeyValuePair<MobileNavigationTabType, Fragment>> _fragments;
private MainTabPagerAdapter _tabPagerAdapter;
private TextView _lblArtistName;
private TextView _lblAlbumTitle;
Expand All @@ -64,13 +62,12 @@ protected override void OnCreate(Bundle bundle)
//string internalDir = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);

RequestWindowFeature(WindowFeatures.ActionBar);
SetContentView(Resource.Layout.MainActivity);
SetContentView(Resource.Layout.Main);

// Setup view pager
_fragments = new List<KeyValuePair<MobileNavigationTabType, Fragment>>();
_viewPager = FindViewById<ViewPager>(Resource.Id.main_pager);
_viewPager.OffscreenPageLimit = 4;
_tabPagerAdapter = new MainTabPagerAdapter(FragmentManager, _fragments, _viewPager, ActionBar);
_tabPagerAdapter = new MainTabPagerAdapter(FragmentManager, _viewPager, ActionBar);
_viewPager.Adapter = _tabPagerAdapter;
_viewPager.SetOnPageChangeListener(_tabPagerAdapter);

Expand Down Expand Up @@ -146,14 +143,14 @@ public override void OnConfigurationChanged(global::Android.Content.Res.Configur
public void AddTab(MobileNavigationTabType type, string title, Fragment fragment)
{
Console.WriteLine("MainActivity - OnCreate - Adding tab {0}", title);
_fragments.Add(new KeyValuePair<MobileNavigationTabType, Fragment>(type, fragment));
_tabPagerAdapter.SetFragment(type, fragment);
_tabPagerAdapter.NotifyDataSetChanged();
}

public void PushTabView(MobileNavigationTabType type, Fragment fragment)
{
Console.WriteLine("MainActivity - PushTabView type: {0} fragment: {1} fragmentCount: {2}", type.ToString(), fragment.GetType().FullName, FragmentManager.BackStackEntryCount);
// Not used on Android
_tabPagerAdapter.SetFragment(type, fragment);
}

public void PushDialogView(string viewTitle, IBaseView sourceView, IBaseView view)
Expand Down Expand Up @@ -219,6 +216,21 @@ protected override void OnDestroy()
base.OnDestroy();
}

public override void OnBackPressed()
{
// Check if the history has another tab
if (_tabPagerAdapter.CanRemoveFragmentFromStack(_tabPagerAdapter.GetCurrentTab(), _viewPager.CurrentItem))
{
Console.WriteLine("MainActivity - OnBackPressed - CanRemoveFragment");
_tabPagerAdapter.RemoveFragmentFromStack(_tabPagerAdapter.GetCurrentTab(), _viewPager.CurrentItem);
}
else
{
Console.WriteLine("MainActivity - OnBackPressed - CannotRemoveFragment");
base.OnBackPressed();
}
}

public override bool OnCreateOptionsMenu(IMenu menu)
{
MenuInflater.Inflate(Resource.Menu.main_menu, menu);
Expand Down
13 changes: 8 additions & 5 deletions MPfm/MPfm.Android/Classes/Activities/PreferencesActivity.cs
Expand Up @@ -36,8 +36,9 @@ public class PreferencesActivity : BaseActivity, IPreferencesView
{
private MobileNavigationManager _navigationManager;
private ViewPager _viewPager;
private MainTabPagerAdapter _tabPagerAdapter;
private List<KeyValuePair<MobileNavigationTabType, Fragment>> _fragments;
private TabPagerAdapter _tabPagerAdapter;
//private List<Tuple<MobileNavigationTabType, Fragment>> _fragments;
private List<Fragment> _fragments;

protected override void OnCreate(Bundle bundle)
{
Expand All @@ -49,9 +50,10 @@ protected override void OnCreate(Bundle bundle)
ActionBar.SetHomeButtonEnabled(true);

_navigationManager = Bootstrapper.GetContainer().Resolve<MobileNavigationManager>();
_fragments = new List<KeyValuePair<MobileNavigationTabType, Fragment>>();
//_fragments = new List<Tuple<MobileNavigationTabType, Fragment>>();
_fragments = new List<Fragment>();
_viewPager = FindViewById<ViewPager>(Resource.Id.preferences_pager);
_tabPagerAdapter = new MainTabPagerAdapter(FragmentManager, _fragments, _viewPager, ActionBar);
_tabPagerAdapter = new TabPagerAdapter(FragmentManager, _fragments, _viewPager, ActionBar);
_viewPager.Adapter = _tabPagerAdapter;
_viewPager.SetOnPageChangeListener(_tabPagerAdapter);

Expand All @@ -68,7 +70,8 @@ protected override void OnStart()
public void AddSubview(IBaseView view)
{
Console.WriteLine("PreferencesActivity - AddSubview view: {0}", view.GetType().FullName);
_fragments.Add(new KeyValuePair<MobileNavigationTabType, Fragment>(MobileNavigationTabType.More, (Fragment)view));
//_fragments.Add(new Tuple<MobileNavigationTabType, Fragment>(MobileNavigationTabType.More, (Fragment)view));
_fragments.Add((Fragment)view);

if (_tabPagerAdapter != null)
_tabPagerAdapter.NotifyDataSetChanged();
Expand Down
75 changes: 68 additions & 7 deletions MPfm/MPfm.Android/Classes/Adapters/MainTabPagerAdapter.cs
Expand Up @@ -26,9 +26,9 @@

namespace MPfm.Android.Classes.Adapters
{
public class MainTabPagerAdapter : FragmentPagerAdapter, ActionBar.ITabListener, ViewPager.IOnPageChangeListener
public class MainTabPagerAdapter : FragmentStatePagerAdapter, ActionBar.ITabListener, ViewPager.IOnPageChangeListener
{
private readonly List<KeyValuePair<MobileNavigationTabType, Fragment>> _fragments;
private readonly List<Tuple<MobileNavigationTabType, List<Fragment>>> _fragments;
private readonly ViewPager _viewPager;
private readonly ActionBar _actionBar;

Expand All @@ -37,14 +37,53 @@ public MainTabPagerAdapter(IntPtr javaReference, JniHandleOwnership transfer)
{
}

public MainTabPagerAdapter(FragmentManager fm, List<KeyValuePair<MobileNavigationTabType, Fragment>> fragments, ViewPager viewPager, ActionBar actionBar)
public MainTabPagerAdapter(FragmentManager fm, ViewPager viewPager, ActionBar actionBar)
: base(fm)
{
_fragments = fragments;
_fragments = new List<Tuple<MobileNavigationTabType, List<Fragment>>>();
_viewPager = viewPager;
_actionBar = actionBar;
}

public void SetFragment(MobileNavigationTabType tabType, Fragment fragment)
{
Console.WriteLine("MainTabPagerAdapter - SetFragment - tabType: {0}", tabType.ToString());
int index = _fragments.FindIndex(x => x.Item1 == tabType);
if (index == -1)
{
// This tab isn't yet in the list; add to list
_fragments.Add(new Tuple<MobileNavigationTabType, List<Fragment>>(tabType, new List<Fragment>(){ fragment }));
return;
}

var fragments = _fragments.FirstOrDefault(x => x.Item1 == tabType);
fragments.Item2.Add(fragment);
NotifyDataSetChanged();
}

public MobileNavigationTabType GetCurrentTab()
{
return _fragments[_viewPager.CurrentItem].Item1;
}

public bool CanRemoveFragmentFromStack(MobileNavigationTabType tabType, int index)
{
var fragmentList = _fragments.FirstOrDefault(x => x.Item1 == tabType);
if (fragmentList != null)
return fragmentList.Item2.Count > 1;

return false;
}

public void RemoveFragmentFromStack(MobileNavigationTabType tabType, int index)
{
var fragmentList = _fragments.FirstOrDefault(x => x.Item1 == tabType);
if (fragmentList != null)
fragmentList.Item2.RemoveAt(fragmentList.Item2.Count - 1);

NotifyDataSetChanged();
}

public void OnTabReselected(ActionBar.Tab tab, FragmentTransaction ft)
{
}
Expand All @@ -61,12 +100,34 @@ public void OnTabUnselected(ActionBar.Tab tab, FragmentTransaction ft)

public override Fragment GetItem(int index)
{
return _fragments[index].Value;
Console.WriteLine("MainTabPagerAdapter - GetItem - index: {0}", index);
return _fragments[index].Item2.Last();
}

public override int GetItemPosition(Java.Lang.Object obj)
{
// If the fragment is different, tell Android to refresh this item
bool foundItem = false;
foreach (var fragmentList in _fragments)
{
bool foundItemInList = fragmentList.Item2.Last() == (Fragment) obj;
if (foundItemInList)
{
foundItem = true;
break;
}
}
Console.WriteLine("MainTabPagerAdapter - GetItemPosition - obj: {0} - foundItem: {1}", obj.GetType().FullName, foundItem);
return foundItem ? PositionUnchanged : PositionNone;
}

public override int Count
{
get { return _fragments.Count; }
get
{
Console.WriteLine("MainTabPagerAdapter - GetCount");
return _fragments.Count;
}
}

public void OnPageScrollStateChanged(int p0)
Expand All @@ -85,7 +146,7 @@ public void OnPageSelected(int position)

public override Java.Lang.ICharSequence GetPageTitleFormatted(int position)
{
return new Java.Lang.String(_fragments[position].Key.ToString());
return new Java.Lang.String(_fragments[position].Item1.ToString());
}
}
}
Expand Up @@ -50,6 +50,7 @@ public MobileLibraryBrowserFragment(Action<IBaseView> onViewReady)

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, global::Android.OS.Bundle savedInstanceState)
{
Console.WriteLine("MLBFragment - OnCreateView");
ListAdapter = new MobileLibraryBrowserListAdapter(Activity, _entities.ToList());
return base.OnCreateView(inflater, container, savedInstanceState);
}
Expand All @@ -60,20 +61,6 @@ public override void OnListItemClick(ListView l, View v, int position, long id)
OnItemClick(position);
}

public void OnClick(View v)
{
if (v == null)
return;

var builder = new AlertDialog.Builder(Activity);
//builder.SetIconAttribute(Android.Resource.Attribute.icon)
builder.SetTitle("Yeah");
//builder.SetMessage("You have typed: " + _editText.Text);
//builder.SetPositiveButton("Yes");// this);
var dialog = builder.Create();
builder.Show();
}

public override void OnResume()
{
Console.WriteLine("MLBFragment - OnResume");
Expand Down
2 changes: 1 addition & 1 deletion MPfm/MPfm.Android/MPfm.Android.csproj
Expand Up @@ -139,7 +139,7 @@
<AndroidResource Include="Resources\Layout\PlayerMetadata.axml">
<SubType>Designer</SubType>
</AndroidResource>
<AndroidResource Include="Resources\Layout\MainActivity.axml">
<AndroidResource Include="Resources\Layout\Main.axml">
<SubType>Designer</SubType>
</AndroidResource>
<AndroidResource Include="Resources\Layout\Sync.axml">
Expand Down
Expand Up @@ -25,33 +25,26 @@
android:layout_height="fill_parent"
android:paddingLeft="8dp"
android:background="#36454F">
<TextView
android:id="@+id/main_miniplayer_lblNowPlaying"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFFFF"
android:text="Now Playing"
android:textSize="14dip" />
<TextView
android:id="@+id/main_miniplayer_lblArtistName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFCCCCCC"
android:text="[Artist Name]"
android:textSize="11dip" />
android:textColor="#FFFFFF"
android:text=""
android:textSize="14dip" />
<TextView
android:id="@+id/main_miniplayer_lblAlbumTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="[Album Title]"
android:textColor="#FFCCCCCC"
android:textSize="11dip" />
android:text=""
android:textColor="#EEEEEE"
android:textSize="12dip" />
<TextView
android:id="@+id/main_miniplayer_lblSongTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="[Song Title]"
android:textColor="#FFCCCCCC"
android:text=""
android:textColor="#CCCCCC"
android:textSize="11dip" />
</LinearLayout>
</LinearLayout>
Expand Down
25 changes: 23 additions & 2 deletions MPfm/MPfm.Android/Resources/Layout/Markers.axml
@@ -1,6 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/markers_lblTitle"
android:text="Markers"
android:textSize="20dp"
android:background="#00000000"
android:textColor="#ffffffff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="6dip" />
<ListView
android:id="@+id/markers_listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:layout_height="fill_parent"
android:background="@android:color/transparent"
android:cacheColorHint="#00000000"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
android:divider="#00000000"
android:listSelector="@android:color/transparent" />
</LinearLayout>
6 changes: 3 additions & 3 deletions MPfm/MPfm.Android/Resources/Layout/PlayerMetadata.axml
Expand Up @@ -6,7 +6,7 @@
android:gravity="bottom">
<TextView
android:id="@+id/playerMetadata_lblArtistName"
android:text="[ArtistName]"
android:text=""
android:textSize="20dp"
android:background="#00000000"
android:textColor="#ffffffff"
Expand All @@ -16,7 +16,7 @@
android:padding="6dip" />
<TextView
android:id="@+id/playerMetadata_lblAlbumTitle"
android:text="[AlbumTitle]"
android:text=""
android:background="#00000000"
android:textColor="#ffffffff"
android:textSize="18dp"
Expand All @@ -26,7 +26,7 @@
android:padding="6dip" />
<TextView
android:id="@+id/playerMetadata_lblSongTitle"
android:text="[SongTitle]"
android:text=""
android:textSize="14dp"
android:background="#00000000"
android:textColor="#ffffffff"
Expand Down

0 comments on commit 5ecda3d

Please sign in to comment.