/*
 * Copyright 2014 Weswit Srl
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lightstreamer.demo.stocklistdemo_advanced;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

public class StocksFragment extends ListFragment {
    
    onStockSelectedListener listener;
    
    public interface onStockSelectedListener {
        /** Called by HeadlinesFragment when a list item is selected */
        public void onStockSelected(int item);
    }
    
    final static String[] items = {"item1", "item2", "item3",
            "item4", "item5", "item6", "item7", "item8", "item9", "item10",
            "item11", "item12", "item13", "item14", "item15", "item16", 
            "item17", "item18", "item19", "item20" };
   
    
    public final static String[] subscriptionFields = {"stock_name", "last_price", "time"};
    
    private Handler handler;
    
    private static ArrayList<StockForList> list;
    private final static MainSubscription mainSubscription;
    
    static {
        list = new ArrayList<StockForList>(items.length);
        for (int i = 0; i < items.length; i++) {
            list.add(new StockForList(items[i],i));
        }
        
        mainSubscription = new MainSubscription(list);
        StockListDemo.lsClient.addSubscription(mainSubscription);
    }
    
    
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {

        return inflater.inflate(R.layout.list_view, container, false);
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        handler = new Handler();
        
        setListAdapter(new StocksAdapter(getActivity(), R.layout.row_layout, list));
    }
    
    
    @Override
    public void onStart() {
        super.onStart();
        
        //there's always only one StocksFragment at a time
        mainSubscription.changeContext(handler, getListView()); 
        
        if (getFragmentManager().findFragmentById(R.id.details_fragment) != null) {
            getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        }
        
    }
    
    
    
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        
      
        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception.
        try {
            listener = (onStockSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnHeadlineSelectedListener");
        }
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // Notify the parent activity of selected item
        listener.onStockSelected(position+1);
        
        // Set the item as checked to be highlighted when in two-pane layout
        getListView().setItemChecked(position, true);
    }
    
    

}

@@ -668,16 +668,116 @@
 yList;%0A%0A
+import com.lightstreamer.demo.stocklistdemo_advanced.LightstreamerClient.LightstreamerClientProxy;%0A%0A
 import a
@@ -1692,24 +1692,63 @@
 er handler;%0A
+    LightstreamerClientProxy lsClient;%0A
     %0A    pri
@@ -1801,22 +1801,16 @@
 private 
-final 
 static M
@@ -2102,85 +2102,9 @@
     
-    StockListDemo.lsClient.addSubscription(mainSubscription);%0A    %7D%0A    %0A    
+%7D
 %0A   
@@ -2562,29 +2562,24 @@
 %0A    %7D%0A    %0A
-    %0A
     @Overrid
@@ -3068,24 +3068,330 @@
 );%0A        %0A
+        try %7B%0A             lsClient = (LightstreamerClientProxy) activity;%0A        %7D catch (ClassCastException e) %7B%0A            throw new ClassCastException(activity.toString()%0A                    + %22 must implement LightstreamerClientProxy%22);%0A        %7D%0A        lsClient.addSubscription(mainSubscription);%0A
       %0A     
@@ -3782,16 +3782,151 @@
  %7D%0A    %7D
+%0A    %0A    @Override%0A    public void onDetach() %7B%0A        super.onDetach();%0A        lsClient.removeSubscription(mainSubscription);%0A    %7D
 %0A%0A    @O

fix bug that crashed the app when static stuff had to be rebuilt